Added the much-requested question reordering tool

This commit is contained in:
gustav_delius
2006-02-19 16:34:06 +00:00
parent 1c34035a40
commit 14afbbf034
2 changed files with 67 additions and 28 deletions
+29 -5
View File
@@ -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 "</form>";
echo "</center><br/ >\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 '<tr><td width="50%" valign="top">';
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');
}
+38 -23
View File
@@ -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 "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
echo "<tr><th colspan=\"3\" nowrap=\"nowrap\" class=\"header\">$strorder</th><th align=\"left\" width=\"100%\" nowrap=\"nowrap\" class=\"header\">$strquestionname</th><th nowrap=\"nowrap\" class=\"header\">$strtype</th><th nowrap=\"nowrap\" class=\"header\">$strgrade</th><th align=\"center\" width=\"60\" nowrap=\"nowrap\" class=\"header\">$straction</th></tr>\n";
foreach ($order as $qnum) {
echo "<tr><th colspan=\"3\" nowrap=\"nowrap\" class=\"header\">$strorder</th><th class=\"header\">#</th><th align=\"left\" width=\"100%\" nowrap=\"nowrap\" class=\"header\">$strquestionname</th><th nowrap=\"nowrap\" class=\"header\">$strtype</th><th nowrap=\"nowrap\" class=\"header\">$strgrade</th><th align=\"center\" width=\"60\" nowrap=\"nowrap\" class=\"header\">$straction</th></tr>\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 '<tr><td><input type="text" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
} else {
echo '<tr><td><input type="hidden" name="o'.$i.'" size="2" value="'.(10*$count+10).'" /></td>';
}
} else {
echo '<tr><td></td>';
}
if ($qnum == 0) { // This is a page break
if ($showbreaks) {
echo '<tr><td colspan ="3">&nbsp;</td>';
echo '<td colspan ="3">&nbsp;</td>';
echo '<td><table width="100%" style="line-height:11px; font-size:9px; margin: -5px -5px;"><tr>';
echo '<td><hr noshade="noshade" /></td>';
echo '<td width="50">Page break</td>';
@@ -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 "<tr>";
echo "<td>";
if ($count != 0) {
echo "<a title=\"$strmoveup\" href=\"edit.php?up=$count&amp;sesskey=$USER->sesskey\"><img
@@ -269,7 +280,7 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) {
echo "<input type=\"hidden\" name=\"q$qnum\" value=\"0\" /> \n";
} else {
echo '<input type="text" name="q'.$qnum.'" size="2" value="'.$quiz->grades[$qnum].
'" tabindex="'.$qno.'" />';
'" tabindex="'.($lastindex+$qno).'" />';
}
echo '</td><td align="center">';
@@ -286,38 +297,35 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) {
<img src=\"$CFG->pixpath/t/removeright.gif\" border=\"0\" alt=\"$strremove\" /></a>";
}
echo "</td>";
echo "</td></tr>";
$count++;
$sumgrade += $quiz->grades[$qnum];
}
echo "<tr><td colspan=\"5\" align=\"right\">\n";
echo "<tr><td colspan=\"6\" align=\"right\">\n";
print_string('total');
echo ": <td align=\"left\">\n";
echo "<b>$sumgrade</b>";
echo "</td><td>&nbsp;\n</td></tr>\n";
echo "<tr><td colspan=\"5\" align=\"right\">\n";
echo "<tr><td colspan=\"6\" align=\"right\">\n";
print_string('maximumgrade');
echo ": <td align=\"left\">\n";
echo '<input type="text" name="maxgrade" size="2" tabindex="'.($qno+1)
.'" value="'.$quiz->grade.'" />';
echo '</td><td align="left">';
helpbutton("maxgrade", get_string("maximumgrade"), "quiz");
echo "</td></tr>\n";
echo "</td></tr></table>\n";
echo "<tr><td colspan=\"6\" align=\"right\">\n";
echo '<div align="center"><input type="submit" value="'.get_string('savechanges').'" />';
echo '<input type="hidden" name="savechanges" value="save" /></div>';
echo "<input type=\"submit\" value=\"$strsavegrades\" />\n";
echo "<input type=\"hidden\" name=\"setgrades\" value=\"save\" />\n";
echo "</td><td>&nbsp;</td><td>\n</td></tr>\n";
echo "</table>\n";
print_simple_box_end();
echo "</form>\n";
/// Form to choose to show pagebreaks and to repaginate quiz
echo '<form method="post" action="edit.php" name="showbreaks">';
echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
echo '<input type="hidden" name="showbreaks" value="0" />';
echo '<input type="checkbox" name="showbreaks" value="1"';
if ($showbreaks) {
@@ -325,7 +333,6 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true) {
}
echo ' onchange="document.showbreaks.submit(); return true;" />';
print_string('showbreaks', 'quiz');
echo ' <noscript><input type="submit" value="'. get_string('go') .'" /></noscript>';
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 '<br />';
echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
echo '<br />&nbsp;&nbsp;';
print_string('repaginate', 'quiz',
choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true));
echo ' <input type="submit" name="repaginate" value="'. get_string('go') .'" />';
}
echo '<br /><input type="hidden" name="reordertool" value="0" />';
echo '<input type="checkbox" name="reordertool" value="1"';
if ($reordertool) {
echo ' checked="checked"';
}
echo ' onchange="document.showbreaks.submit(); return true;" />';
print_string('reordertool', 'quiz');
helpbutton('reorderingtool', get_string('reorderingtool', 'quiz'), 'quiz');
echo '<div align="center"><input type="submit" name="repaginate" value="'. get_string('go') .'" /></div>';
echo '</form>';
return $sumgrade;