Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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   * This file contains the form add/update a competency framework.
  19   *
  20   * @package   tool_lp
  21   * @copyright 2015 Damyon Wiese
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_lp\form;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use stdClass;
  29  use core\form\persistent;
  30  
  31  /**
  32   * Competency framework form.
  33   *
  34   * @package   tool_lp
  35   * @copyright 2015 Damyon Wiese
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class competency_framework extends persistent {
  39  
  40      protected static $persistentclass = 'core_competency\\competency_framework';
  41  
  42      /**
  43       * Define the form - called by parent constructor
  44       */
  45      public function definition() {
  46          global $PAGE;
  47  
  48          $mform = $this->_form;
  49          $context = $this->_customdata['context'];
  50          $framework = $this->get_persistent();
  51  
  52          $mform->addElement('hidden', 'contextid');
  53          $mform->setType('contextid', PARAM_INT);
  54          $mform->setConstant('contextid', $context->id);
  55  
  56          $mform->addElement('header', 'generalhdr', get_string('general'));
  57  
  58          // Name.
  59          $mform->addElement('text', 'shortname', get_string('shortname', 'tool_lp'), 'maxlength="100"');
  60          $mform->setType('shortname', PARAM_TEXT);
  61          $mform->addRule('shortname', null, 'required', null, 'client');
  62          $mform->addRule('shortname', get_string('maximumchars', '', 100), 'maxlength', 100, 'client');
  63          // Description.
  64          $mform->addElement('editor', 'description',
  65                             get_string('description', 'tool_lp'), array('rows' => 4));
  66          $mform->setType('description', PARAM_CLEANHTML);
  67          // ID number.
  68          $mform->addElement('text', 'idnumber', get_string('idnumber', 'tool_lp'), 'maxlength="100"');
  69          $mform->setType('idnumber', PARAM_RAW);
  70          $mform->addRule('idnumber', null, 'required', null, 'client');
  71          $mform->addRule('idnumber', get_string('maximumchars', '', 100), 'maxlength', 100, 'client');
  72  
  73          $scales = get_scales_menu();
  74          $scaleid = $mform->addElement('select', 'scaleid', get_string('scale', 'tool_lp'), $scales);
  75          $mform->setType('scaleid', PARAM_INT);
  76          $mform->addHelpButton('scaleid', 'scale', 'tool_lp');
  77          $mform->addRule('scaleid', null, 'required', null, 'client');
  78          if ($framework && $framework->has_user_competencies()) {
  79              // The scale is used so we "freeze" the element. Though, the javascript code for the scale
  80              // configuration requires this field so we only disable it. It is fine as setting the value
  81              // as a constant will ensure that nobody can change it. And it's validated in the persistent anyway.
  82              $scaleid->updateAttributes(array('readonly' => 'readonly'));
  83              $mform->setConstant('scaleid', $framework->get('scaleid'));
  84          }
  85  
  86          $mform->addElement('button', 'scaleconfigbutton', get_string('configurescale', 'tool_lp'));
  87          // Add js.
  88          $mform->addElement('hidden', 'scaleconfiguration', '', array('id' => 'tool_lp_scaleconfiguration'));
  89          $mform->setType('scaleconfiguration', PARAM_RAW);
  90          $PAGE->requires->js_call_amd('tool_lp/scaleconfig', 'init', array('#id_scaleid',
  91              '#tool_lp_scaleconfiguration', '#id_scaleconfigbutton'));
  92  
  93          $mform->addElement('selectyesno', 'visible',
  94                             get_string('visible', 'tool_lp'));
  95          $mform->setDefault('visible', true);
  96          $mform->addHelpButton('visible', 'visible', 'tool_lp');
  97  
  98          $mform->addElement('static', 'context', get_string('category', 'tool_lp'));
  99          $mform->setDefault('context', $context->get_context_name(false));
 100  
 101          $mform->addElement('header', 'taxonomyhdr', get_string('taxonomies', 'tool_lp'));
 102          $taxonomies = \core_competency\competency_framework::get_taxonomies_list();
 103          $taxdefaults = array();
 104          $taxcount = max($framework ? $framework->get_depth() : 4, 4);
 105          for ($i = 1; $i <= $taxcount; $i++) {
 106              $mform->addElement('select', "taxonomies[$i]", get_string('levela', 'tool_lp', $i), $taxonomies);
 107              $taxdefaults[$i] = \core_competency\competency_framework::TAXONOMY_COMPETENCY;
 108          }
 109          // Not using taxonomies[n] here or it would takes precedence over set_data(array('taxonomies' => ...)).
 110          $mform->setDefault('taxonomies', $taxdefaults);
 111  
 112          $this->add_action_buttons(true, get_string('savechanges', 'tool_lp'));
 113      }
 114  
 115      /**
 116       * Convert some fields.
 117       *
 118       * @param stdClass $data
 119       * @return object
 120       */
 121      protected static function convert_fields(stdClass $data) {
 122          $data = parent::convert_fields($data);
 123          $data->taxonomies = implode(',', $data->taxonomies);
 124          return $data;
 125      }
 126  
 127      /**
 128       * Extra validation.
 129       *
 130       * @param  stdClass $data Data to validate.
 131       * @param  array $files Array of files.
 132       * @param  array $errors Currently reported errors.
 133       * @return array of additional errors, or overridden errors.
 134       */
 135      protected function extra_validation($data, $files, array &$errors) {
 136          $newerrors = array();
 137          // Move the error from scaleconfiguration to the form element scale ID.
 138          if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) {
 139              $newerrors['scaleid'] = $errors['scaleconfiguration'];
 140              unset($errors['scaleconfiguration']);
 141          }
 142          return $newerrors;
 143      }
 144  
 145      /**
 146       * Get the default data.
 147       *
 148       * @return stdClass
 149       */
 150      protected function get_default_data() {
 151          $data = parent::get_default_data();
 152          $data->taxonomies = $this->get_persistent()->get('taxonomies');
 153          return $data;
 154      }
 155  
 156  }
 157