PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/grading/form/rubric/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 217 lines | 115 code | 20 blank | 82 comment | 26 complexity | b2b2502dfd4ee85d8a24010d3301f3e8 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. * The form used at the rubric editor page is defined here
  18. *
  19. * @package gradingform_rubric
  20. * @copyright 2011 Marina Glancy <marina@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->dirroot.'/lib/formslib.php');
  25. require_once(__DIR__.'/rubriceditor.php');
  26. MoodleQuickForm::registerElementType('rubriceditor', $CFG->dirroot.'/grade/grading/form/rubric/rubriceditor.php', 'MoodleQuickForm_rubriceditor');
  27. /**
  28. * Defines the rubric edit form
  29. *
  30. * @package gradingform_rubric
  31. * @copyright 2011 Marina Glancy <marina@moodle.com>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class gradingform_rubric_editrubric extends moodleform {
  35. /**
  36. * Form element definition
  37. */
  38. public function definition() {
  39. $form = $this->_form;
  40. $form->addElement('hidden', 'areaid');
  41. $form->setType('areaid', PARAM_INT);
  42. $form->addElement('hidden', 'returnurl');
  43. $form->setType('returnurl', PARAM_LOCALURL);
  44. // name
  45. $form->addElement('text', 'name', get_string('name', 'gradingform_rubric'), array('size' => 52, 'aria-required' => 'true'));
  46. $form->addRule('name', get_string('required'), 'required', null, 'client');
  47. $form->setType('name', PARAM_TEXT);
  48. // description
  49. $options = gradingform_rubric_controller::description_form_field_options($this->_customdata['context']);
  50. $form->addElement('editor', 'description_editor', get_string('description', 'gradingform_rubric'), null, $options);
  51. $form->setType('description_editor', PARAM_RAW);
  52. // rubric completion status
  53. $choices = array();
  54. $choices[gradingform_controller::DEFINITION_STATUS_DRAFT] = html_writer::tag('span', get_string('statusdraft', 'core_grading'), array('class' => 'status draft'));
  55. $choices[gradingform_controller::DEFINITION_STATUS_READY] = html_writer::tag('span', get_string('statusready', 'core_grading'), array('class' => 'status ready'));
  56. $form->addElement('select', 'status', get_string('rubricstatus', 'gradingform_rubric'), $choices)->freeze();
  57. // rubric editor
  58. $element = $form->addElement('rubriceditor', 'rubric', get_string('rubric', 'gradingform_rubric'));
  59. $form->setType('rubric', PARAM_RAW);
  60. $buttonarray = array();
  61. $buttonarray[] = &$form->createElement('submit', 'saverubric', get_string('saverubric', 'gradingform_rubric'));
  62. if ($this->_customdata['allowdraft']) {
  63. $buttonarray[] = &$form->createElement('submit', 'saverubricdraft', get_string('saverubricdraft', 'gradingform_rubric'));
  64. }
  65. $editbutton = &$form->createElement('submit', 'editrubric', ' ');
  66. $editbutton->freeze();
  67. $buttonarray[] = &$editbutton;
  68. $buttonarray[] = &$form->createElement('cancel');
  69. $form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  70. $form->closeHeaderBefore('buttonar');
  71. }
  72. /**
  73. * Setup the form depending on current values. This method is called after definition(),
  74. * data submission and set_data().
  75. * All form setup that is dependent on form values should go in here.
  76. *
  77. * We remove the element status if there is no current status (i.e. rubric is only being created)
  78. * so the users do not get confused
  79. */
  80. public function definition_after_data() {
  81. $form = $this->_form;
  82. $el = $form->getElement('status');
  83. if (!$el->getValue()) {
  84. $form->removeElement('status');
  85. } else {
  86. $vals = array_values($el->getValue());
  87. if ($vals[0] == gradingform_controller::DEFINITION_STATUS_READY) {
  88. $this->findButton('saverubric')->setValue(get_string('save', 'gradingform_rubric'));
  89. }
  90. }
  91. }
  92. /**
  93. * Form vlidation.
  94. * If there are errors return array of errors ("fieldname"=>"error message"),
  95. * otherwise true if ok.
  96. *
  97. * @param array $data array of ("fieldname"=>value) of submitted data
  98. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  99. * @return array of "element_name"=>"error_description" if there are errors,
  100. * or an empty array if everything is OK (true allowed for backwards compatibility too).
  101. */
  102. public function validation($data, $files) {
  103. $err = parent::validation($data, $files);
  104. $err = array();
  105. $form = $this->_form;
  106. $rubricel = $form->getElement('rubric');
  107. if ($rubricel->non_js_button_pressed($data['rubric'])) {
  108. // if JS is disabled and button such as 'Add criterion' is pressed - prevent from submit
  109. $err['rubricdummy'] = 1;
  110. } else if (isset($data['editrubric'])) {
  111. // continue editing
  112. $err['rubricdummy'] = 1;
  113. } else if (isset($data['saverubric']) && $data['saverubric']) {
  114. // If user attempts to make rubric active - it needs to be validated
  115. if ($rubricel->validate($data['rubric']) !== false) {
  116. $err['rubricdummy'] = 1;
  117. }
  118. }
  119. return $err;
  120. }
  121. /**
  122. * Return submitted data if properly submitted or returns NULL if validation fails or
  123. * if there is no submitted data.
  124. *
  125. * @return object submitted data; NULL if not valid or not submitted or cancelled
  126. */
  127. public function get_data() {
  128. $data = parent::get_data();
  129. if (!empty($data->saverubric)) {
  130. $data->status = gradingform_controller::DEFINITION_STATUS_READY;
  131. } else if (!empty($data->saverubricdraft)) {
  132. $data->status = gradingform_controller::DEFINITION_STATUS_DRAFT;
  133. }
  134. return $data;
  135. }
  136. /**
  137. * Check if there are changes in the rubric and it is needed to ask user whether to
  138. * mark the current grades for re-grading. User may confirm re-grading and continue,
  139. * return to editing or cancel the changes
  140. *
  141. * @param gradingform_rubric_controller $controller
  142. */
  143. public function need_confirm_regrading($controller) {
  144. $data = $this->get_data();
  145. if (isset($data->rubric['regrade'])) {
  146. // we have already displayed the confirmation on the previous step
  147. return false;
  148. }
  149. if (!isset($data->saverubric) || !$data->saverubric) {
  150. // we only need confirmation when button 'Save rubric' is pressed
  151. return false;
  152. }
  153. if (!$controller->has_active_instances()) {
  154. // nothing to re-grade, confirmation not needed
  155. return false;
  156. }
  157. $changelevel = $controller->update_or_check_rubric($data);
  158. if ($changelevel == 0) {
  159. // no changes in the rubric, no confirmation needed
  160. return false;
  161. }
  162. // freeze form elements and pass the values in hidden fields
  163. // TODO MDL-29421 description_editor does not freeze the normal way, uncomment below when fixed
  164. $form = $this->_form;
  165. foreach (array('rubric', 'name'/*, 'description_editor'*/) as $fieldname) {
  166. $el =& $form->getElement($fieldname);
  167. $el->freeze();
  168. $el->setPersistantFreeze(true);
  169. if ($fieldname == 'rubric') {
  170. $el->add_regrade_confirmation($changelevel);
  171. }
  172. }
  173. // replace button text 'saverubric' and unfreeze 'Back to edit' button
  174. $this->findButton('saverubric')->setValue(get_string('continue'));
  175. $el =& $this->findButton('editrubric');
  176. $el->setValue(get_string('backtoediting', 'gradingform_rubric'));
  177. $el->unfreeze();
  178. return true;
  179. }
  180. /**
  181. * Returns a form element (submit button) with the name $elementname
  182. *
  183. * @param string $elementname
  184. * @return HTML_QuickForm_element
  185. */
  186. protected function &findButton($elementname) {
  187. $form = $this->_form;
  188. $buttonar =& $form->getElement('buttonar');
  189. $elements =& $buttonar->getElements();
  190. foreach ($elements as $el) {
  191. if ($el->getName() == $elementname) {
  192. return $el;
  193. }
  194. }
  195. return null;
  196. }
  197. }