2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Functions used to show question editing interface
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) {
38 if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
39 FROM {course_modules} cm,
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);
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 ) {
64 // Build sql bit for $noparent
67 $npsql = " and parent='0' ";
70 // Get list of categories
72 $categorylist = question_categorylist($category->id
);
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
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.
90 $qtype->get_question_options($question);
91 $qresults[] = $question;
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) {
103 return 1 == $DB->count_records_sql("
105 FROM {question_categories} c1,
106 {question_categories} c2
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) {
119 if (question_is_only_toplevel_category_in_context($todelete)) {
120 print_error('cannotdeletecate', 'question');
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 $unused no longer used, do no pass
264 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
266 function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
267 global $DB, $PAGE, $CFG;
269 if ($unused !== null) {
270 debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER
);
273 $thispageurl = new moodle_url($baseurl);
274 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
277 $cmid =required_param('cmid', PARAM_INT
);
279 $cmid = optional_param('cmid', 0, PARAM_INT
);
282 list($module, $cm) = get_module_from_cmid($cmid);
283 $courseid = $cm->course
;
284 $thispageurl->params(compact('cmid'));
285 require_login($courseid, false, $cm);
286 $thiscontext = context_module
::instance($cmid);
290 $courseid = required_param('courseid', PARAM_INT
);
291 $thispageurl->params(compact('courseid'));
292 require_login($courseid, false);
293 $thiscontext = context_course
::instance($courseid);
297 $contexts = new question_edit_contexts($thiscontext);
298 $contexts->require_one_edit_tab_cap($edittab);
304 $PAGE->set_pagelayout('admin');
306 $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT
);
308 //pass 'cat' from page to page and when 'category' comes from a drop down menu
309 //then we also reset the qpage so we go to page 1 of
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?
314 $pagevars['cat'] = $category;
315 $pagevars['qpage'] = 0;
318 if ($pagevars['cat']){
319 $thispageurl->param('cat', $pagevars['cat']);
321 if (strpos($baseurl, '/question/') === 0) {
322 navigation_node
::override_active_url($thispageurl);
325 if ($pagevars['qpage'] > -1) {
326 $thispageurl->param('qpage', $pagevars['qpage']);
328 $pagevars['qpage'] = 0;
331 $pagevars['qperpage'] = question_get_display_preference(
332 'qperpage', DEFAULT_QUESTIONS_PER_PAGE
, PARAM_INT
, $thispageurl);
334 for ($i = 1; $i <= question_bank_view
::MAX_SORTS
; $i++
) {
336 if (!$sort = optional_param($param, '', PARAM_TEXT
)) {
339 $thispageurl->param($param, $sort);
342 $defaultcategory = question_make_default_categories($contexts->all());
344 $contextlistarr = array();
345 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
346 $contextlistarr[] = "'{$context->id}'";
348 $contextlist = join($contextlistarr, ' ,');
349 if (!empty($pagevars['cat'])){
350 $catparts = explode(',', $pagevars['cat']);
351 if (!$catparts[0] ||
(false !== array_search($catparts[1], $contextlistarr)) ||
352 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
353 print_error('invalidcategory', 'question');
356 $category = $defaultcategory;
357 $pagevars['cat'] = "{$category->id},{$category->contextid}";
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);
365 // Category list page.
366 $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT
);
367 if ($pagevars['cpage'] != 1){
368 $thispageurl->param('cpage', $pagevars['cpage']);
371 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
375 * Get the category id from $pagevars.
376 * @param array $pagevars from {@link question_edit_setup()}.
377 * @return int the category id.
379 function question_get_category_id_from_pagevars(array $pagevars) {
380 list($questioncategoryid) = explode(',', $pagevars['cat']);
381 return $questioncategoryid;
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.
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
397 * @return mixed the parameter value to use.
399 function 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);
405 set_user_preference('question_bank_' . $param, $submittedvalue);
406 $thispageurl->param($param, $submittedvalue);
407 return $submittedvalue;
411 * Make sure user is logged in as required in this context.
413 function require_login_in_context($contextorid = null){
415 if (!is_object($contextorid)){
416 $context = context
::instance_by_id($contextorid, IGNORE_MISSING
);
418 $context = $contextorid;
420 if ($context && ($context->contextlevel
== CONTEXT_COURSE
)) {
421 require_login($context->instanceid
);
422 } else if ($context && ($context->contextlevel
== CONTEXT_MODULE
)) {
423 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid
))) {
424 if (!$course = $DB->get_record('course', array('id' => $cm->course
))) {
425 print_error('invalidcourseid');
427 require_course_login($course, true, $cm);
430 print_error('invalidcoursemodule');
432 } else if ($context && ($context->contextlevel
== CONTEXT_SYSTEM
)) {
433 if (!empty($CFG->forcelogin
)) {
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
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').
450 function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
451 global $CFG, $PAGE, $OUTPUT;
453 $chooser = core_question\output\qbank_chooser
::get($PAGE->course
, $hiddenparams, $allowedqtypes);
454 $renderer = $PAGE->get_renderer('question', 'bank');
456 return $renderer->render($chooser);
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).
464 * @param int $categoryid The id of the category that the new question should be added to.
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).
469 * @param bool $disabled if true, the button will be disabled.
471 function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
472 global $CFG, $PAGE, $OUTPUT;
473 static $choiceformprinted = false;
474 $params['category'] = $categoryid;
475 $url = new moodle_url('/question/addquestion.php', $params);
476 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
478 if (!$choiceformprinted) {
479 echo '<div id="qtypechoicecontainer">';
480 echo print_choose_qtype_to_add_form(array());
482 $choiceformprinted = true;