]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16263 Flagging questions during a quiz attempt. Save flag state on the review...
authortjhunt <tjhunt>
Wed, 3 Sep 2008 08:32:29 +0000 (08:32 +0000)
committertjhunt <tjhunt>
Wed, 3 Sep 2008 08:32:29 +0000 (08:32 +0000)
lang/en_utf8/question.php
lib/questionlib.php
mod/quiz/review.php
question/qengine.js
theme/standard/styles_layout.css

index 97c5c3bc73c16c74271cd34badb5b7546355051e..1c4661945d76a3151ad46cc26ec4e10734e63160 100644 (file)
@@ -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';
index 1630c170cc4667d69eff3351caa6e6701ba3db64..b7d3bf51a235620d13507babc9afc9d6a44f93f0 100644 (file)
@@ -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.
index c72577f2409f7a7f55abfa08e3896a6399d6b839..7b89d666738572f720313ba4df6ce635e0e0d780 100644 (file)
@@ -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 {
     $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());
 
 /// Summary table end ==============================================================================
 
-/// Print all the questions
+/// Form for saving flags if necessary.
+    if ($options->flags == QUESTION_FLAGSEDITABLE) {
+        echo '<form action="' . $attemptobj->review_url(0, $page, $showall) .
+                '" method="post"><div>';
+        echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
+    }
+
+/// Print all the questions.
     if ($showall) {
         $thispage = 'all';
         $lastpage = true;
         $attemptobj->print_question($id);
     }
 
+/// Close form if we opened it.
+    if ($options->flags == QUESTION_FLAGSEDITABLE) {
+        echo '<div class="submitbtns">' . "\n" .
+                '<input type="submit" id="savingflagssubmit" name="savingflags" value="' .
+                get_string('saveflags', 'question') . '" />' .
+                "</div>\n" .
+                "\n</div></form>\n" .
+                '<script type="text/javascript">' .
+                "\nquestion_flag_changer.init_flag_save_form('savingflagssubmit');\n" .
+                "</script>\n";
+    }
+
 /// Print a link to the next page.
     echo '<div class="submitbtns">';
     if ($lastpage) {
index 4c47374b21ffc74ed5b4b7f66f84f622ef0b6ee7..4702f985dbfd2f8d100fecd994c8dab8700bfb29 100644 (file)
@@ -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;
index b374821770b7cb0faeb14702ed775d2202fae882..1a5207ae04cadf86d71123dadb30f2919dada98a 100644 (file)
@@ -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;