PageRenderTime 119ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/edit/tree/item_form.php

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