Updated question scripts to work in their new location
This commit is contained in:
+17
-335
@@ -1,16 +1,19 @@
|
||||
<?php // $Id$
|
||||
/**
|
||||
* Functions used by edit.php to edit quizzes
|
||||
* Functions used by showbank.php to show question editing interface
|
||||
*
|
||||
* TODO: currently the function question_list still provides controls specific
|
||||
* to the quiz module. This needs to be generalised.
|
||||
*
|
||||
* @version $Id$
|
||||
* @author Martin Dougiamas and many others. This has recently been extensively
|
||||
* rewritten by members of the Serving Mathematics project
|
||||
* {@link http://maths.york.ac.uk/serving_maths}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package quiz
|
||||
* @package question
|
||||
*/
|
||||
|
||||
require_once("locallib.php");
|
||||
require_once($CFG->libdir.'/questionlib.php');
|
||||
|
||||
/**
|
||||
* Array of question types names translated to the user's language
|
||||
@@ -38,329 +41,8 @@ if ($rqp_types = get_records('quiz_rqp_types')) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a question from a quiz
|
||||
*
|
||||
* Deletes a question or a pagebreak from a quiz by updating $modform
|
||||
* as well as the quiz, quiz_question_instances
|
||||
* @return boolean false if the question was not in the quiz
|
||||
* @param int $id The id of the question to be deleted
|
||||
* @param object $modform The extended quiz object as used by edit.php
|
||||
* This is updated by this function
|
||||
*/
|
||||
function quiz_delete_quiz_question($id, &$modform) {
|
||||
// TODO: For the sake of safety check that this question can be deleted
|
||||
// safely, i.e., that it is not already in use.
|
||||
$questions = explode(",", $modform->questions);
|
||||
|
||||
// only do something if this question exists
|
||||
if (!isset($questions[$id])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$question = $questions[$id];
|
||||
unset($questions[$id]);
|
||||
// If we deleted the question at the top and it was followed by
|
||||
// a page break then delete page break as well
|
||||
if ($id == 0 and $questions[1] == 0) {
|
||||
unset($questions[1]);
|
||||
}
|
||||
$modform->questions = implode(",", $questions);
|
||||
// Avoid duplicate page breaks
|
||||
$modform->questions = str_replace(',0,0', ',0', $modform->questions);
|
||||
// save new questionlist in database
|
||||
if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
|
||||
error('Could not save question list');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a question to a quiz
|
||||
*
|
||||
* Adds a question to a quiz by updating $modform as well as the
|
||||
* quiz and quiz_question_instances tables. It also adds a page break
|
||||
* if required.
|
||||
* @return boolean false if the question was already in the quiz
|
||||
* @param int $id The id of the question to be added
|
||||
* @param object $modform The extended quiz object as used by edit.php
|
||||
* This is updated by this function
|
||||
*/
|
||||
function quiz_add_quiz_question($id, &$modform) {
|
||||
$questions = explode(",", $modform->questions);
|
||||
|
||||
if (in_array($id, $questions)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// remove ending page break if it is not needed
|
||||
if ($breaks = array_keys($questions, 0)) {
|
||||
// determine location of the last two page breaks
|
||||
$end = end($breaks);
|
||||
$last = prev($breaks);
|
||||
$last = $last ? $last : -1;
|
||||
if (!$modform->questionsperpage or (($end - $last -1) < $modform->questionsperpage)) {
|
||||
array_pop($questions);
|
||||
}
|
||||
}
|
||||
// add question
|
||||
$questions[] = $id;
|
||||
// add ending page break
|
||||
$questions[] = 0;
|
||||
|
||||
// Save new questionslist in database
|
||||
$modform->questions = implode(",", $questions);
|
||||
if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->id)) {
|
||||
error('Could not save question list');
|
||||
}
|
||||
|
||||
// update question grades
|
||||
$questionrecord = get_record("quiz_questions", "id", $id);
|
||||
$modform->grades[$id] = $questionrecord->defaultgrade;
|
||||
quiz_update_question_instance($modform->grades[$id], $id, $modform->instance);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save changes to question instance
|
||||
*
|
||||
* Saves changes to the question grades in the quiz_question_instances table.
|
||||
* It does not update 'sumgrades' in the quiz table.
|
||||
* @return boolean Indicates success or failure.
|
||||
* @param integer grade The maximal grade for the question
|
||||
* @param integer $questionid The id of the question
|
||||
* @param integer $quizid The id of the quiz to update / add the instances for.
|
||||
*/
|
||||
function quiz_update_question_instance($grade, $questionid, $quizid) {
|
||||
if ($instance = get_record("quiz_question_instances", "quiz", $quizid, 'question', $questionid)) {
|
||||
$instance->grade = $grade;
|
||||
return update_record('quiz_question_instances', $instance);
|
||||
} else {
|
||||
unset($instance);
|
||||
$instance->quiz = $quizid;
|
||||
$instance->question = $questionid;
|
||||
$instance->grade = $grade;
|
||||
return insert_record("quiz_question_instances", $instance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a list of quiz questions in a small layout form with knobs
|
||||
*
|
||||
* @return int sum of maximum grades
|
||||
* @param object $quiz This is not the standard quiz object used elsewhere but
|
||||
* it contains the quiz layout in $quiz->questions and the grades in
|
||||
* $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, $reordertool=false) {
|
||||
global $USER, $CFG, $QUIZ_QTYPES;
|
||||
|
||||
$strorder = get_string("order");
|
||||
$strquestionname = get_string("questionname", "quiz");
|
||||
$strgrade = get_string("grade");
|
||||
$strremove = get_string('remove', 'quiz');
|
||||
$stredit = get_string("edit");
|
||||
$straction = get_string("action");
|
||||
$strmoveup = get_string("moveup");
|
||||
$strmovedown = get_string("movedown");
|
||||
$strsavegrades = get_string("savegrades", "quiz");
|
||||
$strtype = get_string("type", "quiz");
|
||||
$strpreview = get_string("preview", "quiz");
|
||||
|
||||
if (!$quiz->questions) {
|
||||
echo "<p align=\"center\">";
|
||||
print_string("noquestions", "quiz");
|
||||
echo "</p>";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!$questions = get_records_sql("SELECT q.*,c.course
|
||||
FROM {$CFG->prefix}quiz_questions q,
|
||||
{$CFG->prefix}quiz_categories c
|
||||
WHERE q.id in ($quiz->questions)
|
||||
AND q.category = c.id")) {
|
||||
echo "<p align=\"center\">";
|
||||
print_string("noquestions", "quiz");
|
||||
echo "</p>";
|
||||
return 0;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$qno = 1;
|
||||
$sumgrade = 0;
|
||||
$order = explode(",", $quiz->questions);
|
||||
$lastindex = count($order)-1;
|
||||
// If the list does not end with a pagebreak then add it on.
|
||||
if ($order[$lastindex] != 0) {
|
||||
$order[] = 0;
|
||||
$lastindex++;
|
||||
}
|
||||
echo "<form method=\"post\" action=\"edit.php\">";
|
||||
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
|
||||
|
||||
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 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 '<td colspan ="3"> </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>';
|
||||
echo '<td><hr noshade="noshade" /></td><td width="45">';
|
||||
if ($count > 1) {
|
||||
echo "<a title=\"$strmoveup\" href=\"edit.php?up=$count&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/up.gif\" border=\"0\" alt=\"$strmoveup\" /></a>";
|
||||
}
|
||||
echo ' ';
|
||||
if ($count < $lastindex) {
|
||||
echo "<a title=\"$strmovedown\" href=\"edit.php?down=$count&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/down.gif\" border=\"0\" alt=\"$strmovedown\" /></a>";
|
||||
|
||||
echo "<a title=\"$strremove\" href=\"edit.php?delete=$count&sesskey=$USER->sesskey\">
|
||||
<img src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strremove\" /></a>";
|
||||
}
|
||||
echo '</td></tr></table></td>';
|
||||
echo '<td colspan="2"> </td></tr>';
|
||||
}
|
||||
$count++;
|
||||
continue;
|
||||
}
|
||||
$question = $questions[$qnum];
|
||||
$canedit = isteacheredit($question->course);
|
||||
|
||||
echo "<td>";
|
||||
if ($count != 0) {
|
||||
echo "<a title=\"$strmoveup\" href=\"edit.php?up=$count&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/up.gif\" border=\"0\" alt=\"$strmoveup\" /></a>";
|
||||
}
|
||||
echo "</td>";
|
||||
echo "<td>";
|
||||
if ($count < $lastindex-1) {
|
||||
echo "<a title=\"$strmovedown\" href=\"edit.php?down=$count&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/down.gif\" border=\"0\" alt=\"$strmovedown\" /></a>";
|
||||
}
|
||||
echo "</td>";
|
||||
|
||||
if (!$quiz->shufflequestions) {
|
||||
// Print and increment question number
|
||||
echo '<td>'.($question->length ? $qno : ' ').'</td>';
|
||||
$qno += $question->length;
|
||||
} else {
|
||||
echo '<td> </td>';
|
||||
}
|
||||
|
||||
echo "<td>$question->name</td>";
|
||||
echo "<td align=\"center\">";
|
||||
quiz_print_question_icon($question, $canedit);
|
||||
echo "</td>";
|
||||
echo '<td align="left">';
|
||||
if ($question->qtype == DESCRIPTION) {
|
||||
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="'.($lastindex+$qno).'" />';
|
||||
}
|
||||
echo '</td><td align="center">';
|
||||
|
||||
$context = $quiz->id ? '&contextquiz='.$quiz->id : '';
|
||||
$quiz_id = $quiz->id ? '&quizid=' . $quiz->id : '';
|
||||
echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/mod/quiz/preview.php?id=$qnum$quiz_id','$strpreview','scrollbars=yes,resizable=yes,width=700,height=480', false)\">
|
||||
<img src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a>";
|
||||
if ($canedit) {
|
||||
echo "<a title=\"$stredit\" href=\"question.php?id=$qnum$context\">
|
||||
<img src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>";
|
||||
}
|
||||
if ($allowdelete) {
|
||||
echo "<a title=\"$strremove\" href=\"edit.php?delete=$count&sesskey=$USER->sesskey\">
|
||||
<img src=\"$CFG->pixpath/t/removeright.gif\" border=\"0\" alt=\"$strremove\" /></a>";
|
||||
}
|
||||
|
||||
echo "</td></tr>";
|
||||
$count++;
|
||||
$sumgrade += $quiz->grades[$qnum];
|
||||
}
|
||||
|
||||
echo "<tr><td colspan=\"6\" align=\"right\">\n";
|
||||
print_string('total');
|
||||
echo ": <td align=\"left\">\n";
|
||||
echo "<b>$sumgrade</b>";
|
||||
echo "</td><td> \n</td></tr>\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></table>\n";
|
||||
|
||||
echo '<div align="center"><input type="submit" value="'.get_string('savechanges').'" />';
|
||||
echo '<input type="hidden" name="savechanges" value="save" /></div>';
|
||||
|
||||
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) {
|
||||
echo ' checked="checked"';
|
||||
}
|
||||
echo ' onchange="document.showbreaks.submit(); return true;" />';
|
||||
print_string('showbreaks', 'quiz');
|
||||
|
||||
if ($showbreaks) {
|
||||
$perpage= array();
|
||||
for ($i=0; $i<=50; ++$i) {
|
||||
$perpage[$i] = $i;
|
||||
}
|
||||
$perpage[0] = get_string('allinone', 'quiz');
|
||||
echo '<br /> ';
|
||||
print_string('repaginate', 'quiz',
|
||||
choose_from_menu($perpage, 'questionsperpage', $quiz->questionsperpage, '', '', '', true));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function quiz_print_category_form($course, $current, $recurse=1, $showhidden=false) {
|
||||
function question_category_form($course, $current, $recurse=1, $showhidden=false) {
|
||||
global $CFG;
|
||||
/// Prints a form to choose categories
|
||||
|
||||
/// Make sure the default category exists for this course
|
||||
@@ -393,7 +75,7 @@ function quiz_print_category_form($course, $current, $recurse=1, $showhidden=fal
|
||||
echo "</td><td>";
|
||||
popup_form ("edit.php?cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
|
||||
echo "</td><td align=\"right\">";
|
||||
echo "<form method=\"get\" action=\"category.php\">";
|
||||
echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
|
||||
echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
|
||||
echo "<input type=\"submit\" value=\"$streditcats\" />";
|
||||
echo "</form>";
|
||||
@@ -433,7 +115,7 @@ function quiz_print_category_form($course, $current, $recurse=1, $showhidden=fal
|
||||
* @param int $perpage Number of questions to show per page
|
||||
* @param boolean $showhidden True if also hidden questions should be displayed
|
||||
*/
|
||||
function quiz_print_cat_question_list($course, $categoryid, $quizid,
|
||||
function question_list($course, $categoryid, $quizid,
|
||||
$recurse=1, $page, $perpage, $showhidden=false, $sortorder='qtype, name ASC') {
|
||||
global $QUIZ_QUESTION_TYPE, $USER, $CFG;
|
||||
|
||||
@@ -483,7 +165,7 @@ function quiz_print_cat_question_list($course, $categoryid, $quizid,
|
||||
if (isteacheredit($category->course)) {
|
||||
echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
|
||||
echo '<td valign="top" align="right">';
|
||||
popup_form ("question.php?category=$category->id&qtype=", $QUIZ_QUESTION_TYPE, "addquestion",
|
||||
popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&qtype=", $QUIZ_QUESTION_TYPE, "addquestion",
|
||||
"", "choose", "", "", false, "self");
|
||||
echo '</td><td width="10" valign="top" align="right">';
|
||||
helpbutton("questiontypes", $strcreatenewquestion, "quiz");
|
||||
@@ -497,11 +179,11 @@ function quiz_print_cat_question_list($course, $categoryid, $quizid,
|
||||
|
||||
echo '<tr><td colspan="3" align="right"><font size="2">';
|
||||
if (isteacheredit($category->course)) {
|
||||
echo '<a href="import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
|
||||
echo '<a href="'.$CFG->wwwroot.'/mod/quiz/import.php?category='.$category->id.'">'.$strimportquestions.'</a>';
|
||||
helpbutton("import", $strimportquestions, "quiz");
|
||||
echo ' | ';
|
||||
}
|
||||
echo "<a href=\"export.php?category={$category->id}&courseid={$course->id}\">$strexportquestions</a>";
|
||||
echo "<a href=\"$CFG->wwwroot/mod/quiz/export.php?category={$category->id}&courseid={$course->id}\">$strexportquestions</a>";
|
||||
helpbutton("export", $strexportquestions, "quiz");
|
||||
echo '</font></td></tr>';
|
||||
|
||||
@@ -562,17 +244,17 @@ function quiz_print_cat_question_list($course, $categoryid, $quizid,
|
||||
echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/moveleft.gif\" border=\"0\" alt=\"$straddtoquiz\" /></a> ";
|
||||
}
|
||||
echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/mod/quiz/preview.php?id=$question->id&quizid=$quizid','$strpreview','scrollbars=yes,resizable=yes,width=700,height=480', false)\"><img
|
||||
echo "<a title=\"$strpreview\" href=\"javascript:void();\" onClick=\"openpopup('/question/preview.php?id=$question->id&quizid=$quizid','$strpreview','scrollbars=yes,resizable=yes,width=700,height=480', false)\"><img
|
||||
src=\"$CFG->pixpath/t/preview.gif\" border=\"0\" alt=\"$strpreview\" /></a> ";
|
||||
if ($canedit) {
|
||||
echo "<a title=\"$stredit\" href=\"question.php?id=$question->id\"><img
|
||||
echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
|
||||
src=\"$CFG->pixpath/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a> ";
|
||||
// hide-feature
|
||||
if($question->hidden) {
|
||||
echo "<a title=\"$strrestore\" href=\"question.php?id=$question->id&hide=0&sesskey=$USER->sesskey\"><img
|
||||
echo "<a title=\"$strrestore\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&hide=0&sesskey=$USER->sesskey\"><img
|
||||
src=\"$CFG->pixpath/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
|
||||
} else {
|
||||
echo "<a title=\"$strdelete\" href=\"question.php?id=$question->id&delete=$question->id\"><img
|
||||
echo "<a title=\"$strdelete\" href=\"$CFG->wwwroot/question/question.php?id=$question->id&delete=$question->id\"><img
|
||||
src=\"$CFG->pixpath/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user