Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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. 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 17 /** 18 * Functions used to show question editing interface 19 * 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 24 */ 25 26 27 use core_question\bank\search\category_condition; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 require_once($CFG->libdir . '/questionlib.php'); 32 33 define('DEFAULT_QUESTIONS_PER_PAGE', 20); 34 define('MAXIMUM_QUESTIONS_PER_PAGE', 1000); 35 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'); 46 } 47 $modrec->instance = $modrec->id; 48 $modrec->cmid = $cmrec->id; 49 $cmrec->name = $modrec->name; 50 51 return array($modrec, $cmrec); 52 } 53 54 /** 55 * Function to read all questions for category into big array 56 * 57 * @param object $category category number 58 * @param bool $noparent if true only questions with NO parent will be selected 59 * @param bool $recurse include subdirectories 60 * @param bool $export set true if this is called by questionbank export 61 * @param bool $latestversion if only the latest versions needed 62 * @return array 63 */ 64 function get_questions_category(object $category, bool $noparent, bool $recurse = true, bool $export = true, 65 bool $latestversion = false): array { 66 global $DB; 67 68 // Build sql bit for $noparent. 69 $npsql = ''; 70 if ($noparent) { 71 $npsql = " and q.parent='0' "; 72 } 73 74 // Get list of categories. 75 if ($recurse) { 76 $categorylist = question_categorylist($category->id); 77 } else { 78 $categorylist = [$category->id]; 79 } 80 81 // Get the list of questions for the category. 82 list($usql, $params) = $DB->get_in_or_equal($categorylist); 83 84 // Get the latest version of a question. 85 $version = ''; 86 if ($latestversion) { 87 $version = 'AND (qv.version = (SELECT MAX(v.version) 88 FROM {question_versions} v 89 JOIN {question_bank_entries} be 90 ON be.id = v.questionbankentryid 91 WHERE be.id = qbe.id) OR qv.version is null)'; 92 } 93 $questions = $DB->get_records_sql("SELECT q.*, qv.status, qc.id AS category 94 FROM {question} q 95 JOIN {question_versions} qv ON qv.questionid = q.id 96 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid 97 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid 98 WHERE qc.id {$usql} {$npsql} {$version} 99 ORDER BY qc.id, q.qtype, q.name", $params); 100 101 // Iterate through questions, getting stuff we need. 102 $qresults = []; 103 foreach($questions as $key => $question) { 104 $question->export_process = $export; 105 $qtype = question_bank::get_qtype($question->qtype, false); 106 if ($export && $qtype->name() === 'missingtype') { 107 // Unrecognised question type. Skip this question when exporting. 108 continue; 109 } 110 $qtype->get_question_options($question); 111 $qresults[] = $question; 112 } 113 114 return $qresults; 115 } 116 117 /** 118 * Checks whether this is the only child of a top category in a context. 119 * 120 * @param int $categoryid a category id. 121 * @return bool 122 * @deprecated since Moodle 4.0 MDL-71585 123 * @see qbank_managecategories\helper 124 * @todo Final deprecation on Moodle 4.4 MDL-72438 125 */ 126 function question_is_only_child_of_top_category_in_context($categoryid) { 127 debugging('Function question_is_only_child_of_top_category_in_context() 128 has been deprecated and moved to qbank_managecategories plugin, 129 Please use qbank_managecategories\helper::question_is_only_child_of_top_category_in_context() instead.', 130 DEBUG_DEVELOPER); 131 return \qbank_managecategories\helper::question_is_only_child_of_top_category_in_context($categoryid); 132 } 133 134 /** 135 * Checks whether the category is a "Top" category (with no parent). 136 * 137 * @param int $categoryid a category id. 138 * @return bool 139 * @deprecated since Moodle 4.0 MDL-71585 140 * @see qbank_managecategories\helper 141 * @todo Final deprecation on Moodle 4.4 MDL-72438 142 */ 143 function question_is_top_category($categoryid) { 144 debugging('Function question_is_top_category() has been deprecated and moved to qbank_managecategories plugin, 145 Please use qbank_managecategories\helper::question_is_top_category() instead.', DEBUG_DEVELOPER); 146 return \qbank_managecategories\helper::question_is_top_category($categoryid); 147 } 148 149 /** 150 * Ensures that this user is allowed to delete this category. 151 * 152 * @param int $todelete a category id. 153 * @deprecated since Moodle 4.0 MDL-71585 154 * @see qbank_managecategories\helper 155 * @todo Final deprecation on Moodle 4.4 MDL-72438 156 */ 157 function question_can_delete_cat($todelete) { 158 debugging('Function question_can_delete_cat() has been deprecated and moved to qbank_managecategories plugin, 159 Please use qbank_managecategories\helper::question_can_delete_cat() instead.', DEBUG_DEVELOPER); 160 \qbank_managecategories\helper::question_can_delete_cat($todelete); 161 } 162 163 /** 164 * Common setup for all pages for editing questions. 165 * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'. 166 * @param string $edittab code for this edit tab 167 * @param bool $requirecmid require cmid? default false 168 * @param bool $unused no longer used, do no pass 169 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars 170 */ 171 function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) { 172 global $PAGE; 173 174 if ($unused !== null) { 175 debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER); 176 } 177 178 $params = []; 179 180 if ($requirecmid) { 181 $params['cmid'] = required_param('cmid', PARAM_INT); 182 } else { 183 $params['cmid'] = optional_param('cmid', null, PARAM_INT); 184 } 185 186 if (!$params['cmid']) { 187 $params['courseid'] = required_param('courseid', PARAM_INT); 188 } 189 190 $params['qpage'] = optional_param('qpage', null, PARAM_INT); 191 192 // Pass 'cat' from page to page and when 'category' comes from a drop down menu 193 // then we also reset the qpage so we go to page 1 of 194 // a new cat. 195 $params['cat'] = optional_param('cat', null, PARAM_SEQUENCE); // If empty will be set up later. 196 $params['category'] = optional_param('category', null, PARAM_SEQUENCE); 197 $params['qperpage'] = optional_param('qperpage', null, PARAM_INT); 198 199 // Question table sorting options. 200 for ($i = 1; $i <= core_question\local\bank\view::MAX_SORTS; $i++) { 201 $param = 'qbs' . $i; 202 if ($sort = optional_param($param, '', PARAM_TEXT)) { 203 $params[$param] = $sort; 204 } else { 205 break; 206 } 207 } 208 209 // Display options. 210 $params['recurse'] = optional_param('recurse', null, PARAM_BOOL); 211 $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL); 212 $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_INT); 213 // Category list page. 214 $params['cpage'] = optional_param('cpage', null, PARAM_INT); 215 $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT); 216 217 $PAGE->set_pagelayout('admin'); 218 219 return question_build_edit_resources($edittab, $baseurl, $params); 220 } 221 222 /** 223 * Common function for building the generic resources required by the 224 * editing questions pages. 225 * 226 * Either a cmid or a course id must be provided as keys in $params or 227 * an exception will be thrown. All other params are optional and will have 228 * sane default applied if not provided. 229 * 230 * The acceptable keys for $params are: 231 * [ 232 * 'cmid' => PARAM_INT, 233 * 'courseid' => PARAM_INT, 234 * 'qpage' => PARAM_INT, 235 * 'cat' => PARAM_SEQUENCE, 236 * 'category' => PARAM_SEQUENCE, 237 * 'qperpage' => PARAM_INT, 238 * 'recurse' => PARAM_INT, 239 * 'showhidden' => PARAM_INT, 240 * 'qbshowtext' => PARAM_INT, 241 * 'cpage' => PARAM_INT, 242 * 'recurse' => PARAM_BOOL, 243 * 'showhidden' => PARAM_BOOL, 244 * 'qbshowtext' => PARAM_INT, 245 * 'qtagids' => [PARAM_INT], (array of integers) 246 * 'qbs1' => PARAM_TEXT, 247 * 'qbs2' => PARAM_TEXT, 248 * 'qbs3' => PARAM_TEXT, 249 * ... and more qbs keys up to core_question\local\bank\view::MAX_SORTS ... 250 * ]; 251 * 252 * @param string $edittab Code for this edit tab 253 * @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'. 254 * @param array $params The provided parameters to construct the resources with. 255 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars 256 */ 257 function question_build_edit_resources($edittab, $baseurl, $params) { 258 global $DB, $PAGE, $CFG; 259 260 $thispageurl = new moodle_url($baseurl); 261 $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained. 262 263 $cleanparams = [ 264 'qsorts' => [], 265 'qtagids' => [] 266 ]; 267 $paramtypes = [ 268 'cmid' => PARAM_INT, 269 'courseid' => PARAM_INT, 270 'qpage' => PARAM_INT, 271 'cat' => PARAM_SEQUENCE, 272 'category' => PARAM_SEQUENCE, 273 'qperpage' => PARAM_INT, 274 'recurse' => PARAM_INT, 275 'showhidden' => PARAM_INT, 276 'qbshowtext' => PARAM_INT, 277 'cpage' => PARAM_INT, 278 'recurse' => PARAM_BOOL, 279 'showhidden' => PARAM_BOOL, 280 'qbshowtext' => PARAM_INT, 281 ]; 282 283 foreach ($paramtypes as $name => $type) { 284 if (isset($params[$name])) { 285 $cleanparams[$name] = clean_param($params[$name], $type); 286 } else { 287 $cleanparams[$name] = null; 288 } 289 } 290 291 if (!empty($params['qtagids'])) { 292 $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT); 293 } 294 295 $cmid = $cleanparams['cmid']; 296 $courseid = $cleanparams['courseid']; 297 $qpage = $cleanparams['qpage'] ?: -1; 298 $cat = $cleanparams['cat'] ?: 0; 299 $category = $cleanparams['category'] ?: 0; 300 $qperpage = $cleanparams['qperpage']; 301 $recurse = $cleanparams['recurse']; 302 $showhidden = $cleanparams['showhidden']; 303 $qbshowtext = $cleanparams['qbshowtext']; 304 $cpage = $cleanparams['cpage'] ?: 1; 305 $recurse = $cleanparams['recurse']; 306 $showhidden = $cleanparams['showhidden']; 307 $qbshowtext = $cleanparams['qbshowtext']; 308 $qsorts = $cleanparams['qsorts']; 309 $qtagids = $cleanparams['qtagids']; 310 311 if (is_null($cmid) && is_null($courseid)) { 312 throw new \moodle_exception('Must provide a cmid or courseid'); 313 } 314 315 if ($cmid) { 316 list($module, $cm) = get_module_from_cmid($cmid); 317 $courseid = $cm->course; 318 $thispageurl->params(compact('cmid')); 319 require_login($courseid, false, $cm); 320 $thiscontext = context_module::instance($cmid); 321 } else { 322 $module = null; 323 $cm = null; 324 $thispageurl->params(compact('courseid')); 325 require_login($courseid, false); 326 $thiscontext = context_course::instance($courseid); 327 } 328 329 if ($thiscontext){ 330 $contexts = new core_question\local\bank\question_edit_contexts($thiscontext); 331 $contexts->require_one_edit_tab_cap($edittab); 332 } else { 333 $contexts = null; 334 } 335 336 $pagevars['qpage'] = $qpage; 337 338 // Pass 'cat' from page to page and when 'category' comes from a drop down menu 339 // then we also reset the qpage so we go to page 1 of 340 // a new cat. 341 if ($category && $category != $cat) { // Is this a move to a new category? 342 $pagevars['cat'] = $category; 343 $pagevars['qpage'] = 0; 344 } else { 345 $pagevars['cat'] = $cat; // If empty will be set up later. 346 } 347 348 if ($pagevars['cat']){ 349 $thispageurl->param('cat', $pagevars['cat']); 350 } 351 352 if (strpos($baseurl, '/question/') === 0) { 353 navigation_node::override_active_url($thispageurl); 354 } 355 356 // This need to occur after the override_active_url call above because 357 // these values change on the page request causing the URLs to mismatch 358 // when trying to work out the active node. 359 for ($i = 1; $i <= core_question\local\bank\view::MAX_SORTS; $i++) { 360 $param = 'qbs' . $i; 361 if (isset($params[$param])) { 362 $value = clean_param($params[$param], PARAM_TEXT); 363 } else { 364 break; 365 } 366 $thispageurl->param($param, $value); 367 } 368 369 if ($pagevars['qpage'] > -1) { 370 $thispageurl->param('qpage', $pagevars['qpage']); 371 } else { 372 $pagevars['qpage'] = 0; 373 } 374 375 $pagevars['qperpage'] = question_set_or_get_user_preference( 376 'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl); 377 378 $defaultcategory = question_make_default_categories($contexts->all()); 379 380 $contextlistarr = []; 381 foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){ 382 $contextlistarr[] = "'{$context->id}'"; 383 } 384 $contextlist = join(' ,', $contextlistarr); 385 if (!empty($pagevars['cat'])){ 386 $catparts = explode(',', $pagevars['cat']); 387 if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) || 388 !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) { 389 print_error('invalidcategory', 'question'); 390 } 391 } else { 392 $category = $defaultcategory; 393 $pagevars['cat'] = "{$category->id},{$category->contextid}"; 394 } 395 396 // Display options. 397 $pagevars['recurse'] = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl); 398 $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl); 399 $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl); 400 401 // Category list page. 402 $pagevars['cpage'] = $cpage; 403 if ($pagevars['cpage'] != 1){ 404 $thispageurl->param('cpage', $pagevars['cpage']); 405 } 406 407 $pagevars['qtagids'] = $qtagids; 408 foreach ($pagevars['qtagids'] as $index => $qtagid) { 409 $thispageurl->param("qtagids[{$index}]", $qtagid); 410 } 411 412 return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars); 413 } 414 415 /** 416 * Get the category id from $pagevars. 417 * @param array $pagevars from {@link question_edit_setup()}. 418 * @return int the category id. 419 */ 420 function question_get_category_id_from_pagevars(array $pagevars) { 421 list($questioncategoryid) = explode(',', $pagevars['cat']); 422 return $questioncategoryid; 423 } 424 425 /** 426 * Get a particular question preference that is also stored as a user preference. 427 * If the the value is given in the GET/POST request, then that value is used, 428 * and the user preference is updated to that value. Otherwise, the last set 429 * value of the user preference is used, or if it has never been set the default 430 * passed to this function. 431 * 432 * @param string $param the param name. The URL parameter set, and the GET/POST 433 * parameter read. The user_preference name is 'question_bank_' . $param. 434 * @param mixed $default The default value to use, if not otherwise set. 435 * @param int $type one of the PARAM_... constants. 436 * @param moodle_url $thispageurl if the value has been explicitly set, we add 437 * it to this URL. 438 * @return mixed the parameter value to use. 439 */ 440 function question_get_display_preference($param, $default, $type, $thispageurl) { 441 $submittedvalue = optional_param($param, null, $type); 442 return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl); 443 } 444 445 /** 446 * Get a user preference by name or set the user preference to a given value. 447 * 448 * If $value is null then the function will only attempt to retrieve the 449 * user preference requested by $name. If no user preference is found then the 450 * $default value will be returned. In this case the user preferences are not 451 * modified and nor are the params on $thispageurl. 452 * 453 * If $value is anything other than null then the function will set the user 454 * preference $name to the provided $value and will also set it as a param 455 * on $thispageurl. 456 * 457 * @param string $name The user_preference name is 'question_bank_' . $name. 458 * @param mixed $value The preference value. 459 * @param mixed $default The default value to use, if not otherwise set. 460 * @param moodle_url $thispageurl if the value has been explicitly set, we add 461 * it to this URL. 462 * @return mixed the parameter value to use. 463 */ 464 function question_set_or_get_user_preference($name, $value, $default, $thispageurl) { 465 if (is_null($value)) { 466 return get_user_preferences('question_bank_' . $name, $default); 467 } 468 469 set_user_preference('question_bank_' . $name, $value); 470 $thispageurl->param($name, $value); 471 return $value; 472 } 473 474 /** 475 * Make sure user is logged in as required in this context. 476 */ 477 function require_login_in_context($contextorid = null){ 478 global $DB, $CFG; 479 if (!is_object($contextorid)){ 480 $context = context::instance_by_id($contextorid, IGNORE_MISSING); 481 } else { 482 $context = $contextorid; 483 } 484 if ($context && ($context->contextlevel == CONTEXT_COURSE)) { 485 require_login($context->instanceid); 486 } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) { 487 if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) { 488 if (!$course = $DB->get_record('course', array('id' => $cm->course))) { 489 print_error('invalidcourseid'); 490 } 491 require_course_login($course, true, $cm); 492 493 } else { 494 print_error('invalidcoursemodule'); 495 } 496 } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) { 497 if (!empty($CFG->forcelogin)) { 498 require_login(); 499 } 500 501 } else { 502 require_login(); 503 } 504 } 505 506 /** 507 * Print a form to let the user choose which question type to add. 508 * When the form is submitted, it goes to the question.php script. 509 * @param $hiddenparams hidden parameters to add to the form, in addition to 510 * the qtype radio buttons. 511 * @param $allowedqtypes optional list of qtypes that are allowed. If given, only 512 * those qtypes will be shown. Example value array('description', 'multichoice'). 513 * @deprecated since Moodle 4.0 514 * @see \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form() 515 * @todo Final deprecation of this class in moodle 4.4 MDL-72438 516 */ 517 function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) { 518 debugging('Function print_choose_qtype_to_add_form() is deprecated, 519 please use \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form() instead.', DEBUG_DEVELOPER); 520 global $CFG, $PAGE, $OUTPUT; 521 522 $chooser = \qbank_editquestion\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes); 523 $renderer = $PAGE->get_renderer('question', 'bank'); 524 525 return $renderer->render($chooser); 526 } 527 528 /** 529 * Print a button for creating a new question. This will open question/addquestion.php, 530 * which in turn goes to question/question.php before getting back to $params['returnurl'] 531 * (by default the question bank screen). 532 * 533 * @param int $categoryid The id of the category that the new question should be added to. 534 * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or 535 * $params['courseid'], and you should probably set $params['returnurl'] 536 * @param string $caption the text to display on the button. 537 * @param string $tooltip a tooltip to add to the button (optional). 538 * @param bool $disabled if true, the button will be disabled. 539 * @deprecated since Moodle 4.0 540 * @see \qbank_editquestion\editquestion_helper::create_new_question_button() 541 * @todo Final deprecation of this class in moodle 4.4 MDL-72438 542 */ 543 function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) { 544 debugging('Function create_new_question_button() has been deprecated and moved to bank/editquestion, 545 please use qbank_editquestion\editquestion_helper::create_new_question_button() instead.', DEBUG_DEVELOPER); 546 global $CFG, $PAGE, $OUTPUT; 547 static $choiceformprinted = false; 548 $params['category'] = $categoryid; 549 $url = new moodle_url('/question/bank/editquestion/addquestion.php', $params); 550 echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip)); 551 552 if (!$choiceformprinted) { 553 echo '<div id="qtypechoicecontainer">'; 554 echo print_choose_qtype_to_add_form(array()); 555 echo "</div>\n"; 556 $choiceformprinted = true; 557 } 558 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body