See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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('text', 'grademax', get_string('grademax', 'grades')); 114 $mform->addHelpButton('grademax', 'grademax', 'grades'); 115 $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE); 116 $mform->setType('grademax', PARAM_RAW); 117 118 if ((bool) get_config('moodle', 'grade_report_showmin')) { 119 $mform->addElement('text', 'grademin', get_string('grademin', 'grades')); 120 $mform->addHelpButton('grademin', 'grademin', 'grades'); 121 $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE); 122 $mform->setType('grademin', PARAM_RAW); 123 } 124 125 $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades')); 126 $mform->addHelpButton('gradepass', 'gradepass', 'grades'); 127 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE); 128 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT); 129 $mform->setType('gradepass', PARAM_RAW); 130 131 $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades')); 132 $mform->addHelpButton('multfactor', 'multfactor', 'grades'); 133 $mform->setAdvanced('multfactor'); 134 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); 135 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); 136 $mform->setType('multfactor', PARAM_RAW); 137 138 $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades')); 139 $mform->addHelpButton('plusfactor', 'plusfactor', 'grades'); 140 $mform->setAdvanced('plusfactor'); 141 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE); 142 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT); 143 $mform->setType('plusfactor', PARAM_RAW); 144 145 /// grade display prefs 146 $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype); 147 $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'), 148 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'), 149 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'), 150 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'), 151 GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'), 152 GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'), 153 GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'), 154 GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'), 155 GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'), 156 GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades') 157 ); 158 159 asort($options); 160 161 foreach ($options as $key=>$option) { 162 if ($key == $default_gradedisplaytype) { 163 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option); 164 break; 165 } 166 } 167 $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options); 168 $mform->addHelpButton('display', 'gradedisplaytype', 'grades'); 169 $mform->disabledIf('display', 'gradetype', 'eq', GRADE_TYPE_TEXT); 170 171 $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints); 172 $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5); 173 $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options); 174 $mform->addHelpButton('decimals', 'decimalpoints', 'grades'); 175 $mform->setDefault('decimals', -1); 176 $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER); 177 if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) { 178 $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT); 179 } 180 $mform->disabledIf('decimals', 'gradetype', 'eq', GRADE_TYPE_TEXT); 181 182 /// hiding 183 if ($item->cancontrolvisibility) { 184 $mform->addElement('advcheckbox', 'hidden', get_string('hidden', 'grades'), '', [], [0, 1]); 185 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true)); 186 $mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked'); 187 } else { 188 $mform->addElement('static', 'hidden', get_string('hidden', 'grades'), 189 get_string('componentcontrolsvisibility', 'grades')); 190 // Unset hidden to avoid data override. 191 unset($item->hidden); 192 } 193 $mform->addHelpButton('hidden', 'hidden', 'grades'); 194 195 /// locking 196 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades')); 197 $mform->addHelpButton('locked', 'locked', 'grades'); 198 199 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true)); 200 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE); 201 202 /// parent category related settings 203 $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades')); 204 205 $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades')); 206 $mform->addHelpButton('weightoverride', 'weightoverride', 'grades'); 207 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE); 208 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT); 209 210 $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades')); 211 $mform->addHelpButton('aggregationcoef2', 'weight', 'grades'); 212 $mform->setType('aggregationcoef2', PARAM_RAW); 213 $mform->disabledIf('aggregationcoef2', 'weightoverride'); 214 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE); 215 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT); 216 217 $options = array(); 218 $coefstring = ''; 219 $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id)); 220 221 foreach ($categories as $cat) { 222 $cat->apply_forced_settings(); 223 $options[$cat->id] = $cat->get_name(); 224 } 225 226 if (count($categories) > 1) { 227 $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options); 228 } 229 230 /// hidden params 231 $mform->addElement('hidden', 'id', 0); 232 $mform->setType('id', PARAM_INT); 233 234 $mform->addElement('hidden', 'courseid', $COURSE->id); 235 $mform->setType('courseid', PARAM_INT); 236 237 $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only 238 $mform->setType('itemtype', PARAM_ALPHA); 239 240 /// add return tracking info 241 $gpr = $this->_customdata['gpr']; 242 $gpr->add_mform_elements($mform); 243 244 /// mark advanced according to site settings 245 if (isset($CFG->grade_item_advanced)) { 246 $advanced = explode(',', $CFG->grade_item_advanced); 247 foreach ($advanced as $el) { 248 if ($mform->elementExists($el)) { 249 $mform->setAdvanced($el); 250 } 251 } 252 } 253 //------------------------------------------------------------------------------- 254 // buttons 255 $this->add_action_buttons(); 256 //------------------------------------------------------------------------------- 257 $this->set_data($item); 258 } 259 260 261 /// tweak the form - depending on existing data 262 function definition_after_data() { 263 global $CFG, $COURSE; 264 265 $mform =& $this->_form; 266 267 if ($id = $mform->getElementValue('id')) { 268 $gradeitem = grade_item::fetch(array('id' => $id)); 269 $parentcategory = $gradeitem->get_parent_category(); 270 } else { 271 // If we do not have an id, we are creating a new grade item. 272 $gradeitem = new grade_item(array('courseid' => $COURSE->id, 'itemtype' => 'manual'), false); 273 274 // Assign the course category to this grade item. 275 $parentcategory = grade_category::fetch_course_category($COURSE->id); 276 $gradeitem->parent_category = $parentcategory; 277 } 278 279 if (!$gradeitem->is_raw_used()) { 280 $mform->removeElement('plusfactor'); 281 $mform->removeElement('multfactor'); 282 } 283 284 if ($gradeitem->is_outcome_item()) { 285 // We have to prevent incompatible modifications of outcomes if outcomes disabled. 286 $mform->removeElement('grademax'); 287 if ($mform->elementExists('grademin')) { 288 $mform->removeElement('grademin'); 289 } 290 $mform->removeElement('gradetype'); 291 $mform->removeElement('display'); 292 $mform->removeElement('decimals'); 293 $mform->hardFreeze('scaleid'); 294 295 } else { 296 if ($gradeitem->is_external_item()) { 297 // Following items are set up from modules and should not be overrided by user. 298 if ($mform->elementExists('grademin')) { 299 // The site setting grade_report_showmin may have prevented grademin being added to the form. 300 $mform->hardFreeze('grademin'); 301 } 302 $mform->hardFreeze('itemname,gradetype,grademax,scaleid'); 303 if ($gradeitem->itemnumber == 0) { 304 // The idnumber of grade itemnumber 0 is synced with course_modules. 305 $mform->hardFreeze('idnumber'); 306 } 307 308 // For external items we can not change the grade type, even if no grades exist, so if it is set to 309 // scale, then remove the grademax and grademin fields from the form - no point displaying them. 310 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) { 311 $mform->removeElement('grademax'); 312 if ($mform->elementExists('grademin')) { 313 $mform->removeElement('grademin'); 314 } 315 } else { // Not using scale, so remove it. 316 $mform->removeElement('scaleid'); 317 } 318 319 // Always remove the rescale grades element if it's an external item. 320 $mform->removeElement('rescalegrades'); 321 } else if ($gradeitem->has_grades()) { 322 // Can't change the grade type or the scale if there are grades. 323 $mform->hardFreeze('gradetype, scaleid'); 324 325 // If we are using scales then remove the unnecessary rescale and grade fields. 326 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) { 327 $mform->removeElement('rescalegrades'); 328 $mform->removeElement('grademax'); 329 if ($mform->elementExists('grademin')) { 330 $mform->removeElement('grademin'); 331 } 332 } else { // Remove the scale field. 333 $mform->removeElement('scaleid'); 334 // Set the maximum grade to disabled unless a grade is chosen. 335 $mform->disabledIf('grademax', 'rescalegrades', 'eq', ''); 336 } 337 } else { 338 // Remove the rescale element if there are no grades. 339 $mform->removeElement('rescalegrades'); 340 } 341 } 342 343 // If we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!! 344 if ($id && $mform->elementExists('parentcategory')) { 345 $mform->hardFreeze('parentcategory'); 346 } 347 348 $parentcategory->apply_forced_settings(); 349 350 if (!$parentcategory->is_aggregationcoef_used()) { 351 if ($mform->elementExists('aggregationcoef')) { 352 $mform->removeElement('aggregationcoef'); 353 } 354 355 } else { 356 $coefstring = $gradeitem->get_coefstring(); 357 358 if ($coefstring !== '') { 359 if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') { 360 // The advcheckbox is not compatible with disabledIf! 361 $coefstring = 'aggregationcoefextrasum'; 362 $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades')); 363 } else { 364 $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades')); 365 } 366 if ($mform->elementExists('parentcategory')) { 367 $mform->insertElementBefore($element, 'parentcategory'); 368 } else { 369 $mform->insertElementBefore($element, 'id'); 370 } 371 $mform->addHelpButton('aggregationcoef', $coefstring, 'grades'); 372 } 373 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE); 374 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT); 375 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parentcategory->id); 376 } 377 378 // Remove fields used by natural weighting if the parent category is not using natural weighting. 379 // Or if the item is a scale and scales are not used in aggregation. 380 if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM 381 || (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) { 382 if ($mform->elementExists('weightoverride')) { 383 $mform->removeElement('weightoverride'); 384 } 385 if ($mform->elementExists('aggregationcoef2')) { 386 $mform->removeElement('aggregationcoef2'); 387 } 388 } 389 390 if ($category = $gradeitem->get_item_category()) { 391 if ($category->aggregation == GRADE_AGGREGATE_SUM) { 392 if ($mform->elementExists('gradetype')) { 393 $mform->hardFreeze('gradetype'); 394 } 395 if ($mform->elementExists('grademin')) { 396 $mform->hardFreeze('grademin'); 397 } 398 if ($mform->elementExists('grademax')) { 399 $mform->hardFreeze('grademax'); 400 } 401 if ($mform->elementExists('scaleid')) { 402 $mform->removeElement('scaleid'); 403 } 404 } 405 } 406 407 // no parent header for course category 408 if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) { 409 $mform->removeElement('headerparent'); 410 } 411 } 412 413 /// perform extra validation before submission 414 function validation($data, $files) { 415 global $COURSE; 416 $grade_item = false; 417 if ($data['id']) { 418 $grade_item = new grade_item(array('id' => $data['id'], 'courseid' => $data['courseid'])); 419 } 420 421 $errors = parent::validation($data, $files); 422 423 if (array_key_exists('idnumber', $data)) { 424 if ($grade_item) { 425 if ($grade_item->itemtype == 'mod') { 426 $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid); 427 } else { 428 $cm = null; 429 } 430 } else { 431 $grade_item = null; 432 $cm = null; 433 } 434 if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) { 435 $errors['idnumber'] = get_string('idnumbertaken'); 436 } 437 } 438 439 if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) { 440 if (empty($data['scaleid'])) { 441 $errors['scaleid'] = get_string('missingscale', 'grades'); 442 } 443 } 444 445 if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) { 446 if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) { 447 $errors['grademin'] = get_string('incorrectminmax', 'grades'); 448 $errors['grademax'] = get_string('incorrectminmax', 'grades'); 449 } 450 } 451 452 // We do not want the user to be able to change the grade type or scale for this item if grades exist. 453 if ($grade_item && $grade_item->has_grades()) { 454 // Check that grade type is set - should never not be set unless form has been modified. 455 if (!isset($data['gradetype'])) { 456 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades'); 457 } else if ($data['gradetype'] !== $grade_item->gradetype) { // Check if we are changing the grade type. 458 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades'); 459 } else if ($data['gradetype'] == GRADE_TYPE_SCALE) { 460 // Check if we are changing the scale - can't do this when grades exist. 461 if (isset($data['scaleid']) && ($data['scaleid'] !== $grade_item->scaleid)) { 462 $errors['scaleid'] = get_string('modgradecantchangescale', 'grades'); 463 } 464 } 465 } 466 if ($grade_item) { 467 if ($grade_item->gradetype == GRADE_TYPE_VALUE) { 468 if ((((bool) get_config('moodle', 'grade_report_showmin')) && 469 grade_floats_different($data['grademin'], $grade_item->grademin)) || 470 grade_floats_different($data['grademax'], $grade_item->grademax)) { 471 if ($grade_item->has_grades() && empty($data['rescalegrades'])) { 472 $errors['rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades'); 473 } 474 } 475 } 476 } 477 478 return $errors; 479 } 480 481 } 482
title
Description
Body
title
Description
Body
title
Description
Body
title
Body