]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16263 Flagging questions during a quiz attempt. Update flag state in the navigtio...
authortjhunt <tjhunt>
Wed, 3 Sep 2008 02:35:56 +0000 (02:35 +0000)
committertjhunt <tjhunt>
Wed, 3 Sep 2008 02:35:56 +0000 (02:35 +0000)
mod/quiz/attemptlib.php
mod/quiz/quiz.js
question/qengine.js

index 0c740d4ef64894886a340ebe67493d29058d75b9..af6e33ba9bfe8389b804443109d650cb1a37d8a2 100644 (file)
@@ -800,14 +800,26 @@ abstract class quiz_nav_panel_base {
     }
 
     protected function get_question_buttons() {
-        $html = '<div class="qn_buttons">';
+        $html = '<div class="qn_buttons">' . "\n";
         foreach ($this->attemptobj->get_question_iterator() as $number => $question) {
-            $html .= $this->get_question_button($number, $question);
+            $html .= $this->get_question_button($number, $question) . "\n" .
+                    $this->get_button_update_script($question) . "\n";
         }
-        $html .= '</div>';
+        $html .= "</div>\n";
         return $html;
     }
 
+    protected function get_button_id($question) {
+        // The id to put on the button element in the HTML.
+        return 'quiznavbutton' . $question->id;
+    }
+
+    protected function get_button_update_script($question) {
+        return '<script type="text/javascript">' .
+                "\nquiz_init_nav_button('" . $this->get_button_id($question) . "', " .
+                $question->id . ")\n</script>\n";
+    }
+
     abstract protected function get_question_button($number, $question);
 
     abstract protected function get_end_bits();
@@ -830,8 +842,8 @@ abstract class quiz_nav_panel_base {
 
     public function display() {
         $strquiznavigation = get_string('quiznavigation', 'quiz');
-        $content = $this->get_question_buttons() . '<div class="othernav">' .
-                $this->get_end_bits() . '</div>';
+        $content = $this->get_question_buttons() . "\n" . '<div class="othernav">' .
+                "\n" . $this->get_end_bits() . "\n</div>\n";
         print_side_block($strquiznavigation, $content, NULL, NULL, '', array('id' => 'quiznavigation'), $strquiznavigation);
     }
 }
@@ -850,7 +862,8 @@ class quiz_attempt_nav_panel extends quiz_nav_panel_base {
         }
         return '<input type="submit" name="gotopage' . $question->_page .
                 '" value="' . $number . '" class="qnbutton ' .
-                $this->get_question_state_classes($question) . '"' . $onclick . '/>';
+                $this->get_question_state_classes($question) . '" id="' .
+                $this->get_button_id($question) . '" ' . $onclick . '/>';
     }
 
     protected function get_end_bits() {
@@ -870,8 +883,8 @@ class quiz_review_nav_panel extends quiz_nav_panel_base {
 
     protected function get_question_button($number, $question) {
         return '<a href="' . $this->attemptobj->review_url($question->id) .
-                '" class="qnbutton ' . $this->get_question_state_classes($question) .
-                '">' . $number . '</a>';
+                '" class="qnbutton ' . $this->get_question_state_classes($question) . '" id="' .
+                $this->get_button_id($question) . '">' . $number . '</a>';
     }
 
     protected function get_end_bits() {
index ca6b450f26b7f48aeae49fe7d162637b79267280..b4e082a51097476d73c92576a4f3ad0ee4da9d2a 100644 (file)
@@ -19,6 +19,7 @@ function check_enter(e) {
     }
 }
 
+// Code for updating the countdown timer that is used on timed quizzes.
 quiz_timer = {
     // The outer div, so we can get at it to move it when the page scrolls.
     timerouter: null,
@@ -144,6 +145,25 @@ quiz_timer = {
     }
 };
 
+// Code for updating the navigation panel. At the moment it only updates
+// the flagged states of questions. This is a constructor, it takes 
+function quiz_init_nav_button(buttonid, questionid) {
+    var button = document.getElementById(buttonid);
+    button.stateupdater = new quiz_nav_updater(button, questionid);
+}
+
+function quiz_nav_updater(element, questionid) {
+    this.element = element;
+    question_flag_changer.add_flag_state_listener(questionid, this);
+};
+
+quiz_nav_updater.prototype.flag_state_changed = function(newstate) {
+    this.element.className = this.element.className.replace(/\s*\bflagged\b\s*/, ' ');
+    if (newstate) {
+        this.element.className += ' flagged';
+    }
+};
+
 quiz_secure_window = {
     // The message displayed when the secure window interferes with the user.
     protection_message: null,
index a72804e40e19eef07c2386f5bdce7ded9fb184b0..67ec86eef0d387e55f985cc3d355c8ea4067c7b0 100644 (file)
@@ -2,13 +2,14 @@
 // the require_js calls in get_html_head_contributions in lib/questionlib.php.
 
 question_flag_changer = {
+    flag_state_listeners: new Object(),
+
     init_flag: function(checkboxid, postdata) {
         var checkbox = document.getElementById(checkboxid);
         checkbox.ajaxpostdata = postdata;
         checkbox.className += ' jsworking';
         question_flag_changer.update_image(checkbox);
         YAHOO.util.Event.addListener(checkbox, 'change', this.checkbox_state_change);
-        YAHOO.util.Event.addListener(checkbox, 'focus', 'blur()');
     },
 
     checkbox_state_change: function(e) {
@@ -21,6 +22,7 @@ question_flag_changer = {
             postdata += '&newstate=0'
         }
         YAHOO.util.Connect.asyncRequest('POST', qengine_config.wwwroot + '/question/toggleflag.php', null, postdata);
+        question_flag_changer.fire_state_changed(checkbox);
     },
 
     update_image: function(checkbox) {
@@ -34,5 +36,29 @@ question_flag_changer = {
             img.alt = qengine_config.unflaggedalt;
             img.title = qengine_config.flagtooltip;
         }
+    },
+
+    add_flag_state_listener: function(questionid, listener) {
+        var key = 'q' + questionid;
+        if (!question_flag_changer.flag_state_listeners.hasOwnProperty(key)) {
+            question_flag_changer.flag_state_listeners[key] = [];
+        }
+        question_flag_changer.flag_state_listeners[key].push(listener);
+    },
+
+    questionid_from_cbid: function(cbid) {
+        return cbid.replace(/resp(\d+)__flagged/, '$1');
+    },
+
+    fire_state_changed: function(checkbox) {
+        var questionid = question_flag_changer.questionid_from_cbid(checkbox.id);
+        var key = 'q' + questionid;
+        if (!question_flag_changer.flag_state_listeners.hasOwnProperty(key)) {
+            return;
+        }
+        var newstate = checkbox.checked;
+        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);
+        }
     }
 };