]> git.mjollnir.org Git - moodle.git/commitdiff
quiz settings: MDL-17333 Resolve the interrelations between shufflequestions and...
authortjhunt <tjhunt>
Tue, 17 Mar 2009 07:08:33 +0000 (07:08 +0000)
committertjhunt <tjhunt>
Tue, 17 Mar 2009 07:08:33 +0000 (07:08 +0000)
* change the wording of the settings.
* add a repaginate now checkbox (disabled if shuffle is on).
* JavaScript to automatically check the checkbox when qpp changes.
* (remove some irrelevant return value checks now we use exceptions.)

lang/en_utf8/quiz.php
mod/quiz/edit.js
mod/quiz/lib.php
mod/quiz/mod_form.php

index bb4b6a34454fdd2bd5a26ccd89885f91646d926f..7c7ebf3e97f5376458fa6111942699cc64e6da10 100644 (file)
@@ -64,6 +64,7 @@ $string['answerswithacceptederrormarginmustbenumeric'] = 'Answers with accepted
 $string['answertoolong'] = 'Answer too long after line $a (255 char. max)';
 $string['aon'] = 'AON format';
 $string['areyousureremoveselected'] = 'Are you sure you want to remove all the selected questions?';
+$string['asshownoneditscreen'] = 'As shown on the edit screen';
 $string['attempt'] = 'Attempt $a';
 $string['attemptalreadyclosed'] = 'This attempt has already be finished.';
 $string['attemptclosed'] = 'Attempt has not closed yet';
@@ -283,6 +284,8 @@ $string['event6'] = 'Close&amp;Grade';
 $string['event7'] = 'Submit';
 $string['event8'] = 'Close';
 $string['event9'] = 'Manual Grade';
+$string['everynquestions'] = 'Every $a questions';
+$string['everyquestion'] = 'Every question';
 $string['everythingon'] = 'Everything on';
 $string['examview'] = 'Examview';
 $string['export'] = 'Export';
@@ -430,6 +433,7 @@ $string['multichoice'] = 'Multiple Choice';
 $string['multipleanswers'] = 'Choose at least one answer.';
 $string['multiplier'] = 'Multiplier';
 $string['name'] = 'Name';
+$string['neverallononepage'] = 'Never, all questions on one page';
 $string['newattemptfail'] = 'Error: Could not start a new attempt at the quiz';
 $string['newpageevery'] = 'Automatically start a new page';
 $string['noanswers'] = 'No answers were selected!';
@@ -522,6 +526,7 @@ $string['questionname'] = 'Question name';
 $string['questionnametoolong'] = 'Question name too long at line $a (255 char. max). It has been truncated.';
 $string['questionno'] = 'Question $a';
 $string['questionnotloaded'] = 'Question $a has not been loaded from the database';
+$string['questionorder'] = 'Question order';
 $string['questions'] = 'Questions';
 $string['questionsinclhidden'] = 'Questions (including hidden)';
 $string['questionsinthisquiz'] = 'Questions in this quiz';
@@ -597,6 +602,7 @@ $string['reorderquestions'] = 'Reorder questions';
 $string['reordertool'] = 'Show the reordering tool';
 $string['repaginate'] = 'Repaginate with $a questions per page';
 $string['repaginatecommand'] = 'Repaginate';
+$string['repaginatenow'] = 'Repaginate now';
 $string['replace'] = 'Replace';
 $string['replacementoptions'] = 'Replacement Options';
 $string['report'] = 'Reports';
@@ -675,6 +681,7 @@ $string['showteacherattempts'] = 'Show teacher attempts';
 $string['showuserpicture'] = 'Show the user\'s picture';
 $string['shuffle'] = 'Shuffle';
 $string['shuffleanswers'] = 'Shuffle answers';
+$string['shuffledrandomly'] = 'Shuffled randomly';
 $string['shufflequestions'] = 'Shuffle questions';
 $string['shufflequestionsselected'] = 'Shuffle questions has been set, so some actions relating to pages are not available. To change the shuffle option go to $a.';
 $string['shufflewithin'] = 'Shuffle within questions';
index 2313af80269e54417e5887b38786f1c0e6848be4..8ea797288795dbda0fde612f8dd4180a27293b09 100644 (file)
@@ -21,9 +21,9 @@ function quiz_edit_init() {
     //show the dialog and depending on from which form (corresponding
     // a specific quiz page) it was triggered, set the value of the form's
     // rqpage input element to the form number
-    YAHOO.util.Event.addListener(this.dialoglisteners, "click",
+    YAHOO.util.Event.addListener(quiz_edit_config.dialoglisteners, "click",
            function(e){
-                   this.show();
+                this.show();
                 var rbutton = YAHOO.util.Event.getTarget(e);
                 var rbform = YAHOO.util.Dom.getAncestorByClassName(rbutton,"randomquestionform");
                 //this depends on the fact that the element hierarchy be:
@@ -91,5 +91,31 @@ function quiz_edit_init() {
 
 }
 
-YAHOO.util.Event.onDOMReady(quiz_edit_init, quiz_edit_config, true);
-YAHOO.util.Dom.setStyle('repaginatedialog', 'display', 'block');
\ No newline at end of file
+function quiz_settings_init() {
+    var repaginatecheckbox = document.getElementById('id_repaginatenow');
+    if (!repaginatecheckbox) {
+        // This checkbox does not appear on the create new quiz form.
+        return;
+    }
+    var qppselect = document.getElementById('id_questionsperpage');
+    var qppinitialvalue = qppselect.value;
+    YAHOO.util.Event.addListener([qppselect, 'id_shufflequestions'] , 'change', function() {
+        setTimeout(function() { // Annoyingly, this handler runs before the formlib disabledif code, hence the timeout.
+            if (!repaginatecheckbox.disabled) {
+                repaginatecheckbox.checked = qppselect.value != qppinitialvalue;
+            }
+        }, 50);
+    });
+}
+
+
+function quiz_edit_generic_init() {
+    switch (document.body.id) {
+    case 'mod-quiz-edit':
+        quiz_edit_init();
+        break;
+    case 'mod-quiz-mod':
+        quiz_settings_init();
+    }
+}
+YAHOO.util.Event.onDOMReady(quiz_edit_generic_init);
index f244b7d7ee661e23db0be34d3da9dac0ef6206a8..1562b2686941178505802b736293872d7ec33311 100644 (file)
@@ -118,9 +118,7 @@ function quiz_add_instance($quiz) {
     }
 
     // Try to store it in the database.
-    if (!$quiz->id = $DB->insert_record("quiz", $quiz)) {
-        return false;
-    }
+    $quiz->id = $DB->insert_record('quiz', $quiz);
 
     // Do the processing required after an add or an update.
     quiz_after_add_or_update($quiz);
@@ -136,8 +134,8 @@ function quiz_add_instance($quiz) {
  * @param object $quiz the data that came from the form.
  * @return mixed true on success, false or a string error message on failure.
  */
-function quiz_update_instance($quiz) {
-    global $DB;
+function quiz_update_instance($quiz, $mform) {
+    global $CFG, $DB;
 
     // Process the options from the form.
     $result = quiz_process_options($quiz);
@@ -145,11 +143,17 @@ function quiz_update_instance($quiz) {
         return $result;
     }
 
+    // Repaginate, if asked to.
+    if (!$quiz->shufflequestions && !empty($quiz->repaginatenow)) {
+        require_once($CFG->dirroot . '/mod/quiz/locallib.php');
+        $quiz->questions = $DB->get_field('quiz', 'questions', array('id' => $quiz->instance));
+        $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
+    }
+    unset($quiz->repaginatenow);
+
     // Update the database.
     $quiz->id = $quiz->instance;
-    if (!$DB->update_record("quiz", $quiz)) {
-        return false;  // some error occurred
-    }
+    $DB->update_record('quiz', $quiz);
 
     // Do the processing required after an add or an update.
     quiz_after_add_or_update($quiz);
index 14595652973ef09dc734811e203a6f0df66fc79a..8779599eba41ba903e37ea7de4a18c6f8a719068 100644 (file)
@@ -94,23 +94,35 @@ class mod_quiz_mod_form extends moodleform_mod {
         $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
 
     /// Shuffle questions.
-        $mform->addElement('selectyesno', 'shufflequestions', get_string('shufflequestions', 'quiz'));
+        $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
+        $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
         $mform->setHelpButton('shufflequestions', array('shufflequestions', get_string('shufflequestions','quiz'), 'quiz'));
         $mform->setAdvanced('shufflequestions', $quizconfig->fix_shufflequestions);
         $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
 
     /// Questions per page.
-        $perpage = array();
-        $perpage[0] = get_string('never');
-        $perpage[1] = get_string('aftereachquestion', 'quiz');
+        $pageoptions = array();
+        $pageoptions[0] = get_string('neverallononepage', 'quiz');
+        $pageoptions[1] = get_string('everyquestion', 'quiz');
         for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
-            $perpage[$i] = get_string('afternquestions', 'quiz', $i);
+            $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
         }
-        $mform->addElement('select', 'questionsperpage', get_string('newpageevery', 'quiz'), $perpage);
-        $mform->setHelpButton('questionsperpage', array('questionsperpage', get_string('newpageevery', 'quiz'), 'quiz'));
-        $mform->setAdvanced('questionsperpage', $quizconfig->fix_questionsperpage);
+
+        $pagegroup = array();
+        $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
         $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
 
+        if (!empty($this->_cm)) {
+            $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
+            $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
+            require_js(array('yui_yahoo', 'yui_dom', 'yui_event'));
+            require_js('mod/quiz/edit.js');
+        }
+
+        $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
+        $mform->setHelpButton('questionsperpagegrp', array('questionsperpage', get_string('newpageevery', 'quiz'), 'quiz'));
+        $mform->setAdvanced('questionsperpagegrp', $quizconfig->fix_questionsperpage);
+
 //-------------------------------------------------------------------------------
         $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));