PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/lp/classes/form/competency_framework.php

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