adding calculated question import MDL-5650
[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
36e298bc 127 popup_form ("edit.php?".$pageurl->get_query_string()."&amp;catchange=", $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) {
e586cfb4 324 echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
0fd5feef 325 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
516cf3eb 326 // hide-feature
327 if($question->hidden) {
86909ce0 328 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 329 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
516cf3eb 330 } else {
86909ce0 331 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
0fd5feef 332 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
516cf3eb 333 }
334 }
335 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
336 echo "</td>\n";
337
70ed7667 338 echo "<td $nameclass>" . format_string($question->name) . "</td>\n";
624cbc9c 339 echo "<td $nameclass style='text-align: right'>\n";
dcc2ffde 340 print_question_icon($question);
516cf3eb 341 echo "</td>\n";
342 echo "</tr>\n";
624cbc9c 343 if($showquestiontext){
344 echo '<tr><td colspan="3" ' . $textclass . '>';
345 $formatoptions = new stdClass;
346 $formatoptions->noclean = true;
347 $formatoptions->para = false;
348 echo format_text($question->questiontext, $question->questiontextformat,
349 $formatoptions, $course->id);
350 echo "</td></tr>\n";
351 }
516cf3eb 352 }
624cbc9c 353 echo "</table>\n";
624cbc9c 354
b72ff476 355 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage',
624cbc9c 356 false, true);
f92cf442 357 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
358 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
b72ff476 359 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
f92cf442 360 } else {
b72ff476 361 $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 362 }
363 if ($paging) {
364 $paging = substr($paging, 0, strrpos($paging, '</div>'));
365 $paging .= "<br />$showall</div>";
366 } else {
367 $paging = "<div class='paging'>$showall</div>";
f92cf442 368 }
3abfa3a0 369 }
624cbc9c 370 echo $paging;
516cf3eb 371
372 echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
373 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
374 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
375 '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
95947ac9 376
dcc2ffde 377 if ($editingquiz) {
95947ac9 378 echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
516cf3eb 379 echo '</td><td>';
380 }
3765fd53 381 // print delete and move selected question
dcc2ffde 382 if ($canedit) {
516cf3eb 383 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
384 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
dc1f00de 385 question_category_select_menu($course->id, false, true, $category->id);
516cf3eb 386 }
387 echo "</td></tr></table>";
388
3765fd53 389 // add random question
dcc2ffde 390 if ($editingquiz) {
b28e2577 391 for ($i = 1;$i <= min(10, $totalnumber); $i++) {
392 $randomcount[$i] = $i;
393 }
394 for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
516cf3eb 395 $randomcount[$i] = $i;
396 }
397 echo '<br />';
86909ce0 398 print_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
516cf3eb 399 echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
400 echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
401 echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
402 helpbutton('random', get_string('random', 'quiz'), 'quiz');
403 }
09275894 404 echo '</fieldset>';
405 echo "</form>\n";
516cf3eb 406}
86909ce0 407/**
408 * Shows the question bank editing interface.
409 *
410 * The function also processes a number of actions:
411 *
412 * Actions affecting the question pool:
413 * move Moves a question to a different category
414 * deleteselected Deletes the selected questions from the category
415 * Other actions:
36e298bc 416 * catchange Chooses the category
86909ce0 417 * displayoptions Sets display options
418 *
419 * @author Martin Dougiamas and many others. This has recently been extensively
420 * rewritten by Gustav Delius and other members of the Serving Mathematics project
421 * {@link http://maths.york.ac.uk/serving_maths}
422 * @param moodle_url $pageurl object representing this pages url.
423 */
36e298bc 424function question_showbank($pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
86909ce0 425 global $SESSION, $COURSE;
86909ce0 426
86909ce0 427 $SESSION->fromurl = $pageurl->out();
428
429/// Now, check for commands on this page and modify variables as necessary
430 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
431 $tocategoryid = required_param('category', PARAM_INT);
432 if (!$tocategory = get_record('question_categories', 'id', $tocategoryid)) {
433 error('Invalid category');
434 }
435 if (!has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $tocategory->course))){
436 error(get_string('categorynoedit', 'quiz', $tocategory->name), $pageurl->out());
437 }
438 foreach ($_POST as $key => $value) { // Parse input for question ids
9ab38475 439 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 440 $key = $matches[1];
86909ce0 441 if (!set_field('question', 'category', $tocategory->id, 'id', $key)) {
442 error('Could not update category field');
443 }
444 }
445 }
446 }
447
448 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
449
450 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
451 $deleteselected = required_param('deleteselected');
452 if ($_REQUEST['confirm'] == md5($deleteselected)) {
453 if ($questionlist = explode(',', $deleteselected)) {
454 // for each question either hide it if it is in use or delete it
455 foreach ($questionlist as $questionid) {
456 if (record_exists('quiz_question_instances', 'question', $questionid) or
457 record_exists('question_states', 'originalquestion', $questionid)) {
458 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
459 error('Was not able to hide question');
460 }
461 } else {
462 delete_question($questionid);
463 }
464 }
465 }
466 echo '</td></tr>';
467 echo '</table>';
468 echo '</div>';
469 redirect($pageurl->out());
470 } else {
471 error("Confirmation string was incorrect");
472 }
473
474 } else { // teacher still has to confirm
475 // make a list of all the questions that are selected
476 $rawquestions = $_REQUEST;
477 $questionlist = ''; // comma separated list of ids of questions to be deleted
478 $questionnames = ''; // string with names of questions separated by <br /> with
479 // an asterix in front of those that are in use
480 $inuse = false; // set to true if at least one of the questions is in use
481 foreach ($rawquestions as $key => $value) { // Parse input for question ids
9ab38475 482 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 483 $key = $matches[1]; $questionlist .= $key.',';
86909ce0 484 if (record_exists('quiz_question_instances', 'question', $key) or
485 record_exists('question_states', 'originalquestion', $key)) {
486 $questionnames .= '* ';
487 $inuse = true;
488 }
489 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
490 }
491 }
492 if (!$questionlist) { // no questions were selected
493 redirect($pageurl->out());
494 }
495 $questionlist = rtrim($questionlist, ',');
496
497 // Add an explanation about questions in use
498 if ($inuse) {
499 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
500 }
501 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
502 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
503 $pageurl->out_action());
504
505 echo '</td></tr>';
506 echo '</table>';
507 print_footer($COURSE);
508 exit;
509 }
510 }
511
512 // Unhide a question
513 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
514 $unhide = required_param('unhide', PARAM_INT);
515 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
516 error("Failed to unhide the question.");
517 }
518 redirect($pageurl->out());
519 }
520
86909ce0 521 // starts with category selection form
522 print_box_start('generalbox questionbank');
523 print_heading(get_string('questionbank', 'question'), '', 2);
36e298bc 524 question_category_form($COURSE, $pageurl, $cat, $recurse,
525 $showhidden, $showquestiontext);
86909ce0 526
527 // continues with list of questions
36e298bc 528 question_list($COURSE, $pageurl, $cat, isset($cm) ? $cm : null,
529 $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded,
530 $showquestiontext);
86909ce0 531
532 print_box_end();
533}
b72ff476 534/**
535 * Common setup for all pages for editing questions.
536 * @param boolean $requirecmid require cmid? default false
537 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
538 * @return array $thispageurl, $courseid, $cmid, $cm, $module, $pagevars
539 */
540function question_edit_setup($requirecmid = false, $requirecourseid = true){
36e298bc 541 global $COURSE;
542 //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
543 //of parameters that are passed from page to page.
b72ff476 544 $thispageurl = new moodle_url();
545 if ($requirecmid){
546 $cmid =required_param('cmid', PARAM_INT);
547 } else {
548 $cmid = optional_param('cmid', 0, PARAM_INT);
549 }
550 if ($cmid){
551 list($module, $cm) = get_module_from_cmid($cmid);
552 $courseid = $cm->course;
553 $thispageurl->params(compact('cmid'));
554 } else {
555 $module = null;
556 $cm = null;
557 if ($requirecourseid){
558 $courseid = required_param('courseid', PARAM_INT);
559 } else {
560 $courseid = optional_param('courseid', 0, PARAM_INT);
561 }
562 if ($courseid){
563 $thispageurl->params(compact('courseid'));
564 }
565 }
36e298bc 566 require_login($courseid, false);
b72ff476 567
b72ff476 568
36e298bc 569 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
b72ff476 570 if ($pagevars['qpage'] > -1) {
571 $thispageurl->param('qpage', $pagevars['qpage']);
572 } else {
573 $pagevars['qpage'] = 0;
574 }
575
36e298bc 576 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
b72ff476 577 if ($pagevars['qperpage'] > -1) {
578 $thispageurl->param('qperpage', $pagevars['qperpage']);
579 } else {
580 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
581 }
36e298bc 582
583 $sortoptions = array('alpha' => 'name, qtype ASC',
584 'typealpha' => 'qtype, name ASC',
585 'age' => 'id ASC');
586
587 if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
588 $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
589 $pagevars['qsortorder'] = $sortorder;
590 $thispageurl->param('qsortorder', $sortorder);
b72ff476 591 } else {
592 $pagevars['qsortorder'] = 'qtype, name ASC';
36e298bc 593 }
594
595 //pass cat from page to page and catchange comes a drop down menu
596 //on catchange then we also reset the qpage so we go to page 1 of
597 //a new cat.
598 $pagevars['cat'] = optional_param('cat', 0, PARAM_INT);
599 if ($catchange = optional_param('catchange', 0, PARAM_INT)){
600 $pagevars['cat'] = $catchange;
601 $pagevars['qpage'] = 0;
b72ff476 602 }
36e298bc 603 if ($pagevars['cat']){
604 $thispageurl->param('cat', $pagevars['cat']);
605 }
606
607 if (empty($pagevars['cat']) or !count_records_select("question_categories", "id = '".$pagevars['cat']."' AND (course = '{$COURSE->id}' OR publish = '1')")) {
608 $category = get_default_question_category($COURSE->id);
609 $pagevars['cat'] = $category->id;
610 $thispageurl->param('cat', $category->id);
611 }
612
613 if(($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
614 $pagevars['recurse'] = $recurse;
615 $thispageurl->param('recurse', $recurse);
616 } else {
617 $pagevars['recurse'] = 1;
618 }
619
620 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
621 $pagevars['showhidden'] = $showhidden;
622 $thispageurl->param('showhidden', $showhidden);
623 } else {
624 $pagevars['showhidden'] = 0;
625 }
626
627 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
628 $pagevars['showquestiontext'] = $showquestiontext;
629 $thispageurl->param('showquestiontext', $showquestiontext);
630 } else {
631 $pagevars['showquestiontext'] = 0;
632 }
633
b72ff476 634 return array($thispageurl, $courseid, $cmid, $cm, $module, $pagevars);
635
636}
5fd8f999 637?>