|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot.'/lib/formslib.php'); 00030 require_once(dirname(__FILE__).'/rubriceditor.php'); 00031 MoodleQuickForm::registerElementType('rubriceditor', $CFG->dirroot.'/grade/grading/form/rubric/rubriceditor.php', 'MoodleQuickForm_rubriceditor'); 00032 00036 class gradingform_rubric_editrubric extends moodleform { 00037 00041 public function definition() { 00042 $form = $this->_form; 00043 00044 $form->addElement('hidden', 'areaid'); 00045 $form->setType('areaid', PARAM_INT); 00046 00047 $form->addElement('hidden', 'returnurl'); 00048 00049 // name 00050 $form->addElement('text', 'name', get_string('name', 'gradingform_rubric'), array('size'=>52)); 00051 $form->addRule('name', get_string('required'), 'required'); 00052 $form->setType('name', PARAM_TEXT); 00053 00054 // description 00055 $options = gradingform_rubric_controller::description_form_field_options($this->_customdata['context']); 00056 $form->addElement('editor', 'description_editor', get_string('description', 'gradingform_rubric'), null, $options); 00057 $form->setType('description_editor', PARAM_RAW); 00058 00059 // rubric completion status 00060 $choices = array(); 00061 $choices[gradingform_controller::DEFINITION_STATUS_DRAFT] = html_writer::tag('span', get_string('statusdraft', 'core_grading'), array('class' => 'status draft')); 00062 $choices[gradingform_controller::DEFINITION_STATUS_READY] = html_writer::tag('span', get_string('statusready', 'core_grading'), array('class' => 'status ready')); 00063 $form->addElement('select', 'status', get_string('rubricstatus', 'gradingform_rubric'), $choices)->freeze(); 00064 00065 // rubric editor 00066 $element = $form->addElement('rubriceditor', 'rubric', get_string('rubric', 'gradingform_rubric')); 00067 $form->setType('rubric', PARAM_RAW); 00068 //$element->freeze(); // TODO freeze rubric editor if needed 00069 00070 $buttonarray = array(); 00071 $buttonarray[] = &$form->createElement('submit', 'saverubric', get_string('saverubric', 'gradingform_rubric')); 00072 if ($this->_customdata['allowdraft']) { 00073 $buttonarray[] = &$form->createElement('submit', 'saverubricdraft', get_string('saverubricdraft', 'gradingform_rubric')); 00074 } 00075 $editbutton = &$form->createElement('submit', 'editrubric', ' '); 00076 $editbutton->freeze(); 00077 $buttonarray[] = &$editbutton; 00078 $buttonarray[] = &$form->createElement('cancel'); 00079 $form->addGroup($buttonarray, 'buttonar', '', array(' '), false); 00080 $form->closeHeaderBefore('buttonar'); 00081 } 00082 00091 public function definition_after_data() { 00092 $form = $this->_form; 00093 $el = $form->getElement('status'); 00094 if (!$el->getValue()) { 00095 $form->removeElement('status'); 00096 } else { 00097 $vals = array_values($el->getValue()); 00098 if ($vals[0] == gradingform_controller::DEFINITION_STATUS_READY) { 00099 $this->findButton('saverubric')->setValue(get_string('save', 'gradingform_rubric')); 00100 } 00101 } 00102 } 00103 00114 public function validation($data, $files) { 00115 $err = parent::validation($data, $files); 00116 $err = array(); 00117 $form = $this->_form; 00118 $rubricel = $form->getElement('rubric'); 00119 if ($rubricel->non_js_button_pressed($data['rubric'])) { 00120 // if JS is disabled and button such as 'Add criterion' is pressed - prevent from submit 00121 $err['rubricdummy'] = 1; 00122 } else if (isset($data['editrubric'])) { 00123 // continue editing 00124 $err['rubricdummy'] = 1; 00125 } else if (isset($data['saverubric']) && $data['saverubric']) { 00126 // If user attempts to make rubric active - it needs to be validated 00127 if ($rubricel->validate($data['rubric']) !== false) { 00128 $err['rubricdummy'] = 1; 00129 } 00130 } 00131 return $err; 00132 } 00133 00140 public function get_data() { 00141 $data = parent::get_data(); 00142 if (!empty($data->saverubric)) { 00143 $data->status = gradingform_controller::DEFINITION_STATUS_READY; 00144 } else if (!empty($data->saverubricdraft)) { 00145 $data->status = gradingform_controller::DEFINITION_STATUS_DRAFT; 00146 } 00147 return $data; 00148 } 00149 00157 public function need_confirm_regrading($controller) { 00158 $data = $this->get_data(); 00159 if (isset($data->rubric['regrade'])) { 00160 // we have already displayed the confirmation on the previous step 00161 return false; 00162 } 00163 if (!isset($data->saverubric) || !$data->saverubric) { 00164 // we only need confirmation when button 'Save rubric' is pressed 00165 return false; 00166 } 00167 if (!$controller->has_active_instances()) { 00168 // nothing to re-grade, confirmation not needed 00169 return false; 00170 } 00171 $changelevel = $controller->update_or_check_rubric($data); 00172 if ($changelevel == 0) { 00173 // no changes in the rubric, no confirmation needed 00174 return false; 00175 } 00176 00177 // freeze form elements and pass the values in hidden fields 00178 // TODO description_editor does not freeze the normal way! 00179 $form = $this->_form; 00180 foreach (array('rubric', 'name'/*, 'description_editor'*/) as $fieldname) { 00181 $el =& $form->getElement($fieldname); 00182 $el->freeze(); 00183 $el->setPersistantFreeze(true); 00184 if ($fieldname == 'rubric') { 00185 $el->add_regrade_confirmation($changelevel); 00186 } 00187 } 00188 00189 // replace button text 'saverubric' and unfreeze 'Back to edit' button 00190 $this->findButton('saverubric')->setValue(get_string('continue')); 00191 $el =& $this->findButton('editrubric'); 00192 $el->setValue(get_string('backtoediting', 'gradingform_rubric')); 00193 $el->unfreeze(); 00194 00195 return true; 00196 } 00197 00204 protected function &findButton($elementname) { 00205 $form = $this->_form; 00206 $buttonar =& $form->getElement('buttonar'); 00207 $elements =& $buttonar->getElements(); 00208 foreach ($elements as $el) { 00209 if ($el->getName() == $elementname) { 00210 return $el; 00211 } 00212 } 00213 return null; 00214 } 00215 }