Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 /** 18 * A moodleform allowing the editing of the grade options for an individual grade item 19 * 20 * @package core_grades 21 * @copyright 2007 Petr Skoda 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 if (!defined('MOODLE_INTERNAL')) { 26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page 27 } 28 29 require_once $CFG->libdir.'/formslib.php'; 30 31 class edit_item_form extends moodleform { 32 private $displayoptions; 33 34 function definition() { 35 global $COURSE, $CFG, $DB; 36 37 $mform =& $this->_form; 38 39 $item = $this->_customdata['current']; 40 41 /// visible elements 42 $mform->addElement('header', 'general', get_string('gradeitem', 'grades')); 43 44 $mform->addElement('text', 'itemname', get_string('itemname', 'grades')); 45 $mform->setType('itemname', PARAM_TEXT); 46 $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades')); 47 $mform->addHelpButton('iteminfo', 'iteminfo', 'grades'); 48 $mform->setType('iteminfo', PARAM_TEXT); 49 50 $mform->addElement('text', 'idnumber', get_string('idnumbermod')); 51 $mform->addHelpButton('idnumber', 'idnumbermod'); 52 $mform->setType('idnumber', PARAM_RAW); 53 54 if (!empty($item->id)) { 55 $gradeitem = new grade_item(array('id' => $item->id, 'courseid' => $item->courseid)); 56 // If grades exist set a message so the user knows why they can not alter the grade type or scale. 57 // We could never change the grade type for external items, so only need to show this for manual grade items. 58 if ($gradeitem->has_grades() && !$gradeitem->is_external_item()) { 59 // Set a message so the user knows why they can not alter the grade type or scale. 60 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) { 61 $gradesexistmsg = get_string('modgradecantchangegradetyporscalemsg', 'grades'); 62 } else { 63 $gradesexistmsg = get_string('modgradecantchangegradetypemsg', 'grades'); 64 } 65 66 $gradesexisthtml = '<div class=\'alert\'>' . $gradesexistmsg . '</div>'; 67 $mform->addElement('static', 'gradesexistmsg', '', $gradesexisthtml); 68 } 69 } 70 71 // Manual grade items cannot have grade type GRADE_TYPE_NONE. 72 $options = array(GRADE_TYPE_VALUE => get_string('typevalue', 'grades'), 73 GRADE_TYPE_SCALE => get_string('typescale', 'grades'), 74 GRADE_TYPE_TEXT => get_string('typetext', 'grades')); 75 76 $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options); 77 $mform->addHelpButton('gradetype', 'gradetype', 'grades'); 78 $mform->setDefault('gradetype', GRADE_TYPE_VALUE); 79 80 //$mform->addElement('text', 'calculation', get_string('calculation', 'grades')); 81 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT); 82 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE); 83 84 $options = array(0=>get_string('usenoscale', 'grades')); 85 if ($scales = grade_scale::fetch_all_local($COURSE->id)) { 86 foreach ($scales as $scale) { 87 $options[$scale->id] = $scale->get_name(); 88 } 89 } 90 if ($scales = grade_scale::fetch_all_global()) { 91 foreach ($scales as $scale) { 92 $options[$scale->id] = $scale->get_name(); 93 } 94 } 95 // ugly BC hack - it was possible to use custom scale from other courses :-( 96 if (!empty($item->scaleid) and !isset($options[$item->scaleid])) { 97 if ($scale = grade_scale::fetch(array('id'=>$item->scaleid))) { 98 $options[$scale->id] = $scale->get_name().get_string('incorrectcustomscale', 'grades'); 99 } 100 } 101 $mform->addElement('select', 'scaleid', get_string('scale'), $options); 102 $mform->addHelpButton('scaleid', 'typescale', 'grades'); 103 $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE); 104 105 $choices = array(); 106 $choices[''] = get_string('choose'); 107 $choices['no'] = get_string('no'); 108 $choices['yes'] = get_string('yes'); 109 $mform->addElement('select', 'rescalegrades', get_string('modgraderescalegrades', 'grades'), $choices); 110 $mform->addHelpButton('rescalegrades', 'modgraderescalegrades', 'grades'); 111 $mform->disabledIf('rescalegrades', 'gradetype', 'noteq', GRADE_TYPE_VALUE); 112 113 $mform->addElement('float', 'grademax', get_string('grademax', 'grades')); 114 $mform->addHelpButton('grademax', 'grademax', 'grades'); 115 $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE); 116 117 if ((bool) get_config('moodle', 'grade_report_showmin')) { 118 $mform->addElement('float', 'grademin', get_string('grademin', 'grades')); 119 $mform->addHelpButton('grademin', 'grademin', 'grades'); 120 $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE); 121 } 122 123 $mform->addElement('float', 'gradepass', get_string('gradepass', 'grades')); 124 $mform->addHelpButton('gradepass', 'gradepass', 'grades'); 125 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE); 126 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT); 127 128 $mform->addElement('float', 'multfactor', get_string('multfactor', 'grades')); 129 $mform->addHelpButton('multfactor', 'multfactor', 'grades'); 130 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); 131 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); 132 133 $mform->addElement('float', 'plusfactor', get_string('plusfactor', 'grades')); 134 $mform->addHelpButton('plusfactor', 'plusfactor', 'grades'); 135 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); 136 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); 137 138 /// grade display prefs 139 $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype); 140 $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'), 141 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'), 142 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'), 143 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'), 144 GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'), 145 GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'), 146 GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'), 147 GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'), 148 GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'), 149 GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades') 150 ); 151 152 asort($options); 153 154 foreach ($options as $key=>$option) { 155 if ($key == $default_gradedisplaytype) { 156 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option); 157 break; 158 } 159 } 160 $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options); 161 $mform->addHelpButton('display', 'gradedisplaytype', 'grades'); 162 $mform->disabledIf('display', 'gradetype', 'eq', GRADE_TYPE_TEXT); 163 164 $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints); 165 $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5); 166 $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options); 167 $mform->addHelpButton('decimals', 'decimalpoints', 'grades'); 168 $mform->setDefault('decimals', -1); 169 $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER); 170 if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) { 171 $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT); 172 } 173 $mform->disabledIf('decimals', 'gradetype', 'eq', GRADE_TYPE_TEXT); 174 175 /// hiding 176 if ($item->cancontrolvisibility) { 177 $mform->addElement('advcheckbox', 'hidden', get_string('hidden', 'grades'), '', [], [0, 1]); 178 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true)); 179 $mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked'); 180 } else { 181 $mform->addElement('static', 'hidden', get_string('hidden', 'grades'), 182 get_string('componentcontrolsvisibility', 'grades')); 183 // Unset hidden to avoid data override. 184 unset($item->hidden); 185 } 186 $mform->addHelpButton('hidden', 'hidden', 'grades'); 187 188 /// locking 189 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades')); 190 $mform->addHelpButton('locked', 'locked', 'grades'); 191 192 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true)); 193 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE); 194 195 /// parent category related settings 196 $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades')); 197 198 $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades')); 199 $mform->addHelpButton('weightoverride', 'weightoverride', 'grades'); 200 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE); 201 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT); 202 203 $mform->addElement('float', 'aggregationcoef2', get_string('weight', 'grades')); 204 $mform->addHelpButton('aggregationcoef2', 'weight', 'grades'); 205 $mform->disabledIf('aggregationcoef2', 'weightoverride'); 206 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE); 207 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT); 208 209 $options = array(); 210 $coefstring = ''; 211 $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id)); 212 213 foreach ($categories as $cat) { 214 $cat->apply_forced_settings(); 215 $options[$cat->id] = $cat->get_name(); 216 } 217 218 if (count($categories) > 1) { 219 $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options); 220 } 221 222 /// hidden params 223 $mform->addElement('hidden', 'id', 0); 224 $mform->setType('id', PARAM_INT); 225 226 $mform->addElement('hidden', 'courseid', $COURSE->id); 227 $mform->setType('courseid', PARAM_INT); 228 229 $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only 230 $mform->setType('itemtype', PARAM_ALPHA); 231 232 /// add return tracking info 233 $gpr = $this->_customdata['gpr']; 234 $gpr->add_mform_elements($mform); 235 //------------------------------------------------------------------------------- 236 // buttons 237 $this->add_action_buttons(); 238 //------------------------------------------------------------------------------- 239 $this->set_data($item); 240 } 241 242 243 /// tweak the form - depending on existing data 244 function definition_after_data() { 245 global $CFG, $COURSE; 246 247 $mform =& $this->_form; 248 249 if ($id = $mform->getElementValue('id')) { 250 $gradeitem = grade_item::fetch(array('id' => $id)); 251 $parentcategory = $gradeitem->get_parent_category(); 252 } else { 253 // If we do not have an id, we are creating a new grade item. 254 $gradeitem = new grade_item(array('courseid' => $COURSE->id, 'itemtype' => 'manual'), false); 255 256 // Assign the course category to this grade item. 257 $parentcategory = grade_category::fetch_course_category($COURSE->id); 258 $gradeitem->parent_category = $parentcategory; 259 } 260 261 if (!$gradeitem->is_raw_used()) { 262 $mform->removeElement('plusfactor'); 263 $mform->removeElement('multfactor'); 264 } 265 266 if ($gradeitem->is_outcome_item()) { 267 // We have to prevent incompatible modifications of outcomes if outcomes disabled. 268 $mform->removeElement('grademax'); 269 if ($mform->elementExists('grademin')) { 270 $mform->removeElement('grademin'); 271 } 272 $mform->removeElement('gradetype'); 273 $mform->removeElement('display'); 274 $mform->removeElement('decimals'); 275 $mform->hardFreeze('scaleid'); 276 277 } else { 278 if ($gradeitem->is_external_item()) { 279 // Following items are set up from modules and should not be overrided by user. 280 if ($mform->elementExists('grademin')) { 281 // The site setting grade_report_showmin may have prevented grademin being added to the form. 282 $mform->hardFreeze('grademin'); 283 } 284 $mform->hardFreeze('itemname,gradetype,grademax,scaleid'); 285 if ($gradeitem->itemnumber == 0) { 286 // The idnumber of grade itemnumber 0 is synced with course_modules. 287 $mform->hardFreeze('idnumber'); 288 } 289 290 // For external items we can not change the grade type, even if no grades exist, so if it is set to 291 // scale, then remove the grademax and grademin fields from the form - no point displaying them. 292 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) { 293 $mform->removeElement('grademax'); 294 if ($mform->elementExists('grademin')) { 295 $mform->removeElement('grademin'); 296 } 297 } else { // Not using scale, so remove it. 298 $mform->removeElement('scaleid'); 299 } 300 301 // Always remove the rescale grades element if it's an external item. 302 $mform->removeElement('rescalegrades'); 303 } else if ($gradeitem->has_grades()) { 304 // Can't change the grade type or the scale if there are grades. 305 $mform->hardFreeze('gradetype, scaleid'); 306 307 // If we are using scales then remove the unnecessary rescale and grade fields. 308 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) { 309 $mform->removeElement('rescalegrades'); 310 $mform->removeElement('grademax'); 311 if ($mform->elementExists('grademin')) { 312 $mform->removeElement('grademin'); 313 } 314 } else { // Remove the scale field. 315 $mform->removeElement('scaleid'); 316 // Set the maximum grade to disabled unless a grade is chosen. 317 $mform->disabledIf('grademax', 'rescalegrades', 'eq', ''); 318 } 319 } else { 320 // Remove the rescale element if there are no grades. 321 $mform->removeElement('rescalegrades'); 322 } 323 } 324 325 // If we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!! 326 if ($id && $mform->elementExists('parentcategory')) { 327 $mform->hardFreeze('parentcategory'); 328 } 329 330 $parentcategory->apply_forced_settings(); 331 332 if (!$parentcategory->is_aggregationcoef_used()) { 333 if ($mform->elementExists('aggregationcoef')) { 334 $mform->removeElement('aggregationcoef'); 335 } 336 337 } else { 338 $coefstring = $gradeitem->get_coefstring(); 339 340 if ($coefstring !== '') { 341 if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') { 342 // The advcheckbox is not compatible with disabledIf! 343 $coefstring = 'aggregationcoefextrasum'; 344 $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades')); 345 } else { 346 $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades')); 347 } 348 if ($mform->elementExists('parentcategory')) { 349 $mform->insertElementBefore($element, 'parentcategory'); 350 } else { 351 $mform->insertElementBefore($element, 'id'); 352 } 353 $mform->addHelpButton('aggregationcoef', $coefstring, 'grades'); 354 } 355 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE); 356 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT); 357 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parentcategory->id); 358 } 359 360 // Remove fields used by natural weighting if the parent category is not using natural weighting. 361 // Or if the item is a scale and scales are not used in aggregation. 362 if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM 363 || (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) { 364 if ($mform->elementExists('weightoverride')) { 365 $mform->removeElement('weightoverride'); 366 } 367 if ($mform->elementExists('aggregationcoef2')) { 368 $mform->removeElement('aggregationcoef2'); 369 } 370 } 371 372 if ($category = $gradeitem->get_item_category()) { 373 if ($category->aggregation == GRADE_AGGREGATE_SUM) { 374 if ($mform->elementExists('gradetype')) { 375 $mform->hardFreeze('gradetype'); 376 } 377 if ($mform->elementExists('grademin')) { 378 $mform->hardFreeze('grademin'); 379 } 380 if ($mform->elementExists('grademax')) { 381 $mform->hardFreeze('grademax'); 382 } 383 if ($mform->elementExists('scaleid')) { 384 $mform->removeElement('scaleid'); 385 } 386 } 387 } 388 389 // no parent header for course category 390 if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) { 391 $mform->removeElement('headerparent'); 392 } 393 } 394 395 /// perform extra validation before submission 396 function validation($data, $files) { 397 global $COURSE; 398 $grade_item = false; 399 if ($data['id']) { 400 $grade_item = new grade_item(array('id' => $data['id'], 'courseid' => $data['courseid'])); 401 } 402 403 $errors = parent::validation($data, $files); 404 405 if (array_key_exists('idnumber', $data)) { 406 if ($grade_item) { 407 if ($grade_item->itemtype == 'mod') { 408 $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid); 409 } else { 410 $cm = null; 411 } 412 } else { 413 $grade_item = null; 414 $cm = null; 415 } 416 if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) { 417 $errors['idnumber'] = get_string('idnumbertaken'); 418 } 419 } 420 421 if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) { 422 if (empty($data['scaleid'])) { 423 $errors['scaleid'] = get_string('missingscale', 'grades'); 424 } 425 } 426 427 // We need to make all the validations related with grademax and grademin 428 // with them being correct floats, keeping the originals unmodified for 429 // later validations / showing the form back... 430 // TODO: Note that once MDL-73994 is fixed we'll have to re-visit this and 431 // adapt the code below to the new values arriving here, without forgetting 432 // the special case of empties and nulls. 433 $grademax = isset($data['grademax']) ? unformat_float($data['grademax']) : null; 434 $grademin = isset($data['grademin']) ? unformat_float($data['grademin']) : null; 435 436 if (!is_null($grademin) and !is_null($grademax)) { 437 if ($grademax == $grademin or $grademax < $grademin) { 438 $errors['grademin'] = get_string('incorrectminmax', 'grades'); 439 $errors['grademax'] = get_string('incorrectminmax', 'grades'); 440 } 441 } 442 443 // We do not want the user to be able to change the grade type or scale for this item if grades exist. 444 if ($grade_item && $grade_item->has_grades()) { 445 // Check that grade type is set - should never not be set unless form has been modified. 446 if (!isset($data['gradetype'])) { 447 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades'); 448 } else if ($data['gradetype'] !== $grade_item->gradetype) { // Check if we are changing the grade type. 449 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades'); 450 } else if ($data['gradetype'] == GRADE_TYPE_SCALE) { 451 // Check if we are changing the scale - can't do this when grades exist. 452 if (isset($data['scaleid']) && ($data['scaleid'] !== $grade_item->scaleid)) { 453 $errors['scaleid'] = get_string('modgradecantchangescale', 'grades'); 454 } 455 } 456 } 457 if ($grade_item) { 458 if ($grade_item->gradetype == GRADE_TYPE_VALUE) { 459 if ((((bool) get_config('moodle', 'grade_report_showmin')) && 460 grade_floats_different($grademin, $grade_item->grademin)) || 461 grade_floats_different($grademax, $grade_item->grademax)) { 462 if ($grade_item->has_grades() && empty($data['rescalegrades'])) { 463 $errors['rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades'); 464 } 465 } 466 } 467 } 468 469 return $errors; 470 } 471 472 } 473
title
Description
Body
title
Description
Body
title
Description
Body
title
Body