Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  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                  $fieldarray['editfieldurl'] = (new \moodle_url('/customfield/edit.php', [
 100                          'id' => $fieldarray['id'],
 101                  ]))->out(false);
 102  
 103                  $categoryarray['fields'][] = $fieldarray;
 104              }
 105  
 106              $menu = new \action_menu();
 107              $menu->set_alignment(\action_menu::BL, \action_menu::BL);
 108              $menu->set_menu_trigger(get_string('createnewcustomfield', 'core_customfield'));
 109  
 110              $baseaddfieldurl = new \moodle_url('/customfield/edit.php',
 111                      array('action' => 'editfield', 'categoryid' => $category->get('id')));
 112              foreach ($fieldtypes as $type => $fieldname) {
 113                  $addfieldurl = new \moodle_url($baseaddfieldurl, array('type' => $type));
 114                  $action = new \action_menu_link_secondary($addfieldurl, null, $fieldname);
 115                  $menu->add($action);
 116              }
 117              $menu->attributes['class'] .= ' float-left mr-1';
 118  
 119              $categoryarray['addfieldmenu'] = $output->render($menu);
 120  
 121              $categoriesarray[] = $categoryarray;
 122          }
 123  
 124          $data->categories = $categoriesarray;
 125  
 126          if (empty($data->categories)) {
 127              $data->nocategories = get_string('nocategories', 'core_customfield');
 128          }
 129  
 130          return $data;
 131      }
 132  }