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

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

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