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  /**
  18   *  core_customfield field value renderable.
  19   *
  20   * @package   core_customfield
  21   * @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_customfield\output;
  26  
  27  use core_customfield\data_controller;
  28  
  29  defined('MOODLE_INTERNAL') || die;
  30  
  31  /**
  32   * core_customfield field value renderable class.
  33   *
  34   * @package   core_customfield
  35   * @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class field_data implements \renderable, \templatable {
  39  
  40      /** @var \core_customfield\data_controller */
  41      protected $data;
  42  
  43      /**
  44       * Renderable constructor.
  45       *
  46       * @param \core_customfield\data_controller $data
  47       */
  48      public function __construct(\core_customfield\data_controller $data) {
  49          $this->data = $data;
  50      }
  51  
  52      /**
  53       * Returns the data value formatted for the output
  54       *
  55       * @return mixed|null
  56       */
  57      public function get_value() {
  58          return $this->data->export_value();
  59      }
  60  
  61      /**
  62       * Returns the field type (checkbox, date, text, ...)
  63       *
  64       * @return string
  65       */
  66      public function get_type() : string {
  67          return $this->data->get_field()->get('type');
  68      }
  69  
  70      /**
  71       * Returns the field short name
  72       *
  73       * @return string
  74       */
  75      public function get_shortname() : string {
  76          return $this->data->get_field()->get('shortname');
  77      }
  78  
  79      /**
  80       * Returns the field name formatted for the output
  81       *
  82       * @return string
  83       */
  84      public function get_name() : string {
  85          return $this->data->get_field()->get_formatted_name();
  86      }
  87  
  88      /**
  89       * Returns the data controller used to create this object if additional attributes are needed
  90       *
  91       * @return data_controller
  92       */
  93      public function get_data_controller() : data_controller {
  94          return $this->data;
  95      }
  96  
  97      /**
  98       * Export data for using as template context.
  99       *
 100       * @param \renderer_base $output
 101       * @return \stdClass
 102       */
 103      public function export_for_template(\renderer_base $output) {
 104          $value = $this->get_value();
 105          return (object)[
 106              'value' => $value,
 107              'type' => $this->get_type(),
 108              'shortname' => $this->get_shortname(),
 109              'name' => $this->get_name(),
 110              'hasvalue' => ($value !== null),
 111              'instanceid' => $this->data->get('instanceid')
 112          ];
 113      }
 114  }