PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/edit/tree/category_form.php

https://bitbucket.org/moodle/moodle
PHP | 575 lines | 427 code | 84 blank | 64 comment | 110 complexity | c3b65c7e17ab57eab1e719470fd0f7fd MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. * A moodleform to edit the grade options for an individual grade category
  18. *
  19. * @package core_grades
  20. * @copyright 2007 Petr Skoda
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once $CFG->libdir.'/formslib.php';
  27. class edit_category_form extends moodleform {
  28. private $aggregation_options = array();
  29. function definition() {
  30. global $CFG, $COURSE, $DB, $OUTPUT;
  31. $mform =& $this->_form;
  32. $category = $this->_customdata['current'];
  33. $this->aggregation_options = grade_helper::get_aggregation_strings();
  34. // visible elements
  35. $mform->addElement('header', 'headercategory', get_string('gradecategory', 'grades'));
  36. $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
  37. $mform->setType('fullname', PARAM_TEXT);
  38. $mform->addRule('fullname', null, 'required', null, 'client');
  39. $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
  40. $mform->addHelpButton('aggregation', 'aggregation', 'grades');
  41. if ((int)$CFG->grade_aggregation_flag & 2) {
  42. $mform->setAdvanced('aggregation');
  43. }
  44. $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
  45. $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
  46. if ((int)$CFG->grade_aggregateonlygraded_flag & 2) {
  47. $mform->setAdvanced('aggregateonlygraded');
  48. }
  49. if (empty($CFG->enableoutcomes)) {
  50. $mform->addElement('hidden', 'aggregateoutcomes');
  51. $mform->setType('aggregateoutcomes', PARAM_INT);
  52. } else {
  53. $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
  54. $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
  55. if ((int)$CFG->grade_aggregateoutcomes_flag & 2) {
  56. $mform->setAdvanced('aggregateoutcomes');
  57. }
  58. }
  59. $mform->addElement('text', 'keephigh', get_string('keephigh', 'grades'), 'size="3"');
  60. $mform->setType('keephigh', PARAM_INT);
  61. $mform->addHelpButton('keephigh', 'keephigh', 'grades');
  62. if ((int)$CFG->grade_keephigh_flag & 2) {
  63. $mform->setAdvanced('keephigh');
  64. }
  65. $mform->addElement('text', 'droplow', get_string('droplow', 'grades'), 'size="3"');
  66. $mform->setType('droplow', PARAM_INT);
  67. $mform->addHelpButton('droplow', 'droplow', 'grades');
  68. $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  69. if ((int)$CFG->grade_droplow_flag & 2) {
  70. $mform->setAdvanced('droplow');
  71. }
  72. $mform->disabledIf('keephigh', 'droplow', 'noteq', 0);
  73. $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  74. // Grade item settings
  75. // Displayed as Category total to avoid confusion between grade items requiring marking and category totals
  76. $mform->addElement('header', 'general', get_string('categorytotal', 'grades'));
  77. $mform->addElement('text', 'grade_item_itemname', get_string('categorytotalname', 'grades'));
  78. $mform->setType('grade_item_itemname', PARAM_TEXT);
  79. $mform->setAdvanced('grade_item_itemname');
  80. $mform->addElement('text', 'grade_item_iteminfo', get_string('iteminfo', 'grades'));
  81. $mform->addHelpButton('grade_item_iteminfo', 'iteminfo', 'grades');
  82. $mform->setType('grade_item_iteminfo', PARAM_TEXT);
  83. $mform->addElement('text', 'grade_item_idnumber', get_string('idnumbermod'));
  84. $mform->addHelpButton('grade_item_idnumber', 'idnumbermod');
  85. $mform->setType('grade_item_idnumber', PARAM_RAW);
  86. if (!empty($category->id)) {
  87. $gradecategory = grade_category::fetch(array('id' => $category->id));
  88. $gradeitem = $gradecategory->load_grade_item();
  89. // If grades exist set a message so the user knows why they can not alter the grade type or scale.
  90. // We could never change the grade type for external items, so only need to show this for manual grade items.
  91. if ($gradeitem->has_overridden_grades()) {
  92. // Set a message so the user knows why the can not alter the grade type or scale.
  93. if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
  94. $gradesexistmsg = get_string('modgradecategorycantchangegradetyporscalemsg', 'grades');
  95. } else {
  96. $gradesexistmsg = get_string('modgradecategorycantchangegradetypemsg', 'grades');
  97. }
  98. $notification = new \core\output\notification($gradesexistmsg, \core\output\notification::NOTIFY_INFO);
  99. $notification->set_show_closebutton(false);
  100. $mform->addElement('static', 'gradesexistmsg', '', $OUTPUT->render($notification));
  101. }
  102. }
  103. $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
  104. GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
  105. GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
  106. GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
  107. $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
  108. $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
  109. $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
  110. $mform->disabledIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  111. //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
  112. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  113. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
  114. $options = array(0=>get_string('usenoscale', 'grades'));
  115. if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
  116. foreach ($scales as $scale) {
  117. $options[$scale->id] = $scale->get_name();
  118. }
  119. }
  120. if ($scales = grade_scale::fetch_all_global()) {
  121. foreach ($scales as $scale) {
  122. $options[$scale->id] = $scale->get_name();
  123. }
  124. }
  125. // ugly BC hack - it was possible to use custom scale from other courses :-(
  126. if (!empty($category->grade_item_scaleid) and !isset($options[$category->grade_item_scaleid])) {
  127. if ($scale = grade_scale::fetch(array('id'=>$category->grade_item_scaleid))) {
  128. $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
  129. }
  130. }
  131. $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
  132. $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
  133. $mform->disabledIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
  134. $mform->disabledIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  135. $choices = array();
  136. $choices[''] = get_string('choose');
  137. $choices['no'] = get_string('no');
  138. $choices['yes'] = get_string('yes');
  139. $mform->addElement('select', 'grade_item_rescalegrades', get_string('modgradecategoryrescalegrades', 'grades'), $choices);
  140. $mform->addHelpButton('grade_item_rescalegrades', 'modgradecategoryrescalegrades', 'grades');
  141. $mform->disabledIf('grade_item_rescalegrades', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
  142. $mform->addElement('text', 'grade_item_grademax', get_string('grademax', 'grades'));
  143. $mform->setType('grade_item_grademax', PARAM_RAW);
  144. $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
  145. $mform->disabledIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
  146. $mform->disabledIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  147. if ((bool) get_config('moodle', 'grade_report_showmin')) {
  148. $mform->addElement('text', 'grade_item_grademin', get_string('grademin', 'grades'));
  149. $mform->setType('grade_item_grademin', PARAM_RAW);
  150. $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
  151. $mform->disabledIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
  152. $mform->disabledIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  153. }
  154. $mform->addElement('text', 'grade_item_gradepass', get_string('gradepass', 'grades'));
  155. $mform->setType('grade_item_gradepass', PARAM_RAW);
  156. $mform->addHelpButton('grade_item_gradepass', 'gradepass', 'grades');
  157. $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
  158. $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_TEXT);
  159. /// grade display prefs
  160. $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
  161. $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'),
  162. GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  163. GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  164. GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'),
  165. GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'),
  166. GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'),
  167. GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'),
  168. GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'),
  169. GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'),
  170. GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades')
  171. );
  172. asort($options);
  173. foreach ($options as $key=>$option) {
  174. if ($key == $default_gradedisplaytype) {
  175. $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
  176. break;
  177. }
  178. }
  179. $mform->addElement('select', 'grade_item_display', get_string('gradedisplaytype', 'grades'), $options);
  180. $mform->addHelpButton('grade_item_display', 'gradedisplaytype', 'grades');
  181. $mform->disabledIf('grade_item_display', 'grade_item_gradetype', 'in',
  182. array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
  183. $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
  184. $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
  185. $mform->addElement('select', 'grade_item_decimals', get_string('decimalpoints', 'grades'), $options);
  186. $mform->addHelpButton('grade_item_decimals', 'decimalpoints', 'grades');
  187. $mform->setDefault('grade_item_decimals', -1);
  188. $mform->disabledIf('grade_item_decimals', 'grade_item_display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
  189. $mform->disabledIf('grade_item_decimals', 'grade_item_gradetype', 'in',
  190. array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
  191. if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
  192. $mform->disabledIf('grade_item_decimals', 'grade_item_display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
  193. }
  194. /// hiding
  195. // advcheckbox is not compatible with disabledIf!
  196. $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
  197. $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
  198. $mform->addElement('date_time_selector', 'grade_item_hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  199. $mform->disabledIf('grade_item_hidden', 'grade_item_hiddenuntil[off]', 'notchecked');
  200. /// locking
  201. $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
  202. $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
  203. $mform->addElement('date_time_selector', 'grade_item_locktime', get_string('locktime', 'grades'), array('optional'=>true));
  204. $mform->disabledIf('grade_item_locktime', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
  205. /// parent category related settings
  206. $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  207. $mform->addElement('advcheckbox', 'grade_item_weightoverride', get_string('adjustedweight', 'grades'));
  208. $mform->addHelpButton('grade_item_weightoverride', 'weightoverride', 'grades');
  209. $mform->addElement('text', 'grade_item_aggregationcoef2', get_string('weight', 'grades'));
  210. $mform->addHelpButton('grade_item_aggregationcoef2', 'weight', 'grades');
  211. $mform->setType('grade_item_aggregationcoef2', PARAM_RAW);
  212. $mform->disabledIf('grade_item_aggregationcoef2', 'grade_item_weightoverride');
  213. $options = array();
  214. $default = -1;
  215. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  216. foreach ($categories as $cat) {
  217. $cat->apply_forced_settings();
  218. $options[$cat->id] = $cat->get_name();
  219. if ($cat->is_course_category()) {
  220. $default = $cat->id;
  221. }
  222. }
  223. if (count($categories) > 1) {
  224. $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
  225. $mform->setDefault('parentcategory', $default);
  226. $mform->addElement('static', 'currentparentaggregation', get_string('currentparentaggregation', 'grades'));
  227. }
  228. // hidden params
  229. $mform->addElement('hidden', 'id', 0);
  230. $mform->setType('id', PARAM_INT);
  231. $mform->addElement('hidden', 'courseid', 0);
  232. $mform->setType('courseid', PARAM_INT);
  233. /// add return tracking info
  234. $gpr = $this->_customdata['gpr'];
  235. $gpr->add_mform_elements($mform);
  236. /// mark advanced according to site settings
  237. if (isset($CFG->grade_item_advanced)) {
  238. $advanced = explode(',', $CFG->grade_item_advanced);
  239. foreach ($advanced as $el) {
  240. $el = 'grade_item_'.$el;
  241. if ($mform->elementExists($el)) {
  242. $mform->setAdvanced($el);
  243. }
  244. }
  245. }
  246. //-------------------------------------------------------------------------------
  247. // buttons
  248. $this->add_action_buttons();
  249. //-------------------------------------------------------------------------------
  250. $this->set_data($category);
  251. }
  252. /// tweak the form - depending on existing data
  253. function definition_after_data() {
  254. global $CFG, $COURSE;
  255. $mform =& $this->_form;
  256. $somecat = new grade_category();
  257. foreach ($somecat->forceable as $property) {
  258. if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
  259. if ($mform->elementExists($property)) {
  260. if (empty($CFG->grade_hideforcedsettings)) {
  261. $mform->hardFreeze($property);
  262. } else {
  263. if ($mform->elementExists($property)) {
  264. $mform->removeElement($property);
  265. }
  266. }
  267. }
  268. }
  269. }
  270. if ($CFG->grade_droplow > 0) {
  271. if ($mform->elementExists('keephigh')) {
  272. $mform->removeElement('keephigh');
  273. }
  274. } else if ($CFG->grade_keephigh > 0) {
  275. if ($mform->elementExists('droplow')) {
  276. $mform->removeElement('droplow');
  277. }
  278. }
  279. if ($id = $mform->getElementValue('id')) {
  280. $grade_category = grade_category::fetch(array('id'=>$id));
  281. $grade_item = $grade_category->load_grade_item();
  282. // remove agg coef if not used
  283. if ($grade_category->is_course_category()) {
  284. if ($mform->elementExists('parentcategory')) {
  285. $mform->removeElement('parentcategory');
  286. }
  287. if ($mform->elementExists('currentparentaggregation')) {
  288. $mform->removeElement('currentparentaggregation');
  289. }
  290. } else {
  291. // if we wanted to change parent of existing category - we would have to verify there are no circular references in parents!!!
  292. if ($mform->elementExists('parentcategory')) {
  293. $mform->hardFreeze('parentcategory');
  294. }
  295. $parent_cat = $grade_category->get_parent_category();
  296. $mform->setDefault('currentparentaggregation', $this->aggregation_options[$parent_cat->aggregation]);
  297. }
  298. // Prevent the user from using drop lowest/keep highest when the aggregation method cannot handle it.
  299. if (!$grade_category->can_apply_limit_rules()) {
  300. if ($mform->elementExists('keephigh')) {
  301. $mform->setConstant('keephigh', 0);
  302. $mform->hardFreeze('keephigh');
  303. }
  304. if ($mform->elementExists('droplow')) {
  305. $mform->setConstant('droplow', 0);
  306. $mform->hardFreeze('droplow');
  307. }
  308. }
  309. if ($grade_item->is_calculated()) {
  310. // following elements are ignored when calculation formula used
  311. if ($mform->elementExists('aggregation')) {
  312. $mform->removeElement('aggregation');
  313. }
  314. if ($mform->elementExists('keephigh')) {
  315. $mform->removeElement('keephigh');
  316. }
  317. if ($mform->elementExists('droplow')) {
  318. $mform->removeElement('droplow');
  319. }
  320. if ($mform->elementExists('aggregateonlygraded')) {
  321. $mform->removeElement('aggregateonlygraded');
  322. }
  323. if ($mform->elementExists('aggregateoutcomes')) {
  324. $mform->removeElement('aggregateoutcomes');
  325. }
  326. }
  327. // If it is a course category, remove the "required" rule from the "fullname" element
  328. if ($grade_category->is_course_category()) {
  329. unset($mform->_rules['fullname']);
  330. $key = array_search('fullname', $mform->_required);
  331. unset($mform->_required[$key]);
  332. }
  333. // If it is a course category and its fullname is ?, show an empty field
  334. if ($grade_category->is_course_category() && $mform->getElementValue('fullname') == '?') {
  335. $mform->setDefault('fullname', '');
  336. }
  337. // remove unwanted aggregation options
  338. if ($mform->elementExists('aggregation')) {
  339. $allaggoptions = array_keys($this->aggregation_options);
  340. $agg_el =& $mform->getElement('aggregation');
  341. $visible = explode(',', $CFG->grade_aggregations_visible);
  342. if (!is_null($grade_category->aggregation)) {
  343. // current type is always visible
  344. $visible[] = $grade_category->aggregation;
  345. }
  346. foreach ($allaggoptions as $type) {
  347. if (!in_array($type, $visible)) {
  348. $agg_el->removeOption($type);
  349. }
  350. }
  351. }
  352. } else {
  353. // adding new category
  354. if ($mform->elementExists('currentparentaggregation')) {
  355. $mform->removeElement('currentparentaggregation');
  356. }
  357. // remove unwanted aggregation options
  358. if ($mform->elementExists('aggregation')) {
  359. $allaggoptions = array_keys($this->aggregation_options);
  360. $agg_el =& $mform->getElement('aggregation');
  361. $visible = explode(',', $CFG->grade_aggregations_visible);
  362. foreach ($allaggoptions as $type) {
  363. if (!in_array($type, $visible)) {
  364. $agg_el->removeOption($type);
  365. }
  366. }
  367. }
  368. $mform->removeElement('grade_item_rescalegrades');
  369. }
  370. // no parent header for course category
  371. if (!$mform->elementExists('parentcategory')) {
  372. $mform->removeElement('headerparent');
  373. }
  374. /// GRADE ITEM
  375. if ($id = $mform->getElementValue('id')) {
  376. $grade_category = grade_category::fetch(array('id'=>$id));
  377. $grade_item = $grade_category->load_grade_item();
  378. $mform->setDefault('grade_item_hidden', (int) $grade_item->hidden);
  379. if ($grade_item->is_outcome_item()) {
  380. // we have to prevent incompatible modifications of outcomes if outcomes disabled
  381. $mform->removeElement('grade_item_grademax');
  382. if ($mform->elementExists('grade_item_grademin')) {
  383. $mform->removeElement('grade_item_grademin');
  384. }
  385. $mform->removeElement('grade_item_gradetype');
  386. $mform->removeElement('grade_item_display');
  387. $mform->removeElement('grade_item_decimals');
  388. $mform->hardFreeze('grade_item_scaleid');
  389. // Only show the option to rescale grades on a category if its corresponding grade_item has overridden grade_grades.
  390. } else if ($grade_item->has_overridden_grades()) {
  391. // Can't change the grade type or the scale if there are grades.
  392. $mform->hardFreeze('grade_item_gradetype, grade_item_scaleid');
  393. // If we are using scles then remove the unnecessary rescale and grade fields.
  394. if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
  395. $mform->removeElement('grade_item_rescalegrades');
  396. $mform->removeElement('grade_item_grademax');
  397. if ($mform->elementExists('grade_item_grademin')) {
  398. $mform->removeElement('grade_item_grademin');
  399. }
  400. } else { // Not using scale, so remove it.
  401. $mform->removeElement('grade_item_scaleid');
  402. $mform->disabledIf('grade_item_grademax', 'grade_item_rescalegrades', 'eq', '');
  403. $mform->disabledIf('grade_item_grademin', 'grade_item_rescalegrades', 'eq', '');
  404. }
  405. } else { // Remove the rescale element if there are no grades.
  406. $mform->removeElement('grade_item_rescalegrades');
  407. }
  408. //remove the aggregation coef element if not needed
  409. if ($grade_item->is_course_item()) {
  410. if ($mform->elementExists('grade_item_aggregationcoef')) {
  411. $mform->removeElement('grade_item_aggregationcoef');
  412. }
  413. if ($mform->elementExists('grade_item_weightoverride')) {
  414. $mform->removeElement('grade_item_weightoverride');
  415. }
  416. if ($mform->elementExists('grade_item_aggregationcoef2')) {
  417. $mform->removeElement('grade_item_aggregationcoef2');
  418. }
  419. } else {
  420. if ($grade_item->is_category_item()) {
  421. $category = $grade_item->get_item_category();
  422. $parent_category = $category->get_parent_category();
  423. } else {
  424. $parent_category = $grade_item->get_parent_category();
  425. }
  426. $parent_category->apply_forced_settings();
  427. if (!$parent_category->is_aggregationcoef_used()) {
  428. if ($mform->elementExists('grade_item_aggregationcoef')) {
  429. $mform->removeElement('grade_item_aggregationcoef');
  430. }
  431. } else {
  432. $coefstring = $grade_item->get_coefstring();
  433. if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
  434. // advcheckbox is not compatible with disabledIf!
  435. $coefstring = 'aggregationcoefextrasum';
  436. $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
  437. } else {
  438. $element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
  439. }
  440. $mform->insertElementBefore($element, 'parentcategory');
  441. $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
  442. }
  443. // Remove fields used by natural weighting if the parent category is not using natural weighting.
  444. // Or if the item is a scale and scales are not used in aggregation.
  445. if ($parent_category->aggregation != GRADE_AGGREGATE_SUM
  446. || (empty($CFG->grade_includescalesinaggregation) && $grade_item->gradetype == GRADE_TYPE_SCALE)) {
  447. if ($mform->elementExists('grade_item_weightoverride')) {
  448. $mform->removeElement('grade_item_weightoverride');
  449. }
  450. if ($mform->elementExists('grade_item_aggregationcoef2')) {
  451. $mform->removeElement('grade_item_aggregationcoef2');
  452. }
  453. }
  454. }
  455. }
  456. }
  457. /// perform extra validation before submission
  458. function validation($data, $files) {
  459. global $COURSE;
  460. $gradeitem = false;
  461. if ($data['id']) {
  462. $gradecategory = grade_category::fetch(array('id' => $data['id']));
  463. $gradeitem = $gradecategory->load_grade_item();
  464. }
  465. $errors = parent::validation($data, $files);
  466. if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
  467. if (empty($data['grade_item_scaleid'])) {
  468. $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
  469. }
  470. }
  471. if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
  472. if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
  473. ($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
  474. $data['grade_item_grademax'] < $data['grade_item_grademin'])) {
  475. $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
  476. $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
  477. }
  478. }
  479. if ($data['id'] && $gradeitem->has_overridden_grades()) {
  480. if ($gradeitem->gradetype == GRADE_TYPE_VALUE) {
  481. if (grade_floats_different($data['grade_item_grademin'], $gradeitem->grademin) ||
  482. grade_floats_different($data['grade_item_grademax'], $gradeitem->grademax)) {
  483. if (empty($data['grade_item_rescalegrades'])) {
  484. $errors['grade_item_rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
  485. }
  486. }
  487. }
  488. }
  489. return $errors;
  490. }
  491. }