Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  namespace qbank_editquestion\output;
  18  
  19  use qbank_editquestion\editquestion_helper;
  20  use renderer_base;
  21  
  22  /**
  23   * Create new question button
  24   *
  25   * @package   qbank_editquestion
  26   * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  27   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class add_new_question implements \renderable, \templatable {
  31      /** @var int $categoryid The ID of the category the quesiton will be added to. */
  32      protected int $categoryid;
  33  
  34      /** @var array $params URL parameters to pass to the add question form. */
  35      protected array $params;
  36  
  37      /** @var bool $canadd True if the add question button should be displayed. If false, a placeholder will be shown.*/
  38      protected bool $canadd;
  39  
  40      /**
  41       * Store data for building the template context.
  42       *
  43       * @param int $categoryid
  44       * @param array $params
  45       * @param bool $canadd
  46       */
  47      public function __construct(int $categoryid, array $params, bool $canadd) {
  48          $this->categoryid = $categoryid;
  49          $this->params = $params;
  50          $this->canadd = $canadd;
  51      }
  52  
  53      public function export_for_template(renderer_base $output): array {
  54          $addquestiondisplay = [];
  55          $addquestiondisplay['canadd'] = $this->canadd;
  56          if ($this->canadd) {
  57              $this->params['category'] = $this->categoryid;
  58              $url = new \moodle_url('/question/bank/editquestion/addquestion.php', $this->params);
  59              $addquestiondisplay['buttonhtml'] = $output->single_button(
  60                  $url,
  61                  get_string('createnewquestion', 'question'),
  62                  'get'
  63              );
  64              $addquestiondisplay['qtypeform'] = editquestion_helper::print_choose_qtype_to_add_form([]);
  65          }
  66          return $addquestiondisplay;
  67      }
  68  }