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.

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [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_customfields;
  18  
  19  use core_question\local\bank\column_base;
  20  use core_question\local\bank\view;
  21  use qbank_customfields\customfield\question_handler;
  22  
  23  /**
  24   * A column type for the name of the question creator.
  25   *
  26   * @package     qbank_customfields
  27   * @copyright   2021 Catalyst IT Australia Pty Ltd
  28   * @author      Matt Porritt <mattp@catalyst-au.net>
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class custom_field_column extends column_base {
  32  
  33      /** @var \core_customfield\field_controller The custom field this column is displaying. */
  34      protected $field;
  35  
  36      /**
  37       * Constructor.
  38       *
  39       * @param view $qbank the question bank view we are helping to render.
  40       * @param \core_customfield\field_controller $field The custom field this column is displaying.
  41       */
  42      public function __construct(\core_question\local\bank\view $qbank, \core_customfield\field_controller $field) {
  43          parent::__construct($qbank);
  44          $this->field = $field;
  45      }
  46  
  47      public static function from_column_name(view $view, string $columnname): custom_field_column {
  48          $handler = question_handler::create();
  49          foreach ($handler->get_fields() as $field) {
  50              if ($field->get('shortname') == $columnname) {
  51                  return new static($view, $field);
  52              }
  53          }
  54          throw new \coding_exception('Custom field ' . $columnname . ' does not exist.');
  55      }
  56  
  57      /**
  58       * Get the internal name for this column. Used as a CSS class name,
  59       * and to store information about the current sort. Must match PARAM_ALPHA.
  60       *
  61       * @return string column name.
  62       */
  63      public function get_name(): string {
  64          return 'customfield';
  65      }
  66  
  67      /**
  68       * Get the name of this column. This must be unique.
  69       * When using the inherited class to make many columns from one parent,
  70       * ensure each instance returns a unique value.
  71       *
  72       * @return string The unique name;
  73       */
  74      public function get_column_name(): string {
  75          return $this->field->get('shortname');
  76      }
  77  
  78      /**
  79       * Title for this column. Not used if is_sortable returns an array.
  80       *
  81       * @return string
  82       */
  83      public function get_title(): string {
  84          return $this->field->get_formatted_name();
  85      }
  86  
  87      /**
  88       * Output the contents of this column.
  89       *
  90       * @param object $question the row from the $question table, augmented with extra information.
  91       * @param string $rowclasses CSS class names that should be applied to this row of output.
  92       */
  93      protected function display_content($question, $rowclasses): void {
  94          $fieldhandler = $this->field->get_handler();
  95          if ($fieldhandler->can_view($this->field, $question->id)) {
  96              $fielddata = $fieldhandler->get_field_data($this->field, $question->id);
  97              echo $fieldhandler->display_custom_field_table($fielddata);
  98          } else {
  99              echo '';
 100          }
 101      }
 102  
 103      public function get_extra_classes(): array {
 104          return ['pr-3'];
 105      }
 106  
 107  }