PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/edit/tree/item_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 352 lines | 244 code | 60 blank | 48 comment | 53 complexity | db05d09c274ff1d118c389c699beadc4 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  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->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
  37. $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
  38. $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
  39. $mform->addHelpButton('idnumber', 'idnumbermod');
  40. $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
  41. GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
  42. GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
  43. GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
  44. $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
  45. $mform->addHelpButton('gradetype', 'gradetype', 'grades');
  46. $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
  47. //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
  48. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  49. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
  50. $options = array(0=>get_string('usenoscale', 'grades'));
  51. if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
  52. foreach ($scales as $scale) {
  53. $options[$scale->id] = $scale->get_name();
  54. }
  55. }
  56. if ($scales = grade_scale::fetch_all_global()) {
  57. foreach ($scales as $scale) {
  58. $options[$scale->id] = $scale->get_name();
  59. }
  60. }
  61. // ugly BC hack - it was possible to use custom scale from other courses :-(
  62. if (!empty($item->scaleid) and !isset($options[$item->scaleid])) {
  63. if ($scale = grade_scale::fetch(array('id'=>$item->scaleid))) {
  64. $options[$scale->id] = $scale->get_name().get_string('incorrectcustomscale', 'grades');
  65. }
  66. }
  67. $mform->addElement('select', 'scaleid', get_string('scale'), $options);
  68. $mform->addHelpButton('scaleid', 'typescale', 'grades');
  69. $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
  70. $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
  71. $mform->addHelpButton('grademax', 'grademax', 'grades');
  72. $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
  73. $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
  74. $mform->addHelpButton('grademin', 'grademin', 'grades');
  75. $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
  76. $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
  77. $mform->addHelpButton('gradepass', 'gradepass', 'grades');
  78. $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
  79. $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  80. $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
  81. $mform->addHelpButton('multfactor', 'multfactor', 'grades');
  82. $mform->setAdvanced('multfactor');
  83. $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
  84. $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  85. $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
  86. $mform->addHelpButton('plusfactor', 'plusfactor', 'grades');
  87. $mform->setAdvanced('plusfactor');
  88. $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
  89. $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  90. /// grade display prefs
  91. $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
  92. $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'),
  93. GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  94. GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  95. GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'),
  96. GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'),
  97. GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'),
  98. GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'),
  99. GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'),
  100. GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'),
  101. GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades')
  102. );
  103. asort($options);
  104. foreach ($options as $key=>$option) {
  105. if ($key == $default_gradedisplaytype) {
  106. $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
  107. break;
  108. }
  109. }
  110. $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options);
  111. $mform->addHelpButton('display', 'gradedisplaytype', 'grades');
  112. $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
  113. $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
  114. $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options);
  115. $mform->addHelpButton('decimals', 'decimalpoints', 'grades');
  116. $mform->setDefault('decimals', -1);
  117. $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
  118. if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
  119. $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
  120. }
  121. /// hiding
  122. if ($item->cancontrolvisibility) {
  123. // advcheckbox is not compatible with disabledIf!
  124. $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
  125. $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  126. $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
  127. } else {
  128. $mform->addElement('static', 'hidden', get_string('hidden', 'grades'),
  129. get_string('componentcontrolsvisibility', 'grades'));
  130. // Unset hidden to avoid data override.
  131. unset($item->hidden);
  132. }
  133. $mform->addHelpButton('hidden', 'hidden', 'grades');
  134. /// locking
  135. $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
  136. $mform->addHelpButton('locked', 'locked', 'grades');
  137. $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
  138. $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
  139. /// parent category related settings
  140. $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  141. $options = array();
  142. $coefstring = '';
  143. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  144. foreach ($categories as $cat) {
  145. $cat->apply_forced_settings();
  146. $options[$cat->id] = $cat->get_name();
  147. }
  148. if (count($categories) > 1) {
  149. $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
  150. }
  151. /// hidden params
  152. $mform->addElement('hidden', 'id', 0);
  153. $mform->setType('id', PARAM_INT);
  154. $mform->addElement('hidden', 'courseid', $COURSE->id);
  155. $mform->setType('courseid', PARAM_INT);
  156. $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
  157. $mform->setType('itemtype', PARAM_ALPHA);
  158. /// add return tracking info
  159. $gpr = $this->_customdata['gpr'];
  160. $gpr->add_mform_elements($mform);
  161. /// mark advanced according to site settings
  162. if (isset($CFG->grade_item_advanced)) {
  163. $advanced = explode(',', $CFG->grade_item_advanced);
  164. foreach ($advanced as $el) {
  165. if ($mform->elementExists($el)) {
  166. $mform->setAdvanced($el);
  167. }
  168. }
  169. }
  170. //-------------------------------------------------------------------------------
  171. // buttons
  172. $this->add_action_buttons();
  173. //-------------------------------------------------------------------------------
  174. $this->set_data($item);
  175. }
  176. /// tweak the form - depending on existing data
  177. function definition_after_data() {
  178. global $CFG, $COURSE;
  179. $mform =& $this->_form;
  180. if ($id = $mform->getElementValue('id')) {
  181. $grade_item = grade_item::fetch(array('id'=>$id));
  182. if (!$grade_item->is_raw_used()) {
  183. $mform->removeElement('plusfactor');
  184. $mform->removeElement('multfactor');
  185. }
  186. if ($grade_item->is_outcome_item()) {
  187. // we have to prevent incompatible modifications of outcomes if outcomes disabled
  188. $mform->removeElement('grademax');
  189. $mform->removeElement('grademin');
  190. $mform->removeElement('gradetype');
  191. $mform->removeElement('display');
  192. $mform->removeElement('decimals');
  193. $mform->hardFreeze('scaleid');
  194. } else {
  195. if ($grade_item->is_external_item()) {
  196. // following items are set up from modules and should not be overrided by user
  197. $mform->hardFreeze('itemname,gradetype,grademax,grademin,scaleid');
  198. if ($grade_item->itemnumber == 0) {
  199. // the idnumber of grade itemnumber 0 is synced with course_modules
  200. $mform->hardFreeze('idnumber');
  201. }
  202. //$mform->removeElement('calculation');
  203. }
  204. }
  205. // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
  206. if ($mform->elementExists('parentcategory')) {
  207. $mform->hardFreeze('parentcategory');
  208. }
  209. $parent_category = $grade_item->get_parent_category();
  210. $parent_category->apply_forced_settings();
  211. if (!$parent_category->is_aggregationcoef_used()) {
  212. if ($mform->elementExists('aggregationcoef')) {
  213. $mform->removeElement('aggregationcoef');
  214. }
  215. } else {
  216. $coefstring = $grade_item->get_coefstring();
  217. if ($coefstring !== '') {
  218. if ($coefstring == 'aggregationcoefextrasum') {
  219. // advcheckbox is not compatible with disabledIf!
  220. $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
  221. } else {
  222. $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
  223. }
  224. if ($mform->elementExists('parentcategory')) {
  225. $mform->insertElementBefore($element, 'parentcategory');
  226. } else {
  227. $mform->insertElementBefore($element, 'id');
  228. }
  229. $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
  230. }
  231. $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
  232. }
  233. if ($category = $grade_item->get_item_category()) {
  234. if ($category->aggregation == GRADE_AGGREGATE_SUM) {
  235. if ($mform->elementExists('gradetype')) {
  236. $mform->hardFreeze('gradetype');
  237. }
  238. if ($mform->elementExists('grademin')) {
  239. $mform->hardFreeze('grademin');
  240. }
  241. if ($mform->elementExists('grademax')) {
  242. $mform->hardFreeze('grademax');
  243. }
  244. if ($mform->elementExists('scaleid')) {
  245. $mform->removeElement('scaleid');
  246. }
  247. }
  248. }
  249. } else {
  250. // all new items are manual, children of course category
  251. $mform->removeElement('plusfactor');
  252. $mform->removeElement('multfactor');
  253. }
  254. // no parent header for course category
  255. if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
  256. $mform->removeElement('headerparent');
  257. }
  258. }
  259. /// perform extra validation before submission
  260. function validation($data, $files) {
  261. global $COURSE;
  262. $errors = parent::validation($data, $files);
  263. if (array_key_exists('idnumber', $data)) {
  264. if ($data['id']) {
  265. $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  266. if ($grade_item->itemtype == 'mod') {
  267. $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
  268. } else {
  269. $cm = null;
  270. }
  271. } else {
  272. $grade_item = null;
  273. $cm = null;
  274. }
  275. if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
  276. $errors['idnumber'] = get_string('idnumbertaken');
  277. }
  278. }
  279. if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
  280. if (empty($data['scaleid'])) {
  281. $errors['scaleid'] = get_string('missingscale', 'grades');
  282. }
  283. }
  284. if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
  285. if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
  286. $errors['grademin'] = get_string('incorrectminmax', 'grades');
  287. $errors['grademax'] = get_string('incorrectminmax', 'grades');
  288. }
  289. }
  290. return $errors;
  291. }
  292. }