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