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 * Gets the default category in the most specific context.
80 * If no categories exist yet then default ones are created in all contexts.
82 * @param array $contexts The context objects for this context and all parent contexts.
83 * @return object The default category - the category in the course context
85 function question_make_default_categories($contexts) {
86 // If it already exists, just return it.
87 foreach ($contexts as $key => $context) {
88 if (!$categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1)) {
89 error('error getting category record');
91 if (!$category = rs_fetch_record($categoryrs)){
92 // Otherwise, we need to make one
93 $category = new stdClass
;
94 $contextname = print_context_name($context, false, true);
95 $category->name
= addslashes(get_string('defaultfor', 'question', $contextname));
96 $category->info
= addslashes(get_string('defaultinfofor', 'question', $contextname));
97 $category->contextid
= $context->id
;
98 $category->parent
= 0;
99 $category->sortorder
= 999; // By default, all categories get this number, and are sorted alphabetically.
100 $category->stamp
= make_unique_id_code();
101 if (!$category->id
= insert_record('question_categories', $category)) {
102 error('Error creating a default category for context '.print_context_name($context));
106 if ($context->contextlevel
== CONTEXT_COURSE
){
107 $toreturn = clone($category);
115 function question_can_delete_cat($todelete){
117 $record = get_record_sql("SELECT count(*) as count, c1.contextid as contextid FROM {$CFG->prefix}question_categories c1,
118 {$CFG->prefix}question_categories c2 WHERE c2.id = $todelete
119 AND c1.contextid = c2.contextid GROUP BY c1.contextid");
120 $contextid = $record->contextid
;
121 $count = $record->count
;
123 error('You can\'t delete that category it is the default category for this context.');
125 require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
129 * prints a form to choose categories
131 function question_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
135 /// Get all the existing categories now
136 $catmenu = question_category_options($contexts, false, 0, true);
138 $strcategory = get_string('category', 'quiz');
139 $strshow = get_string('show', 'quiz');
140 $streditcats = get_string('editcategories', 'quiz');
142 popup_form ('edit.php?'.$pageurl->get_query_string().'&category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "<strong>$strcategory</strong>");
144 echo '<form method="get" action="edit.php" id="displayoptions">';
145 echo "<fieldset class='invisiblefieldset'>";
146 echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext'));
147 question_category_form_checkbox('recurse', $recurse);
148 question_category_form_checkbox('showhidden', $showhidden);
149 question_category_form_checkbox('showquestiontext', $showquestiontext);
150 echo '<noscript><div class="centerpara"><input type="submit" value="'. get_string('go') .'" />';
151 echo '</div></noscript></fieldset></form>';
155 * Private funciton to help the preceeding function.
157 function question_category_form_checkbox($name, $checked) {
158 echo '<div><input type="hidden" id="' . $name . '_off" name="' . $name . '" value="0" />';
159 echo '<input type="checkbox" id="' . $name . '_on" name="' . $name . '" value="1"';
161 echo ' checked="checked"';
163 echo ' onchange="getElementById(\'displayoptions\').submit(); return true;" />';
164 echo '<label for="' . $name . '_on">';
165 print_string($name, 'quiz');
166 echo "</label></div>\n";
170 * Prints the table of questions in a category with interactions
172 * @param object $course The course object
173 * @param int $categoryid The id of the question category to be displayed
174 * @param int $cm The course module record if we are in the context of a particular module, 0 otherwise
175 * @param int $recurse This is 1 if subcategories should be included, 0 otherwise
176 * @param int $page The number of the page to be displayed
177 * @param int $perpage Number of questions to show per page
178 * @param boolean $showhidden True if also hidden questions should be displayed
179 * @param boolean $showquestiontext whether the text of each question should be shown in the list
181 function question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
182 $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
183 $showquestiontext = false, $addcontexts = array()) {
184 global $QTYPE_MENU, $USER, $CFG, $THEME, $COURSE;
186 list($categoryid, $contextid)= explode(',', $categoryandcontext);
188 $qtypemenu = $QTYPE_MENU;
191 $strcategory = get_string("category", "quiz");
192 $strquestion = get_string("question", "quiz");
193 $straddquestions = get_string("addquestions", "quiz");
194 $strimportquestions = get_string("importquestions", "quiz");
195 $strexportquestions = get_string("exportquestions", "quiz");
196 $strnoquestions = get_string("noquestions", "quiz");
197 $strselect = get_string("select", "quiz");
198 $strselectall = get_string("selectall", "quiz");
199 $strselectnone = get_string("selectnone", "quiz");
200 $strcreatenewquestion = get_string("createnewquestion", "quiz");
201 $strquestionname = get_string("questionname", "quiz");
202 $strdelete = get_string("delete");
203 $stredit = get_string("edit");
204 $strmove = get_string('moveqtoanothercontext', 'question');
205 $strview = get_string("view");
206 $straction = get_string("action");
207 $strrestore = get_string('restore');
209 $strtype = get_string("type", "quiz");
210 $strcreatemultiple = get_string("createmultiple", "quiz");
211 $strpreview = get_string("preview","quiz");
214 echo "<p style=\"text-align:center;\"><b>";
215 print_string("selectcategoryabove", "quiz");
220 if (!$category = get_record('question_categories', 'id', $categoryid, 'contextid', $contextid)) {
221 notify('Category not found!');
224 $catcontext = get_context_instance_by_id($contextid);
225 $canadd = has_capability('moodle/question:add', $catcontext);
226 //check for capabilities on all questions in category, will also apply to sub cats.
227 $caneditall =has_capability('moodle/question:editall', $catcontext);
228 $canuseall =has_capability('moodle/question:useall', $catcontext);
229 $canmoveall =has_capability('moodle/question:moveall', $catcontext);
231 if ($cm AND $cm->modname
== 'quiz') {
232 $quizid = $cm->instance
;
236 $returnurl = $pageurl->out();
237 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
238 array('returnurl' => $returnurl));
240 $questionurl->param('cmid', $cm->id
);
242 $questionurl->param('courseid', $COURSE->id
);
244 $questionmoveurl = new moodle_url("$CFG->wwwroot/question/contextmoveq.php",
245 array('returnurl' => $returnurl));
247 $questionmoveurl->param('cmid', $cm->id
);
249 $questionmoveurl->param('courseid', $COURSE->id
);
251 echo '<div class="boxaligncenter">';
252 $formatoptions = new stdClass
;
253 $formatoptions->noclean
= true;
254 echo format_text($category->info
, FORMAT_MOODLE
, $formatoptions, $COURSE->id
);
259 echo '<td valign="top" align="right">';
260 popup_form ($questionurl->out(false, array('category' => $category->id
)).'&qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
261 echo '</td><td valign="top" align="right">';
262 helpbutton("questiontypes", $strcreatenewquestion, "quiz");
267 print_string('nopermissionadd', 'question');
271 echo '</tr></table>';
274 $categorylist = ($recurse) ?
question_categorylist($category->id
) : $category->id
;
277 $showhidden = $showhidden ?
'' : " AND hidden = '0'";
279 if (!$totalnumber = count_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden")) {
280 echo "<p style=\"text-align:center;\">";
281 print_string("noquestions", "quiz");
286 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', $page*$perpage, $perpage)) {
287 // There are no questions on the requested page.
289 if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', 0, $perpage)) {
290 // There are no questions at all
291 echo "<p style=\"text-align:center;\">";
292 print_string("noquestions", "quiz");
298 print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
300 echo question_sort_options($pageurl, $sortorder);
303 echo '<form method="post" action="edit.php">';
304 echo '<fieldset class="invisiblefieldset" style="display: block;">';
305 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
306 echo $pageurl->hidden_params_out(array('qsortorder'));
307 echo '<table id="categoryquestions" style="width: 100%"><tr>';
308 echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
310 echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname</th>
311 <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
313 foreach ($questions as $question) {
316 if ($question->hidden
) {
317 $nameclass = 'dimmed_text';
318 $textclass = 'dimmed_text';
320 if ($showquestiontext) {
321 $nameclass .= ' header';
324 $nameclass = 'class="' . $nameclass . '"';
327 $textclass = 'class="' . $textclass . '"';
330 echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
332 $canuseq = question_has_capability_on($question, 'use', $question->category
);
333 if (function_exists('module_specific_actions')) {
334 echo module_specific_actions($pageurl, $question->id
, $cm->id
, $canuseq);
339 $quizorcourseid = $quizid?
('&quizid=' . $quizid):('&courseid=' .$COURSE->id
);
340 link_to_popup_window('/question/preview.php?id=' . $question->id
. $quizorcourseid, 'questionpreview',
341 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
342 0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS
);
344 // edit, hide, delete question, using question capabilities, not quiz capabilieies
345 if (question_has_capability_on($question, 'edit', $question->category
) ||
question_has_capability_on($question, 'move', $question->category
)) {
346 echo "<a title=\"$stredit\" href=\"".$questionurl->out(false, array('id'=>$question->id
))."\"><img
347 src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a> ";
348 } elseif (question_has_capability_on($question, 'view', $question->category
)){
349 echo "<a title=\"$strview\" href=\"".$questionurl->out(false, array('id'=>$question->id
))."\"><img
350 src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a> ";
353 if (question_has_capability_on($question, 'move', $question->category
) && question_has_capability_on($question, 'view', $question->category
)) {
354 echo "<a title=\"$strmove\" href=\"".$questionurl->out(false, array('id'=>$question->id
, 'movecontext'=>1))."\"><img
355 src=\"$CFG->pixpath/t/move.gif\" alt=\"$strmove\" /></a> ";
358 if (question_has_capability_on($question, 'edit', $question->category
)) {
360 if($question->hidden
) {
361 echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&unhide=$question->id&sesskey=$USER->sesskey\"><img
362 src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
364 echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&deleteselected=$question->id&q$question->id=1\"><img
365 src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
368 if ($caneditall ||
$canmoveall ||
$canuseall){
369 echo " <input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
373 echo "<td $nameclass>" . format_string($question->name
) . "</td>\n";
374 echo "<td $nameclass style='text-align: right'>\n";
375 print_question_icon($question);
378 if($showquestiontext){
379 echo '<tr><td colspan="3" ' . $textclass . '>';
380 $formatoptions = new stdClass
;
381 $formatoptions->noclean
= true;
382 $formatoptions->para
= false;
383 echo format_text($question->questiontext
, $question->questiontextformat
,
384 $formatoptions, $COURSE->id
);
390 $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true);
391 if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE
) {
392 if ($perpage == DEFAULT_QUESTIONS_PER_PAGE
) {
393 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
395 $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>DEFAULT_QUESTIONS_PER_PAGE
)).'">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE
).'</a>';
398 $paging = substr($paging, 0, strrpos($paging, '</div>'));
399 $paging .= "<br />$showall</div>";
401 $paging = "<div class='paging'>$showall</div>";
406 if ($caneditall ||
$canmoveall ||
$canuseall){
407 echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
408 ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>';
410 echo '<strong> '.get_string('withselected', 'quiz').':</strong><br />';
412 if (function_exists('module_specific_buttons')) {
413 echo module_specific_buttons($cm->id
);
415 // print delete and move selected question
417 echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" />\n";
419 if ($canmoveall && count($addcontexts)) {
420 echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
421 question_category_select_menu($addcontexts, false, 0, "$category->id,$category->contextid");
424 if (function_exists('module_specific_controls') && $canuseall) {
425 echo module_specific_controls($totalnumber, $recurse, $category, $cm->id
);
431 function question_sort_options($pageurl, $sortorder){
434 $html = "<div class=\"mdl-align\">";
435 $html .= '<form method="post" action="edit.php">';
436 $html .= '<fieldset class="invisiblefieldset" style="display: block;">';
437 $html .= '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
438 $html .= $pageurl->hidden_params_out(array('qsortorder'));
439 $sortoptions = array('alpha' => get_string("sortalpha", "quiz"),
440 'typealpha' => get_string("sorttypealpha", "quiz"),
441 'age' => get_string("sortage", "quiz"));
442 $html .= choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
443 $html .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
444 $html .= '</fieldset>';
445 $html .= "</form>\n";
450 function question_showbank_actions($pageurl, $cm){
452 /// Now, check for commands on this page and modify variables as necessary
453 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
454 $category = required_param('category', PARAM_SEQUENCE
);
455 list($tocategoryid, $contextid) = explode(',', $category);
456 if (! $tocategory = get_record('question_categories', 'id', $tocategoryid, 'contextid', $contextid)) {
457 error('Could not find category record');
459 $tocontext = get_context_instance_by_id($contextid);
460 require_capability('moodle/question:add', $tocontext);
461 $questionids = array();
462 foreach ($_POST as $key => $value) { // Parse input for question ids
463 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
465 $questionids[] = $key;
469 $questionidlist = join($questionids, ',');
470 $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";
471 if (!$questions = get_records_sql($sql)){
472 print_error('questiondoesnotexist', 'question', $pageurl->out());
474 $checkforfiles = false;
475 foreach ($questions as $question){
477 question_require_capability_on($question, 'move');
478 $fromcontext = get_context_instance_by_id($question->contextid
);
479 if (get_filesdir_from_context($fromcontext) != get_filesdir_from_context($tocontext)){
480 $checkforfiles = true;
483 $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid"));
484 if (!$checkforfiles){
485 foreach ($questionids as $questionid){
487 if (!set_field('question', 'category', $tocategory->id
, 'id', $questionid)) {
488 error('Could not update category field');
491 redirect($returnurl);
493 $movecontexturl = new moodle_url($CFG->wwwroot
.'/question/contextmoveq.php',
494 array('returnurl' => $returnurl,
495 'ids'=>$questionidlist,
496 'tocatid'=> $tocategoryid));
498 $movecontexturl->param('cmid', $cm->id
);
500 $movecontexturl->param('courseid', $COURSE->id
);
502 redirect($movecontexturl->out());
507 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
509 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
510 $deleteselected = required_param('deleteselected');
511 if ($_REQUEST['confirm'] == md5($deleteselected)) {
512 if ($questionlist = explode(',', $deleteselected)) {
513 // for each question either hide it if it is in use or delete it
514 foreach ($questionlist as $questionid) {
515 question_require_capability_on($questionid, 'edit');
516 if (record_exists('quiz_question_instances', 'question', $questionid) or
517 record_exists('question_states', 'originalquestion', $questionid)) {
518 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
519 question_require_capability_on($questionid, 'edit');
520 error('Was not able to hide question');
523 delete_question($questionid);
527 redirect($pageurl->out());
529 error("Confirmation string was incorrect");
537 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
538 $unhide = required_param('unhide', PARAM_INT
);
539 question_require_capability_on($unhide, 'edit');
540 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
541 error("Failed to unhide the question.");
543 redirect($pageurl->out());
547 * Shows the question bank editing interface.
549 * The function also processes a number of actions:
551 * Actions affecting the question pool:
552 * move Moves a question to a different category
553 * deleteselected Deletes the selected questions from the category
555 * category Chooses the category
556 * displayoptions Sets display options
558 * @author Martin Dougiamas and many others. This has recently been extensively
559 * rewritten by Gustav Delius and other members of the Serving Mathematics project
560 * {@link http://maths.york.ac.uk/serving_maths}
561 * @param moodle_url $pageurl object representing this pages url.
563 function question_showbank($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
566 if (isset($_REQUEST['deleteselected'])){ // teacher still has to confirm
567 // make a list of all the questions that are selected
568 $rawquestions = $_REQUEST;
569 $questionlist = ''; // comma separated list of ids of questions to be deleted
570 $questionnames = ''; // string with names of questions separated by <br /> with
571 // an asterix in front of those that are in use
572 $inuse = false; // set to true if at least one of the questions is in use
573 foreach ($rawquestions as $key => $value) { // Parse input for question ids
574 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
575 $key = $matches[1]; $questionlist .= $key.',';
576 question_require_capability_on($key, 'edit');
577 if (record_exists('quiz_question_instances', 'question', $key) or
578 record_exists('question_states', 'originalquestion', $key)) {
579 $questionnames .= '* ';
582 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
585 if (!$questionlist) { // no questions were selected
586 redirect($pageurl->out());
588 $questionlist = rtrim($questionlist, ',');
590 // Add an explanation about questions in use
592 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
594 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
595 $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
596 $pageurl->out_action());
600 print_footer($COURSE);
605 // starts with category selection form
606 print_box_start('generalbox questionbank');
607 print_heading(get_string('questionbank', 'question'), '', 2);
608 question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
610 // continues with list of questions
611 question_list($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, isset($cm) ?
$cm : null,
612 $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
613 $contexts->having_cap('moodle/question:add'));
618 * Common setup for all pages for editing questions.
619 * @param string $edittab code for this edit tab
620 * @param boolean $requirecmid require cmid? default false
621 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
622 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
624 function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true){
625 global $COURSE, $QUESTION_EDITTABCAPS;
627 //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
628 //of parameters that are passed from page to page.
629 $thispageurl = new moodle_url();
631 $cmid =required_param('cmid', PARAM_INT
);
633 $cmid = optional_param('cmid', 0, PARAM_INT
);
636 list($module, $cm) = get_module_from_cmid($cmid);
637 $courseid = $cm->course
;
638 $thispageurl->params(compact('cmid'));
639 require_login($courseid, false, $cm);
640 $thiscontext = get_context_instance(CONTEXT_MODULE
, $cmid);
644 if ($requirecourseid){
645 $courseid = required_param('courseid', PARAM_INT
);
647 $courseid = optional_param('courseid', 0, PARAM_INT
);
650 $thispageurl->params(compact('courseid'));
651 require_login($courseid, false);
652 $thiscontext = get_context_instance(CONTEXT_COURSE
, $courseid);
659 $contexts = new question_edit_contexts($thiscontext);
660 $contexts->require_one_edit_tab_cap($edittab);
668 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT
);
670 //pass 'cat' from page to page and when 'category' comes from a drop down menu
671 //then we also reset the qpage so we go to page 1 of
673 $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE
);// if empty will be set up later
674 if ($category = optional_param('category', 0, PARAM_SEQUENCE
)){
675 $pagevars['cat'] = $category;
676 $pagevars['qpage'] = 0;
678 if ($pagevars['cat']){
679 $thispageurl->param('cat', $pagevars['cat']);
681 if ($pagevars['qpage'] > -1) {
682 $thispageurl->param('qpage', $pagevars['qpage']);
684 $pagevars['qpage'] = 0;
687 $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT
);
688 if ($pagevars['qperpage'] > -1) {
689 $thispageurl->param('qperpage', $pagevars['qperpage']);
691 $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE
;
694 $sortoptions = array('alpha' => 'name, qtype ASC',
695 'typealpha' => 'qtype, name ASC',
698 if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA
)) {
699 $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
700 $pagevars['qsortorder'] = $sortorder;
701 $thispageurl->param('qsortorder', $sortorder);
703 $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
704 $pagevars['qsortorder'] = 'typealpha';
707 $defaultcategory = question_make_default_categories($contexts->all());
709 $contextlistarr = array();
710 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
711 $contextlistarr[] = "'$context->id'";
713 $contextlist = join($contextlistarr, ' ,');
714 if (!empty($pagevars['cat'])){
715 $catparts = explode(',', $pagevars['cat']);
716 if (!$catparts[0] ||
(FALSE !== array_search($catparts[1], $contextlistarr)) ||
!count_records_select("question_categories", "id = '".$catparts[0]."' AND contextid = $catparts[1]")) {
717 error(get_string('invalidcategory', 'quiz'));
720 $category = $defaultcategory;
721 $pagevars['cat'] = "$category->id,$category->contextid";
724 if(($recurse = optional_param('recurse', -1, PARAM_BOOL
)) != -1) {
725 $pagevars['recurse'] = $recurse;
726 $thispageurl->param('recurse', $recurse);
728 $pagevars['recurse'] = 1;
731 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL
)) != -1) {
732 $pagevars['showhidden'] = $showhidden;
733 $thispageurl->param('showhidden', $showhidden);
735 $pagevars['showhidden'] = 0;
738 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL
)) != -1) {
739 $pagevars['showquestiontext'] = $showquestiontext;
740 $thispageurl->param('showquestiontext', $showquestiontext);
742 $pagevars['showquestiontext'] = 0;
746 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT
);
747 if ($pagevars['cpage'] != 1){
748 $thispageurl->param('cpage', $pagevars['cpage']);
752 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
754 class question_edit_contexts
{
757 * @param current context
759 function question_edit_contexts($thiscontext){
760 $pcontextids = get_parent_contexts($thiscontext);
761 $contexts = array($thiscontext);
762 foreach ($pcontextids as $pcontextid){
763 $contexts[] = get_context_instance_by_id($pcontextid);
765 $this->allcontexts
= $contexts;
768 * @return array all parent contexts
771 return $this->allcontexts
;
774 * @return object lowest context which must be either the module or course context
777 return $this->allcontexts
[0];
780 * @param string $cap capability
781 * @return array parent contexts having capability, zero based index
783 function having_cap($cap){
784 $contextswithcap = array();
785 foreach ($this->allcontexts
as $context){
786 if (has_capability($cap, $context)){
787 $contextswithcap[] = $context;
790 return $contextswithcap;
793 * @param array $caps capabilities
794 * @return array parent contexts having at least one of $caps, zero based index
796 function having_one_cap($caps){
797 $contextswithacap = array();
798 foreach ($this->allcontexts
as $context){
799 foreach ($caps as $cap){
800 if (has_capability($cap, $context)){
801 $contextswithacap[] = $context;
802 break; //done with caps loop
806 return $contextswithacap;
809 * @param string $tabname edit tab name
810 * @return array parent contexts having at least one of $caps, zero based index
812 function having_one_edit_tab_cap($tabname){
813 global $QUESTION_EDITTABCAPS;
814 return $this->having_one_cap($QUESTION_EDITTABCAPS[$tabname]);
817 * Has at least one parent context got the cap $cap?
819 * @param string $cap capability
822 function have_cap($cap){
823 return (count($this->having_cap($cap)));
827 * Has at least one parent context got one of the caps $caps?
829 * @param string $cap capability
832 function have_one_cap($caps){
833 foreach ($caps as $cap){
834 if ($this->have_cap($cap)){
841 * Has at least one parent context got one of the caps for actions on $tabname
843 * @param string $tabname edit tab name
846 function have_one_edit_tab_cap($tabname){
847 global $QUESTION_EDITTABCAPS;
848 return $this->have_one_cap($QUESTION_EDITTABCAPS[$tabname]);
851 * Throw error if at least one parent context hasn't got the cap $cap
853 * @param string $cap capability
855 function require_cap($cap){
856 if (!$this->have_cap($cap)){
857 print_error('nopermissions', '', '', $cap);
861 * Throw error if at least one parent context hasn't got one of the caps $caps
863 * @param array $cap capabilities
865 function require_one_cap($caps){
866 if (!$this->have_one_cap($caps)){
867 $capsstring = join($caps, ', ');
868 print_error('nopermissions', '', '', $capsstring);
872 * Throw error if at least one parent context hasn't got one of the caps $caps
874 * @param string $tabname edit tab name
876 function require_one_edit_tab_cap($tabname){
877 if (!$this->have_one_edit_tab_cap($tabname)){
878 print_error('nopermissions', '', '', 'access question edit tab '.$tabname);
883 //capabilities for each page of edit tab.
884 //this determines which contexts' categories are available. At least one
885 //page is displayed if user has one of the capability on at least one context
886 $QUESTION_EDITTABCAPS = array(
887 'editq' => array('moodle/question:add',
888 'moodle/question:editmine',
889 'moodle/question:editall',
890 'moodle/question:viewmine',
891 'moodle/question:viewall',
892 'moodle/question:usemine',
893 'moodle/question:useall',
894 'moodle/question:movemine',
895 'moodle/question:moveall'),
896 'questions'=>array('moodle/question:add',
897 'moodle/question:editmine',
898 'moodle/question:editall',
899 'moodle/question:viewmine',
900 'moodle/question:viewall',
901 'moodle/question:movemine',
902 'moodle/question:moveall'),
903 'categories'=>array('moodle/question:managecategory'),
904 'import'=>array('moodle/question:add'),
905 'export'=>array('moodle/question:viewall', 'moodle/question:viewmine'));
910 * Make sure user is logged in as required in this context.
912 function require_login_in_context($contextorid = null){
913 if (!is_object($contextorid)){
914 $context = get_context_instance_by_id($contextorid);
916 $context = $contextorid;
918 if ($context && ($context->contextlevel
== CONTEXT_COURSE
)) {
919 require_login($context->instanceid
);
920 } else if ($context && ($context->contextlevel
== CONTEXT_MODULE
)) {
921 if ($cm = get_record('course_modules','id',$context->instanceid
)) {
922 if (!$course = get_record('course', 'id', $cm->course
)) {
923 error('Incorrect course.');
925 require_course_login($course, true, $cm);
928 error('Incorrect course module id.');
930 } else if ($context && ($context->contextlevel
== CONTEXT_SYSTEM
)) {
931 if (!empty($CFG->forcelogin
)) {