5 * This page generally has two columns:
6 * The right column lists all available questions in a chosen category and
7 * allows them to be edited or more to be added. This column is only there if
8 * the quiz does not already have student attempts
9 * The left column lists all questions that have been added to the current quiz.
10 * The lecturer can add questions from the right hand list to the quiz or remove them
12 * The script also processes a number of actions:
13 * Actions affecting a quiz:
14 * up and down Changes the order of questions and page breaks
15 * addquestion Adds a single question to the quiz
16 * add Adds several selected questions to the quiz
17 * addrandom Adds a certain number of random questions to the quiz
18 * repaginate Re-paginates the quiz
19 * delete Removes a question from the quiz
20 * savechanges Saves the order and grades for questions in the quiz
23 * @author Martin Dougiamas and many others. This has recently been extensively
24 * rewritten by Gustav Delius and other members of the Serving Mathematics project
25 * {@link http://maths.york.ac.uk/serving_maths}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
29 require_once("../../config.php");
30 require_once($CFG->dirroot
.'/mod/quiz/editlib.php');
33 * Callback function called from question_list() function (which is called from showbank()
34 * Displays action icon as first action for each question.
36 function module_specific_actions($pageurl, $questionid, $cmid){
38 if (has_capability("mod/quiz:manage", get_context_instance(CONTEXT_MODULE
, $cmid))){
39 $straddtoquiz = get_string("addtoquiz", "quiz");
40 $out = "<a title=\"$straddtoquiz\" href=\"edit.php?".$pageurl->get_query_string()."&addquestion=$questionid&sesskey=".sesskey()."\"><img
41 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a> ";
48 * Callback function called from question_list() function (which is called from showbank()
49 * Displays button in form with checkboxes for each question.
51 function module_specific_buttons($cmid){
53 if (has_capability("mod/quiz:manage", get_context_instance(CONTEXT_MODULE
, $cmid))){
54 $straddtoquiz = get_string("addtoquiz", "quiz");
55 $out = "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
65 * Callback function called from question_list() function (which is called from showbank()
66 * Displays button in form with checkboxes for each question.
68 function module_specific_controls($totalnumber, $recurse, $categoryid, $cmid){
69 if (has_capability("mod/quiz:manage", get_context_instance(CONTEXT_MODULE
, $cmid))){
70 for ($i = 1;$i <= min(10, $totalnumber); $i++
) {
71 $randomcount[$i] = $i;
73 for ($i = 20;$i <= min(100, $totalnumber); $i +
= 10) {
74 $randomcount[$i] = $i;
77 $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
78 $out .= '<input type="hidden" name="recurse" value="'.$recurse.'" />';
79 $out .= "<input type=\"hidden\" name=\"categoryid\" value=\"$categoryid\" />";
80 $out .= ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
81 $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true);
88 list($thispageurl, $courseid, $cmid, $cm, $quiz, $pagevars) = question_edit_setup(true);
90 //these params are only passed from page request to request while we stay on this page
91 //otherwise they would go in question_edit_setup
92 $quiz_showbreaks = optional_param('showbreaks', -1, PARAM_BOOL
);
93 $quiz_reordertool = optional_param('reordertool', 0, PARAM_BOOL
);
94 if ($quiz_showbreaks > -1) {
95 $thispageurl->param('showbreaks', $quiz_showbreaks);
97 $quiz_showbreaks = ($CFG->quiz_questionsperpage
< 2) ?
0 : 1;
99 if ($quiz_reordertool != 0) {
100 $thispageurl->param('reordertool', $quiz_reordertool);
103 $strquizzes = get_string('modulenameplural', 'quiz');
104 $strquiz = get_string('modulename', 'quiz');
105 $streditingquestions = get_string('editquestions', "quiz");
106 $streditingquiz = get_string('editinga', 'moodle', $strquiz);
111 // Get the course object and related bits.
112 if (! $course = get_record("course", "id", $quiz->course
)) {
113 error("This course doesn't exist");
115 $coursecontext = get_context_instance(CONTEXT_COURSE
, $quiz->course
);
116 $quizcontext = get_context_instance(CONTEXT_MODULE
, $quiz->cmid
);
120 add_to_log($cm->course
, 'quiz', 'editquestions',
121 "view.php?id=$cm->id", "$quiz->id", $cm->id
);
123 require_capability('mod/quiz:manage', $quizcontext);
125 if (isset($quiz->instance
)
126 && empty($quiz->grades
)){ // Construct an array to hold all the grades.
127 $quiz->grades
= quiz_get_all_question_grades($quiz);
131 /// Now, check for commands on this page and modify variables as necessary
133 if (isset($_REQUEST['up']) and confirm_sesskey()) { /// Move the given question up a slot
134 $up = optional_param('up', 0, PARAM_INT
);
135 $questions = explode(",", $quiz->questions
);
136 if ($up > 0 and isset($questions[$up])) {
137 $prevkey = ($questions[$up-1] == 0) ?
$up-2 : $up-1;
138 $swap = $questions[$prevkey];
139 $questions[$prevkey] = $questions[$up];
140 $questions[$up] = $swap;
141 $quiz->questions
= implode(",", $questions);
142 // Always have a page break at the end
143 $quiz->questions
= $quiz->questions
. ',0';
144 // Avoid duplicate page breaks
145 $quiz->questions
= str_replace(',0,0', ',0', $quiz->questions
);
146 if (!set_field('quiz', 'questions', $quiz->questions
, 'id', $quiz->instance
)) {
147 error('Could not save question list');
152 if (isset($_REQUEST['down']) and confirm_sesskey()) { /// Move the given question down a slot
153 $down = optional_param('down', 0, PARAM_INT
);
154 $questions = explode(",", $quiz->questions
);
155 if ($down < count($questions)) {
156 $nextkey = ($questions[$down+
1] == 0) ?
$down+
2 : $down+
1;
157 $swap = $questions[$nextkey];
158 $questions[$nextkey] = $questions[$down];
159 $questions[$down] = $swap;
160 $quiz->questions
= implode(",", $questions);
161 // Avoid duplicate page breaks
162 $quiz->questions
= str_replace(',0,0', ',0', $quiz->questions
);
163 if (!set_field('quiz', 'questions', $quiz->questions
, 'id', $quiz->instance
)) {
164 error('Could not save question list');
169 if (isset($_REQUEST['addquestion']) and confirm_sesskey()) { /// Add a single question to the current quiz
170 quiz_add_quiz_question($_REQUEST['addquestion'], $quiz);
173 if (isset($_REQUEST['add']) and confirm_sesskey()) { /// Add selected questions to the current quiz
174 foreach ($_POST as $key => $value) { // Parse input for question ids
175 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
177 quiz_add_quiz_question($key, $quiz);
182 if (isset($_REQUEST['addrandom']) and confirm_sesskey()) { /// Add random questions to the quiz
183 $recurse = optional_param('recurse', 0, PARAM_BOOL
);
184 $categoryid = required_param('categoryid', PARAM_INT
);
185 $randomcount = required_param('randomcount', PARAM_INT
);
187 if (! $category = get_record('question_categories', 'id', $categoryid)) {
188 error('Category ID is incorrect');
190 $category->name
= addslashes($category->name
);
191 // find existing random questions in this category
193 if ($existingquestions = get_records_select('question', "qtype = '$random' AND category = '$category->id'")) {
194 // now remove the ones that are already used in this quiz
195 if ($questionids = explode(',', $quiz->questions
)) {
196 foreach ($questionids as $questionid) {
197 unset($existingquestions[$questionid]);
200 // now take as many of these as needed
202 while (($existingquestion = array_pop($existingquestions)) and ($i < $randomcount)) {
203 if ($existingquestion->questiontext
== $recurse) {
204 // this question has the right recurse property, so use it
205 quiz_add_quiz_question($existingquestion->id
, $quiz);
209 $randomcreate = $randomcount - $i; // the number of additional random questions needed.
211 $randomcreate = $randomcount;
214 if ($randomcreate > 0) {
216 $form->name
= get_string('random', 'quiz') .' ('. $category->name
.')';
217 $form->category
= $category->id
;
218 $form->questiontext
= $recurse; // we use the questiontext field to store the info
219 // on whether to include questions in subcategories
220 $form->questiontextformat
= 0;
222 $form->defaultgrade
= 1;
224 for ($i=0; $i<$randomcreate; $i++
) {
225 $form->stamp
= make_unique_id_code(); // Set the unique code (not to be changed)
226 $question = new stdClass
;
227 $question->qtype
= RANDOM
;
228 $question = $QTYPES[RANDOM
]->save_question($question, $form, $course);
229 if(!isset($question->id
)) {
230 error('Could not insert new random question!');
232 quiz_add_quiz_question($question->id
, $quiz);
237 if (isset($_REQUEST['repaginate']) and confirm_sesskey()) { /// Re-paginate the quiz
238 if (isset($_REQUEST['questionsperpage'])) {
239 $quiz->questionsperpage
= required_param('questionsperpage', PARAM_INT
);
240 if (!set_field('quiz', 'questionsperpage', $quiz->questionsperpage
, 'id', $quiz->id
)) {
241 error('Could not save number of questions per page');
244 $quiz->questions
= quiz_repaginate($quiz->questions
, $quiz->questionsperpage
);
245 if (!set_field('quiz', 'questions', $quiz->questions
, 'id', $quiz->id
)) {
246 error('Could not save layout');
250 if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
251 quiz_delete_quiz_question($_REQUEST['delete'], $quiz);
254 if (isset($_REQUEST['savechanges']) and confirm_sesskey()) {
255 /// We need to save the new ordering (if given) and the new grades
256 $oldquestions = explode(",", $quiz->questions
); // the questions in the old order
257 $questions = array(); // for questions in the new order
259 unset($quiz->grades
);
260 foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
261 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
263 $quiz->grades
[$key] = $value;
264 quiz_update_question_instance($quiz->grades
[$key], $key, $quiz->instance
);
265 } elseif (preg_match('!^q([0-9]+)$!', $key, $matches)) { // Parse input for ordering info
267 $questions[$value] = $oldquestions[$key];
271 // If ordering info was given, reorder the questions
274 $quiz->questions
= implode(",", $questions);
275 // Always have a page break at the end
276 $quiz->questions
= $quiz->questions
. ',0';
277 // Avoid duplicate page breaks
278 while (strpos($quiz->questions
, ',0,0')) {
279 $quiz->questions
= str_replace(',0,0', ',0', $quiz->questions
);
281 if (!set_field('quiz', 'questions', $quiz->questions
, 'id', $quiz->instance
)) {
282 error('Could not save question list');
286 // If rescaling is required save the new maximum
287 if (isset($_REQUEST['maxgrade'])) {
288 if (!quiz_set_grade(optional_param('maxgrade', 0), $quiz)) {
289 error('Could not set a new maximum grade for the quiz');
294 /// Delete any teacher preview attempts if the quiz has been modified
295 if (isset($_REQUEST['savechanges']) or isset($_REQUEST['delete']) or isset($_REQUEST['repaginate']) or isset($_REQUEST['addrandom']) or isset($_REQUEST['addquestion']) or isset($_REQUEST['up']) or isset($_REQUEST['down']) or isset($_REQUEST['add'])) {
296 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id
);
299 /// all commands have been dealt with, now print the page
301 if (empty($quiz->category
) or !record_exists('question_categories', 'id', $quiz->category
)) {
302 $category = get_default_question_category($course->id
);
303 $quiz->category
= $category->id
;
306 // Print basic page layout.
308 if (isset($quiz->instance
) and record_exists_select('quiz_attempts', "quiz = '$quiz->instance' AND preview = '0'")){
309 // one column layout with table of questions used in this quiz
310 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
311 ?
update_module_button($cm->id
, $course->id
, get_string('modulename', 'quiz'))
314 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
315 $navlinks[] = array('name' => format_string($quiz->name
), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
316 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
317 $navigation = build_navigation($navlinks);
319 print_header_simple($streditingquiz, '', $navigation, "", "",
320 true, $strupdatemodule);
322 $currenttab = 'edit';
329 $a->attemptnum
= count_records('quiz_attempts', 'quiz', $quiz->id
, 'preview', 0);
330 $a->studentnum
= count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
331 $a->studentstring
= $course->students
;
332 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->instance
, $course->id
)) {
333 error("Course Module ID was incorrect");
335 echo "<div class=\"attemptsnotice\">\n";
336 echo "<a href=\"report.php?mode=overview&id=$cm->id\">".get_string('numattempts', 'quiz', $a)."</a><br />".get_string("attemptsexist","quiz");
337 echo "</div><br />\n";
339 $sumgrades = quiz_print_question_list($quiz, $thispageurl, false, $quiz_showbreaks, $quiz_reordertool);
340 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance
)) {
341 error('Failed to set sumgrades');
345 print_footer($course);
349 // two column layout with quiz info in left column
350 $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
351 ?
update_module_button($cm->id
, $course->id
, get_string('modulename', 'quiz'))
354 $navlinks[] = array('name' => $strquizzes, 'link' => "index.php?id=$course->id", 'type' => 'activity');
355 $navlinks[] = array('name' => format_string($quiz->name
), 'link' => "view.php?q=$quiz->instance", 'type' => 'activityinstance');
356 $navlinks[] = array('name' => $streditingquiz, 'link' => '', 'type' => 'title');
357 $navigation = build_navigation($navlinks);
359 print_header_simple($streditingquiz, '', $navigation, "", "", true, $strupdatemodule);
361 $currenttab = 'edit';
366 echo '<table border="0" style="width:100%" cellpadding="2" cellspacing="0">';
367 echo '<tr><td style="width:50%" valign="top">';
368 print_box_start('generalbox quizquestions');
369 print_heading(get_string('questionsinthisquiz', 'quiz'), '', 2);
371 $sumgrades = quiz_print_question_list($quiz, $thispageurl, true, $quiz_showbreaks, $quiz_reordertool);
372 if (!set_field('quiz', 'sumgrades', $sumgrades, 'id', $quiz->instance
)) {
373 error('Failed to set sumgrades');
378 echo '</td><td style="width:50%" valign="top">';
380 question_showbank($thispageurl, $cm, $pagevars['qpage'], $pagevars['qperpage'], $pagevars['qsortorder'], $pagevars['qsortorderdecoded'],
381 $pagevars['cat'], $pagevars['recurse'], $pagevars['showhidden'], $pagevars['showquestiontext']);
386 print_footer($course);