Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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', 100);
  34  define('MAXIMUM_QUESTIONS_PER_PAGE', 4000);
  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          throw new \moodle_exception('invalidcoursemodule');
  44      } elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
  45          throw new \moodle_exception('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   * @param int $defaultquestionsperpage number of questions per page, if not given in the URL.
 256   * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 257   */
 258  function question_build_edit_resources($edittab, $baseurl, $params,
 259          $defaultquestionsperpage = DEFAULT_QUESTIONS_PER_PAGE) {
 260      global $DB;
 261  
 262      $thispageurl = new moodle_url($baseurl);
 263      $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
 264  
 265      $cleanparams = [
 266          'qsorts' => [],
 267          'qtagids' => []
 268      ];
 269      $paramtypes = [
 270          'cmid' => PARAM_INT,
 271          'courseid' => PARAM_INT,
 272          'qpage' => PARAM_INT,
 273          'cat' => PARAM_SEQUENCE,
 274          'category' => PARAM_SEQUENCE,
 275          'qperpage' => PARAM_INT,
 276          'recurse' => PARAM_INT,
 277          'showhidden' => PARAM_INT,
 278          'qbshowtext' => PARAM_INT,
 279          'cpage' => PARAM_INT,
 280          'recurse' => PARAM_BOOL,
 281          'showhidden' => PARAM_BOOL,
 282          'qbshowtext' => PARAM_INT,
 283      ];
 284  
 285      foreach ($paramtypes as $name => $type) {
 286          if (isset($params[$name])) {
 287              $cleanparams[$name] = clean_param($params[$name], $type);
 288          } else {
 289              $cleanparams[$name] = null;
 290          }
 291      }
 292  
 293      if (!empty($params['qtagids'])) {
 294          $cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT);
 295      }
 296  
 297      $cmid = $cleanparams['cmid'];
 298      $courseid = $cleanparams['courseid'];
 299      $qpage = $cleanparams['qpage'] ?: -1;
 300      $cat = $cleanparams['cat'] ?: 0;
 301      $category = $cleanparams['category'] ?: 0;
 302      $qperpage = $cleanparams['qperpage'];
 303      $recurse = $cleanparams['recurse'];
 304      $showhidden = $cleanparams['showhidden'];
 305      $qbshowtext = $cleanparams['qbshowtext'];
 306      $cpage = $cleanparams['cpage'] ?: 1;
 307      $recurse = $cleanparams['recurse'];
 308      $showhidden = $cleanparams['showhidden'];
 309      $qbshowtext = $cleanparams['qbshowtext'];
 310      $qsorts = $cleanparams['qsorts'];
 311      $qtagids = $cleanparams['qtagids'];
 312  
 313      if (is_null($cmid) && is_null($courseid)) {
 314          throw new \moodle_exception('Must provide a cmid or courseid');
 315      }
 316  
 317      if ($cmid) {
 318          list($module, $cm) = get_module_from_cmid($cmid);
 319          $courseid = $cm->course;
 320          $thispageurl->params(compact('cmid'));
 321          require_login($courseid, false, $cm);
 322          $thiscontext = context_module::instance($cmid);
 323      } else {
 324          $module = null;
 325          $cm = null;
 326          $thispageurl->params(compact('courseid'));
 327          require_login($courseid, false);
 328          $thiscontext = context_course::instance($courseid);
 329      }
 330  
 331      if ($thiscontext){
 332          $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
 333          $contexts->require_one_edit_tab_cap($edittab);
 334      } else {
 335          $contexts = null;
 336      }
 337  
 338      $pagevars['qpage'] = $qpage;
 339  
 340      // Pass 'cat' from page to page and when 'category' comes from a drop down menu
 341      // then we also reset the qpage so we go to page 1 of
 342      // a new cat.
 343      if ($category && $category != $cat) { // Is this a move to a new category?
 344          $pagevars['cat'] = $category;
 345          $pagevars['qpage'] = 0;
 346      } else {
 347          $pagevars['cat'] = $cat; // If empty will be set up later.
 348      }
 349  
 350      if ($pagevars['cat']){
 351          $thispageurl->param('cat', $pagevars['cat']);
 352      }
 353  
 354      if (strpos($baseurl, '/question/') === 0) {
 355          navigation_node::override_active_url($thispageurl);
 356      }
 357  
 358      // This need to occur after the override_active_url call above because
 359      // these values change on the page request causing the URLs to mismatch
 360      // when trying to work out the active node.
 361      for ($i = 1; $i <= core_question\local\bank\view::MAX_SORTS; $i++) {
 362          $param = 'qbs' . $i;
 363          if (isset($params[$param])) {
 364              $value = clean_param($params[$param], PARAM_TEXT);
 365          } else {
 366              break;
 367          }
 368          $thispageurl->param($param, $value);
 369      }
 370  
 371      if ($pagevars['qpage'] > -1) {
 372          $thispageurl->param('qpage', $pagevars['qpage']);
 373      } else {
 374          $pagevars['qpage'] = 0;
 375      }
 376  
 377      if ($defaultquestionsperpage == DEFAULT_QUESTIONS_PER_PAGE) {
 378          $pagevars['qperpage'] = question_set_or_get_user_preference(
 379                  'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
 380      } else {
 381          $pagevars['qperpage'] = $qperpage ?? $defaultquestionsperpage;
 382      }
 383  
 384      $defaultcategory = question_make_default_categories($contexts->all());
 385  
 386      $contextlistarr = [];
 387      foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
 388          $contextlistarr[] = "'{$context->id}'";
 389      }
 390      $contextlist = join(' ,', $contextlistarr);
 391      if (!empty($pagevars['cat'])){
 392          $catparts = explode(',', $pagevars['cat']);
 393          if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
 394                  !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
 395              throw new \moodle_exception('invalidcategory', 'question');
 396          }
 397      } else {
 398          $category = $defaultcategory;
 399          $pagevars['cat'] = "{$category->id},{$category->contextid}";
 400      }
 401  
 402      // Display options.
 403      $pagevars['recurse']    = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
 404      $pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
 405      $pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
 406  
 407      // Category list page.
 408      $pagevars['cpage'] = $cpage;
 409      if ($pagevars['cpage'] != 1){
 410          $thispageurl->param('cpage', $pagevars['cpage']);
 411      }
 412  
 413      $pagevars['qtagids'] = $qtagids;
 414      foreach ($pagevars['qtagids'] as $index => $qtagid) {
 415          $thispageurl->param("qtagids[{$index}]", $qtagid);
 416      }
 417  
 418      return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
 419  }
 420  
 421  /**
 422   * Get the category id from $pagevars.
 423   * @param array $pagevars from {@link question_edit_setup()}.
 424   * @return int the category id.
 425   */
 426  function question_get_category_id_from_pagevars(array $pagevars) {
 427      list($questioncategoryid) = explode(',', $pagevars['cat']);
 428      return $questioncategoryid;
 429  }
 430  
 431  /**
 432   * Get a particular question preference that is also stored as a user preference.
 433   * If the the value is given in the GET/POST request, then that value is used,
 434   * and the user preference is updated to that value. Otherwise, the last set
 435   * value of the user preference is used, or if it has never been set the default
 436   * passed to this function.
 437   *
 438   * @param string $param the param name. The URL parameter set, and the GET/POST
 439   *      parameter read. The user_preference name is 'question_bank_' . $param.
 440   * @param mixed $default The default value to use, if not otherwise set.
 441   * @param int $type one of the PARAM_... constants.
 442   * @param moodle_url $thispageurl if the value has been explicitly set, we add
 443   *      it to this URL.
 444   * @return mixed the parameter value to use.
 445   */
 446  function question_get_display_preference($param, $default, $type, $thispageurl) {
 447      $submittedvalue = optional_param($param, null, $type);
 448      return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
 449  }
 450  
 451  /**
 452   * Get a user preference by name or set the user preference to a given value.
 453   *
 454   * If $value is null then the function will only attempt to retrieve the
 455   * user preference requested by $name. If no user preference is found then the
 456   * $default value will be returned. In this case the user preferences are not
 457   * modified and nor are the params on $thispageurl.
 458   *
 459   * If $value is anything other than null then the function will set the user
 460   * preference $name to the provided $value and will also set it as a param
 461   * on $thispageurl.
 462   *
 463   * @param string $name The user_preference name is 'question_bank_' . $name.
 464   * @param mixed $value The preference value.
 465   * @param mixed $default The default value to use, if not otherwise set.
 466   * @param moodle_url $thispageurl if the value has been explicitly set, we add
 467   *      it to this URL.
 468   * @return mixed the parameter value to use.
 469   */
 470  function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
 471      if (is_null($value)) {
 472          return get_user_preferences('question_bank_' . $name, $default);
 473      }
 474  
 475      set_user_preference('question_bank_' . $name, $value);
 476      $thispageurl->param($name, $value);
 477      return $value;
 478  }
 479  
 480  /**
 481   * Make sure user is logged in as required in this context.
 482   */
 483  function require_login_in_context($contextorid = null){
 484      global $DB, $CFG;
 485      if (!is_object($contextorid)){
 486          $context = context::instance_by_id($contextorid, IGNORE_MISSING);
 487      } else {
 488          $context = $contextorid;
 489      }
 490      if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
 491          require_login($context->instanceid);
 492      } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
 493          if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
 494              if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
 495                  throw new \moodle_exception('invalidcourseid');
 496              }
 497              require_course_login($course, true, $cm);
 498  
 499          } else {
 500              throw new \moodle_exception('invalidcoursemodule');
 501          }
 502      } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
 503          if (!empty($CFG->forcelogin)) {
 504              require_login();
 505          }
 506  
 507      } else {
 508          require_login();
 509      }
 510  }
 511  
 512  /**
 513   * Print a form to let the user choose which question type to add.
 514   * When the form is submitted, it goes to the question.php script.
 515   * @param $hiddenparams hidden parameters to add to the form, in addition to
 516   *      the qtype radio buttons.
 517   * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
 518   *      those qtypes will be shown. Example value array('description', 'multichoice').
 519   * @deprecated since Moodle 4.0
 520   * @see \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form()
 521   * @todo Final deprecation of this class in moodle 4.4 MDL-72438
 522   */
 523  function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
 524      debugging('Function print_choose_qtype_to_add_form() is deprecated,
 525       please use \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form() instead.', DEBUG_DEVELOPER);
 526      global $CFG, $PAGE, $OUTPUT;
 527  
 528      $chooser = \qbank_editquestion\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
 529      $renderer = $PAGE->get_renderer('question', 'bank');
 530  
 531      return $renderer->render($chooser);
 532  }
 533  
 534  /**
 535   * Print a button for creating a new question. This will open question/addquestion.php,
 536   * which in turn goes to question/question.php before getting back to $params['returnurl']
 537   * (by default the question bank screen).
 538   *
 539   * @param int $categoryid The id of the category that the new question should be added to.
 540   * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
 541   *      $params['courseid'], and you should probably set $params['returnurl']
 542   * @param string $caption the text to display on the button.
 543   * @param string $tooltip a tooltip to add to the button (optional).
 544   * @param bool $disabled if true, the button will be disabled.
 545   * @deprecated since Moodle 4.0
 546   * @see \qbank_editquestion\editquestion_helper::create_new_question_button()
 547   * @todo Final deprecation of this class in moodle 4.4 MDL-72438
 548   */
 549  function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
 550      debugging('Function create_new_question_button() has been deprecated and moved to bank/editquestion,
 551       please use qbank_editquestion\editquestion_helper::create_new_question_button() instead.', DEBUG_DEVELOPER);
 552      global $CFG, $PAGE, $OUTPUT;
 553      static $choiceformprinted = false;
 554      $params['category'] = $categoryid;
 555      $url = new moodle_url('/question/bank/editquestion/addquestion.php', $params);
 556      echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
 557  
 558      if (!$choiceformprinted) {
 559          echo '<div id="qtypechoicecontainer">';
 560          echo print_choose_qtype_to_add_form(array());
 561          echo "</div>\n";
 562          $choiceformprinted = true;
 563      }
 564  }