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.
   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 data generator.
  19   *
  20   * @package    core_customfield
  21   * @category   test
  22   * @copyright  2018 Ruslan Kabalin
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use \core_customfield\category_controller;
  29  use \core_customfield\field_controller;
  30  use \core_customfield\api;
  31  
  32  /**
  33   * Customfield data generator class.
  34   *
  35   * @package    core_customfield
  36   * @category   test
  37   * @copyright  2018 Ruslan Kabalin
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class core_customfield_generator extends component_generator_base {
  41  
  42      /** @var int Number of created categories. */
  43      protected $categorycount = 0;
  44  
  45      /** @var int Number of created fields. */
  46      protected $fieldcount = 0;
  47  
  48      /**
  49       * Create a new category.
  50       *
  51       * @param array|stdClass $record
  52       * @return category_controller
  53       */
  54      public function create_category($record = null) {
  55          $this->categorycount++;
  56          $i = $this->categorycount;
  57          $record = (object) $record;
  58  
  59          if (!isset($record->name)) {
  60              $record->name = "Category $i";
  61          }
  62          if (!isset($record->component)) {
  63              $record->component = 'core_course';
  64          }
  65          if (!isset($record->area)) {
  66              $record->area = 'course';
  67          }
  68          if (!isset($record->itemid)) {
  69              $record->itemid = 0;
  70          }
  71  
  72          $handler = \core_customfield\handler::get_handler($record->component, $record->area, $record->itemid);
  73          $categoryid = $handler->create_category($record->name);
  74          return $handler->get_categories_with_fields()[$categoryid];
  75      }
  76  
  77      /**
  78       * Create a new field.
  79       *
  80       * @param array|stdClass $record
  81       * @return field_controller
  82       */
  83      public function create_field($record) : field_controller {
  84          $this->fieldcount++;
  85          $i = $this->fieldcount;
  86          $record = (object) $record;
  87  
  88          if (empty($record->categoryid)) {
  89              throw new coding_exception('The categoryid value is required.');
  90          }
  91          $category = category_controller::create($record->categoryid);
  92          $handler = $category->get_handler();
  93  
  94          if (!isset($record->name)) {
  95              $record->name = "Field $i";
  96          }
  97          if (!isset($record->shortname)) {
  98              $record->shortname = "fld$i";
  99          }
 100          if (!isset($record->description)) {
 101              $record->description = "Field $i description";
 102          }
 103          if (!isset($record->descriptionformat)) {
 104              $record->descriptionformat = FORMAT_HTML;
 105          }
 106          if (!isset($record->type)) {
 107              $record->type = 'text';
 108          }
 109          if (!isset($record->sortorder)) {
 110              $record->sortorder = 0;
 111          }
 112  
 113          if (empty($record->configdata)) {
 114              $configdata = [];
 115          } else if (is_array($record->configdata)) {
 116              $configdata = $record->configdata;
 117          } else {
 118              $configdata = @json_decode($record->configdata, true);
 119              $configdata = $configdata ?? [];
 120          }
 121          $configdata += [
 122              'required' => 0,
 123              'uniquevalues' => 0,
 124              'locked' => 0,
 125              'visibility' => 2,
 126              'defaultvalue' => '',
 127              'displaysize' => 0,
 128              'maxlength' => 0,
 129              'ispassword' => 0,
 130              'link' => '',
 131              'linktarget' => '',
 132              'checkbydefault' => 0,
 133              'startyear' => 2000,
 134              'endyear' => 3000,
 135              'includetime' => 1,
 136          ];
 137          $record->configdata = json_encode($configdata);
 138  
 139          $field = field_controller::create(0, (object)['type' => $record->type], $category);
 140          $handler->save_field_configuration($field, $record);
 141          return $handler->get_categories_with_fields()[$field->get('categoryid')]->get_fields()[$field->get('id')];
 142      }
 143  
 144      /**
 145       * Adds instance data for one field
 146       *
 147       * @param field_controller $field
 148       * @param int $instanceid
 149       * @param mixed $value
 150       * @return \core_customfield\data_controller
 151       */
 152      public function add_instance_data(field_controller $field, int $instanceid, $value) : \core_customfield\data_controller {
 153          $data = \core_customfield\data_controller::create(0, (object)['instanceid' => $instanceid], $field);
 154          $data->set('contextid', $data->get_context()->id);
 155  
 156          $rc = new ReflectionClass(get_class($data));
 157          $rcm = $rc->getMethod('get_form_element_name');
 158          $rcm->setAccessible(true);
 159          $formelementname = $rcm->invokeArgs($data, []);
 160          $record = (object)[$formelementname => $value];
 161          $data->instance_form_save($record);
 162          return $data;
 163      }
 164  }