Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 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  namespace qbank_viewquestiontext;
  18  
  19  use core_question\local\bank\row_base;
  20  use question_utils;
  21  
  22  /**
  23   * A column type for the name of the question name.
  24   *
  25   * @package   qbank_viewquestiontext
  26   * @copyright 2009 Tim Hunt
  27   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class question_text_row extends row_base {
  31  
  32      /** @var bool if true, we will show the question text reduced to plain text, else it is fully rendered. */
  33      protected $plain;
  34  
  35      /** @var \stdClass $formatoptions options used when displaying the question text as HTML. */
  36      protected $formatoptions;
  37  
  38      protected function init(): void {
  39  
  40          // Cannot use $this->get_preference because of PHP type hints.
  41          $preference = question_get_display_preference($this->get_preference_key(), 0, PARAM_INT, new \moodle_url(''));
  42          $this->plain = 1 === (int) $preference;
  43          $this->formatoptions = new \stdClass();
  44          $this->formatoptions->noclean = true;
  45          $this->formatoptions->para = false;
  46      }
  47  
  48      public function get_name(): string {
  49          return 'questiontext';
  50      }
  51  
  52      public function get_title(): string {
  53          return get_string('questiontext', 'question');
  54      }
  55  
  56      protected function display_content($question, $rowclasses): void {
  57          if ($this->plain) {
  58              $text = question_utils::to_plain_text($question->questiontext,
  59                      $question->questiontextformat, ['noclean' => true, 'para' => false, 'filter' => false]);
  60          } else {
  61              $text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
  62                      $question->contextid, 'question', 'questiontext', $question->id,
  63                      $question->contextid, 'core_question');
  64              $text = format_text($text, $question->questiontextformat,
  65                      $this->formatoptions);
  66          }
  67          if ($text == '') {
  68              $text = '&#160;';
  69          }
  70          echo $text;
  71      }
  72  
  73      public function get_required_fields(): array {
  74          return ['q.questiontext', 'q.questiontextformat'];
  75      }
  76  
  77      public function has_preference(): bool {
  78          return true;
  79      }
  80  
  81      public function get_preference_key(): string {
  82          return 'qbshowtext';
  83      }
  84  
  85      public function get_preference(): bool {
  86          return question_get_display_preference($this->get_preference_key(), 0, PARAM_BOOL, new \moodle_url(''));
  87      }
  88  }