MDL-15094 fixed stats
[moodle.git] / question / editlib.php
CommitLineData
516cf3eb 1<?php // $Id$
2/**
b72ff476 3 * Functions used to show question editing interface
4323d029 4 *
4323d029 5 *
6 * @author Martin Dougiamas and many others. This has recently been extensively
7 * rewritten by members of the Serving Mathematics project
8 * {@link http://maths.york.ac.uk/serving_maths}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package questionbank
11 */
516cf3eb 12
4fbfd971 13require_once($CFG->libdir.'/questionlib.php');
516cf3eb 14
f92cf442 15define('DEFAULT_QUESTIONS_PER_PAGE', 20);
16
86909ce0 17function get_module_from_cmid($cmid){
18 global $CFG;
19 if (!$cmrec = get_record_sql("SELECT cm.*, md.name as modname
20 FROM {$CFG->prefix}course_modules cm,
21 {$CFG->prefix}modules md
22 WHERE cm.id = '$cmid' AND
23 md.id = cm.module")){
5a2a5331 24 print_error('cmunknown');
86909ce0 25 } elseif (!$modrec =get_record($cmrec->modname, 'id', $cmrec->instance)) {
5a2a5331 26 print_error('cmunknown');
86909ce0 27 }
28 $modrec->instance = $modrec->id;
d0e88092 29 $modrec->cmid = $cmrec->id;
ea16c547 30 $cmrec->name = $modrec->name;
271e6dec 31
86909ce0 32 return array($modrec, $cmrec);
33}
4fbfd971 34/**
35* Function to read all questions for category into big array
36*
37* @param int $category category number
36e2232e 38* @param bool $noparent if true only questions with NO parent will be selected
39* @param bool $recurse include subdirectories
5fd8f999 40* @param bool $export set true if this is called by questionbank export
4fbfd971 41* @author added by Howard Miller June 2004
42*/
5fd8f999 43function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
4fbfd971 44
45 global $QTYPES;
46
47 // questions will be added to an array
48 $qresults = array();
49
50 // build sql bit for $noparent
51 $npsql = '';
52 if ($noparent) {
53 $npsql = " and parent='0' ";
54 }
55
36e2232e 56 // get (list) of categories
57 if ($recurse) {
58 $categorylist = question_categorylist( $category->id );
59 }
60 else {
61 $categorylist = $category->id;
62 }
63
4fbfd971 64 // get the list of questions for the category
36e2232e 65 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
4fbfd971 66
67 // iterate through questions, getting stuff we need
68 foreach($questions as $question) {
69 $questiontype = $QTYPES[$question->qtype];
5fd8f999 70 $question->export_process = $export;
4fbfd971 71 $questiontype->get_question_options( $question );
72 $qresults[] = $question;
73 }
74 }
75
76 return $qresults;
77}
78
4fbfd971 79
271e6dec 80function question_can_delete_cat($todelete){
81 global $CFG;
871ab9a3 82 $record = get_record_sql("SELECT count(*) as count, c1.contextid as contextid FROM {$CFG->prefix}question_categories c1,
83 {$CFG->prefix}question_categories c2 WHERE c2.id = $todelete
271e6dec 84 AND c1.contextid = c2.contextid GROUP BY c1.contextid");
85 $contextid = $record->contextid;
86 $count = $record->count;
87 if ($count < 2) {
92a95034 88 error('You can\'t delete that category it is the default category for this context.');
271e6dec 89 } else {
90 require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
91 }
92}
4fbfd971 93/**
94 * prints a form to choose categories
95 */
271e6dec 96function question_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
e586cfb4 97 global $CFG;
516cf3eb 98
516cf3eb 99
100/// Get all the existing categories now
271e6dec 101 $catmenu = question_category_options($contexts, false, 0, true);
dcd51df2 102
271e6dec 103 $strcategory = get_string('category', 'quiz');
104 $strshow = get_string('show', 'quiz');
105 $streditcats = get_string('editcategories', 'quiz');
516cf3eb 106
271e6dec 107 popup_form ('edit.php?'.$pageurl->get_query_string().'&amp;category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "<strong>$strcategory</strong>");
0fd5feef 108
d1f2b9e1 109 echo '<form method="get" action="edit.php" id="displayoptions">';
0fd5feef 110 echo "<fieldset class='invisiblefieldset'>";
36e298bc 111 echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext'));
624cbc9c 112 question_category_form_checkbox('recurse', $recurse);
113 question_category_form_checkbox('showhidden', $showhidden);
114 question_category_form_checkbox('showquestiontext', $showquestiontext);
115 echo '<noscript><div class="centerpara"><input type="submit" value="'. get_string('go') .'" />';
116 echo '</div></noscript></fieldset></form>';
117}
118
119/**
120 * Private funciton to help the preceeding function.
121 */
122function question_category_form_checkbox($name, $checked) {
123 echo '<div><input type="hidden" id="' . $name . '_off" name="' . $name . '" value="0" />';
124 echo '<input type="checkbox" id="' . $name . '_on" name="' . $name . '" value="1"';
125 if ($checked) {
516cf3eb 126 echo ' checked="checked"';
127 }
d2ce367f 128 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
624cbc9c 129 echo '<label for="' . $name . '_on">';
130 print_string($name, 'quiz');
131 echo "</label></div>\n";
516cf3eb 132}
133
516cf3eb 134/**
135* Prints the table of questions in a category with interactions
136*
137* @param object $course The course object
138* @param int $categoryid The id of the question category to be displayed
86909ce0 139* @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
516cf3eb 140* @param int $recurse This is 1 if subcategories should be included, 0 otherwise
141* @param int $page The number of the page to be displayed
142* @param int $perpage Number of questions to show per page
143* @param boolean $showhidden True if also hidden questions should be displayed
dcc2ffde 144* @param boolean $showquestiontext whether the text of each question should be shown in the list
516cf3eb 145*/
271e6dec 146function question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
36e298bc 147 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
271e6dec 148 $showquestiontext = false, $addcontexts = array()) {
e2ae84a2 149 global $USER, $CFG, $THEME, $COURSE;
271e6dec 150
151 list($categoryid, $contextid)= explode(',', $categoryandcontext);
152
e2ae84a2 153 $qtypemenu = question_type_menu();
335768f4 154
516cf3eb 155 $strcategory = get_string("category", "quiz");
156 $strquestion = get_string("question", "quiz");
157 $straddquestions = get_string("addquestions", "quiz");
158 $strimportquestions = get_string("importquestions", "quiz");
159 $strexportquestions = get_string("exportquestions", "quiz");
160 $strnoquestions = get_string("noquestions", "quiz");
161 $strselect = get_string("select", "quiz");
162 $strselectall = get_string("selectall", "quiz");
163 $strselectnone = get_string("selectnone", "quiz");
164 $strcreatenewquestion = get_string("createnewquestion", "quiz");
165 $strquestionname = get_string("questionname", "quiz");
166 $strdelete = get_string("delete");
167 $stredit = get_string("edit");
271e6dec 168 $strmove = get_string('moveqtoanothercontext', 'question');
169 $strview = get_string("view");
516cf3eb 170 $straction = get_string("action");
171 $strrestore = get_string('restore');
172
516cf3eb 173 $strtype = get_string("type", "quiz");
174 $strcreatemultiple = get_string("createmultiple", "quiz");
175 $strpreview = get_string("preview","quiz");
176
177 if (!$categoryid) {
0fd5feef 178 echo "<p style=\"text-align:center;\"><b>";
516cf3eb 179 print_string("selectcategoryabove", "quiz");
180 echo "</b></p>";
516cf3eb 181 return;
182 }
183
271e6dec 184 if (!$category = get_record('question_categories', 'id', $categoryid, 'contextid', $contextid)) {
dcc2ffde 185 notify('Category not found!');
516cf3eb 186 return;
187 }
271e6dec 188 $catcontext = get_context_instance_by_id($contextid);
189 $canadd = has_capability('moodle/question:add', $catcontext);
190 //check for capabilities on all questions in category, will also apply to sub cats.
191 $caneditall =has_capability('moodle/question:editall', $catcontext);
192 $canuseall =has_capability('moodle/question:useall', $catcontext);
193 $canmoveall =has_capability('moodle/question:moveall', $catcontext);
86909ce0 194
195 if ($cm AND $cm->modname == 'quiz') {
86909ce0 196 $quizid = $cm->instance;
197 } else {
86909ce0 198 $quizid = 0;
dcc2ffde 199 }
271e6dec 200 $returnurl = $pageurl->out();
201 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
202 array('returnurl' => $returnurl));
203 if ($cm!==null){
204 $questionurl->param('cmid', $cm->id);
205 } else {
206 $questionurl->param('courseid', $COURSE->id);
207 }
208 $questionmoveurl = new moodle_url("$CFG->wwwroot/question/contextmoveq.php",
209 array('returnurl' => $returnurl));
210 if ($cm!==null){
211 $questionmoveurl->param('cmid', $cm->id);
212 } else {
213 $questionmoveurl->param('courseid', $COURSE->id);
214 }
09275894 215 echo '<div class="boxaligncenter">';
4abc23b2 216 $formatoptions = new stdClass;
7347c60b 217 $formatoptions->noclean = true;
271e6dec 218 echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $COURSE->id);
516cf3eb 219
220 echo '<table><tr>';
221
271e6dec 222 if ($canadd) {
516cf3eb 223 echo '<td valign="top" align="right">';
271e6dec 224 popup_form ($questionurl->out(false, array('category' => $category->id)).'&amp;qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
624cbc9c 225 echo '</td><td valign="top" align="right">';
516cf3eb 226 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
dcc2ffde 227 echo '</td>';
516cf3eb 228 }
229 else {
dcc2ffde 230 echo '<td>';
271e6dec 231 print_string('nopermissionadd', 'question');
dcc2ffde 232 echo '</td>';
516cf3eb 233 }
234
dcc2ffde 235 echo '</tr></table>';
09275894 236 echo '</div>';
516cf3eb 237
dc1f00de 238 $categorylist = ($recurse) ? question_categorylist($category->id) : $category->id;
516cf3eb 239
240 // hide-feature
241 $showhidden = $showhidden ? '' : " AND hidden = '0'";
242
4f48fb42 243 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
0fd5feef 244 echo "<p style=\"text-align:center;\">";
516cf3eb 245 print_string("noquestions", "quiz");
246 echo "</p>";
247 return;
248 }
249
36e298bc 250 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', $page*$perpage, $perpage)) {
516cf3eb 251 // There are no questions on the requested page.
252 $page = 0;
36e298bc 253 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', 0, $perpage)) {
516cf3eb 254 // There are no questions at all
0fd5feef 255 echo "<p style=\"text-align:center;\">";
516cf3eb 256 print_string("noquestions", "quiz");
257 echo "</p>";
258 return;
259 }
260 }
261
b72ff476 262 print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
516cf3eb 263
271e6dec 264 echo question_sort_options($pageurl, $sortorder);
265
266
86909ce0 267 echo '<form method="post" action="edit.php">';
624cbc9c 268 echo '<fieldset class="invisiblefieldset" style="display: block;">';
516cf3eb 269 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
987c5893 270 echo $pageurl->hidden_params_out();
624cbc9c 271 echo '<table id="categoryquestions" style="width: 100%"><tr>';
272 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
271e6dec 273
274 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname</th>
624cbc9c 275 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
516cf3eb 276 echo "</tr>\n";
277 foreach ($questions as $question) {
624cbc9c 278 $nameclass = '';
279 $textclass = '';
280 if ($question->hidden) {
281 $nameclass = 'dimmed_text';
282 $textclass = 'dimmed_text';
283 }
284 if ($showquestiontext) {
285 $nameclass .= ' header';
286 }
287 if ($nameclass) {
288 $nameclass = 'class="' . $nameclass . '"';
289 }
290 if ($textclass) {
291 $textclass = 'class="' . $textclass . '"';
292 }
271e6dec 293
624cbc9c 294 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
271e6dec 295
296 $canuseq = question_has_capability_on($question, 'use', $question->category);
85c375c3 297 if (function_exists('module_specific_actions')) {
271e6dec 298 echo module_specific_actions($pageurl, $question->id, $cm->id, $canuseq);
516cf3eb 299 }
271e6dec 300
3765fd53 301 // preview
271e6dec 302 if ($canuseq) {
e7e62d45 303 $quizorcourseid = $quizid?('&amp;quizid=' . $quizid):('&amp;courseid=' .$COURSE->id);
304 link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview',
271e6dec 305 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
306 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS);
307 }
3765fd53 308 // edit, hide, delete question, using question capabilities, not quiz capabilieies
271e6dec 309 if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) {
310 echo "<a title=\"$stredit\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
0fd5feef 311 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
271e6dec 312 } elseif (question_has_capability_on($question, 'view', $question->category)){
313 echo "<a title=\"$strview\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
314 src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a>&nbsp;";
315 }
316
317 if (question_has_capability_on($question, 'move', $question->category) && question_has_capability_on($question, 'view', $question->category)) {
318 echo "<a title=\"$strmove\" href=\"".$questionurl->out(false, array('id'=>$question->id, 'movecontext'=>1))."\"><img
319 src=\"$CFG->pixpath/t/move.gif\" alt=\"$strmove\" /></a>&nbsp;";
320 }
321
322 if (question_has_capability_on($question, 'edit', $question->category)) {
516cf3eb 323 // hide-feature
324 if($question->hidden) {
86909ce0 325 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
0fd5feef 326 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
516cf3eb 327 } else {
86909ce0 328 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
0fd5feef 329 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
516cf3eb 330 }
331 }
271e6dec 332 if ($caneditall || $canmoveall || $canuseall){
333 echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
334 }
516cf3eb 335 echo "</td>\n";
336
70ed7667 337 echo "<td $nameclass>" . format_string($question->name) . "</td>\n";
624cbc9c 338 echo "<td $nameclass style='text-align: right'>\n";
dcc2ffde 339 print_question_icon($question);
516cf3eb 340 echo "</td>\n";
341 echo "</tr>\n";
624cbc9c 342 if($showquestiontext){
343 echo '<tr><td colspan="3" ' . $textclass . '>';
344 $formatoptions = new stdClass;
345 $formatoptions->noclean = true;
346 $formatoptions->para = false;
347 echo format_text($question->questiontext, $question->questiontextformat,
271e6dec 348 $formatoptions, $COURSE->id);
624cbc9c 349 echo "</td></tr>\n";
350 }
516cf3eb 351 }
624cbc9c 352 echo "</table>\n";
624cbc9c 353
271e6dec 354 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', 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 {
271e6dec 365 $paging = "<div class='paging'>$showall</div>";
f92cf442 366 }
3abfa3a0 367 }
624cbc9c 368 echo $paging;
516cf3eb 369
271e6dec 370 if ($caneditall || $canmoveall || $canuseall){
314fc083 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>';
271e6dec 373 echo '<br />';
374 echo '<strong>&nbsp;'.get_string('withselected', 'quiz').':</strong><br />';
95947ac9 375
271e6dec 376 if (function_exists('module_specific_buttons')) {
377 echo module_specific_buttons($cm->id);
378 }
379 // print delete and move selected question
380 if ($caneditall) {
381 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" />\n";
382 }
383 if ($canmoveall && count($addcontexts)) {
384 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
385 question_category_select_menu($addcontexts, false, 0, "$category->id,$category->contextid");
386 }
516cf3eb 387
271e6dec 388 if (function_exists('module_specific_controls') && $canuseall) {
389 echo module_specific_controls($totalnumber, $recurse, $category, $cm->id);
390 }
516cf3eb 391 }
09275894 392 echo '</fieldset>';
393 echo "</form>\n";
516cf3eb 394}
271e6dec 395function question_sort_options($pageurl, $sortorder){
396 global $USER;
397 //sort options
398 $html = "<div class=\"mdl-align\">";
399 $html .= '<form method="post" action="edit.php">';
400 $html .= '<fieldset class="invisiblefieldset" style="display: block;">';
401 $html .= '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
402 $html .= $pageurl->hidden_params_out(array('qsortorder'));
403 $sortoptions = array('alpha' => get_string("sortalpha", "quiz"),
404 'typealpha' => get_string("sorttypealpha", "quiz"),
405 'age' => get_string("sortage", "quiz"));
406 $html .= choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
407 $html .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
408 $html .= '</fieldset>';
409 $html .= "</form>\n";
410 $html .= "</div>\n";
411 return $html;
412}
86909ce0 413
271e6dec 414function question_showbank_actions($pageurl, $cm){
be801b8b 415 global $CFG, $COURSE;
271e6dec 416 /// Now, check for commands on this page and modify variables as necessary
8e77884c 417 if (optional_param('move', false, PARAM_BOOL) and confirm_sesskey()) { /// Move selected questions to new category
271e6dec 418 $category = required_param('category', PARAM_SEQUENCE);
419 list($tocategoryid, $contextid) = explode(',', $category);
420 if (! $tocategory = get_record('question_categories', 'id', $tocategoryid, 'contextid', $contextid)) {
92a95034 421 error('Could not find category record');
86909ce0 422 }
271e6dec 423 $tocontext = get_context_instance_by_id($contextid);
424 require_capability('moodle/question:add', $tocontext);
8e77884c 425 $rawdata = (array) data_submitted();
271e6dec 426 $questionids = array();
8e77884c 427 foreach ($rawdata as $key => $value) { // Parse input for question ids
9ab38475 428 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
b72ff476 429 $key = $matches[1];
271e6dec 430 $questionids[] = $key;
431 }
432 }
433 if ($questionids){
434 $questionidlist = join($questionids, ',');
435 $sql = "SELECT q.*, c.contextid FROM {$CFG->prefix}question q, {$CFG->prefix}question_categories c WHERE q.id IN ($questionidlist) AND c.id = q.category";
436 if (!$questions = get_records_sql($sql)){
437 print_error('questiondoesnotexist', 'question', $pageurl->out());
438 }
439 $checkforfiles = false;
440 foreach ($questions as $question){
441 //check capabilities
442 question_require_capability_on($question, 'move');
443 $fromcontext = get_context_instance_by_id($question->contextid);
444 if (get_filesdir_from_context($fromcontext) != get_filesdir_from_context($tocontext)){
445 $checkforfiles = true;
446 }
447 }
448 $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid"));
449 if (!$checkforfiles){
7fb1b88d 450 if (!question_move_questions_to_category(implode(',', $questionids), $tocategory->id)) {
451 print_error('errormovingquestions', 'question', $returnurl, $questionids);
271e6dec 452 }
453 redirect($returnurl);
454 } else {
455 $movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
456 array('returnurl' => $returnurl,
457 'ids'=>$questionidlist,
458 'tocatid'=> $tocategoryid));
459 if ($cm){
460 $movecontexturl->param('cmid', $cm->id);
461 } else {
462 $movecontexturl->param('courseid', $COURSE->id);
86909ce0 463 }
271e6dec 464 redirect($movecontexturl->out());
86909ce0 465 }
466 }
467 }
468
8e77884c 469 if (optional_param('deleteselected', false, PARAM_BOOL)) { // delete selected questions from the category
470 if (($confirm = optional_param('confirm', '', PARAM_ALPHANUM)) and confirm_sesskey()) { // teacher has already confirmed the action
86909ce0 471 $deleteselected = required_param('deleteselected');
8e77884c 472 if ($confirm == md5($deleteselected)) {
86909ce0 473 if ($questionlist = explode(',', $deleteselected)) {
474 // for each question either hide it if it is in use or delete it
475 foreach ($questionlist as $questionid) {
271e6dec 476 question_require_capability_on($questionid, 'edit');
8e77884c 477 if (record_exists('quiz_question_instances', 'question', $questionid)) {
86909ce0 478 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
271e6dec 479 question_require_capability_on($questionid, 'edit');
92a95034 480 error('Was not able to hide question');
86909ce0 481 }
482 } else {
483 delete_question($questionid);
484 }
485 }
486 }
86909ce0 487 redirect($pageurl->out());
488 } else {
92a95034 489 error("Confirmation string was incorrect");
86909ce0 490 }
86909ce0 491 }
492 }
493
494 // Unhide a question
8e77884c 495 if(($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
271e6dec 496 question_require_capability_on($unhide, 'edit');
86909ce0 497 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
92a95034 498 error("Failed to unhide the question.");
86909ce0 499 }
500 redirect($pageurl->out());
501 }
271e6dec 502}
8e77884c 503
271e6dec 504/**
505 * Shows the question bank editing interface.
506 *
507 * The function also processes a number of actions:
508 *
509 * Actions affecting the question pool:
510 * move Moves a question to a different category
511 * deleteselected Deletes the selected questions from the category
512 * Other actions:
513 * category Chooses the category
514 * displayoptions Sets display options
515 *
516 * @author Martin Dougiamas and many others. This has recently been extensively
517 * rewritten by Gustav Delius and other members of the Serving Mathematics project
518 * {@link http://maths.york.ac.uk/serving_maths}
519 * @param moodle_url $pageurl object representing this pages url.
520 */
521function question_showbank($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
522 global $COURSE;
523
8e77884c 524 if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
271e6dec 525 // make a list of all the questions that are selected
5412cd28 526 $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
271e6dec 527 $questionlist = ''; // comma separated list of ids of questions to be deleted
528 $questionnames = ''; // string with names of questions separated by <br /> with
529 // an asterix in front of those that are in use
530 $inuse = false; // set to true if at least one of the questions is in use
531 foreach ($rawquestions as $key => $value) { // Parse input for question ids
532 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
8e77884c 533 $key = $matches[1];
534 $questionlist .= $key.',';
271e6dec 535 question_require_capability_on($key, 'edit');
8e77884c 536 if (record_exists('quiz_question_instances', 'question', $key)) {
271e6dec 537 $questionnames .= '* ';
538 $inuse = true;
539 }
540 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
541 }
542 }
543 if (!$questionlist) { // no questions were selected
544 redirect($pageurl->out());
545 }
546 $questionlist = rtrim($questionlist, ',');
547
548 // Add an explanation about questions in use
549 if ($inuse) {
550 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
551 }
552 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
553 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
554 $pageurl->out_action());
555
556 echo '</td></tr>';
557 echo '</table>';
558 print_footer($COURSE);
559 exit;
560 }
561
86909ce0 562
86909ce0 563 // starts with category selection form
564 print_box_start('generalbox questionbank');
565 print_heading(get_string('questionbank', 'question'), '', 2);
271e6dec 566 question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
567
86909ce0 568 // continues with list of questions
271e6dec 569 question_list($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, isset($cm) ? $cm : null,
570 $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
571 $contexts->having_cap('moodle/question:add'));
86909ce0 572
573 print_box_end();
574}
b72ff476 575/**
576 * Common setup for all pages for editing questions.
271e6dec 577 * @param string $edittab code for this edit tab
b72ff476 578 * @param boolean $requirecmid require cmid? default false
579 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
271e6dec 580 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
b72ff476 581 */
271e6dec 582function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true){
583 global $COURSE, $QUESTION_EDITTABCAPS;
584
585 //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
36e298bc 586 //of parameters that are passed from page to page.
b72ff476 587 $thispageurl = new moodle_url();
588 if ($requirecmid){
589 $cmid =required_param('cmid', PARAM_INT);
590 } else {
591 $cmid = optional_param('cmid', 0, PARAM_INT);
592 }
593 if ($cmid){
271e6dec 594 list($module, $cm) = get_module_from_cmid($cmid);
b72ff476 595 $courseid = $cm->course;
596 $thispageurl->params(compact('cmid'));
271e6dec 597 require_login($courseid, false, $cm);
598 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
b72ff476 599 } else {
600 $module = null;
601 $cm = null;
602 if ($requirecourseid){
603 $courseid = required_param('courseid', PARAM_INT);
604 } else {
605 $courseid = optional_param('courseid', 0, PARAM_INT);
606 }
607 if ($courseid){
608 $thispageurl->params(compact('courseid'));
271e6dec 609 require_login($courseid, false);
610 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
611 } else {
612 $thiscontext = null;
b72ff476 613 }
614 }
271e6dec 615
616 if ($thiscontext){
617 $contexts = new question_edit_contexts($thiscontext);
618 $contexts->require_one_edit_tab_cap($edittab);
619
620 } else {
621 $contexts = null;
622 }
623
624
625
36e298bc 626 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
271e6dec 627
7bc26c8f 628 //pass 'cat' from page to page and when 'category' comes from a drop down menu
271e6dec 629 //then we also reset the qpage so we go to page 1 of
7bc26c8f 630 //a new cat.
271e6dec 631 $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE);// if empty will be set up later
632 if ($category = optional_param('category', 0, PARAM_SEQUENCE)){
a18fbcfb 633 if ($pagevars['cat'] != $category){ // is this a move to a new category?
634 $pagevars['cat'] = $category;
635 $pagevars['qpage'] = 0;
636 }
7bc26c8f 637 }
638 if ($pagevars['cat']){
639 $thispageurl->param('cat', $pagevars['cat']);
640 }
b72ff476 641 if ($pagevars['qpage'] > -1) {
642 $thispageurl->param('qpage', $pagevars['qpage']);
643 } else {
644 $pagevars['qpage'] = 0;
645 }
646
36e298bc 647 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
b72ff476 648 if ($pagevars['qperpage'] > -1) {
649 $thispageurl->param('qperpage', $pagevars['qperpage']);
650 } else {
651 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
652 }
271e6dec 653
36e298bc 654 $sortoptions = array('alpha' => 'name, qtype ASC',
655 'typealpha' => 'qtype, name ASC',
656 'age' => 'id ASC');
657
658 if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
659 $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
660 $pagevars['qsortorder'] = $sortorder;
661 $thispageurl->param('qsortorder', $sortorder);
b72ff476 662 } else {
7bc26c8f 663 $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
664 $pagevars['qsortorder'] = 'typealpha';
271e6dec 665 }
36e298bc 666
271e6dec 667 $defaultcategory = question_make_default_categories($contexts->all());
668
669 $contextlistarr = array();
670 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
671 $contextlistarr[] = "'$context->id'";
672 }
673 $contextlist = join($contextlistarr, ' ,');
674 if (!empty($pagevars['cat'])){
675 $catparts = explode(',', $pagevars['cat']);
676 if (!$catparts[0] || (FALSE !== array_search($catparts[1], $contextlistarr)) || !count_records_select("question_categories", "id = '".$catparts[0]."' AND contextid = $catparts[1]")) {
5a2a5331 677 print_error('invalidcategory', 'quiz');
271e6dec 678 }
679 } else {
680 $category = $defaultcategory;
681 $pagevars['cat'] = "$category->id,$category->contextid";
36e298bc 682 }
683
684 if(($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
685 $pagevars['recurse'] = $recurse;
686 $thispageurl->param('recurse', $recurse);
687 } else {
688 $pagevars['recurse'] = 1;
689 }
271e6dec 690
36e298bc 691 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
692 $pagevars['showhidden'] = $showhidden;
693 $thispageurl->param('showhidden', $showhidden);
694 } else {
695 $pagevars['showhidden'] = 0;
696 }
271e6dec 697
36e298bc 698 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
699 $pagevars['showquestiontext'] = $showquestiontext;
700 $thispageurl->param('showquestiontext', $showquestiontext);
701 } else {
702 $pagevars['showquestiontext'] = 0;
703 }
271e6dec 704
986effb6 705 //category list page
706 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
707 if ($pagevars['cpage'] != 1){
708 $thispageurl->param('cpage', $pagevars['cpage']);
709 }
36e298bc 710
271e6dec 711
712 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
713}
714class question_edit_contexts{
715 var $allcontexts;
716 /**
717 * @param current context
718 */
719 function question_edit_contexts($thiscontext){
720 $pcontextids = get_parent_contexts($thiscontext);
721 $contexts = array($thiscontext);
722 foreach ($pcontextids as $pcontextid){
723 $contexts[] = get_context_instance_by_id($pcontextid);
724 }
725 $this->allcontexts = $contexts;
726 }
727 /**
728 * @return array all parent contexts
729 */
730 function all(){
731 return $this->allcontexts;
732 }
733 /**
734 * @return object lowest context which must be either the module or course context
735 */
736 function lowest(){
737 return $this->allcontexts[0];
738 }
739 /**
740 * @param string $cap capability
741 * @return array parent contexts having capability, zero based index
742 */
743 function having_cap($cap){
744 $contextswithcap = array();
745 foreach ($this->allcontexts as $context){
746 if (has_capability($cap, $context)){
747 $contextswithcap[] = $context;
748 }
749 }
750 return $contextswithcap;
751 }
752 /**
753 * @param array $caps capabilities
754 * @return array parent contexts having at least one of $caps, zero based index
755 */
756 function having_one_cap($caps){
757 $contextswithacap = array();
758 foreach ($this->allcontexts as $context){
759 foreach ($caps as $cap){
760 if (has_capability($cap, $context)){
761 $contextswithacap[] = $context;
762 break; //done with caps loop
763 }
764 }
765 }
766 return $contextswithacap;
767 }
768 /**
769 * @param string $tabname edit tab name
770 * @return array parent contexts having at least one of $caps, zero based index
771 */
772 function having_one_edit_tab_cap($tabname){
773 global $QUESTION_EDITTABCAPS;
774 return $this->having_one_cap($QUESTION_EDITTABCAPS[$tabname]);
775 }
776 /**
777 * Has at least one parent context got the cap $cap?
778 *
779 * @param string $cap capability
780 * @return boolean
781 */
782 function have_cap($cap){
783 return (count($this->having_cap($cap)));
784 }
785
786 /**
787 * Has at least one parent context got one of the caps $caps?
788 *
789 * @param string $cap capability
790 * @return boolean
791 */
792 function have_one_cap($caps){
793 foreach ($caps as $cap){
794 if ($this->have_cap($cap)){
795 return true;
796 }
797 }
798 return false;
799 }
800 /**
801 * Has at least one parent context got one of the caps for actions on $tabname
802 *
803 * @param string $tabname edit tab name
804 * @return boolean
805 */
806 function have_one_edit_tab_cap($tabname){
807 global $QUESTION_EDITTABCAPS;
808 return $this->have_one_cap($QUESTION_EDITTABCAPS[$tabname]);
809 }
810 /**
811 * Throw error if at least one parent context hasn't got the cap $cap
812 *
813 * @param string $cap capability
814 */
815 function require_cap($cap){
816 if (!$this->have_cap($cap)){
817 print_error('nopermissions', '', '', $cap);
818 }
819 }
820 /**
821 * Throw error if at least one parent context hasn't got one of the caps $caps
822 *
823 * @param array $cap capabilities
824 */
825 function require_one_cap($caps){
826 if (!$this->have_one_cap($caps)){
827 $capsstring = join($caps, ', ');
828 print_error('nopermissions', '', '', $capsstring);
829 }
830 }
831 /**
832 * Throw error if at least one parent context hasn't got one of the caps $caps
833 *
834 * @param string $tabname edit tab name
835 */
836 function require_one_edit_tab_cap($tabname){
837 if (!$this->have_one_edit_tab_cap($tabname)){
838 print_error('nopermissions', '', '', 'access question edit tab '.$tabname);
839 }
840 }
841}
842
843//capabilities for each page of edit tab.
844//this determines which contexts' categories are available. At least one
845//page is displayed if user has one of the capability on at least one context
846$QUESTION_EDITTABCAPS = array(
847 'editq' => array('moodle/question:add',
848 'moodle/question:editmine',
849 'moodle/question:editall',
850 'moodle/question:viewmine',
851 'moodle/question:viewall',
852 'moodle/question:usemine',
853 'moodle/question:useall',
854 'moodle/question:movemine',
855 'moodle/question:moveall'),
856 'questions'=>array('moodle/question:add',
857 'moodle/question:editmine',
858 'moodle/question:editall',
859 'moodle/question:viewmine',
860 'moodle/question:viewall',
861 'moodle/question:movemine',
862 'moodle/question:moveall'),
863 'categories'=>array('moodle/question:managecategory'),
864 'import'=>array('moodle/question:add'),
865 'export'=>array('moodle/question:viewall', 'moodle/question:viewmine'));
866
867
868
869/**
870 * Make sure user is logged in as required in this context.
871 */
872function require_login_in_context($contextorid = null){
873 if (!is_object($contextorid)){
874 $context = get_context_instance_by_id($contextorid);
875 } else {
876 $context = $contextorid;
877 }
878 if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
879 require_login($context->instanceid);
880 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
881 if ($cm = get_record('course_modules','id',$context->instanceid)) {
882 if (!$course = get_record('course', 'id', $cm->course)) {
0be2c858 883 print_error('invalidcourseid');
271e6dec 884 }
885 require_course_login($course, true, $cm);
886
887 } else {
0be2c858 888 print_error('invalidcoursemodule');
271e6dec 889 }
890 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
891 if (!empty($CFG->forcelogin)) {
892 require_login();
893 }
894
895 } else {
896 require_login();
897 }
b72ff476 898}
871ab9a3 899?>