3 * Functions used to show question editing interface
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
13 require_once($CFG->libdir
.'/questionlib.php');
15 define('DEFAULT_QUESTIONS_PER_PAGE', 20);
17 function get_module_from_cmid($cmid){
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
25 } elseif (!$modrec =get_record($cmrec->modname
, 'id', $cmrec->instance
)) {
28 $modrec->instance
= $modrec->id
;
29 $modrec->cmid
= $cmrec->id
;
31 return array($modrec, $cmrec);
34 * Function to read all questions for category into big array
36 * @param int $category category number
37 * @param bool $noparent if true only questions with NO parent will be selected
38 * @param bool $recurse include subdirectories
39 * @param bool $export set true if this is called by questionbank export
40 * @author added by Howard Miller June 2004
42 function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
46 // questions will be added to an array
49 // build sql bit for $noparent
52 $npsql = " and parent='0' ";
55 // get (list) of categories
57 $categorylist = question_categorylist( $category->id
);
60 $categorylist = $category->id
;
63 // get the list of questions for the category
64 if ($questions = get_records_select("question","category IN ($categorylist) $npsql", "qtype, name ASC")) {
66 // iterate through questions, getting stuff we need
67 foreach($questions as $question) {
68 $questiontype = $QTYPES[$question->qtype
];
69 $question->export_process
= $export;
70 $questiontype->get_question_options( $question );
71 $qresults[] = $question;
79 function question_can_delete_cat($todelete){
81 $record = get_record_sql("SELECT count(*) as count, c1.contextid as contextid FROM {$CFG->prefix}question_categories c1,
82 {$CFG->prefix}question_categories c2 WHERE c2.id = $todelete
83 AND c1.contextid = c2.contextid GROUP BY c1.contextid");
84 $contextid = $record->contextid
;
85 $count = $record->count
;
87 error('You can\'t delete that category it is the default category for this context.');
89 require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
93 * prints a form to choose categories
95 function question_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
99 /// Get all the existing categories now
100 $catmenu = question_category_options($contexts, false, 0, true);
102 $strcategory = get_string('category', 'quiz');
103 $strshow = get_string('show', 'quiz');
104 $streditcats = get_string('editcategories', 'quiz');
106 popup_form ('edit.php?'.$pageurl->get_query_string().'&category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "<strong>$strcategory</strong>");
108 echo '<form method="get" action="edit.php" id="displayoptions">';
109 echo "<fieldset class='invisiblefieldset'>";
110 echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext'));
111 question_category_form_checkbox('recurse', $recurse);
112 question_category_form_checkbox('showhidden', $showhidden);
113 question_category_form_checkbox('showquestiontext', $showquestiontext);
114 echo '<noscript><div class="centerpara"><input type="submit" value="'. get_string('go') .'" />';
115 echo '</div></noscript></fieldset></form>';
119 * Private funciton to help the preceeding function.
121 function question_category_form_checkbox($name, $checked) {
122 echo '<div><input type="hidden" id="' . $name . '_off" name="' . $name . '" value="0" />';
123 echo '<input type="checkbox" id="' . $name . '_on" name="' . $name . '" value="1"';
125 echo ' checked="checked"';
127 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
128 echo '<label for="' . $name . '_on">';
129 print_string($name, 'quiz');
130 echo "</label></div>\n";
134 * Prints the table of questions in a category with interactions
136 * @param object $course The course object
137 * @param int $categoryid The id of the question category to be displayed
138 * @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
139 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
140 * @param int $page The number of the page to be displayed
141 * @param int $perpage Number of questions to show per page
142 * @param boolean $showhidden True if also hidden questions should be displayed
143 * @param boolean $showquestiontext whether the text of each question should be shown in the list
145 function question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
146 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
147 $showquestiontext = false, $addcontexts = array()) {
148 global $QTYPE_MENU, $USER, $CFG, $THEME, $COURSE;
150 list($categoryid, $contextid)= explode(',', $categoryandcontext);
152 $qtypemenu = $QTYPE_MENU;
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");
168 $strmove = get_string('moveqtoanothercontext', 'question');
169 $strview = get_string("view");
170 $straction = get_string("action");
171 $strrestore = get_string('restore');
173 $strtype = get_string("type", "quiz");
174 $strcreatemultiple = get_string("createmultiple", "quiz");
175 $strpreview = get_string("preview","quiz");
178 echo "<p style=\"text-align:center;\"><b>";
179 print_string("selectcategoryabove", "quiz");
184 if (!$category = get_record('question_categories', 'id', $categoryid, 'contextid', $contextid)) {
185 notify('Category not found!');
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);
195 if ($cm AND $cm->modname
== 'quiz') {
196 $quizid = $cm->instance
;
200 $returnurl = $pageurl->out();
201 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
202 array('returnurl' => $returnurl));
204 $questionurl->param('cmid', $cm->id
);
206 $questionurl->param('courseid', $COURSE->id
);
208 $questionmoveurl = new moodle_url("$CFG->wwwroot/question/contextmoveq.php",
209 array('returnurl' => $returnurl));
211 $questionmoveurl->param('cmid', $cm->id
);
213 $questionmoveurl->param('courseid', $COURSE->id
);
215 echo '<div class="boxaligncenter">';
216 $formatoptions = new stdClass
;
217 $formatoptions->noclean
= true;
218 echo format_text($category->info
, FORMAT_MOODLE
, $formatoptions, $COURSE->id
);
223 echo '<td valign="top" align="right">';
224 popup_form ($questionurl->out(false, array('category' => $category->id
)).'&qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
225 echo '</td><td valign="top" align="right">';
226 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
231 print_string('nopermissionadd', 'question');
235 echo '</tr></table>';
238 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
241 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
243 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
244 echo "<p style=\"text-align:center;\">";
245 print_string("noquestions", "quiz");
250 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', $page*$perpage, $perpage)) {
251 // There are no questions on the requested page.
253 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', 0, $perpage)) {
254 // There are no questions at all
255 echo "<p style=\"text-align:center;\">";
256 print_string("noquestions", "quiz");
262 print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
264 echo question_sort_options($pageurl, $sortorder);
267 echo '<form method="post" action="edit.php">';
268 echo '<fieldset class="invisiblefieldset" style="display: block;">';
269 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
270 echo $pageurl->hidden_params_out(array('qsortorder'));
271 echo '<table id="categoryquestions" style="width: 100%"><tr>';
272 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
274 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname</th>
275 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
277 foreach ($questions as $question) {
280 if ($question->hidden
) {
281 $nameclass = 'dimmed_text';
282 $textclass = 'dimmed_text';
284 if ($showquestiontext) {
285 $nameclass .= ' header';
288 $nameclass = 'class="' . $nameclass . '"';
291 $textclass = 'class="' . $textclass . '"';
294 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
296 $canuseq = question_has_capability_on($question, 'use', $question->category
);
297 if (function_exists('module_specific_actions')) {
298 echo module_specific_actions($pageurl, $question->id
, $cm->id
, $canuseq);
303 $quizorcourseid = $quizid?
('&quizid=' . $quizid):('&courseid=' .$COURSE->id
);
304 link_to_popup_window('/question/preview.php?id=' . $question->id
. $quizorcourseid, 'questionpreview',
305 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
306 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS
);
308 // edit, hide, delete question, using question capabilities, not quiz capabilieies
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
311 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a> ";
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> ";
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> ";
322 if (question_has_capability_on($question, 'edit', $question->category
)) {
324 if($question->hidden
) {
325 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&unhide=$question->id&sesskey=$USER->sesskey\"><img
326 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
328 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&deleteselected=$question->id&q$question->id=1\"><img
329 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
332 if ($caneditall ||
$canmoveall ||
$canuseall){
333 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
337 echo "<td $nameclass>" . format_string($question->name
) . "</td>\n";
338 echo "<td $nameclass style='text-align: right'>\n";
339 print_question_icon($question);
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
,
348 $formatoptions, $COURSE->id
);
354 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true);
355 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE
) {
356 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE
) {
357 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
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>';
362 $paging = substr($paging, 0, strrpos($paging, '</div>'));
363 $paging .= "<br />$showall</div>";
365 $paging = "<div class='paging'>$showall</div>";
370 if ($caneditall ||
$canmoveall ||
$canuseall){
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>';
374 echo '<strong> '.get_string('withselected', 'quiz').':</strong><br />';
376 if (function_exists('module_specific_buttons')) {
377 echo module_specific_buttons($cm->id
);
379 // print delete and move selected question
381 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" />\n";
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");
388 if (function_exists('module_specific_controls') && $canuseall) {
389 echo module_specific_controls($totalnumber, $recurse, $category, $cm->id
);
395 function question_sort_options($pageurl, $sortorder){
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";
414 function question_showbank_actions($pageurl, $cm){
416 /// Now, check for commands on this page and modify variables as necessary
417 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
418 $category = required_param('category', PARAM_SEQUENCE
);
419 list($tocategoryid, $contextid) = explode(',', $category);
420 if (! $tocategory = get_record('question_categories', 'id', $tocategoryid, 'contextid', $contextid)) {
421 error('Could not find category record');
423 $tocontext = get_context_instance_by_id($contextid);
424 require_capability('moodle/question:add', $tocontext);
425 $questionids = array();
426 foreach ($_POST as $key => $value) { // Parse input for question ids
427 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
429 $questionids[] = $key;
433 $questionidlist = join($questionids, ',');
434 $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";
435 if (!$questions = get_records_sql($sql)){
436 print_error('questiondoesnotexist', 'question', $pageurl->out());
438 $checkforfiles = false;
439 foreach ($questions as $question){
441 question_require_capability_on($question, 'move');
442 $fromcontext = get_context_instance_by_id($question->contextid
);
443 if (get_filesdir_from_context($fromcontext) != get_filesdir_from_context($tocontext)){
444 $checkforfiles = true;
447 $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid"));
448 if (!$checkforfiles){
449 foreach ($questionids as $questionid){
451 if (!set_field('question', 'category', $tocategory->id
, 'id', $questionid)) {
452 error('Could not update category field');
455 redirect($returnurl);
457 $movecontexturl = new moodle_url($CFG->wwwroot
.'/question/contextmoveq.php',
458 array('returnurl' => $returnurl,
459 'ids'=>$questionidlist,
460 'tocatid'=> $tocategoryid));
462 $movecontexturl->param('cmid', $cm->id
);
464 $movecontexturl->param('courseid', $COURSE->id
);
466 redirect($movecontexturl->out());
471 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
473 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
474 $deleteselected = required_param('deleteselected');
475 if ($_REQUEST['confirm'] == md5($deleteselected)) {
476 if ($questionlist = explode(',', $deleteselected)) {
477 // for each question either hide it if it is in use or delete it
478 foreach ($questionlist as $questionid) {
479 question_require_capability_on($questionid, 'edit');
480 if (record_exists('quiz_question_instances', 'question', $questionid) or
481 record_exists('question_states', 'originalquestion', $questionid)) {
482 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
483 question_require_capability_on($questionid, 'edit');
484 error('Was not able to hide question');
487 delete_question($questionid);
491 redirect($pageurl->out());
493 error("Confirmation string was incorrect");
501 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
502 $unhide = required_param('unhide', PARAM_INT
);
503 question_require_capability_on($unhide, 'edit');
504 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
505 error("Failed to unhide the question.");
507 redirect($pageurl->out());
511 * Shows the question bank editing interface.
513 * The function also processes a number of actions:
515 * Actions affecting the question pool:
516 * move Moves a question to a different category
517 * deleteselected Deletes the selected questions from the category
519 * category Chooses the category
520 * displayoptions Sets display options
522 * @author Martin Dougiamas and many others. This has recently been extensively
523 * rewritten by Gustav Delius and other members of the Serving Mathematics project
524 * {@link http://maths.york.ac.uk/serving_maths}
525 * @param moodle_url $pageurl object representing this pages url.
527 function question_showbank($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
530 if (isset($_REQUEST['deleteselected'])){ // teacher still has to confirm
531 // make a list of all the questions that are selected
532 $rawquestions = $_REQUEST;
533 $questionlist = ''; // comma separated list of ids of questions to be deleted
534 $questionnames = ''; // string with names of questions separated by <br /> with
535 // an asterix in front of those that are in use
536 $inuse = false; // set to true if at least one of the questions is in use
537 foreach ($rawquestions as $key => $value) { // Parse input for question ids
538 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
539 $key = $matches[1]; $questionlist .= $key.',';
540 question_require_capability_on($key, 'edit');
541 if (record_exists('quiz_question_instances', 'question', $key) or
542 record_exists('question_states', 'originalquestion', $key)) {
543 $questionnames .= '* ';
546 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
549 if (!$questionlist) { // no questions were selected
550 redirect($pageurl->out());
552 $questionlist = rtrim($questionlist, ',');
554 // Add an explanation about questions in use
556 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
558 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
559 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
560 $pageurl->out_action());
564 print_footer($COURSE);
569 // starts with category selection form
570 print_box_start('generalbox questionbank');
571 print_heading(get_string('questionbank', 'question'), '', 2);
572 question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
574 // continues with list of questions
575 question_list($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, isset($cm) ?
$cm : null,
576 $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
577 $contexts->having_cap('moodle/question:add'));
582 * Common setup for all pages for editing questions.
583 * @param string $edittab code for this edit tab
584 * @param boolean $requirecmid require cmid? default false
585 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
586 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
588 function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true){
589 global $COURSE, $QUESTION_EDITTABCAPS;
591 //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
592 //of parameters that are passed from page to page.
593 $thispageurl = new moodle_url();
595 $cmid =required_param('cmid', PARAM_INT
);
597 $cmid = optional_param('cmid', 0, PARAM_INT
);
600 list($module, $cm) = get_module_from_cmid($cmid);
601 $courseid = $cm->course
;
602 $thispageurl->params(compact('cmid'));
603 require_login($courseid, false, $cm);
604 $thiscontext = get_context_instance(CONTEXT_MODULE
, $cmid);
608 if ($requirecourseid){
609 $courseid = required_param('courseid', PARAM_INT
);
611 $courseid = optional_param('courseid', 0, PARAM_INT
);
614 $thispageurl->params(compact('courseid'));
615 require_login($courseid, false);
616 $thiscontext = get_context_instance(CONTEXT_COURSE
, $courseid);
623 $contexts = new question_edit_contexts($thiscontext);
624 $contexts->require_one_edit_tab_cap($edittab);
632 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT
);
634 //pass 'cat' from page to page and when 'category' comes from a drop down menu
635 //then we also reset the qpage so we go to page 1 of
637 $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE
);// if empty will be set up later
638 if ($category = optional_param('category', 0, PARAM_SEQUENCE
)){
639 $pagevars['cat'] = $category;
640 $pagevars['qpage'] = 0;
642 if ($pagevars['cat']){
643 $thispageurl->param('cat', $pagevars['cat']);
645 if ($pagevars['qpage'] > -1) {
646 $thispageurl->param('qpage', $pagevars['qpage']);
648 $pagevars['qpage'] = 0;
651 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT
);
652 if ($pagevars['qperpage'] > -1) {
653 $thispageurl->param('qperpage', $pagevars['qperpage']);
655 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE
;
658 $sortoptions = array('alpha' => 'name, qtype ASC',
659 'typealpha' => 'qtype, name ASC',
662 if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA
)) {
663 $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
664 $pagevars['qsortorder'] = $sortorder;
665 $thispageurl->param('qsortorder', $sortorder);
667 $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
668 $pagevars['qsortorder'] = 'typealpha';
671 $defaultcategory = question_make_default_categories($contexts->all());
673 $contextlistarr = array();
674 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
675 $contextlistarr[] = "'$context->id'";
677 $contextlist = join($contextlistarr, ' ,');
678 if (!empty($pagevars['cat'])){
679 $catparts = explode(',', $pagevars['cat']);
680 if (!$catparts[0] ||
(FALSE !== array_search($catparts[1], $contextlistarr)) ||
!count_records_select("question_categories", "id = '".$catparts[0]."' AND contextid = $catparts[1]")) {
681 error(get_string('invalidcategory', 'quiz'));
684 $category = $defaultcategory;
685 $pagevars['cat'] = "$category->id,$category->contextid";
688 if(($recurse = optional_param('recurse', -1, PARAM_BOOL
)) != -1) {
689 $pagevars['recurse'] = $recurse;
690 $thispageurl->param('recurse', $recurse);
692 $pagevars['recurse'] = 1;
695 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL
)) != -1) {
696 $pagevars['showhidden'] = $showhidden;
697 $thispageurl->param('showhidden', $showhidden);
699 $pagevars['showhidden'] = 0;
702 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL
)) != -1) {
703 $pagevars['showquestiontext'] = $showquestiontext;
704 $thispageurl->param('showquestiontext', $showquestiontext);
706 $pagevars['showquestiontext'] = 0;
710 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT
);
711 if ($pagevars['cpage'] != 1){
712 $thispageurl->param('cpage', $pagevars['cpage']);
716 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
718 class question_edit_contexts
{
721 * @param current context
723 function question_edit_contexts($thiscontext){
724 $pcontextids = get_parent_contexts($thiscontext);
725 $contexts = array($thiscontext);
726 foreach ($pcontextids as $pcontextid){
727 $contexts[] = get_context_instance_by_id($pcontextid);
729 $this->allcontexts
= $contexts;
732 * @return array all parent contexts
735 return $this->allcontexts
;
738 * @return object lowest context which must be either the module or course context
741 return $this->allcontexts
[0];
744 * @param string $cap capability
745 * @return array parent contexts having capability, zero based index
747 function having_cap($cap){
748 $contextswithcap = array();
749 foreach ($this->allcontexts
as $context){
750 if (has_capability($cap, $context)){
751 $contextswithcap[] = $context;
754 return $contextswithcap;
757 * @param array $caps capabilities
758 * @return array parent contexts having at least one of $caps, zero based index
760 function having_one_cap($caps){
761 $contextswithacap = array();
762 foreach ($this->allcontexts
as $context){
763 foreach ($caps as $cap){
764 if (has_capability($cap, $context)){
765 $contextswithacap[] = $context;
766 break; //done with caps loop
770 return $contextswithacap;
773 * @param string $tabname edit tab name
774 * @return array parent contexts having at least one of $caps, zero based index
776 function having_one_edit_tab_cap($tabname){
777 global $QUESTION_EDITTABCAPS;
778 return $this->having_one_cap($QUESTION_EDITTABCAPS[$tabname]);
781 * Has at least one parent context got the cap $cap?
783 * @param string $cap capability
786 function have_cap($cap){
787 return (count($this->having_cap($cap)));
791 * Has at least one parent context got one of the caps $caps?
793 * @param string $cap capability
796 function have_one_cap($caps){
797 foreach ($caps as $cap){
798 if ($this->have_cap($cap)){
805 * Has at least one parent context got one of the caps for actions on $tabname
807 * @param string $tabname edit tab name
810 function have_one_edit_tab_cap($tabname){
811 global $QUESTION_EDITTABCAPS;
812 return $this->have_one_cap($QUESTION_EDITTABCAPS[$tabname]);
815 * Throw error if at least one parent context hasn't got the cap $cap
817 * @param string $cap capability
819 function require_cap($cap){
820 if (!$this->have_cap($cap)){
821 print_error('nopermissions', '', '', $cap);
825 * Throw error if at least one parent context hasn't got one of the caps $caps
827 * @param array $cap capabilities
829 function require_one_cap($caps){
830 if (!$this->have_one_cap($caps)){
831 $capsstring = join($caps, ', ');
832 print_error('nopermissions', '', '', $capsstring);
836 * Throw error if at least one parent context hasn't got one of the caps $caps
838 * @param string $tabname edit tab name
840 function require_one_edit_tab_cap($tabname){
841 if (!$this->have_one_edit_tab_cap($tabname)){
842 print_error('nopermissions', '', '', 'access question edit tab '.$tabname);
847 //capabilities for each page of edit tab.
848 //this determines which contexts' categories are available. At least one
849 //page is displayed if user has one of the capability on at least one context
850 $QUESTION_EDITTABCAPS = array(
851 'editq' => array('moodle/question:add',
852 'moodle/question:editmine',
853 'moodle/question:editall',
854 'moodle/question:viewmine',
855 'moodle/question:viewall',
856 'moodle/question:usemine',
857 'moodle/question:useall',
858 'moodle/question:movemine',
859 'moodle/question:moveall'),
860 'questions'=>array('moodle/question:add',
861 'moodle/question:editmine',
862 'moodle/question:editall',
863 'moodle/question:viewmine',
864 'moodle/question:viewall',
865 'moodle/question:movemine',
866 'moodle/question:moveall'),
867 'categories'=>array('moodle/question:managecategory'),
868 'import'=>array('moodle/question:add'),
869 'export'=>array('moodle/question:viewall', 'moodle/question:viewmine'));
874 * Make sure user is logged in as required in this context.
876 function require_login_in_context($contextorid = null){
877 if (!is_object($contextorid)){
878 $context = get_context_instance_by_id($contextorid);
880 $context = $contextorid;
882 if ($context && ($context->contextlevel
== CONTEXT_COURSE
)) {
883 require_login($context->instanceid
);
884 } else if ($context && ($context->contextlevel
== CONTEXT_MODULE
)) {
885 if ($cm = get_record('course_modules','id',$context->instanceid
)) {
886 if (!$course = get_record('course', 'id', $cm->course
)) {
887 error('Incorrect course.');
889 require_course_login($course, true, $cm);
892 error('Incorrect course module id.');
894 } else if ($context && ($context->contextlevel
== CONTEXT_SYSTEM
)) {
895 if (!empty($CFG->forcelogin
)) {