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.
   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   * The qbank_chooser renderable.
  19   *
  20   * @package    qbank_editquestion
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace qbank_editquestion;
  26  
  27  use context;
  28  use context_course;
  29  use core\output\chooser_section;
  30  use lang_string;
  31  use moodle_url;
  32  use question_bank;
  33  use stdClass;
  34  
  35  /**
  36   * The qbank_chooser renderable class.
  37   *
  38   * @package    qbank_editquestion
  39   * @copyright  2016 Frédéric Massart - FMCorz.net
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class qbank_chooser extends \core\output\chooser {
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param array $real The real question types.
  48       * @param array $fake The fake question types.
  49       * @param stdClass $course The course.
  50       * @param array $hiddenparams Hidden parameters.
  51       * @param context $context The relevant context.
  52       */
  53      public function __construct($real, $fake, $course, $hiddenparams, $context) {
  54          $sections = [];
  55          $sections[] = new chooser_section('questions', new lang_string('questions', 'question'),
  56                  array_map(function($qtype) use ($context) {
  57                      return new qbank_chooser_item($qtype, $context);
  58                  }, $real));
  59  
  60          if (!empty($fake)) {
  61              $sections[] = new chooser_section('other', new lang_string('other'),
  62                  array_map(function ($qtype) use ($context) {
  63                      return new qbank_chooser_item($qtype, $context);
  64                  }, $fake));
  65          }
  66  
  67          parent::__construct(new moodle_url('/question/bank/editquestion/question.php'),
  68                  new lang_string('chooseqtypetoadd', 'question'), $sections, 'qtype');
  69  
  70          $this->set_instructions(new lang_string('selectaqtypefordescription', 'question'));
  71  
  72          $this->set_method('get');
  73          $this->add_param('courseid', $course->id);
  74          foreach ($hiddenparams as $k => $v) {
  75              $this->add_param($k, $v);
  76          }
  77      }
  78  
  79      /**
  80       * Get an instance of the question bank chooser.
  81       *
  82       * @param stdClass $course The course.
  83       * @param array $hiddenparams Hidden parameters.
  84       * @param array|null $allowedqtypes Allowed question types.
  85       * @return qbank_chooser
  86       */
  87      public static function get($course, $hiddenparams, array $allowedqtypes = null): qbank_chooser {
  88          $realqtypes = array();
  89          $fakeqtypes = array();
  90  
  91          foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) {
  92              if ($allowedqtypes && !in_array($qtypename, $allowedqtypes)) {
  93                  continue;
  94              }
  95              if ($qtype->is_real_question_type()) {
  96                  $realqtypes[] = $qtype;
  97              } else {
  98                  $fakeqtypes[] = $qtype;
  99              }
 100          }
 101  
 102          return new static($realqtypes, $fakeqtypes, $course, $hiddenparams, context_course::instance($course->id));
 103      }
 104  
 105  }