From 14afbbf0344c275e655b0d51eb01fee7cdb0ce0b Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sun, 19 Feb 2006 16:34:06 +0000 Subject: [PATCH] Added the much-requested question reordering tool --- mod/quiz/edit.php | 34 ++++++++++++++++++++---- mod/quiz/editlib.php | 61 +++++++++++++++++++++++++++----------------- 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index f45898f747e..2441ae698c8 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -17,7 +17,7 @@ * add Adds several selected questions to the quiz * addrandom Adds a certain number of random questions to the quiz * delete Removes a question from the quiz -* setgrades Changes the maximum grades for questions in the quiz +* savechanges Saves the order and grades for questions in the quiz * repaginate Re-paginates the quiz * Actions affecting the question pool: * move Moves a question to a different category @@ -330,8 +330,10 @@ if (self.name == 'editquestion') { } } - if (isset($_REQUEST['setgrades']) and confirm_sesskey()) { - /// The grades have been updated, so update our internal list + if (isset($_REQUEST['savechanges']) and confirm_sesskey()) { + /// We need to save the new ordering (if given) and the new grades + $oldquestions = explode(",", $modform->questions); // the questions in the old order + $questions = array(); // for questions in the new order $rawgrades = $_POST; unset($modform->grades); foreach ($rawgrades as $key => $value) { // Parse input for question -> grades @@ -339,6 +341,24 @@ if (self.name == 'editquestion') { $key = substr($key,1); $modform->grades[$key] = $value; quiz_update_question_instance($modform->grades[$key], $key, $modform->instance); + } elseif (substr($key, 0, 1) == "o") { // Parse input for ordering info + $key = substr($key,1); + $questions[$value] = $oldquestions[$key]; + } + } + + // If ordering info was given, reorder the questions + if ($questions) { + ksort($questions); + $modform->questions = implode(",", $questions); + // Always have a page break at the end + $modform->questions = $modform->questions . ',0'; + // Avoid duplicate page breaks + while (strpos($modform->questions, ',0,0')) { + $modform->questions = str_replace(',0,0', ',0', $modform->questions); + } + if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) { + error('Could not save question list'); } } @@ -362,6 +382,7 @@ if (self.name == 'editquestion') { } if(isset($_REQUEST['showbreaks'])) { $SESSION->quiz_showbreaks = optional_param('showbreaks', 0, PARAM_BOOL); + $SESSION->quiz_reordertool = optional_param('reordertool', 0, PARAM_BOOL); } if(isset($_REQUEST['showhidden'])) { $SESSION->quiz_showhidden = optional_param('showhidden', 0, PARAM_BOOL); @@ -387,6 +408,9 @@ if (self.name == 'editquestion') { if (!isset($SESSION->quiz_showbreaks)) { $SESSION->quiz_showbreaks = ($CFG->quiz_questionsperpage < 2) ? 0 : 1; } + if (!isset($SESSION->quiz_reordertool)) { + $SESSION->quiz_reordertool = 0; + } $SESSION->modform = $modform; @@ -427,7 +451,7 @@ if (self.name == 'editquestion') { echo ""; echo "
\n"; - $sumgrades = quiz_print_question_list($modform, false, $SESSION->quiz_showbreaks); + $sumgrades = quiz_print_question_list($modform, false, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) { error('Failed to set sumgrades'); } @@ -465,7 +489,7 @@ if (self.name == 'editquestion') { echo ''; print_simple_box_start("center", "100%"); - $sumgrades = quiz_print_question_list($modform, true, $SESSION->quiz_showbreaks); + $sumgrades = quiz_print_question_list($modform, true, $SESSION->quiz_showbreaks, $SESSION->quiz_reordertool); if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $modform->instance)) { error('Failed to set sumgrades'); } diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 1c5dc9ecb0c..1b161aa9ec8 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -155,8 +155,9 @@ function quiz_update_question_instance($grade, $questionid, $quizid) { * $quiz->grades * @param boolean $allowdelete Indicates whether the delete icons should be displayed * @param boolean $showbreaks Indicates whether the page breaks should be displayed +* @param boolean $showbreaks Indicates whether the reorder tool should be displayed */ -function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { +function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $reordertool=false) { global $USER, $CFG, $QUIZ_QTYPES; $strorder = get_string("order"); @@ -204,12 +205,26 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { print_simple_box_start('center', '100%', '#ffffff', 0); echo "\n"; - echo "\n"; - foreach ($order as $qnum) { + echo "\n"; + foreach ($order as $i => $qnum) { + if ($qnum and empty($questions[$qnum])) { + continue; + } + // Show the re-ordering field if the tool is turned on. + // But don't show it in front of pagebreaks if they are hidden. + if ($reordertool) { + if ($qnum or $showbreaks) { + echo ''; + } else { + echo ''; + } + } else { + echo ''; + } if ($qnum == 0) { // This is a page break if ($showbreaks) { - echo ''; + echo ''; echo '\n"; - - echo "
$strorder$strquestionname$strtype$strgrade$straction
$strorder#$strquestionname$strtype$strgrade$straction
  '; echo ''; echo ''; @@ -232,13 +247,9 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { $count++; continue; } - if (empty($questions[$qnum])) { - continue; - } $question = $questions[$qnum]; $canedit = isteacheredit($question->course); - echo ""; echo ""; + echo ""; $count++; $sumgrade += $quiz->grades[$qnum]; } - echo "\n"; - echo "\n"; + echo "

Page break
"; if ($count != 0) { echo "sesskey\"> \n"; } else { echo ''; + '" tabindex="'.($lastindex+$qno).'" />'; } echo ''; @@ -286,38 +297,35 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { pixpath/t/removeright.gif\" border=\"0\" alt=\"$strremove\" />"; } - echo "
\n"; + echo "
\n"; print_string('total'); echo ": \n"; echo "$sumgrade"; echo " \n
\n"; + echo "
\n"; print_string('maximumgrade'); echo ": \n"; echo ''; echo ''; helpbutton("maxgrade", get_string("maximumgrade"), "quiz"); - echo "
\n"; - echo "
\n"; + echo '
'; + echo '
'; - echo "\n"; - echo "\n"; - echo "
 \n
\n"; print_simple_box_end(); echo "\n"; /// Form to choose to show pagebreaks and to repaginate quiz echo '
'; + echo ''; echo ''; echo ''; print_string('showbreaks', 'quiz'); - echo ' '; if ($showbreaks) { $perpage= array(); @@ -333,13 +340,21 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) { $perpage[$i] = $i; } $perpage[0] = get_string('allinone', 'quiz'); - echo '
'; - echo ''; + echo '
  '; print_string('repaginate', 'quiz', choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true)); - echo ' '; } + echo '
'; + echo ''; + print_string('reordertool', 'quiz'); + helpbutton('reorderingtool', get_string('reorderingtool', 'quiz'), 'quiz'); + + echo '
'; echo '
'; return $sumgrade;