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 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * Customfield component output.
  19   *
  20   * @package   core_customfield
  21   * @copyright 2018 David Matamoros <davidmc@moodle.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\api;
  28  use core_customfield\handler;
  29  use renderable;
  30  use templatable;
  31  
  32  defined('MOODLE_INTERNAL') || die;
  33  
  34  /**
  35   * Class management
  36   *
  37   * @package core_customfield
  38   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class management implements renderable, templatable {
  42  
  43      /**
  44       * @var handler
  45       */
  46      protected $handler;
  47      /**
  48       * @var
  49       */
  50      protected $categoryid;
  51  
  52      /**
  53       * management constructor.
  54       *
  55       * @param \core_customfield\handler $handler
  56       */
  57      public function __construct(\core_customfield\handler $handler) {
  58          $this->handler = $handler;
  59      }
  60  
  61      /**
  62       * Export for template
  63       *
  64       * @param \renderer_base $output
  65       * @return array|object|\stdClass
  66       */
  67      public function export_for_template(\renderer_base $output) {
  68          $data = new \stdClass();
  69  
  70          $fieldtypes = $this->handler->get_available_field_types();
  71  
  72          $data->component = $this->handler->get_component();
  73          $data->area = $this->handler->get_area();
  74          $data->itemid = $this->handler->get_itemid();
  75          $data->usescategories = $this->handler->uses_categories();
  76          $categories = $this->handler->get_categories_with_fields();
  77  
  78          $categoriesarray = array();
  79  
  80          foreach ($categories as $category) {
  81  
  82              $categoryarray = array();
  83              $categoryarray['id'] = $category->get('id');
  84              $categoryarray['nameeditable'] = $output->render(api::get_category_inplace_editable($category, true));
  85              $categoryarray['movetitle'] = get_string('movecategory', 'core_customfield',
  86                  $category->get_formatted_name());
  87  
  88              $categoryarray['fields'] = array();
  89  
  90              foreach ($category->get_fields() as $field) {
  91  
  92                  $fieldname = $field->get_formatted_name();
  93                  $fieldarray['type'] = $fieldtypes[$field->get('type')];
  94                  $fieldarray['id'] = $field->get('id');
  95                  $fieldarray['name'] = $fieldname;
  96                  $fieldarray['shortname'] = $field->get('shortname');
  97                  $fieldarray['movetitle'] = get_string('movefield', 'core_customfield', $fieldname);
  98  
  99                  $categoryarray['fields'][] = $fieldarray;
 100              }
 101  
 102              $menu = new \action_menu();
 103              $menu->set_menu_trigger(get_string('createnewcustomfield', 'core_customfield'));
 104  
 105              foreach ($fieldtypes as $type => $fieldname) {
 106                  $action = new \action_menu_link_secondary(new \moodle_url('#'), null, $fieldname,
 107                      ['data-role' => 'addfield', 'data-categoryid' => $category->get('id'), 'data-type' => $type,
 108                          'data-typename' => $fieldname]);
 109                  $menu->add($action);
 110              }
 111              $menu->attributes['class'] .= ' float-left mr-1';
 112  
 113              $categoryarray['addfieldmenu'] = $output->render($menu);
 114  
 115              $categoriesarray[] = $categoryarray;
 116          }
 117  
 118          $data->categories = $categoriesarray;
 119  
 120          if (empty($data->categories)) {
 121              $data->nocategories = get_string('nocategories', 'core_customfield');
 122          }
 123  
 124          return $data;
 125      }
 126  }