Merge branch 'MDL-61652-master' of git://github.com/mihailges/moodle
[moodle.git] / question / editlib.php
blobda255f515ddd73d1be67628246dcdb14bfb83e82
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Functions used to show question editing interface
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use core_question\bank\search\category_condition;
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->libdir . '/questionlib.php');
33 define('DEFAULT_QUESTIONS_PER_PAGE', 20);
34 define('MAXIMUM_QUESTIONS_PER_PAGE', 1000);
36 function get_module_from_cmid($cmid) {
37 global $CFG, $DB;
38 if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
39 FROM {course_modules} cm,
40 {modules} md
41 WHERE cm.id = ? AND
42 md.id = cm.module", array($cmid))){
43 print_error('invalidcoursemodule');
44 } elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
45 print_error('invalidcoursemodule');
47 $modrec->instance = $modrec->id;
48 $modrec->cmid = $cmrec->id;
49 $cmrec->name = $modrec->name;
51 return array($modrec, $cmrec);
53 /**
54 * Function to read all questions for category into big array
56 * @param int $category category number
57 * @param bool $noparent if true only questions with NO parent will be selected
58 * @param bool $recurse include subdirectories
59 * @param bool $export set true if this is called by questionbank export
61 function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
62 global $DB;
64 // Build sql bit for $noparent
65 $npsql = '';
66 if ($noparent) {
67 $npsql = " and parent='0' ";
70 // Get list of categories
71 if ($recurse) {
72 $categorylist = question_categorylist($category->id);
73 } else {
74 $categorylist = array($category->id);
77 // Get the list of questions for the category
78 list($usql, $params) = $DB->get_in_or_equal($categorylist);
79 $questions = $DB->get_records_select('question', "category {$usql} {$npsql}", $params, 'qtype, name');
81 // Iterate through questions, getting stuff we need
82 $qresults = array();
83 foreach($questions as $key => $question) {
84 $question->export_process = $export;
85 $qtype = question_bank::get_qtype($question->qtype, false);
86 if ($export && $qtype->name() == 'missingtype') {
87 // Unrecognised question type. Skip this question when exporting.
88 continue;
90 $qtype->get_question_options($question);
91 $qresults[] = $question;
94 return $qresults;
97 /**
98 * Checks whether this is the only child of a top category in a context.
100 * @param int $categoryid a category id.
101 * @return bool
103 function question_is_only_child_of_top_category_in_context($categoryid) {
104 global $DB;
105 return 1 == $DB->count_records_sql("
106 SELECT count(*)
107 FROM {question_categories} c
108 JOIN {question_categories} p ON c.parent = p.id
109 JOIN {question_categories} s ON s.parent = c.parent
110 WHERE c.id = ? AND p.parent = 0", array($categoryid));
114 * Checks whether the category is a "Top" category (with no parent).
116 * @param int $categoryid a category id.
117 * @return bool
119 function question_is_top_category($categoryid) {
120 global $DB;
121 return 0 == $DB->get_field('question_categories', 'parent', array('id' => $categoryid));
125 * Ensures that this user is allowed to delete this category.
127 * @param int $todelete a category id.
129 function question_can_delete_cat($todelete) {
130 global $DB;
131 if (question_is_top_category($todelete)) {
132 print_error('cannotdeletetopcat', 'question');
133 } else if (question_is_only_child_of_top_category_in_context($todelete)) {
134 print_error('cannotdeletecate', 'question');
135 } else {
136 $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
137 require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
143 * Base class for representing a column in a {@link question_bank_view}.
145 * @copyright 2009 Tim Hunt
146 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
147 * @deprecated since Moodle 2.7 MDL-40457
149 class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
152 * A column with a checkbox for each question with name q{questionid}.
154 * @copyright 2009 Tim Hunt
155 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
156 * @deprecated since Moodle 2.7 MDL-40457
158 class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
161 * A column type for the name of the question type.
163 * @copyright 2009 Tim Hunt
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 * @deprecated since Moodle 2.7 MDL-40457
167 class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
171 * A column type for the name of the question name.
173 * @copyright 2009 Tim Hunt
174 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
175 * @deprecated since Moodle 2.7 MDL-40457
177 class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
181 * A column type for the name of the question creator.
183 * @copyright 2009 Tim Hunt
184 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
185 * @deprecated since Moodle 2.7 MDL-40457
187 class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
191 * A column type for the name of the question last modifier.
193 * @copyright 2009 Tim Hunt
194 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
195 * @deprecated since Moodle 2.7 MDL-40457
197 class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
201 * A base class for actions that are an icon that lets you manipulate the question in some way.
203 * @copyright 2009 Tim Hunt
204 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
205 * @deprecated since Moodle 2.7 MDL-40457
207 class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
211 * Base class for question bank columns that just contain an action icon.
213 * @copyright 2009 Tim Hunt
214 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
215 * @deprecated since Moodle 2.7 MDL-40457
217 class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
220 * Question bank column for the duplicate action icon.
222 * @copyright 2013 The Open University
223 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
224 * @deprecated since Moodle 2.7 MDL-40457
226 class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
229 * Question bank columns for the preview action icon.
231 * @copyright 2009 Tim Hunt
232 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
233 * @deprecated since Moodle 2.7 MDL-40457
235 class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
239 * action to delete (or hide) a question, or restore a previously hidden question.
241 * @copyright 2009 Tim Hunt
242 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
243 * @deprecated since Moodle 2.7 MDL-40457
245 class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
248 * Base class for 'columns' that are actually displayed as a row following the main question row.
250 * @copyright 2009 Tim Hunt
251 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
252 * @deprecated since Moodle 2.7 MDL-40457
254 class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
257 * A column type for the name of the question name.
259 * @copyright 2009 Tim Hunt
260 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
261 * @deprecated since Moodle 2.7 MDL-40457
263 class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
266 * @copyright 2009 Tim Hunt
267 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
268 * @deprecated since Moodle 2.7 MDL-40457
270 class_alias('core_question\bank\view', 'question_bank_view', true);
273 * Common setup for all pages for editing questions.
274 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
275 * @param string $edittab code for this edit tab
276 * @param bool $requirecmid require cmid? default false
277 * @param bool $unused no longer used, do no pass
278 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
280 function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
281 global $PAGE;
283 if ($unused !== null) {
284 debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER);
287 $params = [];
289 if ($requirecmid) {
290 $params['cmid'] = required_param('cmid', PARAM_INT);
291 } else {
292 $params['cmid'] = optional_param('cmid', null, PARAM_INT);
295 if (!$params['cmid']) {
296 $params['courseid'] = required_param('courseid', PARAM_INT);
299 $params['qpage'] = optional_param('qpage', null, PARAM_INT);
301 // Pass 'cat' from page to page and when 'category' comes from a drop down menu
302 // then we also reset the qpage so we go to page 1 of
303 // a new cat.
304 $params['cat'] = optional_param('cat', null, PARAM_SEQUENCE); // If empty will be set up later.
305 $params['category'] = optional_param('category', null, PARAM_SEQUENCE);
306 $params['qperpage'] = optional_param('qperpage', null, PARAM_INT);
308 // Question table sorting options.
309 for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
310 $param = 'qbs' . $i;
311 if ($sort = optional_param($param, '', PARAM_TEXT)) {
312 $params[$param] = $sort;
313 } else {
314 break;
318 // Display options.
319 $params['recurse'] = optional_param('recurse', null, PARAM_BOOL);
320 $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL);
321 $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL);
322 // Category list page.
323 $params['cpage'] = optional_param('cpage', null, PARAM_INT);
324 $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT);
326 $PAGE->set_pagelayout('admin');
328 return question_build_edit_resources($edittab, $baseurl, $params);
332 * Common function for building the generic resources required by the
333 * editing questions pages.
335 * Either a cmid or a course id must be provided as keys in $params or
336 * an exception will be thrown. All other params are optional and will have
337 * sane default applied if not provided.
339 * The acceptable keys for $params are:
341 * 'cmid' => PARAM_INT,
342 * 'courseid' => PARAM_INT,
343 * 'qpage' => PARAM_INT,
344 * 'cat' => PARAM_SEQUENCE,
345 * 'category' => PARAM_SEQUENCE,
346 * 'qperpage' => PARAM_INT,
347 * 'recurse' => PARAM_INT,
348 * 'showhidden' => PARAM_INT,
349 * 'qbshowtext' => PARAM_INT,
350 * 'cpage' => PARAM_INT,
351 * 'recurse' => PARAM_BOOL,
352 * 'showhidden' => PARAM_BOOL,
353 * 'qbshowtext' => PARAM_BOOL,
354 * 'qtagids' => [PARAM_INT], (array of integers)
355 * 'qbs1' => PARAM_TEXT,
356 * 'qbs2' => PARAM_TEXT,
357 * 'qbs3' => PARAM_TEXT,
358 * ... and more qbs keys up to question_bank_view::MAX_SORTS ...
359 * ];
361 * @param string $edittab Code for this edit tab
362 * @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'.
363 * @param array $params The provided parameters to construct the resources with.
364 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
366 function question_build_edit_resources($edittab, $baseurl, $params) {
367 global $DB, $PAGE, $CFG;
369 $thispageurl = new moodle_url($baseurl);
370 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
372 $cleanparams = [
373 'qsorts' => [],
374 'qtagids' => []
376 $paramtypes = [
377 'cmid' => PARAM_INT,
378 'courseid' => PARAM_INT,
379 'qpage' => PARAM_INT,
380 'cat' => PARAM_SEQUENCE,
381 'category' => PARAM_SEQUENCE,
382 'qperpage' => PARAM_INT,
383 'recurse' => PARAM_INT,
384 'showhidden' => PARAM_INT,
385 'qbshowtext' => PARAM_INT,
386 'cpage' => PARAM_INT,
387 'recurse' => PARAM_BOOL,
388 'showhidden' => PARAM_BOOL,
389 'qbshowtext' => PARAM_BOOL
392 foreach ($paramtypes as $name => $type) {
393 if (isset($params[$name])) {
394 $cleanparams[$name] = clean_param($params[$name], $type);
395 } else {
396 $cleanparams[$name] = null;
400 if (!empty($params['qtagids'])) {
401 $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT);
404 $cmid = $cleanparams['cmid'];
405 $courseid = $cleanparams['courseid'];
406 $qpage = $cleanparams['qpage'] ?: -1;
407 $cat = $cleanparams['cat'] ?: 0;
408 $category = $cleanparams['category'] ?: 0;
409 $qperpage = $cleanparams['qperpage'];
410 $recurse = $cleanparams['recurse'];
411 $showhidden = $cleanparams['showhidden'];
412 $qbshowtext = $cleanparams['qbshowtext'];
413 $cpage = $cleanparams['cpage'] ?: 1;
414 $recurse = $cleanparams['recurse'];
415 $showhidden = $cleanparams['showhidden'];
416 $qbshowtext = $cleanparams['qbshowtext'];
417 $qsorts = $cleanparams['qsorts'];
418 $qtagids = $cleanparams['qtagids'];
420 if (is_null($cmid) && is_null($courseid)) {
421 throw new \moodle_exception('Must provide a cmid or courseid');
424 if ($cmid) {
425 list($module, $cm) = get_module_from_cmid($cmid);
426 $courseid = $cm->course;
427 $thispageurl->params(compact('cmid'));
428 require_login($courseid, false, $cm);
429 $thiscontext = context_module::instance($cmid);
430 } else {
431 $module = null;
432 $cm = null;
433 $thispageurl->params(compact('courseid'));
434 require_login($courseid, false);
435 $thiscontext = context_course::instance($courseid);
438 if ($thiscontext){
439 $contexts = new question_edit_contexts($thiscontext);
440 $contexts->require_one_edit_tab_cap($edittab);
441 } else {
442 $contexts = null;
445 $pagevars['qpage'] = $qpage;
447 // Pass 'cat' from page to page and when 'category' comes from a drop down menu
448 // then we also reset the qpage so we go to page 1 of
449 // a new cat.
450 if ($category && $category != $cat) { // Is this a move to a new category?
451 $pagevars['cat'] = $category;
452 $pagevars['qpage'] = 0;
453 } else {
454 $pagevars['cat'] = $cat; // If empty will be set up later.
457 if ($pagevars['cat']){
458 $thispageurl->param('cat', $pagevars['cat']);
461 if (strpos($baseurl, '/question/') === 0) {
462 navigation_node::override_active_url($thispageurl);
465 // This need to occur after the override_active_url call above because
466 // these values change on the page request causing the URLs to mismatch
467 // when trying to work out the active node.
468 for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
469 $param = 'qbs' . $i;
470 if (isset($params[$param])) {
471 $value = clean_param($params[$param], PARAM_TEXT);
472 } else {
473 break;
475 $thispageurl->param($param, $value);
478 if ($pagevars['qpage'] > -1) {
479 $thispageurl->param('qpage', $pagevars['qpage']);
480 } else {
481 $pagevars['qpage'] = 0;
484 $pagevars['qperpage'] = question_set_or_get_user_preference(
485 'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
487 $defaultcategory = question_make_default_categories($contexts->all());
489 $contextlistarr = [];
490 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
491 $contextlistarr[] = "'{$context->id}'";
493 $contextlist = join($contextlistarr, ' ,');
494 if (!empty($pagevars['cat'])){
495 $catparts = explode(',', $pagevars['cat']);
496 if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
497 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
498 print_error('invalidcategory', 'question');
500 } else {
501 $category = $defaultcategory;
502 $pagevars['cat'] = "{$category->id},{$category->contextid}";
505 // Display options.
506 $pagevars['recurse'] = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
507 $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
508 $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
510 // Category list page.
511 $pagevars['cpage'] = $cpage;
512 if ($pagevars['cpage'] != 1){
513 $thispageurl->param('cpage', $pagevars['cpage']);
516 $pagevars['qtagids'] = $qtagids;
517 foreach ($pagevars['qtagids'] as $index => $qtagid) {
518 $thispageurl->param("qtagids[{$index}]", $qtagid);
521 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
525 * Get the category id from $pagevars.
526 * @param array $pagevars from {@link question_edit_setup()}.
527 * @return int the category id.
529 function question_get_category_id_from_pagevars(array $pagevars) {
530 list($questioncategoryid) = explode(',', $pagevars['cat']);
531 return $questioncategoryid;
535 * Get a particular question preference that is also stored as a user preference.
536 * If the the value is given in the GET/POST request, then that value is used,
537 * and the user preference is updated to that value. Otherwise, the last set
538 * value of the user preference is used, or if it has never been set the default
539 * passed to this function.
541 * @param string $param the param name. The URL parameter set, and the GET/POST
542 * parameter read. The user_preference name is 'question_bank_' . $param.
543 * @param mixed $default The default value to use, if not otherwise set.
544 * @param int $type one of the PARAM_... constants.
545 * @param moodle_url $thispageurl if the value has been explicitly set, we add
546 * it to this URL.
547 * @return mixed the parameter value to use.
549 function question_get_display_preference($param, $default, $type, $thispageurl) {
550 $submittedvalue = optional_param($param, null, $type);
551 return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
555 * Get a user preference by name or set the user preference to a given value.
557 * If $value is null then the function will only attempt to retrieve the
558 * user preference requested by $name. If no user preference is found then the
559 * $default value will be returned. In this case the user preferences are not
560 * modified and nor are the params on $thispageurl.
562 * If $value is anything other than null then the function will set the user
563 * preference $name to the provided $value and will also set it as a param
564 * on $thispageurl.
566 * @param string $name The user_preference name is 'question_bank_' . $name.
567 * @param mixed $value The preference value.
568 * @param mixed $default The default value to use, if not otherwise set.
569 * @param moodle_url $thispageurl if the value has been explicitly set, we add
570 * it to this URL.
571 * @return mixed the parameter value to use.
573 function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
574 if (is_null($value)) {
575 return get_user_preferences('question_bank_' . $name, $default);
578 set_user_preference('question_bank_' . $name, $value);
579 $thispageurl->param($name, $value);
580 return $value;
584 * Make sure user is logged in as required in this context.
586 function require_login_in_context($contextorid = null){
587 global $DB, $CFG;
588 if (!is_object($contextorid)){
589 $context = context::instance_by_id($contextorid, IGNORE_MISSING);
590 } else {
591 $context = $contextorid;
593 if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
594 require_login($context->instanceid);
595 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
596 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
597 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
598 print_error('invalidcourseid');
600 require_course_login($course, true, $cm);
602 } else {
603 print_error('invalidcoursemodule');
605 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
606 if (!empty($CFG->forcelogin)) {
607 require_login();
610 } else {
611 require_login();
616 * Print a form to let the user choose which question type to add.
617 * When the form is submitted, it goes to the question.php script.
618 * @param $hiddenparams hidden parameters to add to the form, in addition to
619 * the qtype radio buttons.
620 * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
621 * those qtypes will be shown. Example value array('description', 'multichoice').
623 function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
624 global $CFG, $PAGE, $OUTPUT;
626 $chooser = core_question\output\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
627 $renderer = $PAGE->get_renderer('question', 'bank');
629 return $renderer->render($chooser);
633 * Print a button for creating a new question. This will open question/addquestion.php,
634 * which in turn goes to question/question.php before getting back to $params['returnurl']
635 * (by default the question bank screen).
637 * @param int $categoryid The id of the category that the new question should be added to.
638 * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
639 * $params['courseid'], and you should probably set $params['returnurl']
640 * @param string $caption the text to display on the button.
641 * @param string $tooltip a tooltip to add to the button (optional).
642 * @param bool $disabled if true, the button will be disabled.
644 function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
645 global $CFG, $PAGE, $OUTPUT;
646 static $choiceformprinted = false;
647 $params['category'] = $categoryid;
648 $url = new moodle_url('/question/addquestion.php', $params);
649 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
651 if (!$choiceformprinted) {
652 echo '<div id="qtypechoicecontainer">';
653 echo print_choose_qtype_to_add_form(array());
654 echo "</div>\n";
655 $choiceformprinted = true;