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 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   * Helper class for adding/editing a question.
  19   *
  20   * This code is based on question/editlib.php by Martin Dougiamas.
  21   *
  22   * @package    qbank_editquestion
  23   * @copyright  2021 Catalyst IT Australia Pty Ltd
  24   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  namespace qbank_editquestion;
  29  
  30  use core_question\local\bank\question_version_status;
  31  
  32  /**
  33   * Class editquestion_helper for methods related to add/edit/copy
  34   *
  35   * @package    qbank_editquestion
  36   * @copyright  2021 Catalyst IT Australia Pty Ltd
  37   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  38   */
  39  class editquestion_helper {
  40  
  41      /**
  42       * Print a form to let the user choose which question type to add.
  43       * When the form is submitted, it goes to the question.php script.
  44       *
  45       * @param array|null $hiddenparams hidden parameters to add to the form, in addition to
  46       *      the qtype radio buttons.
  47       * @param array|null $allowedqtypes optional list of qtypes that are allowed. If given, only
  48       *      those qtypes will be shown. Example value array('description', 'multichoice').
  49       * @param bool $enablejs
  50       * @return bool|string
  51       */
  52      public static function print_choose_qtype_to_add_form(array $hiddenparams, array $allowedqtypes = null, $enablejs = true) {
  53          global $PAGE;
  54  
  55          $chooser = \qbank_editquestion\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
  56          $renderer = $PAGE->get_renderer('qbank_editquestion');
  57  
  58          return $renderer->render($chooser);
  59      }
  60  
  61      /**
  62       * Print a button for creating a new question. This will open question/addquestion.php,
  63       * which in turn goes to question/question.php before getting back to $params['returnurl']
  64       * (by default the question bank screen).
  65       *
  66       * @param int $categoryid The id of the category that the new question should be added to.
  67       * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
  68       *      $params['courseid'], and you should probably set $params['returnurl']
  69       * @param bool $canadd the text to display on the button.
  70       * @param string $tooltip a tooltip to add to the button (optional).
  71       * @param bool $disabled if true, the button will be disabled.
  72       */
  73      public static function create_new_question_button($categoryid, $params, $canadd, $tooltip = '', $disabled = false) {
  74          global $PAGE, $OUTPUT;
  75  
  76          $addquestiondisplay = array();
  77          $addquestiondisplay['canadd'] = $canadd;
  78          if ($canadd) {
  79              $params['category'] = $categoryid;
  80              $url = new \moodle_url('/question/bank/editquestion/addquestion.php', $params);
  81              $buttonparams = ['disabled' => $disabled];
  82              if (!empty($tooltip)) {
  83                  $buttonparams['title'] = $tooltip;
  84              }
  85              $addquestiondisplay['buttonhtml'] = $OUTPUT->single_button($url,
  86                      get_string('createnewquestion', 'question'),
  87                      'get', $buttonparams);
  88              $addquestiondisplay['qtypeform'] = self::print_choose_qtype_to_add_form(array());
  89          }
  90          return $PAGE->get_renderer('qbank_editquestion')->render_create_new_question_button($addquestiondisplay);
  91      }
  92  
  93      /**
  94       * Get the string for the status of the question.
  95       *
  96       * @param string $status
  97       * @return string
  98       */
  99      public static function get_question_status_string($status): string {
 100          return get_string('questionstatus' . $status, 'qbank_editquestion');
 101      }
 102  
 103      /**
 104       * Get the array of status of the questions.
 105       *
 106       * @return array
 107       */
 108      public static function get_question_status_list(): array {
 109          $statuslist = [];
 110          $statuslist[question_version_status::QUESTION_STATUS_READY] = get_string('questionstatusready', 'qbank_editquestion');
 111          $statuslist[question_version_status::QUESTION_STATUS_DRAFT] = get_string('questionstatusdraft', 'qbank_editquestion');
 112          return $statuslist;
 113      }
 114  
 115  }