[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lp/classes/form/ -> competency_framework.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1