Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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_alignment(\action_menu::BL, \action_menu::BL);
 104              $menu->set_menu_trigger(get_string('createnewcustomfield', 'core_customfield'));
 105  
 106              foreach ($fieldtypes as $type => $fieldname) {
 107                  $action = new \action_menu_link_secondary(new \moodle_url('#'), null, $fieldname,
 108                      ['data-role' => 'addfield', 'data-categoryid' => $category->get('id'), 'data-type' => $type,
 109                          'data-typename' => $fieldname]);
 110                  $menu->add($action);
 111              }
 112              $menu->attributes['class'] .= ' float-left mr-1';
 113  
 114              $categoryarray['addfieldmenu'] = $output->render($menu);
 115  
 116              $categoriesarray[] = $categoryarray;
 117          }
 118  
 119          $data->categories = $categoriesarray;
 120  
 121          if (empty($data->categories)) {
 122              $data->nocategories = get_string('nocategories', 'core_customfield');
 123          }
 124  
 125          return $data;
 126      }
 127  }