MDL-52591 grunt: make shifter task respect --no-color
[moodle.git] / question / editlib.php
blob922bc6728597fc852a64a6387799bc0943f0591f
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 * @param int $categoryid a category id.
99 * @return bool whether this is the only top-level category in a context.
101 function 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));
113 * Check whether this user is allowed to delete this category.
115 * @param int $todelete a category id.
117 function question_can_delete_cat($todelete) {
118 global $DB;
119 if (question_is_only_toplevel_category_in_context($todelete)) {
120 print_error('cannotdeletecate', 'question');
121 } else {
122 $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
123 require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
129 * Base class for representing a column in a {@link question_bank_view}.
131 * @copyright 2009 Tim Hunt
132 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
133 * @deprecated since Moodle 2.7 MDL-40457
135 class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
138 * A column with a checkbox for each question with name q{questionid}.
140 * @copyright 2009 Tim Hunt
141 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
142 * @deprecated since Moodle 2.7 MDL-40457
144 class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
147 * A column type for the name of the question type.
149 * @copyright 2009 Tim Hunt
150 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
151 * @deprecated since Moodle 2.7 MDL-40457
153 class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
157 * A column type for the name of the question name.
159 * @copyright 2009 Tim Hunt
160 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
161 * @deprecated since Moodle 2.7 MDL-40457
163 class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
167 * A column type for the name of the question creator.
169 * @copyright 2009 Tim Hunt
170 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
171 * @deprecated since Moodle 2.7 MDL-40457
173 class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
177 * A column type for the name of the question last modifier.
179 * @copyright 2009 Tim Hunt
180 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
181 * @deprecated since Moodle 2.7 MDL-40457
183 class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
187 * A base class for actions that are an icon that lets you manipulate the question in some way.
189 * @copyright 2009 Tim Hunt
190 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
191 * @deprecated since Moodle 2.7 MDL-40457
193 class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
197 * Base class for question bank columns that just contain an action icon.
199 * @copyright 2009 Tim Hunt
200 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
201 * @deprecated since Moodle 2.7 MDL-40457
203 class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
206 * Question bank column for the duplicate action icon.
208 * @copyright 2013 The Open University
209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
210 * @deprecated since Moodle 2.7 MDL-40457
212 class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
215 * Question bank columns for the preview action icon.
217 * @copyright 2009 Tim Hunt
218 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
219 * @deprecated since Moodle 2.7 MDL-40457
221 class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
225 * action to delete (or hide) a question, or restore a previously hidden question.
227 * @copyright 2009 Tim Hunt
228 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
229 * @deprecated since Moodle 2.7 MDL-40457
231 class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
234 * Base class for 'columns' that are actually displayed as a row following the main question row.
236 * @copyright 2009 Tim Hunt
237 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
238 * @deprecated since Moodle 2.7 MDL-40457
240 class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
243 * A column type for the name of the question name.
245 * @copyright 2009 Tim Hunt
246 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
247 * @deprecated since Moodle 2.7 MDL-40457
249 class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
252 * @copyright 2009 Tim Hunt
253 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
254 * @deprecated since Moodle 2.7 MDL-40457
256 class_alias('core_question\bank\view', 'question_bank_view', true);
259 * Common setup for all pages for editing questions.
260 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
261 * @param string $edittab code for this edit tab
262 * @param bool $requirecmid require cmid? default false
263 * @param bool $requirecourseid require courseid, if cmid is not given? default true
264 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
266 function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true) {
267 global $DB, $PAGE;
269 $thispageurl = new moodle_url($baseurl);
270 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
272 if ($requirecmid){
273 $cmid =required_param('cmid', PARAM_INT);
274 } else {
275 $cmid = optional_param('cmid', 0, PARAM_INT);
277 if ($cmid){
278 list($module, $cm) = get_module_from_cmid($cmid);
279 $courseid = $cm->course;
280 $thispageurl->params(compact('cmid'));
281 require_login($courseid, false, $cm);
282 $thiscontext = context_module::instance($cmid);
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);
291 if ($courseid){
292 $thispageurl->params(compact('courseid'));
293 require_login($courseid, false);
294 $thiscontext = context_course::instance($courseid);
295 } else {
296 $thiscontext = null;
300 if ($thiscontext){
301 $contexts = new question_edit_contexts($thiscontext);
302 $contexts->require_one_edit_tab_cap($edittab);
304 } else {
305 $contexts = null;
308 $PAGE->set_pagelayout('admin');
310 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
312 //pass 'cat' from page to page and when 'category' comes from a drop down menu
313 //then we also reset the qpage so we go to page 1 of
314 //a new cat.
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?
318 $pagevars['cat'] = $category;
319 $pagevars['qpage'] = 0;
322 if ($pagevars['cat']){
323 $thispageurl->param('cat', $pagevars['cat']);
325 if (strpos($baseurl, '/question/') === 0) {
326 navigation_node::override_active_url($thispageurl);
329 if ($pagevars['qpage'] > -1) {
330 $thispageurl->param('qpage', $pagevars['qpage']);
331 } else {
332 $pagevars['qpage'] = 0;
335 $pagevars['qperpage'] = question_get_display_preference(
336 'qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl);
338 for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
339 $param = 'qbs' . $i;
340 if (!$sort = optional_param($param, '', PARAM_TEXT)) {
341 break;
343 $thispageurl->param($param, $sort);
346 $defaultcategory = question_make_default_categories($contexts->all());
348 $contextlistarr = array();
349 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
350 $contextlistarr[] = "'{$context->id}'";
352 $contextlist = join($contextlistarr, ' ,');
353 if (!empty($pagevars['cat'])){
354 $catparts = explode(',', $pagevars['cat']);
355 if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
356 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
357 print_error('invalidcategory', 'question');
359 } else {
360 $category = $defaultcategory;
361 $pagevars['cat'] = "{$category->id},{$category->contextid}";
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);
369 // Category list page.
370 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
371 if ($pagevars['cpage'] != 1){
372 $thispageurl->param('cpage', $pagevars['cpage']);
375 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
379 * Get the category id from $pagevars.
380 * @param array $pagevars from {@link question_edit_setup()}.
381 * @return int the category id.
383 function question_get_category_id_from_pagevars(array $pagevars) {
384 list($questioncategoryid) = explode(',', $pagevars['cat']);
385 return $questioncategoryid;
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.
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.
403 function 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);
409 set_user_preference('question_bank_' . $param, $submittedvalue);
410 $thispageurl->param($param, $submittedvalue);
411 return $submittedvalue;
415 * Make sure user is logged in as required in this context.
417 function require_login_in_context($contextorid = null){
418 global $DB, $CFG;
419 if (!is_object($contextorid)){
420 $context = context::instance_by_id($contextorid, IGNORE_MISSING);
421 } else {
422 $context = $contextorid;
424 if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
425 require_login($context->instanceid);
426 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
427 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
428 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
429 print_error('invalidcourseid');
431 require_course_login($course, true, $cm);
433 } else {
434 print_error('invalidcoursemodule');
436 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
437 if (!empty($CFG->forcelogin)) {
438 require_login();
441 } else {
442 require_login();
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
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').
454 function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
455 global $CFG, $PAGE, $OUTPUT;
457 if ($enablejs) {
458 // Add the chooser.
459 $PAGE->requires->yui_module('moodle-question-chooser', 'M.question.init_chooser', array(array()));
462 $realqtypes = array();
463 $fakeqtypes = array();
464 foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) {
465 if ($allowedqtypes && !in_array($qtypename, $allowedqtypes)) {
466 continue;
468 if ($qtype->is_real_question_type()) {
469 $realqtypes[] = $qtype;
470 } else {
471 $fakeqtypes[] = $qtype;
475 $renderer = $PAGE->get_renderer('question', 'bank');
476 return $renderer->qbank_chooser($realqtypes, $fakeqtypes, $PAGE->course, $hiddenparams);
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).
484 * @param int $categoryid The id of the category that the new question should be added to.
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).
489 * @param bool $disabled if true, the button will be disabled.
491 function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
492 global $CFG, $PAGE, $OUTPUT;
493 static $choiceformprinted = false;
494 $params['category'] = $categoryid;
495 $url = new moodle_url('/question/addquestion.php', $params);
496 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
498 if (!$choiceformprinted) {
499 echo '<div id="qtypechoicecontainer">';
500 echo print_choose_qtype_to_add_form(array());
501 echo "</div>\n";
502 $choiceformprinted = true;