Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  * Function to read all questions for category into big array
  55  *
  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
  60  */
  61  function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
  62      global $DB;
  63  
  64      // Build sql bit for $noparent
  65      $npsql = '';
  66      if ($noparent) {
  67        $npsql = " and parent='0' ";
  68      }
  69  
  70      // Get list of categories
  71      if ($recurse) {
  72          $categorylist = question_categorylist($category->id);
  73      } else {
  74          $categorylist = array($category->id);
  75      }
  76  
  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, 'category, qtype, name');
  80  
  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;
  89          }
  90          $qtype->get_question_options($question);
  91          $qresults[] = $question;
  92      }
  93  
  94      return $qresults;
  95  }
  96  
  97  /**
  98   * Checks whether this is the only child of a top category in a context.
  99   *
 100   * @param int $categoryid a category id.
 101   * @return bool
 102   */
 103  function question_is_only_child_of_top_category_in_context($categoryid) {
 104      global $DB;
 105      return 1 == $DB->count_records_sql("
 106              SELECT count(*)
 107                FROM {question_categories} c
 108                JOIN {question_categories} p ON c.parent = p.id
 109                JOIN {question_categories} s ON s.parent = c.parent
 110               WHERE c.id = ? AND p.parent = 0", array($categoryid));
 111  }
 112  
 113  /**
 114   * Checks whether the category is a "Top" category (with no parent).
 115   *
 116   * @param int $categoryid a category id.
 117   * @return bool
 118   */
 119  function question_is_top_category($categoryid) {
 120      global $DB;
 121      return 0 == $DB->get_field('question_categories', 'parent', array('id' => $categoryid));
 122  }
 123  
 124  /**
 125   * Ensures that this user is allowed to delete this category.
 126   *
 127   * @param int $todelete a category id.
 128   */
 129  function question_can_delete_cat($todelete) {
 130      global $DB;
 131      if (question_is_top_category($todelete)) {
 132          print_error('cannotdeletetopcat', 'question');
 133      } else if (question_is_only_child_of_top_category_in_context($todelete)) {
 134          print_error('cannotdeletecate', 'question');
 135      } else {
 136          $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
 137          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
 138      }
 139  }
 140  
 141  
 142  /**
 143   * Base class for representing a column in a {@link question_bank_view}.
 144   *
 145   * @copyright  2009 Tim Hunt
 146   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 147   * @deprecated since Moodle 2.7 MDL-40457
 148   */
 149  class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
 150  
 151  /**
 152   * A column with a checkbox for each question with name q{questionid}.
 153   *
 154   * @copyright  2009 Tim Hunt
 155   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 156   * @deprecated since Moodle 2.7 MDL-40457
 157   */
 158  class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
 159  
 160  /**
 161   * A column type for the name of the question type.
 162   *
 163   * @copyright  2009 Tim Hunt
 164   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 165   * @deprecated since Moodle 2.7 MDL-40457
 166   */
 167  class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
 168  
 169  
 170  /**
 171   * A column type for the name of the question name.
 172   *
 173   * @copyright  2009 Tim Hunt
 174   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 175   * @deprecated since Moodle 2.7 MDL-40457
 176   */
 177  class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
 178  
 179  
 180  /**
 181   * A column type for the name of the question creator.
 182   *
 183   * @copyright  2009 Tim Hunt
 184   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 185   * @deprecated since Moodle 2.7 MDL-40457
 186   */
 187  class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
 188  
 189  
 190  /**
 191   * A column type for the name of the question last modifier.
 192   *
 193   * @copyright  2009 Tim Hunt
 194   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 195   * @deprecated since Moodle 2.7 MDL-40457
 196   */
 197  class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
 198  
 199  
 200  /**
 201   * A base class for actions that are an icon that lets you manipulate the question in some way.
 202   *
 203   * @copyright  2009 Tim Hunt
 204   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 205   * @deprecated since Moodle 2.7 MDL-40457
 206   */
 207  class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
 208  
 209  
 210  /**
 211   * Base class for question bank columns that just contain an action icon.
 212   *
 213   * @copyright  2009 Tim Hunt
 214   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 215   * @deprecated since Moodle 2.7 MDL-40457
 216   */
 217  class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
 218  
 219  /**
 220   * Question bank column for the duplicate action icon.
 221   *
 222   * @copyright  2013 The Open University
 223   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 224   * @deprecated since Moodle 2.7 MDL-40457
 225   */
 226  class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
 227  
 228  /**
 229   * Question bank columns for the preview action icon.
 230   *
 231   * @copyright  2009 Tim Hunt
 232   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 233   * @deprecated since Moodle 2.7 MDL-40457
 234   */
 235  class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
 236  
 237  
 238  /**
 239   * action to delete (or hide) a question, or restore a previously hidden question.
 240   *
 241   * @copyright  2009 Tim Hunt
 242   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 243   * @deprecated since Moodle 2.7 MDL-40457
 244   */
 245  class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
 246  
 247  /**
 248   * Base class for 'columns' that are actually displayed as a row following the main question row.
 249   *
 250   * @copyright  2009 Tim Hunt
 251   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 252   * @deprecated since Moodle 2.7 MDL-40457
 253   */
 254  class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
 255  
 256  /**
 257   * A column type for the name of the question name.
 258   *
 259   * @copyright  2009 Tim Hunt
 260   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 261   * @deprecated since Moodle 2.7 MDL-40457
 262   */
 263  class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
 264  
 265  /**
 266   * @copyright  2009 Tim Hunt
 267   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 268   * @deprecated since Moodle 2.7 MDL-40457
 269   */
 270  class_alias('core_question\bank\view', 'question_bank_view', true);
 271  
 272  /**
 273   * Common setup for all pages for editing questions.
 274   * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
 275   * @param string $edittab code for this edit tab
 276   * @param bool $requirecmid require cmid? default false
 277   * @param bool $unused no longer used, do no pass
 278   * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 279   */
 280  function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
 281      global $PAGE;
 282  
 283      if ($unused !== null) {
 284          debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER);
 285      }
 286  
 287      $params = [];
 288  
 289      if ($requirecmid) {
 290          $params['cmid'] = required_param('cmid', PARAM_INT);
 291      } else {
 292          $params['cmid'] = optional_param('cmid', null, PARAM_INT);
 293      }
 294  
 295      if (!$params['cmid']) {
 296          $params['courseid'] = required_param('courseid', PARAM_INT);
 297      }
 298  
 299      $params['qpage'] = optional_param('qpage', null, PARAM_INT);
 300  
 301      // Pass 'cat' from page to page and when 'category' comes from a drop down menu
 302      // then we also reset the qpage so we go to page 1 of
 303      // a new cat.
 304      $params['cat'] = optional_param('cat', null, PARAM_SEQUENCE); // If empty will be set up later.
 305      $params['category'] = optional_param('category', null, PARAM_SEQUENCE);
 306      $params['qperpage'] = optional_param('qperpage', null, PARAM_INT);
 307  
 308      // Question table sorting options.
 309      for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
 310          $param = 'qbs' . $i;
 311          if ($sort = optional_param($param, '', PARAM_TEXT)) {
 312              $params[$param] = $sort;
 313          } else {
 314              break;
 315          }
 316      }
 317  
 318      // Display options.
 319      $params['recurse'] = optional_param('recurse',    null, PARAM_BOOL);
 320      $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL);
 321      $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL);
 322      // Category list page.
 323      $params['cpage'] = optional_param('cpage', null, PARAM_INT);
 324      $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT);
 325  
 326      $PAGE->set_pagelayout('admin');
 327  
 328      return question_build_edit_resources($edittab, $baseurl, $params);
 329  }
 330  
 331  /**
 332   * Common function for building the generic resources required by the
 333   * editing questions pages.
 334   *
 335   * Either a cmid or a course id must be provided as keys in $params or
 336   * an exception will be thrown. All other params are optional and will have
 337   * sane default applied if not provided.
 338   *
 339   * The acceptable keys for $params are:
 340   * [
 341   *      'cmid' => PARAM_INT,
 342   *      'courseid' => PARAM_INT,
 343   *      'qpage' => PARAM_INT,
 344   *      'cat' => PARAM_SEQUENCE,
 345   *      'category' => PARAM_SEQUENCE,
 346   *      'qperpage' => PARAM_INT,
 347   *      'recurse' => PARAM_INT,
 348   *      'showhidden' => PARAM_INT,
 349   *      'qbshowtext' => PARAM_INT,
 350   *      'cpage' => PARAM_INT,
 351   *      'recurse' => PARAM_BOOL,
 352   *      'showhidden' => PARAM_BOOL,
 353   *      'qbshowtext' => PARAM_BOOL,
 354   *      'qtagids' => [PARAM_INT], (array of integers)
 355   *      'qbs1' => PARAM_TEXT,
 356   *      'qbs2' => PARAM_TEXT,
 357   *      'qbs3' => PARAM_TEXT,
 358   *      ... and more qbs keys up to question_bank_view::MAX_SORTS ...
 359   *  ];
 360   *
 361   * @param string $edittab Code for this edit tab
 362   * @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'.
 363   * @param array $params The provided parameters to construct the resources with.
 364   * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 365   */
 366  function question_build_edit_resources($edittab, $baseurl, $params) {
 367      global $DB, $PAGE, $CFG;
 368  
 369      $thispageurl = new moodle_url($baseurl);
 370      $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
 371  
 372      $cleanparams = [
 373          'qsorts' => [],
 374          'qtagids' => []
 375      ];
 376      $paramtypes = [
 377          'cmid' => PARAM_INT,
 378          'courseid' => PARAM_INT,
 379          'qpage' => PARAM_INT,
 380          'cat' => PARAM_SEQUENCE,
 381          'category' => PARAM_SEQUENCE,
 382          'qperpage' => PARAM_INT,
 383          'recurse' => PARAM_INT,
 384          'showhidden' => PARAM_INT,
 385          'qbshowtext' => PARAM_INT,
 386          'cpage' => PARAM_INT,
 387          'recurse' => PARAM_BOOL,
 388          'showhidden' => PARAM_BOOL,
 389          'qbshowtext' => PARAM_BOOL
 390      ];
 391  
 392      foreach ($paramtypes as $name => $type) {
 393          if (isset($params[$name])) {
 394              $cleanparams[$name] = clean_param($params[$name], $type);
 395          } else {
 396              $cleanparams[$name] = null;
 397          }
 398      }
 399  
 400      if (!empty($params['qtagids'])) {
 401          $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT);
 402      }
 403  
 404      $cmid = $cleanparams['cmid'];
 405      $courseid = $cleanparams['courseid'];
 406      $qpage = $cleanparams['qpage'] ?: -1;
 407      $cat = $cleanparams['cat'] ?: 0;
 408      $category = $cleanparams['category'] ?: 0;
 409      $qperpage = $cleanparams['qperpage'];
 410      $recurse = $cleanparams['recurse'];
 411      $showhidden = $cleanparams['showhidden'];
 412      $qbshowtext = $cleanparams['qbshowtext'];
 413      $cpage = $cleanparams['cpage'] ?: 1;
 414      $recurse = $cleanparams['recurse'];
 415      $showhidden = $cleanparams['showhidden'];
 416      $qbshowtext = $cleanparams['qbshowtext'];
 417      $qsorts = $cleanparams['qsorts'];
 418      $qtagids = $cleanparams['qtagids'];
 419  
 420      if (is_null($cmid) && is_null($courseid)) {
 421          throw new \moodle_exception('Must provide a cmid or courseid');
 422      }
 423  
 424      if ($cmid) {
 425          list($module, $cm) = get_module_from_cmid($cmid);
 426          $courseid = $cm->course;
 427          $thispageurl->params(compact('cmid'));
 428          require_login($courseid, false, $cm);
 429          $thiscontext = context_module::instance($cmid);
 430      } else {
 431          $module = null;
 432          $cm = null;
 433          $thispageurl->params(compact('courseid'));
 434          require_login($courseid, false);
 435          $thiscontext = context_course::instance($courseid);
 436      }
 437  
 438      if ($thiscontext){
 439          $contexts = new question_edit_contexts($thiscontext);
 440          $contexts->require_one_edit_tab_cap($edittab);
 441      } else {
 442          $contexts = null;
 443      }
 444  
 445      $pagevars['qpage'] = $qpage;
 446  
 447      // Pass 'cat' from page to page and when 'category' comes from a drop down menu
 448      // then we also reset the qpage so we go to page 1 of
 449      // a new cat.
 450      if ($category && $category != $cat) { // Is this a move to a new category?
 451          $pagevars['cat'] = $category;
 452          $pagevars['qpage'] = 0;
 453      } else {
 454          $pagevars['cat'] = $cat; // If empty will be set up later.
 455      }
 456  
 457      if ($pagevars['cat']){
 458          $thispageurl->param('cat', $pagevars['cat']);
 459      }
 460  
 461      if (strpos($baseurl, '/question/') === 0) {
 462          navigation_node::override_active_url($thispageurl);
 463      }
 464  
 465      // This need to occur after the override_active_url call above because
 466      // these values change on the page request causing the URLs to mismatch
 467      // when trying to work out the active node.
 468      for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
 469          $param = 'qbs' . $i;
 470          if (isset($params[$param])) {
 471              $value = clean_param($params[$param], PARAM_TEXT);
 472          } else {
 473              break;
 474          }
 475          $thispageurl->param($param, $value);
 476      }
 477  
 478      if ($pagevars['qpage'] > -1) {
 479          $thispageurl->param('qpage', $pagevars['qpage']);
 480      } else {
 481          $pagevars['qpage'] = 0;
 482      }
 483  
 484      $pagevars['qperpage'] = question_set_or_get_user_preference(
 485              'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
 486  
 487      $defaultcategory = question_make_default_categories($contexts->all());
 488  
 489      $contextlistarr = [];
 490      foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
 491          $contextlistarr[] = "'{$context->id}'";
 492      }
 493      $contextlist = join(' ,', $contextlistarr);
 494      if (!empty($pagevars['cat'])){
 495          $catparts = explode(',', $pagevars['cat']);
 496          if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
 497                  !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
 498              print_error('invalidcategory', 'question');
 499          }
 500      } else {
 501          $category = $defaultcategory;
 502          $pagevars['cat'] = "{$category->id},{$category->contextid}";
 503      }
 504  
 505      // Display options.
 506      $pagevars['recurse']    = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
 507      $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
 508      $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
 509  
 510      // Category list page.
 511      $pagevars['cpage'] = $cpage;
 512      if ($pagevars['cpage'] != 1){
 513          $thispageurl->param('cpage', $pagevars['cpage']);
 514      }
 515  
 516      $pagevars['qtagids'] = $qtagids;
 517      foreach ($pagevars['qtagids'] as $index => $qtagid) {
 518          $thispageurl->param("qtagids[{$index}]", $qtagid);
 519      }
 520  
 521      return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
 522  }
 523  
 524  /**
 525   * Get the category id from $pagevars.
 526   * @param array $pagevars from {@link question_edit_setup()}.
 527   * @return int the category id.
 528   */
 529  function question_get_category_id_from_pagevars(array $pagevars) {
 530      list($questioncategoryid) = explode(',', $pagevars['cat']);
 531      return $questioncategoryid;
 532  }
 533  
 534  /**
 535   * Get a particular question preference that is also stored as a user preference.
 536   * If the the value is given in the GET/POST request, then that value is used,
 537   * and the user preference is updated to that value. Otherwise, the last set
 538   * value of the user preference is used, or if it has never been set the default
 539   * passed to this function.
 540   *
 541   * @param string $param the param name. The URL parameter set, and the GET/POST
 542   *      parameter read. The user_preference name is 'question_bank_' . $param.
 543   * @param mixed $default The default value to use, if not otherwise set.
 544   * @param int $type one of the PARAM_... constants.
 545   * @param moodle_url $thispageurl if the value has been explicitly set, we add
 546   *      it to this URL.
 547   * @return mixed the parameter value to use.
 548   */
 549  function question_get_display_preference($param, $default, $type, $thispageurl) {
 550      $submittedvalue = optional_param($param, null, $type);
 551      return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
 552  }
 553  
 554  /**
 555   * Get a user preference by name or set the user preference to a given value.
 556   *
 557   * If $value is null then the function will only attempt to retrieve the
 558   * user preference requested by $name. If no user preference is found then the
 559   * $default value will be returned. In this case the user preferences are not
 560   * modified and nor are the params on $thispageurl.
 561   *
 562   * If $value is anything other than null then the function will set the user
 563   * preference $name to the provided $value and will also set it as a param
 564   * on $thispageurl.
 565   *
 566   * @param string $name The user_preference name is 'question_bank_' . $name.
 567   * @param mixed $value The preference value.
 568   * @param mixed $default The default value to use, if not otherwise set.
 569   * @param moodle_url $thispageurl if the value has been explicitly set, we add
 570   *      it to this URL.
 571   * @return mixed the parameter value to use.
 572   */
 573  function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
 574      if (is_null($value)) {
 575          return get_user_preferences('question_bank_' . $name, $default);
 576      }
 577  
 578      set_user_preference('question_bank_' . $name, $value);
 579      $thispageurl->param($name, $value);
 580      return $value;
 581  }
 582  
 583  /**
 584   * Make sure user is logged in as required in this context.
 585   */
 586  function require_login_in_context($contextorid = null){
 587      global $DB, $CFG;
 588      if (!is_object($contextorid)){
 589          $context = context::instance_by_id($contextorid, IGNORE_MISSING);
 590      } else {
 591          $context = $contextorid;
 592      }
 593      if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
 594          require_login($context->instanceid);
 595      } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
 596          if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
 597              if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
 598                  print_error('invalidcourseid');
 599              }
 600              require_course_login($course, true, $cm);
 601  
 602          } else {
 603              print_error('invalidcoursemodule');
 604          }
 605      } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
 606          if (!empty($CFG->forcelogin)) {
 607              require_login();
 608          }
 609  
 610      } else {
 611          require_login();
 612      }
 613  }
 614  
 615  /**
 616   * Print a form to let the user choose which question type to add.
 617   * When the form is submitted, it goes to the question.php script.
 618   * @param $hiddenparams hidden parameters to add to the form, in addition to
 619   *      the qtype radio buttons.
 620   * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
 621   *      those qtypes will be shown. Example value array('description', 'multichoice').
 622   */
 623  function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
 624      global $CFG, $PAGE, $OUTPUT;
 625  
 626      $chooser = core_question\output\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
 627      $renderer = $PAGE->get_renderer('question', 'bank');
 628  
 629      return $renderer->render($chooser);
 630  }
 631  
 632  /**
 633   * Print a button for creating a new question. This will open question/addquestion.php,
 634   * which in turn goes to question/question.php before getting back to $params['returnurl']
 635   * (by default the question bank screen).
 636   *
 637   * @param int $categoryid The id of the category that the new question should be added to.
 638   * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
 639   *      $params['courseid'], and you should probably set $params['returnurl']
 640   * @param string $caption the text to display on the button.
 641   * @param string $tooltip a tooltip to add to the button (optional).
 642   * @param bool $disabled if true, the button will be disabled.
 643   */
 644  function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
 645      global $CFG, $PAGE, $OUTPUT;
 646      static $choiceformprinted = false;
 647      $params['category'] = $categoryid;
 648      $url = new moodle_url('/question/addquestion.php', $params);
 649      echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
 650  
 651      if (!$choiceformprinted) {
 652          echo '<div id="qtypechoicecontainer">';
 653          echo print_choose_qtype_to_add_form(array());
 654          echo "</div>\n";
 655          $choiceformprinted = true;
 656      }
 657  }
 658  
 659