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