From: tjhunt Date: Wed, 3 Sep 2008 08:32:29 +0000 (+0000) Subject: MDL-16263 Flagging questions during a quiz attempt. Save flag state on the review... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f62040ed1fa4a1330b4137a4883328fbf994de70;p=moodle.git MDL-16263 Flagging questions during a quiz attempt. Save flag state on the review page when JS is off too. --- diff --git a/lang/en_utf8/question.php b/lang/en_utf8/question.php index 97c5c3bc73..1c4661945d 100644 --- a/lang/en_utf8/question.php +++ b/lang/en_utf8/question.php @@ -146,6 +146,7 @@ $string['questionsmovedto'] = 'Questions still in use moved to "$a" in the paren $string['questionsrescuedfrom'] = 'Questions saved from context $a.'; $string['questionsrescuedfrominfo'] = 'These questions (some of which may be hidden) where saved when context $a was deleted because they are still used by some quizzes or other activities.'; $string['questionuse'] = 'Use question in this activity'; +$string['saveflags'] = 'Save the state of the flags'; $string['shareincontext'] = 'Share in context for $a'; $string['tofilecategory'] = 'Write category to file'; $string['tofilecontext'] = 'Write context to file'; diff --git a/lib/questionlib.php b/lib/questionlib.php index 1630c170cc..b7d3bf51a2 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1780,18 +1780,33 @@ function question_make_name_prefix($id) { } /** -* Extract question id from the prefix of form element names -* -* @return integer The question id -* @param string $name The name that contains a prefix that was -* constructed with {@link question_make_name_prefix()} -*/ + * Extract question id from the prefix of form element names + * + * @return integer The question id + * @param string $name The name that contains a prefix that was + * constructed with {@link question_make_name_prefix()} + */ function question_get_id_from_name_prefix($name) { - if (!preg_match('/^resp([0-9]+)_/', $name, $matches)) + if (!preg_match('/^resp([0-9]+)_/', $name, $matches)) { return false; + } return (integer) $matches[1]; } +/** + * Extract question id from the prefix of form element names + * + * @return integer The question id + * @param string $name The name that contains a prefix that was + * constructed with {@link question_make_name_prefix()} + */ +function question_id_and_key_from_post_name($name) { + if (!preg_match('/^resp([0-9]+)_(.*)$/', $name, $matches)) { + return array(false, false); + } + return array((integer) $matches[1], $matches[2]); +} + /** * Returns the unique id for a new attempt * @@ -2577,6 +2592,34 @@ function question_update_flag($sessionid, $newstate) { return $DB->set_field('question_sessions', 'flagged', $newstate, array('id' => $sessionid)); } +/** + * Update the flagged state of all the questions in an attempt, where a new . + * + * @param integer $sessionid question_session id. + * @param boolean $newstate the new state for the flag. + * @return boolean success or failure. + */ +function question_save_flags($formdata, $attemptid, $questionids) { + global $DB; + $donequestionids = array(); + foreach ($formdata as $postvariable => $value) { + list($qid, $key) = question_id_and_key_from_post_name($postvariable); + if ($qid !== false && in_array($qid, $questionids)) { + if ($key == '_flagged') { + $DB->set_field('question_sessions', 'flagged', !empty($value), + array('attemptid' => $attemptid, 'questionid' => $qid)); + $donequestionids[$qid] = 1; + } + } + } + foreach ($questionids as $qid) { + if (!isset($donequestionids[$qid])) { + $DB->set_field('question_sessions', 'flagged', 0, + array('attemptid' => $attemptid, 'questionid' => $qid)); + } + } +} + /** * @param integer $attemptid the question_attempt id. * @param integer $questionid the question id. diff --git a/mod/quiz/review.php b/mod/quiz/review.php index c72577f240..7b89d66673 100644 --- a/mod/quiz/review.php +++ b/mod/quiz/review.php @@ -40,7 +40,7 @@ } } -/// load the questions and states needed by this page. +/// Load the questions and states needed by this page. if ($showall) { $questionids = $attemptobj->get_question_ids(); } else { @@ -49,6 +49,15 @@ $attemptobj->load_questions($questionids); $attemptobj->load_question_states($questionids); +/// Save the flag states, if they are being changed. + if ($options->flags == QUESTION_FLAGSEDITABLE && optional_param('savingflags', false, PARAM_BOOL)) { + confirm_sesskey(); + $formdata = data_submitted(); + + question_save_flags($formdata, $attemptid, $questionids); + redirect($attemptobj->review_url(0, $page, $showall)); + } + /// Log this review. add_to_log($attemptobj->get_courseid(), 'quiz', 'review', 'review.php?attempt=' . $attemptobj->get_attemptid(), $attemptobj->get_quizid(), $attemptobj->get_cmid()); @@ -199,7 +208,14 @@ /// Summary table end ============================================================================== -/// Print all the questions +/// Form for saving flags if necessary. + if ($options->flags == QUESTION_FLAGSEDITABLE) { + echo '
'; + echo ''; + } + +/// Print all the questions. if ($showall) { $thispage = 'all'; $lastpage = true; @@ -211,6 +227,18 @@ $attemptobj->print_question($id); } +/// Close form if we opened it. + if ($options->flags == QUESTION_FLAGSEDITABLE) { + echo '
' . "\n" . + '' . + "
\n" . + "\n
\n" . + '\n"; + } + /// Print a link to the next page. echo '
'; if ($lastpage) { diff --git a/question/qengine.js b/question/qengine.js index 4c47374b21..4702f985db 100644 --- a/question/qengine.js +++ b/question/qengine.js @@ -27,6 +27,12 @@ question_flag_changer = { YAHOO.util.Event.addListener(image, 'click', this.flag_state_change); }, + init_flag_save_form: function(submitbuttonid) { + // With JS on, we just want to remove all visible traces of the form. + var button = document.getElementById(submitbuttonid); + button.parentNode.removeChild(button); + }, + flag_state_change: function(e) { var image = e.target ? e.target : e.srcElement; var input = image.statestore; diff --git a/theme/standard/styles_layout.css b/theme/standard/styles_layout.css index b374821770..1a5207ae04 100644 --- a/theme/standard/styles_layout.css +++ b/theme/standard/styles_layout.css @@ -3915,6 +3915,7 @@ body#mod-forum-search .introcontent { #mod-quiz-attempt .submitbtns, #mod-quiz-review .submitbtns { text-align: left; + margin-top: 1.5em; } #mod-quiz-summary .submitbtns { margin-top: 1.5em;