fixed links on edit icons so that they return to question bank page.
[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
7bc26c8f 127 popup_form ("edit.php?".$pageurl->get_query_string()."&amp;category=", $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'>";
36e298bc 131 echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext'));
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,
36e298bc 167 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
624cbc9c 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",
36e298bc 232 array('returnurl' => $returnurl,
9ab75b2b 233 'category' => $category->id));
36e298bc 234 if ($cm!==null){
235 $questionurl->param('cmid', $cm->id);
236 }
9ab75b2b 237 popup_form ($questionurl->out().'&amp;qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
624cbc9c 238 echo '</td><td valign="top" align="right">';
516cf3eb 239 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
dcc2ffde 240 echo '</td>';
516cf3eb 241 }
242 else {
dcc2ffde 243 echo '<td>';
516cf3eb 244 print_string("publishedit","quiz");
dcc2ffde 245 echo '</td>';
516cf3eb 246 }
247
dcc2ffde 248 echo '</tr></table>';
09275894 249 echo '</div>';
516cf3eb 250
dc1f00de 251 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 252
253 // hide-feature
254 $showhidden = $showhidden ? '' : " AND hidden = '0'";
255
4f48fb42 256 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
0fd5feef 257 echo "<p style=\"text-align:center;\">";
516cf3eb 258 print_string("noquestions", "quiz");
259 echo "</p>";
260 return;
261 }
262
36e298bc 263 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', $page*$perpage, $perpage)) {
516cf3eb 264 // There are no questions on the requested page.
265 $page = 0;
36e298bc 266 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', 0, $perpage)) {
516cf3eb 267 // There are no questions at all
0fd5feef 268 echo "<p style=\"text-align:center;\">";
516cf3eb 269 print_string("noquestions", "quiz");
270 echo "</p>";
271 return;
272 }
273 }
274
b72ff476 275 print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
516cf3eb 276
86909ce0 277 echo '<form method="post" action="edit.php">';
624cbc9c 278 echo '<fieldset class="invisiblefieldset" style="display: block;">';
516cf3eb 279 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
b72ff476 280 echo $pageurl->hidden_params_out(array('qsortorder'));
624cbc9c 281 echo '<table id="categoryquestions" style="width: 100%"><tr>';
282 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
516cf3eb 283
36e298bc 284 $sortoptions = array('alpha' => get_string("sortalpha", "quiz"),
285 'typealpha' => get_string("sorttypealpha", "quiz"),
286 'age' => get_string("sortage", "quiz"));
b72ff476 287 $orderselect = choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
09275894 288 $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
624cbc9c 289 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
290 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
516cf3eb 291 echo "</tr>\n";
292 foreach ($questions as $question) {
624cbc9c 293 $nameclass = '';
294 $textclass = '';
295 if ($question->hidden) {
296 $nameclass = 'dimmed_text';
297 $textclass = 'dimmed_text';
298 }
299 if ($showquestiontext) {
300 $nameclass .= ' header';
301 }
302 if ($nameclass) {
303 $nameclass = 'class="' . $nameclass . '"';
304 }
305 if ($textclass) {
306 $textclass = 'class="' . $textclass . '"';
307 }
308
309 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
3765fd53 310
311 // add to quiz
dcc2ffde 312 if ($editingquiz) {
86909ce0 313 echo "<a title=\"$straddtoquiz\" href=\"edit.php?".$pageurl->get_query_string()."&amp;addquestion=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 314 src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
516cf3eb 315 }
3765fd53 316
317 // preview
7d87171b 318 link_to_popup_window('/question/preview.php?id=' . $question->id . '&amp;quizid=' . $quizid, 'questionpreview',
319 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
320 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS);
3765fd53 321
322 // edit, hide, delete question, using question capabilities, not quiz capabilieies
dcc2ffde 323 if ($canedit) {
561fe61d 324 $questionparams = (($cm !== null)? array('cmid' => $cm->id) : array()) +
325 (array('returnurl' => $pageurl->out(), 'id'=>$question->id));
326 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams);
327 echo "<a title=\"$stredit\" href=\"".$questionurl->out()."\"><img
0fd5feef 328 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
516cf3eb 329 // hide-feature
330 if($question->hidden) {
86909ce0 331 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 332 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
516cf3eb 333 } else {
86909ce0 334 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
0fd5feef 335 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
516cf3eb 336 }
337 }
338 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
339 echo "</td>\n";
340
70ed7667 341 echo "<td $nameclass>" . format_string($question->name) . "</td>\n";
624cbc9c 342 echo "<td $nameclass style='text-align: right'>\n";
dcc2ffde 343 print_question_icon($question);
516cf3eb 344 echo "</td>\n";
345 echo "</tr>\n";
624cbc9c 346 if($showquestiontext){
347 echo '<tr><td colspan="3" ' . $textclass . '>';
348 $formatoptions = new stdClass;
349 $formatoptions->noclean = true;
350 $formatoptions->para = false;
351 echo format_text($question->questiontext, $question->questiontextformat,
352 $formatoptions, $course->id);
353 echo "</td></tr>\n";
354 }
516cf3eb 355 }
624cbc9c 356 echo "</table>\n";
624cbc9c 357
b72ff476 358 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage',
624cbc9c 359 false, true);
f92cf442 360 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
361 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
b72ff476 362 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
f92cf442 363 } else {
b72ff476 364 $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 365 }
366 if ($paging) {
367 $paging = substr($paging, 0, strrpos($paging, '</div>'));
368 $paging .= "<br />$showall</div>";
369 } else {
370 $paging = "<div class='paging'>$showall</div>";
f92cf442 371 }
3abfa3a0 372 }
624cbc9c 373 echo $paging;
516cf3eb 374
375 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
376 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
377 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
378 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
95947ac9 379
dcc2ffde 380 if ($editingquiz) {
95947ac9 381 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
516cf3eb 382 echo '</td><td>';
383 }
3765fd53 384 // print delete and move selected question
dcc2ffde 385 if ($canedit) {
516cf3eb 386 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
387 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 388 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 389 }
390 echo "</td></tr></table>";
391
3765fd53 392 // add random question
dcc2ffde 393 if ($editingquiz) {
b28e2577 394 for ($i = 1;$i <= min(10, $totalnumber); $i++) {
395 $randomcount[$i] = $i;
396 }
397 for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
516cf3eb 398 $randomcount[$i] = $i;
399 }
400 echo '<br />';
86909ce0 401 print_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
516cf3eb 402 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
403 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
404 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
405 helpbutton('random', get_string('random', 'quiz'), 'quiz');
406 }
09275894 407 echo '</fieldset>';
408 echo "</form>\n";
516cf3eb 409}
86909ce0 410/**
411 * Shows the question bank editing interface.
412 *
413 * The function also processes a number of actions:
414 *
415 * Actions affecting the question pool:
416 * move Moves a question to a different category
417 * deleteselected Deletes the selected questions from the category
418 * Other actions:
7bc26c8f 419 * category Chooses the category
86909ce0 420 * displayoptions Sets display options
421 *
422 * @author Martin Dougiamas and many others. This has recently been extensively
423 * rewritten by Gustav Delius and other members of the Serving Mathematics project
424 * {@link http://maths.york.ac.uk/serving_maths}
425 * @param moodle_url $pageurl object representing this pages url.
426 */
36e298bc 427function question_showbank($pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
64eeaf47 428 global $COURSE;
86909ce0 429
430/// Now, check for commands on this page and modify variables as necessary
431 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
432 $tocategoryid = required_param('category', PARAM_INT);
433 if (!$tocategory = get_record('question_categories', 'id', $tocategoryid)) {
434 error('Invalid category');
435 }
436 if (!has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $tocategory->course))){
437 error(get_string('categorynoedit', 'quiz', $tocategory->name), $pageurl->out());
438 }
439 foreach ($_POST as $key => $value) { // Parse input for question ids
9ab38475 440 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 441 $key = $matches[1];
86909ce0 442 if (!set_field('question', 'category', $tocategory->id, 'id', $key)) {
443 error('Could not update category field');
444 }
445 }
446 }
447 }
448
449 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
450
451 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
452 $deleteselected = required_param('deleteselected');
453 if ($_REQUEST['confirm'] == md5($deleteselected)) {
454 if ($questionlist = explode(',', $deleteselected)) {
455 // for each question either hide it if it is in use or delete it
456 foreach ($questionlist as $questionid) {
457 if (record_exists('quiz_question_instances', 'question', $questionid) or
458 record_exists('question_states', 'originalquestion', $questionid)) {
459 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
460 error('Was not able to hide question');
461 }
462 } else {
463 delete_question($questionid);
464 }
465 }
466 }
467 echo '</td></tr>';
468 echo '</table>';
469 echo '</div>';
470 redirect($pageurl->out());
471 } else {
472 error("Confirmation string was incorrect");
473 }
474
475 } else { // teacher still has to confirm
476 // make a list of all the questions that are selected
477 $rawquestions = $_REQUEST;
478 $questionlist = ''; // comma separated list of ids of questions to be deleted
479 $questionnames = ''; // string with names of questions separated by <br /> with
480 // an asterix in front of those that are in use
481 $inuse = false; // set to true if at least one of the questions is in use
482 foreach ($rawquestions as $key => $value) { // Parse input for question ids
9ab38475 483 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 484 $key = $matches[1]; $questionlist .= $key.',';
86909ce0 485 if (record_exists('quiz_question_instances', 'question', $key) or
486 record_exists('question_states', 'originalquestion', $key)) {
487 $questionnames .= '* ';
488 $inuse = true;
489 }
490 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
491 }
492 }
493 if (!$questionlist) { // no questions were selected
494 redirect($pageurl->out());
495 }
496 $questionlist = rtrim($questionlist, ',');
497
498 // Add an explanation about questions in use
499 if ($inuse) {
500 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
501 }
502 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
503 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
504 $pageurl->out_action());
505
506 echo '</td></tr>';
507 echo '</table>';
508 print_footer($COURSE);
509 exit;
510 }
511 }
512
513 // Unhide a question
514 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
515 $unhide = required_param('unhide', PARAM_INT);
516 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
517 error("Failed to unhide the question.");
518 }
519 redirect($pageurl->out());
520 }
521
86909ce0 522 // starts with category selection form
523 print_box_start('generalbox questionbank');
524 print_heading(get_string('questionbank', 'question'), '', 2);
36e298bc 525 question_category_form($COURSE, $pageurl, $cat, $recurse,
526 $showhidden, $showquestiontext);
86909ce0 527
528 // continues with list of questions
36e298bc 529 question_list($COURSE, $pageurl, $cat, isset($cm) ? $cm : null,
530 $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded,
531 $showquestiontext);
86909ce0 532
533 print_box_end();
534}
b72ff476 535/**
536 * Common setup for all pages for editing questions.
537 * @param boolean $requirecmid require cmid? default false
538 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
539 * @return array $thispageurl, $courseid, $cmid, $cm, $module, $pagevars
540 */
541function question_edit_setup($requirecmid = false, $requirecourseid = true){
36e298bc 542 global $COURSE;
543 //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
544 //of parameters that are passed from page to page.
b72ff476 545 $thispageurl = new moodle_url();
546 if ($requirecmid){
547 $cmid =required_param('cmid', PARAM_INT);
548 } else {
549 $cmid = optional_param('cmid', 0, PARAM_INT);
550 }
551 if ($cmid){
552 list($module, $cm) = get_module_from_cmid($cmid);
553 $courseid = $cm->course;
554 $thispageurl->params(compact('cmid'));
555 } else {
556 $module = null;
557 $cm = null;
558 if ($requirecourseid){
559 $courseid = required_param('courseid', PARAM_INT);
560 } else {
561 $courseid = optional_param('courseid', 0, PARAM_INT);
562 }
563 if ($courseid){
564 $thispageurl->params(compact('courseid'));
565 }
566 }
36e298bc 567 require_login($courseid, false);
b72ff476 568
b72ff476 569
36e298bc 570 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
7bc26c8f 571
572 //pass 'cat' from page to page and when 'category' comes from a drop down menu
573 //then we also reset the qpage so we go to page 1 of
574 //a new cat.
575 $pagevars['cat'] = optional_param('cat', 0, PARAM_INT);
576 if ($category = optional_param('category', 0, PARAM_INT)){
577 $pagevars['cat'] = $category;
578 $pagevars['qpage'] = 0;
579 }
580 if ($pagevars['cat']){
581 $thispageurl->param('cat', $pagevars['cat']);
582 }
b72ff476 583 if ($pagevars['qpage'] > -1) {
584 $thispageurl->param('qpage', $pagevars['qpage']);
585 } else {
586 $pagevars['qpage'] = 0;
587 }
588
36e298bc 589 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
b72ff476 590 if ($pagevars['qperpage'] > -1) {
591 $thispageurl->param('qperpage', $pagevars['qperpage']);
592 } else {
593 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
594 }
36e298bc 595
596 $sortoptions = array('alpha' => 'name, qtype ASC',
597 'typealpha' => 'qtype, name ASC',
598 'age' => 'id ASC');
599
600 if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
601 $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
602 $pagevars['qsortorder'] = $sortorder;
603 $thispageurl->param('qsortorder', $sortorder);
b72ff476 604 } else {
7bc26c8f 605 $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
606 $pagevars['qsortorder'] = 'typealpha';
36e298bc 607 }
608
36e298bc 609
610 if (empty($pagevars['cat']) or !count_records_select("question_categories", "id = '".$pagevars['cat']."' AND (course = '{$COURSE->id}' OR publish = '1')")) {
611 $category = get_default_question_category($COURSE->id);
612 $pagevars['cat'] = $category->id;
613 $thispageurl->param('cat', $category->id);
614 }
615
616 if(($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
617 $pagevars['recurse'] = $recurse;
618 $thispageurl->param('recurse', $recurse);
619 } else {
620 $pagevars['recurse'] = 1;
621 }
622
623 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
624 $pagevars['showhidden'] = $showhidden;
625 $thispageurl->param('showhidden', $showhidden);
626 } else {
627 $pagevars['showhidden'] = 0;
628 }
629
630 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
631 $pagevars['showquestiontext'] = $showquestiontext;
632 $thispageurl->param('showquestiontext', $showquestiontext);
633 } else {
634 $pagevars['showquestiontext'] = 0;
635 }
986effb6 636
637 //category list page
638 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
639 if ($pagevars['cpage'] != 1){
640 $thispageurl->param('cpage', $pagevars['cpage']);
641 }
642
36e298bc 643
b72ff476 644 return array($thispageurl, $courseid, $cmid, $cm, $module, $pagevars);
b72ff476 645}
5fd8f999 646?>