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 extends persistent {
  39  
  40      /** @var core_competency\competency persistent class for form */
  41      protected static $persistentclass = 'core_competency\\competency';
  42  
  43      /**
  44       * Define the form - called by parent constructor
  45       */
  46      public function definition() {
  47          global $PAGE, $OUTPUT;
  48  
  49          $mform = $this->_form;
  50          $framework = $this->_customdata['competencyframework'];
  51          $parent = $this->_customdata['parent'];
  52          $pagecontextid = $this->_customdata['pagecontextid'];
  53          $competency = $this->get_persistent();
  54  
  55          $mform->addElement('hidden', 'competencyframeworkid');
  56          $mform->setType('competencyframeworkid', PARAM_INT);
  57          $mform->setConstant('competencyframeworkid', $framework->get('id'));
  58  
  59          $mform->addElement('header', 'generalhdr', get_string('general'));
  60  
  61          $mform->addElement('static',
  62                             'frameworkdesc',
  63                             get_string('competencyframework', 'tool_lp'),
  64                             s($framework->get('shortname')));
  65  
  66          $mform->addElement('hidden', 'parentid', '', array('id' => 'tool_lp_parentcompetency'));
  67  
  68          $mform->setType('parentid', PARAM_INT);
  69          $mform->setConstant('parentid', ($parent) ? $parent->get('id') : 0);
  70          $parentlevel = ($parent) ? $parent->get_level() : 0;
  71          $parentname = ($parent) ? $parent->get('shortname') : get_string('competencyframeworkroot', 'tool_lp');
  72          $parentlabel = '';
  73          if (!empty($competency->get('id'))) {
  74              $parentlabel = get_string('taxonomy_parent_' . $framework->get_taxonomy($parentlevel), 'tool_lp');
  75          } else {
  76              $parentlabel = get_string('parentcompetency', 'tool_lp');
  77          }
  78          $editaction = '';
  79          if (!$competency->get('id')) {
  80              $icon = $OUTPUT->pix_icon('t/editinline', get_string('parentcompetency_edit', 'tool_lp'));
  81              $editaction = $OUTPUT->action_link('#', $icon, null, array('id' => 'id_parentcompetencybutton'));
  82          }
  83  
  84          $mform->addElement('static',
  85                             'parentdesc',
  86                             $parentlabel,
  87                             "<span id='id_parentdesc'>$parentname</span>&nbsp;".$editaction);
  88          // Set the picker competency when adding new competency.
  89          if (!$competency->get('id')) {
  90              // Call the parentcompetency_form init to initialize the competency picker for parent competency.
  91              $PAGE->requires->js_call_amd('tool_lp/parentcompetency_form', 'init', array('#id_parentcompetencybutton',
  92                  '#tool_lp_parentcompetency',
  93                  '#id_parentdesc',
  94                  $framework->get('id'),
  95                  $pagecontextid));
  96          }
  97  
  98          // Name.
  99          $mform->addElement('text', 'shortname', get_string('shortname', 'tool_lp'), 'maxlength="100"');
 100          $mform->setType('shortname', PARAM_TEXT);
 101          $mform->addRule('shortname', null, 'required', null, 'client');
 102          $mform->addRule('shortname', get_string('maximumchars', '', 100), 'maxlength', 100, 'client');
 103          // Description.
 104          $mform->addElement('editor', 'description',
 105                             get_string('description', 'tool_lp'), array('rows' => 4));
 106          $mform->setType('description', PARAM_CLEANHTML);
 107          // ID number.
 108          $mform->addElement('text', 'idnumber', get_string('idnumber', 'tool_lp'), 'maxlength="100"');
 109          $mform->setType('idnumber', PARAM_RAW);
 110          $mform->addRule('idnumber', null, 'required', null, 'client');
 111          $mform->addRule('idnumber', get_string('maximumchars', '', 100), 'maxlength', 100, 'client');
 112  
 113          $scales = array(null => get_string('inheritfromframework', 'tool_lp')) + get_scales_menu();
 114          $scaleid = $mform->addElement('select', 'scaleid', get_string('scale', 'tool_lp'), $scales);
 115          $mform->setType('scaleid', PARAM_INT);
 116          $mform->addHelpButton('scaleid', 'scale', 'tool_lp');
 117  
 118          $mform->addElement('hidden', 'scaleconfiguration', '', array('id' => 'tool_lp_scaleconfiguration'));
 119          $mform->setType('scaleconfiguration', PARAM_RAW);
 120  
 121          $mform->addElement('button', 'scaleconfigbutton', get_string('configurescale', 'tool_lp'));
 122          $PAGE->requires->js_call_amd('tool_lp/scaleconfig', 'init', array('#id_scaleid',
 123              '#tool_lp_scaleconfiguration', '#id_scaleconfigbutton'));
 124  
 125          if ($competency && $competency->has_user_competencies()) {
 126              // The scale is used so we "freeze" the element. Though, the javascript code for the scale
 127              // configuration requires this field so we only disable it. It is fine as setting the value
 128              // as a constant will ensure that nobody can change it. And it's validated in the persistent anyway.
 129              $scaleid->updateAttributes(array('disabled' => 'disabled'));
 130              $mform->setConstant('scaleid', $competency->get('scaleid'));
 131          }
 132  
 133          // Disable short forms.
 134          $mform->setDisableShortforms();
 135          $this->add_action_buttons(true, get_string('savechanges', 'tool_lp'));
 136      }
 137  
 138      /**
 139       * Convert some fields.
 140       *
 141       * @param stdClass $data
 142       * @return object
 143       */
 144      protected static function convert_fields(stdClass $data) {
 145          $data = parent::convert_fields($data);
 146          if (empty($data->scaleid)) {
 147              $data->scaleid = null;
 148              $data->scaleconfiguration = null;
 149          }
 150          return $data;
 151      }
 152  
 153      /**
 154       * Extra validation.
 155       *
 156       * @param  stdClass $data Data to validate.
 157       * @param  array $files Array of files.
 158       * @param  array $errors Currently reported errors.
 159       * @return array of additional errors, or overridden errors.
 160       */
 161      protected function extra_validation($data, $files, array &$errors) {
 162          $newerrors = array();
 163          // Move the error from scaleconfiguration to the form element scale ID.
 164          if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) {
 165              $newerrors['scaleid'] = $errors['scaleconfiguration'];
 166              unset($errors['scaleconfiguration']);
 167          }
 168          return $newerrors;
 169      }
 170  
 171  }