Matching qestion type was failing to correctly determine wheterh the student had...
[moodle.git] / question / editlib.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
b72ff476 3 * Functions used to show question editing interface
4323d029 4 *
5 * TODO: currently the function question_list still provides controls specific
6 * to the quiz module. This needs to be generalised.
7 *
8 * @author Martin Dougiamas and many others. This has recently been extensively
9 * rewritten by members of the Serving Mathematics project
10 * {@link http://maths.york.ac.uk/serving_maths}
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * @package questionbank
13 */
516cf3eb 14
4fbfd971 15require_once($CFG->libdir.'/questionlib.php');
516cf3eb 16
f92cf442 17define('DEFAULT_QUESTIONS_PER_PAGE', 20);
18
86909ce0 19function get_module_from_cmid($cmid){
20 global $CFG;
21 if (!$cmrec = get_record_sql("SELECT cm.*, md.name as modname
22 FROM {$CFG->prefix}course_modules cm,
23 {$CFG->prefix}modules md
24 WHERE cm.id = '$cmid' AND
25 md.id = cm.module")){
26 error('cmunknown');
27 } elseif (!$modrec =get_record($cmrec->modname, 'id', $cmrec->instance)) {
28 error('cmunknown');
29 }
30 $modrec->instance = $modrec->id;
31 $modrec->cmid = $modrec->id;
32
33 return array($modrec, $cmrec);
34}
4fbfd971 35/**
36* Function to read all questions for category into big array
37*
38* @param int $category category number
36e2232e 39* @param bool $noparent if true only questions with NO parent will be selected
40* @param bool $recurse include subdirectories
5fd8f999 41* @param bool $export set true if this is called by questionbank export
4fbfd971 42* @author added by Howard Miller June 2004
43*/
5fd8f999 44function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
4fbfd971 45
46 global $QTYPES;
47
48 // questions will be added to an array
49 $qresults = array();
50
51 // build sql bit for $noparent
52 $npsql = '';
53 if ($noparent) {
54 $npsql = " and parent='0' ";
55 }
56
36e2232e 57 // get (list) of categories
58 if ($recurse) {
59 $categorylist = question_categorylist( $category->id );
60 }
61 else {
62 $categorylist = $category->id;
63 }
64
4fbfd971 65 // get the list of questions for the category
36e2232e 66 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
4fbfd971 67
68 // iterate through questions, getting stuff we need
69 foreach($questions as $question) {
70 $questiontype = $QTYPES[$question->qtype];
5fd8f999 71 $question->export_process = $export;
4fbfd971 72 $questiontype->get_question_options( $question );
73 $qresults[] = $question;
74 }
75 }
76
77 return $qresults;
78}
79
80/**
81* Gets the default category in a course
82*
83* It returns the first category with no parent category. If no categories
84* exist yet then one is created.
85* @return object The default category
86* @param integer $courseid The id of the course whose default category is wanted
87*/
88function get_default_question_category($courseid) {
b030a630 89 // If it already exists, just return it.
90 if ($category = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", 'id', '*', '', 1)) {
91 return reset($category);
4fbfd971 92 }
93
94 // Otherwise, we need to make one
b030a630 95 $category = new stdClass;
4fbfd971 96 $category->name = get_string("default", "quiz");
97 $category->info = get_string("defaultinfo", "quiz");
98 $category->course = $courseid;
99 $category->parent = 0;
b030a630 100 $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
4fbfd971 101 $category->publish = 0;
102 $category->stamp = make_unique_id_code();
103
104 if (!$category->id = insert_record("question_categories", $category)) {
105 notify("Error creating a default category!");
106 return false;
107 }
108 return $category;
109}
110
4fbfd971 111/**
112 * prints a form to choose categories
113 */
86909ce0 114function question_category_form($course, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
e586cfb4 115 global $CFG;
516cf3eb 116
117/// Make sure the default category exists for this course
b030a630 118 get_default_question_category($course->id);
516cf3eb 119
120/// Get all the existing categories now
b030a630 121 $catmenu = question_category_options($course->id, true);
dcd51df2 122
516cf3eb 123 $strcategory = get_string("category", "quiz");
124 $strshow = get_string("show", "quiz");
125 $streditcats = get_string("editcategories", "quiz");
126
9ab75b2b 127 popup_form ("edit.php?".$pageurl->get_query_string()."&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self", "<strong>$strcategory</strong>");
0fd5feef 128
d2ce367f 129 echo '<form method="post" action="edit.php" id="displayoptions">';
0fd5feef 130 echo "<fieldset class='invisiblefieldset'>";
86909ce0 131 echo $pageurl->hidden_params_out();
624cbc9c 132 question_category_form_checkbox('recurse', $recurse);
133 question_category_form_checkbox('showhidden', $showhidden);
134 question_category_form_checkbox('showquestiontext', $showquestiontext);
135 echo '<noscript><div class="centerpara"><input type="submit" value="'. get_string('go') .'" />';
136 echo '</div></noscript></fieldset></form>';
137}
138
139/**
140 * Private funciton to help the preceeding function.
141 */
142function question_category_form_checkbox($name, $checked) {
143 echo '<div><input type="hidden" id="' . $name . '_off" name="' . $name . '" value="0" />';
144 echo '<input type="checkbox" id="' . $name . '_on" name="' . $name . '" value="1"';
145 if ($checked) {
516cf3eb 146 echo ' checked="checked"';
147 }
d2ce367f 148 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
624cbc9c 149 echo '<label for="' . $name . '_on">';
150 print_string($name, 'quiz');
151 echo "</label></div>\n";
516cf3eb 152}
153
516cf3eb 154/**
155* Prints the table of questions in a category with interactions
156*
157* @param object $course The course object
158* @param int $categoryid The id of the question category to be displayed
86909ce0 159* @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
516cf3eb 160* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
161* @param int $page The number of the page to be displayed
162* @param int $perpage Number of questions to show per page
163* @param boolean $showhidden True if also hidden questions should be displayed
dcc2ffde 164* @param boolean $showquestiontext whether the text of each question should be shown in the list
516cf3eb 165*/
86909ce0 166function question_list($course, $pageurl, $categoryid, $cm = null,
624cbc9c 167 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC',
168 $showquestiontext = false) {
95947ac9 169 global $QTYPE_MENU, $USER, $CFG, $THEME;
3765fd53 170
50530eb7 171 $qtypemenu = $QTYPE_MENU;
172 if ($rqp_types = get_records('question_rqp_types')) {
173 foreach($rqp_types as $type) {
174 $qtypemenu['rqp_'.$type->id] = $type->name;
175 }
176 }
177
516cf3eb 178 $strcategory = get_string("category", "quiz");
179 $strquestion = get_string("question", "quiz");
180 $straddquestions = get_string("addquestions", "quiz");
181 $strimportquestions = get_string("importquestions", "quiz");
182 $strexportquestions = get_string("exportquestions", "quiz");
183 $strnoquestions = get_string("noquestions", "quiz");
184 $strselect = get_string("select", "quiz");
185 $strselectall = get_string("selectall", "quiz");
186 $strselectnone = get_string("selectnone", "quiz");
187 $strcreatenewquestion = get_string("createnewquestion", "quiz");
188 $strquestionname = get_string("questionname", "quiz");
189 $strdelete = get_string("delete");
190 $stredit = get_string("edit");
191 $straction = get_string("action");
192 $strrestore = get_string('restore');
193
194 $straddtoquiz = get_string("addtoquiz", "quiz");
195 $strtype = get_string("type", "quiz");
196 $strcreatemultiple = get_string("createmultiple", "quiz");
197 $strpreview = get_string("preview","quiz");
198
199 if (!$categoryid) {
0fd5feef 200 echo "<p style=\"text-align:center;\"><b>";
516cf3eb 201 print_string("selectcategoryabove", "quiz");
202 echo "</b></p>";
516cf3eb 203 return;
204 }
205
dcc2ffde 206 if (!$category = get_record('question_categories', 'id', $categoryid)) {
207 notify('Category not found!');
516cf3eb 208 return;
209 }
dcc2ffde 210 $canedit = has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $category->course));
86909ce0 211
212 if ($cm AND $cm->modname == 'quiz') {
213 $editingquiz = has_capability("mod/quiz:manage", get_context_instance(CONTEXT_MODULE, $cm->id));
214 $quizid = $cm->instance;
215 } else {
216 $editingquiz = false;
217 $quizid = 0;
dcc2ffde 218 }
219
09275894 220 echo '<div class="boxaligncenter">';
4abc23b2 221 $formatoptions = new stdClass;
7347c60b 222 $formatoptions->noclean = true;
223 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
516cf3eb 224
225 echo '<table><tr>';
226
dcc2ffde 227 // check if editing questions in this category is allowed
228 if ($canedit) {
516cf3eb 229 echo '<td valign="top" align="right">';
86909ce0 230 $returnurl = urlencode($pageurl->out());
9ab75b2b 231 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
232 array('cmid' => $cm->id,
233 'returnurl' => $returnurl,
234 'category' => $category->id));
235 popup_form ($questionurl->out().'&amp;qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
624cbc9c 236 echo '</td><td valign="top" align="right">';
516cf3eb 237 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
dcc2ffde 238 echo '</td>';
516cf3eb 239 }
240 else {
dcc2ffde 241 echo '<td>';
516cf3eb 242 print_string("publishedit","quiz");
dcc2ffde 243 echo '</td>';
516cf3eb 244 }
245
dcc2ffde 246 echo '</tr></table>';
09275894 247 echo '</div>';
516cf3eb 248
dc1f00de 249 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 250
251 // hide-feature
252 $showhidden = $showhidden ? '' : " AND hidden = '0'";
253
4f48fb42 254 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
0fd5feef 255 echo "<p style=\"text-align:center;\">";
516cf3eb 256 print_string("noquestions", "quiz");
257 echo "</p>";
258 return;
259 }
260
4f48fb42 261 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
516cf3eb 262 // There are no questions on the requested page.
263 $page = 0;
4f48fb42 264 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
516cf3eb 265 // There are no questions at all
0fd5feef 266 echo "<p style=\"text-align:center;\">";
516cf3eb 267 print_string("noquestions", "quiz");
268 echo "</p>";
269 return;
270 }
271 }
272
b72ff476 273 print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
516cf3eb 274
86909ce0 275 echo '<form method="post" action="edit.php">';
624cbc9c 276 echo '<fieldset class="invisiblefieldset" style="display: block;">';
516cf3eb 277 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
b72ff476 278 echo $pageurl->hidden_params_out(array('qsortorder'));
624cbc9c 279 echo '<table id="categoryquestions" style="width: 100%"><tr>';
280 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
516cf3eb 281
282 $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
283 'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
284 'id ASC' => get_string("sortage", "quiz"));
b72ff476 285 $orderselect = choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
09275894 286 $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
624cbc9c 287 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
288 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
516cf3eb 289 echo "</tr>\n";
290 foreach ($questions as $question) {
624cbc9c 291 $nameclass = '';
292 $textclass = '';
293 if ($question->hidden) {
294 $nameclass = 'dimmed_text';
295 $textclass = 'dimmed_text';
296 }
297 if ($showquestiontext) {
298 $nameclass .= ' header';
299 }
300 if ($nameclass) {
301 $nameclass = 'class="' . $nameclass . '"';
302 }
303 if ($textclass) {
304 $textclass = 'class="' . $textclass . '"';
305 }
306
307 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
3765fd53 308
309 // add to quiz
dcc2ffde 310 if ($editingquiz) {
86909ce0 311 echo "<a title=\"$straddtoquiz\" href=\"edit.php?".$pageurl->get_query_string()."&amp;addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 312 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
516cf3eb 313 }
3765fd53 314
315 // preview
7d87171b 316 link_to_popup_window('/question/preview.php?id=' . $question->id . '&amp;quizid=' . $quizid, 'questionpreview',
317 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
318 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS);
3765fd53 319
320 // edit, hide, delete question, using question capabilities, not quiz capabilieies
dcc2ffde 321 if ($canedit) {
e586cfb4 322 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
0fd5feef 323 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
516cf3eb 324 // hide-feature
325 if($question->hidden) {
86909ce0 326 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 327 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
516cf3eb 328 } else {
86909ce0 329 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
0fd5feef 330 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
516cf3eb 331 }
332 }
333 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
334 echo "</td>\n";
335
70ed7667 336 echo "<td $nameclass>" . format_string($question->name) . "</td>\n";
624cbc9c 337 echo "<td $nameclass style='text-align: right'>\n";
dcc2ffde 338 print_question_icon($question);
516cf3eb 339 echo "</td>\n";
340 echo "</tr>\n";
624cbc9c 341 if($showquestiontext){
342 echo '<tr><td colspan="3" ' . $textclass . '>';
343 $formatoptions = new stdClass;
344 $formatoptions->noclean = true;
345 $formatoptions->para = false;
346 echo format_text($question->questiontext, $question->questiontextformat,
347 $formatoptions, $course->id);
348 echo "</td></tr>\n";
349 }
516cf3eb 350 }
624cbc9c 351 echo "</table>\n";
624cbc9c 352
b72ff476 353 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage',
624cbc9c 354 false, true);
f92cf442 355 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
356 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
b72ff476 357 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
f92cf442 358 } else {
b72ff476 359 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>DEFAULT_QUESTIONS_PER_PAGE)).'">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
624cbc9c 360 }
361 if ($paging) {
362 $paging = substr($paging, 0, strrpos($paging, '</div>'));
363 $paging .= "<br />$showall</div>";
364 } else {
365 $paging = "<div class='paging'>$showall</div>";
f92cf442 366 }
3abfa3a0 367 }
624cbc9c 368 echo $paging;
516cf3eb 369
370 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
371 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
372 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
373 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
95947ac9 374
dcc2ffde 375 if ($editingquiz) {
95947ac9 376 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
516cf3eb 377 echo '</td><td>';
378 }
3765fd53 379 // print delete and move selected question
dcc2ffde 380 if ($canedit) {
516cf3eb 381 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
382 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 383 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 384 }
385 echo "</td></tr></table>";
386
3765fd53 387 // add random question
dcc2ffde 388 if ($editingquiz) {
b28e2577 389 for ($i = 1;$i <= min(10, $totalnumber); $i++) {
390 $randomcount[$i] = $i;
391 }
392 for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
516cf3eb 393 $randomcount[$i] = $i;
394 }
395 echo '<br />';
86909ce0 396 print_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
516cf3eb 397 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
398 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
399 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
400 helpbutton('random', get_string('random', 'quiz'), 'quiz');
401 }
09275894 402 echo '</fieldset>';
403 echo "</form>\n";
516cf3eb 404}
86909ce0 405/**
406 * Shows the question bank editing interface.
407 *
408 * The function also processes a number of actions:
409 *
410 * Actions affecting the question pool:
411 * move Moves a question to a different category
412 * deleteselected Deletes the selected questions from the category
413 * Other actions:
414 * cat Chooses the category
415 * displayoptions Sets display options
416 *
417 * @author Martin Dougiamas and many others. This has recently been extensively
418 * rewritten by Gustav Delius and other members of the Serving Mathematics project
419 * {@link http://maths.york.ac.uk/serving_maths}
420 * @param moodle_url $pageurl object representing this pages url.
421 */
b72ff476 422function question_showbank($pageurl, $cm, $page, $perpage, $sortorder){
86909ce0 423 global $SESSION, $COURSE;
86909ce0 424
86909ce0 425 $SESSION->fromurl = $pageurl->out();
426
427/// Now, check for commands on this page and modify variables as necessary
428 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
429 $tocategoryid = required_param('category', PARAM_INT);
430 if (!$tocategory = get_record('question_categories', 'id', $tocategoryid)) {
431 error('Invalid category');
432 }
433 if (!has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $tocategory->course))){
434 error(get_string('categorynoedit', 'quiz', $tocategory->name), $pageurl->out());
435 }
436 foreach ($_POST as $key => $value) { // Parse input for question ids
9ab38475 437 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 438 $key = $matches[1];
86909ce0 439 if (!set_field('question', 'category', $tocategory->id, 'id', $key)) {
440 error('Could not update category field');
441 }
442 }
443 }
444 }
445
446 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
447
448 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
449 $deleteselected = required_param('deleteselected');
450 if ($_REQUEST['confirm'] == md5($deleteselected)) {
451 if ($questionlist = explode(',', $deleteselected)) {
452 // for each question either hide it if it is in use or delete it
453 foreach ($questionlist as $questionid) {
454 if (record_exists('quiz_question_instances', 'question', $questionid) or
455 record_exists('question_states', 'originalquestion', $questionid)) {
456 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
457 error('Was not able to hide question');
458 }
459 } else {
460 delete_question($questionid);
461 }
462 }
463 }
464 echo '</td></tr>';
465 echo '</table>';
466 echo '</div>';
467 redirect($pageurl->out());
468 } else {
469 error("Confirmation string was incorrect");
470 }
471
472 } else { // teacher still has to confirm
473 // make a list of all the questions that are selected
474 $rawquestions = $_REQUEST;
475 $questionlist = ''; // comma separated list of ids of questions to be deleted
476 $questionnames = ''; // string with names of questions separated by <br /> with
477 // an asterix in front of those that are in use
478 $inuse = false; // set to true if at least one of the questions is in use
479 foreach ($rawquestions as $key => $value) { // Parse input for question ids
9ab38475 480 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 481 $key = $matches[1]; $questionlist .= $key.',';
86909ce0 482 if (record_exists('quiz_question_instances', 'question', $key) or
483 record_exists('question_states', 'originalquestion', $key)) {
484 $questionnames .= '* ';
485 $inuse = true;
486 }
487 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
488 }
489 }
490 if (!$questionlist) { // no questions were selected
491 redirect($pageurl->out());
492 }
493 $questionlist = rtrim($questionlist, ',');
494
495 // Add an explanation about questions in use
496 if ($inuse) {
497 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
498 }
499 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
500 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
501 $pageurl->out_action());
502
503 echo '</td></tr>';
504 echo '</table>';
505 print_footer($COURSE);
506 exit;
507 }
508 }
509
510 // Unhide a question
511 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
512 $unhide = required_param('unhide', PARAM_INT);
513 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
514 error("Failed to unhide the question.");
515 }
516 redirect($pageurl->out());
517 }
518
519 if ($categoryid = optional_param('cat', 0, PARAM_INT)) { /// coming from category selection drop-down menu
520 $SESSION->questioncat = $categoryid;
521 $page = 0;
522 $SESSION->questionpage = 0;
523 }
524
525 if (empty($SESSION->questioncat) or !count_records_select("question_categories", "id = '{$SESSION->questioncat}' AND (course = '{$COURSE->id}' OR publish = '1')")) {
526 $category = get_default_question_category($COURSE->id);
527 $SESSION->questioncat = $category->id;
528 }
529
530 if(($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
531 $SESSION->questionrecurse = $recurse;
532 }
533 if (!isset($SESSION->questionrecurse)) {
534 $SESSION->questionrecurse = 1;
535 }
536
537 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
538 $SESSION->questionshowhidden = $showhidden;
539 }
540 if (!isset($SESSION->questionshowhidden)) {
541 $SESSION->questionshowhidden = 0;
542 }
543
544 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
545 $SESSION->questionshowquestiontext = $showquestiontext;
546 }
547 if (!isset($SESSION->questionshowquestiontext)) {
548 $SESSION->questionshowquestiontext = 0;
549 }
550
551 // starts with category selection form
552 print_box_start('generalbox questionbank');
553 print_heading(get_string('questionbank', 'question'), '', 2);
554 question_category_form($COURSE, $pageurl, $SESSION->questioncat, $SESSION->questionrecurse,
555 $SESSION->questionshowhidden, $SESSION->questionshowquestiontext);
556
557 // continues with list of questions
558 question_list($COURSE, $pageurl, $SESSION->questioncat, isset($cm) ? $cm : null,
559 $SESSION->questionrecurse, $page, $perpage, $SESSION->questionshowhidden, $sortorder,
560 $SESSION->questionshowquestiontext);
561
562 print_box_end();
563}
b72ff476 564/**
565 * Common setup for all pages for editing questions.
566 * @param boolean $requirecmid require cmid? default false
567 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
568 * @return array $thispageurl, $courseid, $cmid, $cm, $module, $pagevars
569 */
570function question_edit_setup($requirecmid = false, $requirecourseid = true){
571 $thispageurl = new moodle_url();
572 if ($requirecmid){
573 $cmid =required_param('cmid', PARAM_INT);
574 } else {
575 $cmid = optional_param('cmid', 0, PARAM_INT);
576 }
577 if ($cmid){
578 list($module, $cm) = get_module_from_cmid($cmid);
579 $courseid = $cm->course;
580 $thispageurl->params(compact('cmid'));
581 } else {
582 $module = null;
583 $cm = null;
584 if ($requirecourseid){
585 $courseid = required_param('courseid', PARAM_INT);
586 } else {
587 $courseid = optional_param('courseid', 0, PARAM_INT);
588 }
589 if ($courseid){
590 $thispageurl->params(compact('courseid'));
591 }
592 }
593
594 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
595 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
596 $pagevars['qsortorder'] = optional_param('qsortorder', '');
597
598 if (preg_match("/[';]/", $pagevars['qsortorder'])) {
599 error("Incorrect use of the parameter 'qsortorder'");
600 }
601
602 if ($pagevars['qpage'] > -1) {
603 $thispageurl->param('qpage', $pagevars['qpage']);
604 } else {
605 $pagevars['qpage'] = 0;
606 }
607
608 if ($pagevars['qperpage'] > -1) {
609 $thispageurl->param('qperpage', $pagevars['qperpage']);
610 } else {
611 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
612 }
613
614 if ($pagevars['qsortorder']) {
615 $thispageurl->param('qsortorder', $pagevars['qsortorder']);
616 } else {
617 $pagevars['qsortorder'] = 'qtype, name ASC';
618 }
619 return array($thispageurl, $courseid, $cmid, $cm, $module, $pagevars);
620
621}
5fd8f999 622?>