PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/edit/tree/item_form.php

http://github.com/moodle/moodle
PHP | 483 lines | 350 code | 68 blank | 65 comment | 100 complexity | 2ff895f158420b90fe0c706470e08ca6 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. // advcheckbox is not compatible with disabledIf!
  158. $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
  159. $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  160. $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
  161. } else {
  162. $mform->addElement('static', 'hidden', get_string('hidden', 'grades'),
  163. get_string('componentcontrolsvisibility', 'grades'));
  164. // Unset hidden to avoid data override.
  165. unset($item->hidden);
  166. }
  167. $mform->addHelpButton('hidden', 'hidden', 'grades');
  168. /// locking
  169. $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
  170. $mform->addHelpButton('locked', 'locked', 'grades');
  171. $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
  172. $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
  173. /// parent category related settings
  174. $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  175. $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
  176. $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
  177. $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE);
  178. $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  179. $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
  180. $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
  181. $mform->setType('aggregationcoef2', PARAM_RAW);
  182. $mform->disabledIf('aggregationcoef2', 'weightoverride');
  183. $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE);
  184. $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  185. $options = array();
  186. $coefstring = '';
  187. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  188. foreach ($categories as $cat) {
  189. $cat->apply_forced_settings();
  190. $options[$cat->id] = $cat->get_name();
  191. }
  192. if (count($categories) > 1) {
  193. $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
  194. }
  195. /// hidden params
  196. $mform->addElement('hidden', 'id', 0);
  197. $mform->setType('id', PARAM_INT);
  198. $mform->addElement('hidden', 'courseid', $COURSE->id);
  199. $mform->setType('courseid', PARAM_INT);
  200. $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
  201. $mform->setType('itemtype', PARAM_ALPHA);
  202. /// add return tracking info
  203. $gpr = $this->_customdata['gpr'];
  204. $gpr->add_mform_elements($mform);
  205. /// mark advanced according to site settings
  206. if (isset($CFG->grade_item_advanced)) {
  207. $advanced = explode(',', $CFG->grade_item_advanced);
  208. foreach ($advanced as $el) {
  209. if ($mform->elementExists($el)) {
  210. $mform->setAdvanced($el);
  211. }
  212. }
  213. }
  214. //-------------------------------------------------------------------------------
  215. // buttons
  216. $this->add_action_buttons();
  217. //-------------------------------------------------------------------------------
  218. $this->set_data($item);
  219. }
  220. /// tweak the form - depending on existing data
  221. function definition_after_data() {
  222. global $CFG, $COURSE;
  223. $mform =& $this->_form;
  224. if ($id = $mform->getElementValue('id')) {
  225. $gradeitem = grade_item::fetch(array('id' => $id));
  226. $parentcategory = $gradeitem->get_parent_category();
  227. } else {
  228. // If we do not have an id, we are creating a new grade item.
  229. $gradeitem = new grade_item(array('courseid' => $COURSE->id, 'itemtype' => 'manual'), false);
  230. // Assign the course category to this grade item.
  231. $parentcategory = grade_category::fetch_course_category($COURSE->id);
  232. $gradeitem->parent_category = $parentcategory;
  233. }
  234. if (!$gradeitem->is_raw_used()) {
  235. $mform->removeElement('plusfactor');
  236. $mform->removeElement('multfactor');
  237. }
  238. if ($gradeitem->is_outcome_item()) {
  239. // We have to prevent incompatible modifications of outcomes if outcomes disabled.
  240. $mform->removeElement('grademax');
  241. if ($mform->elementExists('grademin')) {
  242. $mform->removeElement('grademin');
  243. }
  244. $mform->removeElement('gradetype');
  245. $mform->removeElement('display');
  246. $mform->removeElement('decimals');
  247. $mform->hardFreeze('scaleid');
  248. } else {
  249. if ($gradeitem->is_external_item()) {
  250. // Following items are set up from modules and should not be overrided by user.
  251. if ($mform->elementExists('grademin')) {
  252. // The site setting grade_report_showmin may have prevented grademin being added to the form.
  253. $mform->hardFreeze('grademin');
  254. }
  255. $mform->hardFreeze('itemname,gradetype,grademax,scaleid');
  256. if ($gradeitem->itemnumber == 0) {
  257. // The idnumber of grade itemnumber 0 is synced with course_modules.
  258. $mform->hardFreeze('idnumber');
  259. }
  260. // For external items we can not change the grade type, even if no grades exist, so if it is set to
  261. // scale, then remove the grademax and grademin fields from the form - no point displaying them.
  262. if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
  263. $mform->removeElement('grademax');
  264. if ($mform->elementExists('grademin')) {
  265. $mform->removeElement('grademin');
  266. }
  267. } else { // Not using scale, so remove it.
  268. $mform->removeElement('scaleid');
  269. }
  270. // Always remove the rescale grades element if it's an external item.
  271. $mform->removeElement('rescalegrades');
  272. } else if ($gradeitem->has_grades()) {
  273. // Can't change the grade type or the scale if there are grades.
  274. $mform->hardFreeze('gradetype, scaleid');
  275. // If we are using scales then remove the unnecessary rescale and grade fields.
  276. if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
  277. $mform->removeElement('rescalegrades');
  278. $mform->removeElement('grademax');
  279. if ($mform->elementExists('grademin')) {
  280. $mform->removeElement('grademin');
  281. }
  282. } else { // Remove the scale field.
  283. $mform->removeElement('scaleid');
  284. // Set the maximum grade to disabled unless a grade is chosen.
  285. $mform->disabledIf('grademax', 'rescalegrades', 'eq', '');
  286. }
  287. } else {
  288. // Remove the rescale element if there are no grades.
  289. $mform->removeElement('rescalegrades');
  290. }
  291. }
  292. // If we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
  293. if ($id && $mform->elementExists('parentcategory')) {
  294. $mform->hardFreeze('parentcategory');
  295. }
  296. $parentcategory->apply_forced_settings();
  297. if (!$parentcategory->is_aggregationcoef_used()) {
  298. if ($mform->elementExists('aggregationcoef')) {
  299. $mform->removeElement('aggregationcoef');
  300. }
  301. } else {
  302. $coefstring = $gradeitem->get_coefstring();
  303. if ($coefstring !== '') {
  304. if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
  305. // The advcheckbox is not compatible with disabledIf!
  306. $coefstring = 'aggregationcoefextrasum';
  307. $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
  308. } else {
  309. $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
  310. }
  311. if ($mform->elementExists('parentcategory')) {
  312. $mform->insertElementBefore($element, 'parentcategory');
  313. } else {
  314. $mform->insertElementBefore($element, 'id');
  315. }
  316. $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
  317. }
  318. $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE);
  319. $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  320. $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parentcategory->id);
  321. }
  322. // Remove fields used by natural weighting if the parent category is not using natural weighting.
  323. // Or if the item is a scale and scales are not used in aggregation.
  324. if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM
  325. || (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) {
  326. if ($mform->elementExists('weightoverride')) {
  327. $mform->removeElement('weightoverride');
  328. }
  329. if ($mform->elementExists('aggregationcoef2')) {
  330. $mform->removeElement('aggregationcoef2');
  331. }
  332. }
  333. if ($category = $gradeitem->get_item_category()) {
  334. if ($category->aggregation == GRADE_AGGREGATE_SUM) {
  335. if ($mform->elementExists('gradetype')) {
  336. $mform->hardFreeze('gradetype');
  337. }
  338. if ($mform->elementExists('grademin')) {
  339. $mform->hardFreeze('grademin');
  340. }
  341. if ($mform->elementExists('grademax')) {
  342. $mform->hardFreeze('grademax');
  343. }
  344. if ($mform->elementExists('scaleid')) {
  345. $mform->removeElement('scaleid');
  346. }
  347. }
  348. }
  349. // no parent header for course category
  350. if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
  351. $mform->removeElement('headerparent');
  352. }
  353. }
  354. /// perform extra validation before submission
  355. function validation($data, $files) {
  356. global $COURSE;
  357. $grade_item = false;
  358. if ($data['id']) {
  359. $grade_item = new grade_item(array('id' => $data['id'], 'courseid' => $data['courseid']));
  360. }
  361. $errors = parent::validation($data, $files);
  362. if (array_key_exists('idnumber', $data)) {
  363. if ($grade_item) {
  364. if ($grade_item->itemtype == 'mod') {
  365. $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
  366. } else {
  367. $cm = null;
  368. }
  369. } else {
  370. $grade_item = null;
  371. $cm = null;
  372. }
  373. if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
  374. $errors['idnumber'] = get_string('idnumbertaken');
  375. }
  376. }
  377. if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
  378. if (empty($data['scaleid'])) {
  379. $errors['scaleid'] = get_string('missingscale', 'grades');
  380. }
  381. }
  382. if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
  383. if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
  384. $errors['grademin'] = get_string('incorrectminmax', 'grades');
  385. $errors['grademax'] = get_string('incorrectminmax', 'grades');
  386. }
  387. }
  388. // We do not want the user to be able to change the grade type or scale for this item if grades exist.
  389. if ($grade_item && $grade_item->has_grades()) {
  390. // Check that grade type is set - should never not be set unless form has been modified.
  391. if (!isset($data['gradetype'])) {
  392. $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades');
  393. } else if ($data['gradetype'] !== $grade_item->gradetype) { // Check if we are changing the grade type.
  394. $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades');
  395. } else if ($data['gradetype'] == GRADE_TYPE_SCALE) {
  396. // Check if we are changing the scale - can't do this when grades exist.
  397. if (isset($data['scaleid']) && ($data['scaleid'] !== $grade_item->scaleid)) {
  398. $errors['scaleid'] = get_string('modgradecantchangescale', 'grades');
  399. }
  400. }
  401. }
  402. if ($grade_item) {
  403. if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
  404. if ((((bool) get_config('moodle', 'grade_report_showmin')) &&
  405. grade_floats_different($data['grademin'], $grade_item->grademin)) ||
  406. grade_floats_different($data['grademax'], $grade_item->grademax)) {
  407. if ($grade_item->has_grades() && empty($data['rescalegrades'])) {
  408. $errors['rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
  409. }
  410. }
  411. }
  412. }
  413. return $errors;
  414. }
  415. }