MDL-48004 question: prevent notices on import screen
[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
TH
262 * @param bool $requirecmid require cmid? default false
263 * @param bool $requirecourseid require courseid, if cmid is not given? default true
271e6dec 264 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
b72ff476 265 */
453b28d8 266function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true) {
56ed242b 267 global $DB, $PAGE;
271e6dec 268
453b28d8 269 $thispageurl = new moodle_url($baseurl);
47ce714b 270 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
b4a2e413 271
b72ff476 272 if ($requirecmid){
273 $cmid =required_param('cmid', PARAM_INT);
274 } else {
275 $cmid = optional_param('cmid', 0, PARAM_INT);
276 }
277 if ($cmid){
271e6dec 278 list($module, $cm) = get_module_from_cmid($cmid);
b72ff476 279 $courseid = $cm->course;
280 $thispageurl->params(compact('cmid'));
271e6dec 281 require_login($courseid, false, $cm);
21c08c63 282 $thiscontext = context_module::instance($cmid);
b72ff476 283 } else {
284 $module = null;
285 $cm = null;
286 if ($requirecourseid){
287 $courseid = required_param('courseid', PARAM_INT);
288 } else {
289 $courseid = optional_param('courseid', 0, PARAM_INT);
290 }
291 if ($courseid){
292 $thispageurl->params(compact('courseid'));
271e6dec 293 require_login($courseid, false);
21c08c63 294 $thiscontext = context_course::instance($courseid);
271e6dec 295 } else {
296 $thiscontext = null;
b72ff476 297 }
298 }
271e6dec 299
300 if ($thiscontext){
301 $contexts = new question_edit_contexts($thiscontext);
302 $contexts->require_one_edit_tab_cap($edittab);
303
304 } else {
305 $contexts = null;
306 }
307
4bf1be35
TH
308 $PAGE->set_pagelayout('admin');
309
36e298bc 310 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
271e6dec 311
7bc26c8f 312 //pass 'cat' from page to page and when 'category' comes from a drop down menu
271e6dec 313 //then we also reset the qpage so we go to page 1 of
7bc26c8f 314 //a new cat.
f19ed874
TH
315 $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later
316 if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
317 if ($pagevars['cat'] != $category) { // is this a move to a new category?
a18fbcfb 318 $pagevars['cat'] = $category;
319 $pagevars['qpage'] = 0;
320 }
7bc26c8f 321 }
322 if ($pagevars['cat']){
323 $thispageurl->param('cat', $pagevars['cat']);
324 }
f19ed874
TH
325 if (strpos($baseurl, '/question/') === 0) {
326 navigation_node::override_active_url($thispageurl);
327 }
328
b72ff476 329 if ($pagevars['qpage'] > -1) {
330 $thispageurl->param('qpage', $pagevars['qpage']);
331 } else {
332 $pagevars['qpage'] = 0;
333 }
334
be77e9b6
TH
335 $pagevars['qperpage'] = question_get_display_preference(
336 'qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl);
271e6dec 337
b72b4137
TH
338 for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
339 $param = 'qbs' . $i;
17f229fa 340 if (!$sort = optional_param($param, '', PARAM_TEXT)) {
b72b4137
TH
341 break;
342 }
343 $thispageurl->param($param, $sort);
271e6dec 344 }
36e298bc 345
271e6dec 346 $defaultcategory = question_make_default_categories($contexts->all());
347
348 $contextlistarr = array();
349 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
f4fe3968 350 $contextlistarr[] = "'{$context->id}'";
271e6dec 351 }
352 $contextlist = join($contextlistarr, ' ,');
353 if (!empty($pagevars['cat'])){
354 $catparts = explode(',', $pagevars['cat']);
e0736817 355 if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
f34488b2 356 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
5e8a85aa 357 print_error('invalidcategory', 'question');
271e6dec 358 }
359 } else {
360 $category = $defaultcategory;
f4fe3968 361 $pagevars['cat'] = "{$category->id},{$category->contextid}";
36e298bc 362 }
363
be77e9b6
TH
364 // Display options.
365 $pagevars['recurse'] = question_get_display_preference('recurse', 1, PARAM_BOOL, $thispageurl);
366 $pagevars['showhidden'] = question_get_display_preference('showhidden', 0, PARAM_BOOL, $thispageurl);
367 $pagevars['qbshowtext'] = question_get_display_preference('qbshowtext', 0, PARAM_BOOL, $thispageurl);
271e6dec 368
be77e9b6 369 // Category list page.
986effb6 370 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
371 if ($pagevars['cpage'] != 1){
372 $thispageurl->param('cpage', $pagevars['cpage']);
373 }
36e298bc 374
271e6dec 375 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
376}
271e6dec 377
e1a2d0d9
CC
378/**
379 * Get the category id from $pagevars.
380 * @param array $pagevars from {@link question_edit_setup()}.
381 * @return int the category id.
382 */
383function question_get_category_id_from_pagevars(array $pagevars) {
384 list($questioncategoryid) = explode(',', $pagevars['cat']);
385 return $questioncategoryid;
386}
387
be77e9b6
TH
388/**
389 * Get a particular question preference that is also stored as a user preference.
390 * If the the value is given in the GET/POST request, then that value is used,
391 * and the user preference is updated to that value. Otherwise, the last set
392 * value of the user preference is used, or if it has never been set the default
393 * passed to this function.
394 *
395 * @param string $param the param name. The URL parameter set, and the GET/POST
396 * parameter read. The user_preference name is 'question_bank_' . $param.
397 * @param mixed $default The default value to use, if not otherwise set.
398 * @param int $type one of the PARAM_... constants.
399 * @param moodle_url $thispageurl if the value has been explicitly set, we add
400 * it to this URL.
401 * @return mixed the parameter value to use.
402 */
403function question_get_display_preference($param, $default, $type, $thispageurl) {
404 $submittedvalue = optional_param($param, null, $type);
405 if (is_null($submittedvalue)) {
406 return get_user_preferences('question_bank_' . $param, $default);
407 }
408
409 set_user_preference('question_bank_' . $param, $submittedvalue);
410 $thispageurl->param($param, $submittedvalue);
411 return $submittedvalue;
412}
413
271e6dec 414/**
415 * Make sure user is logged in as required in this context.
416 */
417function require_login_in_context($contextorid = null){
042a8403 418 global $DB, $CFG;
271e6dec 419 if (!is_object($contextorid)){
5fbe2118 420 $context = context::instance_by_id($contextorid, IGNORE_MISSING);
271e6dec 421 } else {
422 $context = $contextorid;
423 }
424 if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
425 require_login($context->instanceid);
426 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
f34488b2 427 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
428 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
0be2c858 429 print_error('invalidcourseid');
271e6dec 430 }
431 require_course_login($course, true, $cm);
432
433 } else {
0be2c858 434 print_error('invalidcoursemodule');
271e6dec 435 }
436 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
437 if (!empty($CFG->forcelogin)) {
438 require_login();
439 }
440
441 } else {
442 require_login();
443 }
b72ff476 444}
cd120b23 445
446/**
447 * Print a form to let the user choose which question type to add.
448 * When the form is submitted, it goes to the question.php script.
449 * @param $hiddenparams hidden parameters to add to the form, in addition to
75733895
TH
450 * the qtype radio buttons.
451 * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
452 * those qtypes will be shown. Example value array('description', 'multichoice').
cd120b23 453 */
ac32c27e 454function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
f9b0500f 455 global $CFG, $PAGE, $OUTPUT;
7b66b651 456
ac32c27e
ARN
457 if ($enablejs) {
458 // Add the chooser.
e1a2d0d9 459 $PAGE->requires->yui_module('moodle-question-chooser', 'M.question.init_chooser', array(array()));
ac32c27e
ARN
460 }
461
462 $realqtypes = array();
b974f947 463 $fakeqtypes = array();
75733895
TH
464 foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) {
465 if ($allowedqtypes && !in_array($qtypename, $allowedqtypes)) {
466 continue;
467 }
a13d4fbd 468 if ($qtype->is_real_question_type()) {
ac32c27e 469 $realqtypes[] = $qtype;
b974f947 470 } else {
f9b0500f 471 $fakeqtypes[] = $qtype;
b974f947 472 }
473 }
cd120b23 474
ac32c27e 475 $renderer = $PAGE->get_renderer('question', 'bank');
e1a2d0d9 476 return $renderer->qbank_chooser($realqtypes, $fakeqtypes, $PAGE->course, $hiddenparams);
cd120b23 477}
478
479/**
480 * Print a button for creating a new question. This will open question/addquestion.php,
481 * which in turn goes to question/question.php before getting back to $params['returnurl']
482 * (by default the question bank screen).
483 *
f7970e3c 484 * @param int $categoryid The id of the category that the new question should be added to.
cd120b23 485 * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
486 * $params['courseid'], and you should probably set $params['returnurl']
487 * @param string $caption the text to display on the button.
488 * @param string $tooltip a tooltip to add to the button (optional).
f7970e3c 489 * @param bool $disabled if true, the button will be disabled.
cd120b23 490 */
491function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
fef8f84e 492 global $CFG, $PAGE, $OUTPUT;
cd120b23 493 static $choiceformprinted = false;
494 $params['category'] = $categoryid;
a6855934 495 $url = new moodle_url('/question/addquestion.php', $params);
5c2ed7e2 496 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
a941387a 497
cd120b23 498 if (!$choiceformprinted) {
499 echo '<div id="qtypechoicecontainer">';
e1a2d0d9 500 echo print_choose_qtype_to_add_form(array());
cd120b23 501 echo "</div>\n";
502 $choiceformprinted = true;
503 }
504}
505
aeb15530 506