3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * The form used at the rubric editor page is defined here
21 * @package gradingform
23 * @copyright 2011 Marina Glancy <marina@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot.'/lib/formslib.php');
30 require_once(dirname(__FILE__).'/rubriceditor.php');
31 MoodleQuickForm::registerElementType('rubriceditor', $CFG->dirroot.'/grade/grading/form/rubric/rubriceditor.php', 'MoodleQuickForm_rubriceditor');
34 * Defines the rubric edit form
36 class gradingform_rubric_editrubric extends moodleform {
39 * Form element definition
41 public function definition() {
44 $form->addElement('hidden', 'areaid');
45 $form->setType('areaid', PARAM_INT);
47 $form->addElement('hidden', 'returnurl');
50 $form->addElement('text', 'name', get_string('name', 'gradingform_rubric'), array('size'=>52));
51 $form->addRule('name', get_string('required'), 'required');
52 $form->setType('name', PARAM_TEXT);
55 $options = gradingform_rubric_controller::description_form_field_options($this->_customdata['context']);
56 $form->addElement('editor', 'description_editor', get_string('description', 'gradingform_rubric'), null, $options);
57 $form->setType('description_editor', PARAM_RAW);
59 // rubric completion status
61 $choices[gradingform_controller::DEFINITION_STATUS_DRAFT] = get_string('statusdraft', 'grading');
62 $choices[gradingform_controller::DEFINITION_STATUS_READY] = get_string('statusready', 'grading');
63 $form->addElement('select', 'status', get_string('rubricstatus', 'gradingform_rubric'), $choices)->freeze();
66 $element = $form->addElement('rubriceditor', 'rubric', get_string('rubric', 'gradingform_rubric'));
67 $form->setType('rubric', PARAM_RAW);
68 //$element->freeze(); // TODO freeze rubric editor if needed
70 $buttonarray = array();
71 $buttonarray[] = &$form->createElement('submit', 'saverubric', get_string('saverubric', 'gradingform_rubric'));
72 if ($this->_customdata['allowdraft']) {
73 $buttonarray[] = &$form->createElement('submit', 'saverubricdraft', get_string('saverubricdraft', 'gradingform_rubric'));
75 $editbutton = &$form->createElement('submit', 'editrubric', ' ');
76 $editbutton->freeze();
77 $buttonarray[] = &$editbutton;
78 $buttonarray[] = &$form->createElement('cancel');
79 $form->addGroup($buttonarray, 'buttonar', '', array(' '), false);
80 $form->closeHeaderBefore('buttonar');
85 * If there are errors return array of errors ("fieldname"=>"error message"),
86 * otherwise true if ok.
88 * @param array $data array of ("fieldname"=>value) of submitted data
89 * @param array $files array of uploaded files "element_name"=>tmp_file_path
90 * @return array of "element_name"=>"error_description" if there are errors,
91 * or an empty array if everything is OK (true allowed for backwards compatibility too).
93 public function validation($data, $files) {
94 $err = parent::validation($data, $files);
97 $rubricel = $form->getElement('rubric');
98 if ($rubricel->non_js_button_pressed($data['rubric'])) {
99 // if JS is disabled and button such as 'Add criterion' is pressed - prevent from submit
100 $err['rubricdummy'] = 1;
101 } else if (isset($data['editrubric'])) {
103 $err['rubricdummy'] = 1;
104 } else if (isset($data['saverubric']) && $data['saverubric']) {
105 // If user attempts to make rubric active - it needs to be validated
106 if ($rubricel->validate($data['rubric']) !== false) {
107 $err['rubricdummy'] = 1;
114 * Return submitted data if properly submitted or returns NULL if validation fails or
115 * if there is no submitted data.
117 * @return object submitted data; NULL if not valid or not submitted or cancelled
119 public function get_data() {
120 $data = parent::get_data();
121 if (!empty($data->saverubric)) {
122 $data->status = gradingform_controller::DEFINITION_STATUS_READY;
123 } else if (!empty($data->saverubricdraft)) {
124 $data->status = gradingform_controller::DEFINITION_STATUS_DRAFT;
130 * Check if there are changes in the rubric and it is needed to ask user whether to
131 * mark the current grades for re-grading. User may confirm re-grading and continue,
132 * return to editing or cancel the changes
134 * @param gradingform_rubric_controller $controller
136 public function need_confirm_regrading($controller) {
137 $data = $this->get_data();
138 if (isset($data->rubric['regrade'])) {
139 // we have already displayed the confirmation on the previous step
142 if (!isset($data->saverubric) || !$data->saverubric) {
143 // we only need confirmation when button 'Save rubric' is pressed
146 if (!$controller->has_active_instances()) {
147 // nothing to re-grade, confirmation not needed
150 $changelevel = $controller->update_or_check_rubric($data);
151 if ($changelevel == 0) {
152 // no changes in the rubric, no confirmation needed
156 // freeze form elements and pass the values in hidden fields
157 // TODO description_editor does not freeze the normal way!
158 $form = $this->_form;
159 foreach (array('rubric', 'name'/*, 'description_editor'*/) as $fieldname) {
160 $el =& $form->getElement($fieldname);
162 $el->setPersistantFreeze(true);
163 if ($fieldname == 'rubric') {
164 $el->add_regrade_confirmation($changelevel);
168 // replace button text 'saverubric' and unfreeze 'Back to edit' button
169 $this->findButton('saverubric')->setValue(get_string('continue'));
170 $el =& $this->findButton('editrubric');
171 $el->setValue(get_string('backtoediting', 'gradingform_rubric'));
178 * Returns a form element (submit button) with the name $elementname
180 * @param string $elementname
181 * @return HTML_QuickForm_element
183 protected function &findButton($elementname) {
184 $form = $this->_form;
185 $buttonar =& $form->getElement('buttonar');
186 $elements =& $buttonar->getElements();
187 foreach ($elements as $el) {
188 if ($el->getName() == $elementname) {