MDL-61135 core_tag: add function get_tags_by_area_in_contexts
[moodle.git] / question / editlib.php
CommitLineData
aeb15530 1<?php
d1b7e03d
TH
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.
13//
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/>.
16
516cf3eb 17/**
b72ff476 18 * Functions used to show question editing interface
4323d029 19 *
f7970e3c 20 * @package moodlecore
d1b7e03d 21 * @subpackage questionbank
f7970e3c
TH
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
d1b7e03d
TH
24 */
25
516cf3eb 26
d62382d1
TH
27use core_question\bank\search\category_condition;
28
a17b297d 29defined('MOODLE_INTERNAL') || die();
516cf3eb 30
a17b297d 31require_once($CFG->libdir . '/questionlib.php');
d1b7e03d 32
f92cf442 33define('DEFAULT_QUESTIONS_PER_PAGE', 20);
a8524f33 34define('MAXIMUM_QUESTIONS_PER_PAGE', 1000);
f92cf442 35
f4b879dd 36function get_module_from_cmid($cmid) {
f34488b2 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))){
1eda5201 43 print_error('invalidcoursemodule');
f34488b2 44 } elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
1eda5201 45 print_error('invalidcoursemodule');
86909ce0 46 }
47 $modrec->instance = $modrec->id;
d0e88092 48 $modrec->cmid = $cmrec->id;
ea16c547 49 $cmrec->name = $modrec->name;
271e6dec 50
86909ce0 51 return array($modrec, $cmrec);
52}
4fbfd971 53/**
54* Function to read all questions for category into big array
55*
56* @param int $category category number
36e2232e 57* @param bool $noparent if true only questions with NO parent will be selected
58* @param bool $recurse include subdirectories
5fd8f999 59* @param bool $export set true if this is called by questionbank export
4fbfd971 60*/
5fd8f999 61function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
f9b0500f 62 global $DB;
4fbfd971 63
87b564df 64 // Build sql bit for $noparent
4fbfd971 65 $npsql = '';
66 if ($noparent) {
67 $npsql = " and parent='0' ";
68 }
69
2daffca5 70 // Get list of categories
36e2232e 71 if ($recurse) {
fe857668 72 $categorylist = question_categorylist($category->id);
2daffca5
TH
73 } else {
74 $categorylist = array($category->id);
36e2232e 75 }
76
87b564df 77 // Get the list of questions for the category
2daffca5 78 list($usql, $params) = $DB->get_in_or_equal($categorylist);
f4fe3968 79 $questions = $DB->get_records_select('question', "category {$usql} {$npsql}", $params, 'qtype, name');
4fbfd971 80
87b564df
TH
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;
4fbfd971 89 }
87b564df
TH
90 $qtype->get_question_options($question);
91 $qresults[] = $question;
4fbfd971 92 }
93
94 return $qresults;
95}
96
df4e2244 97/**
f7970e3c
TH
98 * @param int $categoryid a category id.
99 * @return bool whether this is the only top-level category in a context.
df4e2244 100 */
101function question_is_only_toplevel_category_in_context($categoryid) {
102 global $DB;
103 return 1 == $DB->count_records_sql("
104 SELECT count(*)
105 FROM {question_categories} c1,
106 {question_categories} c2
107 WHERE c2.id = ?
108 AND c1.contextid = c2.contextid
109 AND c1.parent = 0 AND c2.parent = 0", array($categoryid));
110}
4fbfd971 111
df4e2244 112/**
113 * Check whether this user is allowed to delete this category.
114 *
f7970e3c 115 * @param int $todelete a category id.
df4e2244 116 */
117function question_can_delete_cat($todelete) {
118 global $DB;
119 if (question_is_only_toplevel_category_in_context($todelete)) {
ac93d63d 120 print_error('cannotdeletecate', 'question');
271e6dec 121 } else {
df4e2244 122 $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
d197ea43 123 require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
271e6dec 124 }
125}
f4b879dd 126
f7970e3c
TH
127
128/**
129 * Base class for representing a column in a {@link question_bank_view}.
130 *
131 * @copyright 2009 Tim Hunt
132 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 133 * @deprecated since Moodle 2.7 MDL-40457
f7970e3c 134 */
17f229fa 135class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
f7970e3c 136
46795afc 137/**
138 * A column with a checkbox for each question with name q{questionid}.
f7970e3c
TH
139 *
140 * @copyright 2009 Tim Hunt
141 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 142 * @deprecated since Moodle 2.7 MDL-40457
46795afc 143 */
17f229fa 144class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
f7970e3c 145
46795afc 146/**
147 * A column type for the name of the question type.
f7970e3c
TH
148 *
149 * @copyright 2009 Tim Hunt
150 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 151 * @deprecated since Moodle 2.7 MDL-40457
46795afc 152 */
17f229fa 153class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
46795afc 154
f7970e3c 155
46795afc 156/**
157 * A column type for the name of the question name.
f7970e3c
TH
158 *
159 * @copyright 2009 Tim Hunt
160 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 161 * @deprecated since Moodle 2.7 MDL-40457
46795afc 162 */
17f229fa 163class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
46795afc 164
f7970e3c 165
46795afc 166/**
167 * A column type for the name of the question creator.
f7970e3c
TH
168 *
169 * @copyright 2009 Tim Hunt
170 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 171 * @deprecated since Moodle 2.7 MDL-40457
46795afc 172 */
17f229fa 173class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
46795afc 174
f7970e3c 175
46795afc 176/**
177 * A column type for the name of the question last modifier.
f7970e3c
TH
178 *
179 * @copyright 2009 Tim Hunt
180 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 181 * @deprecated since Moodle 2.7 MDL-40457
46795afc 182 */
17f229fa 183class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
46795afc 184
f7970e3c 185
46795afc 186/**
187 * A base class for actions that are an icon that lets you manipulate the question in some way.
f7970e3c
TH
188 *
189 * @copyright 2009 Tim Hunt
190 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 191 * @deprecated since Moodle 2.7 MDL-40457
46795afc 192 */
17f229fa 193class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
46795afc 194
f7970e3c
TH
195
196/**
197 * Base class for question bank columns that just contain an action icon.
198 *
199 * @copyright 2009 Tim Hunt
200 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 201 * @deprecated since Moodle 2.7 MDL-40457
f7970e3c 202 */
17f229fa 203class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
46795afc 204
e0c41591
K
205/**
206 * Question bank column for the duplicate action icon.
207 *
208 * @copyright 2013 The Open University
209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 210 * @deprecated since Moodle 2.7 MDL-40457
e0c41591 211 */
17f229fa 212class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
f7970e3c
TH
213
214/**
215 * Question bank columns for the preview action icon.
216 *
217 * @copyright 2009 Tim Hunt
218 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 219 * @deprecated since Moodle 2.7 MDL-40457
f7970e3c 220 */
17f229fa 221class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
46795afc 222
f7970e3c 223
46795afc 224/**
225 * action to delete (or hide) a question, or restore a previously hidden question.
f7970e3c
TH
226 *
227 * @copyright 2009 Tim Hunt
228 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 229 * @deprecated since Moodle 2.7 MDL-40457
46795afc 230 */
17f229fa 231class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
46795afc 232
e4d1d5e0 233/**
234 * Base class for 'columns' that are actually displayed as a row following the main question row.
f7970e3c
TH
235 *
236 * @copyright 2009 Tim Hunt
237 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 238 * @deprecated since Moodle 2.7 MDL-40457
e4d1d5e0 239 */
17f229fa 240class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
e4d1d5e0 241
242/**
243 * A column type for the name of the question name.
f7970e3c
TH
244 *
245 * @copyright 2009 Tim Hunt
246 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 247 * @deprecated since Moodle 2.7 MDL-40457
e4d1d5e0 248 */
17f229fa 249class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
e4d1d5e0 250
4fbfd971 251/**
f7970e3c
TH
252 * @copyright 2009 Tim Hunt
253 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
17f229fa 254 * @deprecated since Moodle 2.7 MDL-40457
4fbfd971 255 */
17f229fa 256class_alias('core_question\bank\view', 'question_bank_view', true);
f4b879dd 257
b72ff476 258/**
259 * Common setup for all pages for editing questions.
453b28d8 260 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
271e6dec 261 * @param string $edittab code for this edit tab
f7970e3c 262 * @param bool $requirecmid require cmid? default false
5aeba199 263 * @param bool $unused no longer used, do no pass
271e6dec 264 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
b72ff476 265 */
5aeba199
DP
266function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
267 global $DB, $PAGE, $CFG;
268
269 if ($unused !== null) {
270 debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER);
271 }
271e6dec 272
453b28d8 273 $thispageurl = new moodle_url($baseurl);
47ce714b 274 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
b4a2e413 275
b72ff476 276 if ($requirecmid){
277 $cmid =required_param('cmid', PARAM_INT);
278 } else {
279 $cmid = optional_param('cmid', 0, PARAM_INT);
280 }
281 if ($cmid){
271e6dec 282 list($module, $cm) = get_module_from_cmid($cmid);
b72ff476 283 $courseid = $cm->course;
284 $thispageurl->params(compact('cmid'));
271e6dec 285 require_login($courseid, false, $cm);
21c08c63 286 $thiscontext = context_module::instance($cmid);
b72ff476 287 } else {
288 $module = null;
289 $cm = null;
5aeba199
DP
290 $courseid = required_param('courseid', PARAM_INT);
291 $thispageurl->params(compact('courseid'));
292 require_login($courseid, false);
293 $thiscontext = context_course::instance($courseid);
b72ff476 294 }
271e6dec 295
296 if ($thiscontext){
297 $contexts = new question_edit_contexts($thiscontext);
298 $contexts->require_one_edit_tab_cap($edittab);
299
300 } else {
301 $contexts = null;
302 }
303
4bf1be35
TH
304 $PAGE->set_pagelayout('admin');
305
36e298bc 306 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
271e6dec 307
7bc26c8f 308 //pass 'cat' from page to page and when 'category' comes from a drop down menu
271e6dec 309 //then we also reset the qpage so we go to page 1 of
7bc26c8f 310 //a new cat.
f19ed874
TH
311 $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later
312 if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
313 if ($pagevars['cat'] != $category) { // is this a move to a new category?
a18fbcfb 314 $pagevars['cat'] = $category;
315 $pagevars['qpage'] = 0;
316 }
7bc26c8f 317 }
318 if ($pagevars['cat']){
319 $thispageurl->param('cat', $pagevars['cat']);
320 }
f19ed874
TH
321 if (strpos($baseurl, '/question/') === 0) {
322 navigation_node::override_active_url($thispageurl);
323 }
324
b72ff476 325 if ($pagevars['qpage'] > -1) {
326 $thispageurl->param('qpage', $pagevars['qpage']);
327 } else {
328 $pagevars['qpage'] = 0;
329 }
330
be77e9b6
TH
331 $pagevars['qperpage'] = question_get_display_preference(
332 'qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl);
271e6dec 333
b72b4137
TH
334 for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
335 $param = 'qbs' . $i;
17f229fa 336 if (!$sort = optional_param($param, '', PARAM_TEXT)) {
b72b4137
TH
337 break;
338 }
339 $thispageurl->param($param, $sort);
271e6dec 340 }
36e298bc 341
271e6dec 342 $defaultcategory = question_make_default_categories($contexts->all());
343
344 $contextlistarr = array();
345 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
f4fe3968 346 $contextlistarr[] = "'{$context->id}'";
271e6dec 347 }
348 $contextlist = join($contextlistarr, ' ,');
349 if (!empty($pagevars['cat'])){
350 $catparts = explode(',', $pagevars['cat']);
e0736817 351 if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
f34488b2 352 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
5e8a85aa 353 print_error('invalidcategory', 'question');
271e6dec 354 }
355 } else {
356 $category = $defaultcategory;
f4fe3968 357 $pagevars['cat'] = "{$category->id},{$category->contextid}";
36e298bc 358 }
359
be77e9b6
TH
360 // Display options.
361 $pagevars['recurse'] = question_get_display_preference('recurse', 1, PARAM_BOOL, $thispageurl);
362 $pagevars['showhidden'] = question_get_display_preference('showhidden', 0, PARAM_BOOL, $thispageurl);
363 $pagevars['qbshowtext'] = question_get_display_preference('qbshowtext', 0, PARAM_BOOL, $thispageurl);
271e6dec 364
be77e9b6 365 // Category list page.
986effb6 366 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
367 if ($pagevars['cpage'] != 1){
368 $thispageurl->param('cpage', $pagevars['cpage']);
369 }
36e298bc 370
271e6dec 371 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
372}
271e6dec 373
e1a2d0d9
CC
374/**
375 * Get the category id from $pagevars.
376 * @param array $pagevars from {@link question_edit_setup()}.
377 * @return int the category id.
378 */
379function question_get_category_id_from_pagevars(array $pagevars) {
380 list($questioncategoryid) = explode(',', $pagevars['cat']);
381 return $questioncategoryid;
382}
383
be77e9b6
TH
384/**
385 * Get a particular question preference that is also stored as a user preference.
386 * If the the value is given in the GET/POST request, then that value is used,
387 * and the user preference is updated to that value. Otherwise, the last set
388 * value of the user preference is used, or if it has never been set the default
389 * passed to this function.
390 *
391 * @param string $param the param name. The URL parameter set, and the GET/POST
392 * parameter read. The user_preference name is 'question_bank_' . $param.
393 * @param mixed $default The default value to use, if not otherwise set.
394 * @param int $type one of the PARAM_... constants.
395 * @param moodle_url $thispageurl if the value has been explicitly set, we add
396 * it to this URL.
397 * @return mixed the parameter value to use.
398 */
399function question_get_display_preference($param, $default, $type, $thispageurl) {
400 $submittedvalue = optional_param($param, null, $type);
401 if (is_null($submittedvalue)) {
402 return get_user_preferences('question_bank_' . $param, $default);
403 }
404
405 set_user_preference('question_bank_' . $param, $submittedvalue);
406 $thispageurl->param($param, $submittedvalue);
407 return $submittedvalue;
408}
409
271e6dec 410/**
411 * Make sure user is logged in as required in this context.
412 */
413function require_login_in_context($contextorid = null){
042a8403 414 global $DB, $CFG;
271e6dec 415 if (!is_object($contextorid)){
5fbe2118 416 $context = context::instance_by_id($contextorid, IGNORE_MISSING);
271e6dec 417 } else {
418 $context = $contextorid;
419 }
420 if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
421 require_login($context->instanceid);
422 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
f34488b2 423 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
424 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
0be2c858 425 print_error('invalidcourseid');
271e6dec 426 }
427 require_course_login($course, true, $cm);
428
429 } else {
0be2c858 430 print_error('invalidcoursemodule');
271e6dec 431 }
432 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
433 if (!empty($CFG->forcelogin)) {
434 require_login();
435 }
436
437 } else {
438 require_login();
439 }
b72ff476 440}
cd120b23 441
442/**
443 * Print a form to let the user choose which question type to add.
444 * When the form is submitted, it goes to the question.php script.
445 * @param $hiddenparams hidden parameters to add to the form, in addition to
75733895
TH
446 * the qtype radio buttons.
447 * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
448 * those qtypes will be shown. Example value array('description', 'multichoice').
cd120b23 449 */
ac32c27e 450function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
f9b0500f 451 global $CFG, $PAGE, $OUTPUT;
7b66b651 452
891750bc 453 $chooser = core_question\output\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
ac32c27e 454 $renderer = $PAGE->get_renderer('question', 'bank');
891750bc
FM
455
456 return $renderer->render($chooser);
cd120b23 457}
458
459/**
460 * Print a button for creating a new question. This will open question/addquestion.php,
461 * which in turn goes to question/question.php before getting back to $params['returnurl']
462 * (by default the question bank screen).
463 *
f7970e3c 464 * @param int $categoryid The id of the category that the new question should be added to.
cd120b23 465 * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
466 * $params['courseid'], and you should probably set $params['returnurl']
467 * @param string $caption the text to display on the button.
468 * @param string $tooltip a tooltip to add to the button (optional).
f7970e3c 469 * @param bool $disabled if true, the button will be disabled.
cd120b23 470 */
471function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
fef8f84e 472 global $CFG, $PAGE, $OUTPUT;
cd120b23 473 static $choiceformprinted = false;
474 $params['category'] = $categoryid;
a6855934 475 $url = new moodle_url('/question/addquestion.php', $params);
5c2ed7e2 476 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
a941387a 477
cd120b23 478 if (!$choiceformprinted) {
479 echo '<div id="qtypechoicecontainer">';
e1a2d0d9 480 echo print_choose_qtype_to_add_form(array());
cd120b23 481 echo "</div>\n";
482 $choiceformprinted = true;
483 }
484}
485
aeb15530 486