$states[$i]->penalty = 0;
$states[$i]->sumpenalty = 0;
$states[$i]->manualcomment = '';
+ $states[$i]->flagged = 0;
// Prevent further changes to the session from incrementing the
// sequence number
*/
function save_question_session($question, $state) {
global $QTYPES, $DB;
+
// Check if the state has changed
if (!$state->changed && isset($state->id)) {
+ if (isset($state->newflaggedstate) && $state->flagged != $state->newflaggedstate) {
+ // If this fails, don't worry too much, it is not critical data.
+ question_update_flag($state->questionsessionid, $state->newflaggedstate);
+ }
return $state->id;
}
// Set the legacy answer field
// create or update the session
if (!$session = $DB->get_record('question_sessions', array('attemptid' => $state->attempt, 'questionid' => $question->id))) {
+ $session = new stdClass;
$session->attemptid = $state->attempt;
$session->questionid = $question->id;
$session->newest = $state->id;
$session->newgraded = $state->id;
$session->sumpenalty = $state->sumpenalty;
$session->manualcomment = $state->manualcomment;
+ $session->flagged = !empty($state->newflaggedstate);
if (!$DB->insert_record('question_sessions', $session)) {
- print_error('cannotinsert', 'question');
+ return false;
}
} else {
$session->newest = $state->id;
} else {
$session->manualcomment = $session->manualcomment;
}
- $DB->update_record('question_sessions', $session);
+ $session->flagged = !empty($state->newflaggedstate);
+ if (!$DB->update_record('question_sessions', $session)) {
+ return false;
+ }
}
unset($state->answer);
// Save the question type specific state information and responses
- if (!$QTYPES[$question->qtype]->save_session_and_responses(
- $question, $state)) {
+ if (!$QTYPES[$question->qtype]->save_session_and_responses($question, $state)) {
return false;
}
+
// Reset the changed flag
$state->changed = false;
return $state->id;
} else {
$actions[$quid]->event = $defaultevent;
}
-
// Update the state with the new response
$actions[$quid]->responses[$key] = $response;
$action->responses = array('' => '');
}
+ $state->newflaggedstate = !empty($action->responses['_flagged']);
+
// make sure these are gone!
- unset($action->responses['submit'], $action->responses['validate']);
+ unset($action->responses['submit'], $action->responses['validate'], $action->responses['_flagged']);
// Check the question session is still open
if (question_state_is_closed($state)) {
$newstate->changed = true; // will assure that it gets saved to the database
$newstate->last_graded = clone($state->last_graded);
$newstate->timestamp = $action->timestamp;
+ $newstate->newflaggedstate = $state->newflaggedstate;
+ $newstate->flagged = $state->flagged;
+ $newstate->questionsessionid = $state->questionsessionid;
$state = $newstate;
// Set the event to the action we will perform. The question type specific
flag_state_listeners: new Object(),
init_flag: function(checkboxid, postdata) {
- // Convert the checkbox to an image input.
+ // Convert the checkbox to a hidden input.
var input = document.getElementById(checkboxid);
var state = input.checked;
input.ajaxpostdata = postdata;
- input.flaggedstate = state;
- input.type = 'image';
+ input.value = state ? 1 : 0;
+ input.type = 'hidden';
- // Set up the correct image, alt and title.
- question_flag_changer.update_image(input);
+ // Create an image input to replace the img tag.
+ var image = document.createElement('input');
+ image.type = 'image';
+ image.statestore = input;
+ question_flag_changer.update_image(image);
+ input.parentNode.appendChild(image);
// Remove the label element.
var label = document.getElementById(checkboxid + 'label');
label.parentNode.removeChild(label);
// Add the event handler.
- YAHOO.util.Event.addListener(input, 'click', this.flag_state_change);
+ YAHOO.util.Event.addListener(image, 'click', this.flag_state_change);
},
flag_state_change: function(e) {
- var input = e.target ? e.target : e.srcElement;
- input.flaggedstate = !input.flaggedstate;
- question_flag_changer.update_image(input);
+ var image = e.target ? e.target : e.srcElement;
+ var input = image.statestore;
+ input.value = 1 - input.value;
+ question_flag_changer.update_image(image);
var postdata = input.ajaxpostdata
- if (input.flaggedstate) {
+ if (input.value == 1) {
postdata += '&newstate=1'
} else {
postdata += '&newstate=0'
YAHOO.util.Event.preventDefault(e);
},
- update_image: function(input) {
- if (input.flaggedstate) {
- input.src = qengine_config.pixpath + '/i/flagged.png';
- input.alt = qengine_config.flaggedalt;
- input.title = qengine_config.unflagtooltip;
+ update_image: function(image) {
+ if (image.statestore.value == 1) {
+ image.src = qengine_config.pixpath + '/i/flagged.png';
+ image.alt = qengine_config.flaggedalt;
+ image.title = qengine_config.unflagtooltip;
} else {
- input.src = qengine_config.pixpath + '/i/unflagged.png';
- input.alt = qengine_config.unflaggedalt;
- input.title = qengine_config.flagtooltip;
+ image.src = qengine_config.pixpath + '/i/unflagged.png';
+ image.alt = qengine_config.unflaggedalt;
+ image.title = qengine_config.flagtooltip;
}
},
if (!question_flag_changer.flag_state_listeners.hasOwnProperty(key)) {
return;
}
- var newstate = input.flaggedstate;
+ var newstate = input.value == 1;
for (var i = 0; i < question_flag_changer.flag_state_listeners[key].length; i++) {
question_flag_changer.flag_state_listeners[key][i].flag_state_changed(newstate);
}