PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/grading/form/guide/edit_form.php

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