PageRenderTime 71ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

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

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