PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/edit/tree/category_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 484 lines | 352 code | 77 blank | 55 comment | 77 complexity | 3afa59e37d86c3e5e205e3c0d1a9e94a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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;
  31. $mform =& $this->_form;
  32. $category = $this->_customdata['current'];
  33. $this->aggregation_options = array(GRADE_AGGREGATE_MEAN =>get_string('aggregatemean', 'grades'),
  34. GRADE_AGGREGATE_WEIGHTED_MEAN =>get_string('aggregateweightedmean', 'grades'),
  35. GRADE_AGGREGATE_WEIGHTED_MEAN2 =>get_string('aggregateweightedmean2', 'grades'),
  36. GRADE_AGGREGATE_EXTRACREDIT_MEAN=>get_string('aggregateextracreditmean', 'grades'),
  37. GRADE_AGGREGATE_MEDIAN =>get_string('aggregatemedian', 'grades'),
  38. GRADE_AGGREGATE_MIN =>get_string('aggregatemin', 'grades'),
  39. GRADE_AGGREGATE_MAX =>get_string('aggregatemax', 'grades'),
  40. GRADE_AGGREGATE_MODE =>get_string('aggregatemode', 'grades'),
  41. GRADE_AGGREGATE_SUM =>get_string('aggregatesum', 'grades'));
  42. // visible elements
  43. $mform->addElement('header', 'headercategory', get_string('gradecategory', 'grades'));
  44. $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
  45. $mform->setType('fullname', PARAM_TEXT);
  46. $mform->addRule('fullname', null, 'required', null, 'client');
  47. $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
  48. $mform->addHelpButton('aggregation', 'aggregation', 'grades');
  49. if ((int)$CFG->grade_aggregation_flag & 2) {
  50. $mform->setAdvanced('aggregation');
  51. }
  52. $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
  53. $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
  54. $mform->disabledIf('aggregateonlygraded', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  55. if ((int)$CFG->grade_aggregateonlygraded_flag & 2) {
  56. $mform->setAdvanced('aggregateonlygraded');
  57. }
  58. if (empty($CFG->enableoutcomes)) {
  59. $mform->addElement('hidden', 'aggregateoutcomes');
  60. $mform->setType('aggregateoutcomes', PARAM_INT);
  61. } else {
  62. $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
  63. $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
  64. if ((int)$CFG->grade_aggregateoutcomes_flag & 2) {
  65. $mform->setAdvanced('aggregateoutcomes');
  66. }
  67. }
  68. $mform->addElement('advcheckbox', 'aggregatesubcats', get_string('aggregatesubcats', 'grades'));
  69. $mform->addHelpButton('aggregatesubcats', 'aggregatesubcats', 'grades');
  70. if ((int)$CFG->grade_aggregatesubcats_flag & 2) {
  71. $mform->setAdvanced('aggregatesubcats');
  72. }
  73. $mform->addElement('text', 'keephigh', get_string('keephigh', 'grades'), 'size="3"');
  74. $mform->setType('keephigh', PARAM_INT);
  75. $mform->addHelpButton('keephigh', 'keephigh', 'grades');
  76. if ((int)$CFG->grade_keephigh_flag & 2) {
  77. $mform->setAdvanced('keephigh');
  78. }
  79. $mform->addElement('text', 'droplow', get_string('droplow', 'grades'), 'size="3"');
  80. $mform->setType('droplow', PARAM_INT);
  81. $mform->addHelpButton('droplow', 'droplow', 'grades');
  82. $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  83. if ((int)$CFG->grade_droplow_flag & 2) {
  84. $mform->setAdvanced('droplow');
  85. }
  86. $mform->disabledIf('keephigh', 'droplow', 'noteq', 0);
  87. $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  88. // Grade item settings
  89. // Displayed as Category total to avoid confusion between grade items requiring marking and category totals
  90. $mform->addElement('header', 'general', get_string('categorytotal', 'grades'));
  91. $mform->addElement('text', 'grade_item_itemname', get_string('categorytotalname', 'grades'));
  92. $mform->setType('grade_item_itemname', PARAM_TEXT);
  93. $mform->setAdvanced('grade_item_itemname');
  94. $mform->addElement('text', 'grade_item_iteminfo', get_string('iteminfo', 'grades'));
  95. $mform->addHelpButton('grade_item_iteminfo', 'iteminfo', 'grades');
  96. $mform->setType('grade_item_iteminfo', PARAM_TEXT);
  97. $mform->addElement('text', 'grade_item_idnumber', get_string('idnumbermod'));
  98. $mform->addHelpButton('grade_item_idnumber', 'idnumbermod');
  99. $mform->setType('grade_item_idnumber', PARAM_RAW);
  100. $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
  101. GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
  102. GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
  103. GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
  104. $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
  105. $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
  106. $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
  107. $mform->disabledIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  108. //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
  109. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
  110. //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
  111. $options = array(0=>get_string('usenoscale', 'grades'));
  112. if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
  113. foreach ($scales as $scale) {
  114. $options[$scale->id] = $scale->get_name();
  115. }
  116. }
  117. if ($scales = grade_scale::fetch_all_global()) {
  118. foreach ($scales as $scale) {
  119. $options[$scale->id] = $scale->get_name();
  120. }
  121. }
  122. // ugly BC hack - it was possible to use custom scale from other courses :-(
  123. if (!empty($category->grade_item_scaleid) and !isset($options[$category->grade_item_scaleid])) {
  124. if ($scale = grade_scale::fetch(array('id'=>$category->grade_item_scaleid))) {
  125. $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
  126. }
  127. }
  128. $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
  129. $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
  130. $mform->disabledIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
  131. $mform->disabledIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  132. $mform->addElement('text', 'grade_item_grademax', get_string('grademax', 'grades'));
  133. $mform->setType('grade_item_grademax', PARAM_RAW);
  134. $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
  135. $mform->disabledIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
  136. $mform->disabledIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  137. $mform->addElement('text', 'grade_item_grademin', get_string('grademin', 'grades'));
  138. $mform->setType('grade_item_grademin', PARAM_RAW);
  139. $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
  140. $mform->disabledIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
  141. $mform->disabledIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
  142. $mform->addElement('text', 'grade_item_gradepass', get_string('gradepass', 'grades'));
  143. $mform->setType('grade_item_gradepass', PARAM_RAW);
  144. $mform->addHelpButton('grade_item_gradepass', 'gradepass', 'grades');
  145. $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
  146. $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_TEXT);
  147. /// grade display prefs
  148. $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
  149. $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'),
  150. GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
  151. GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
  152. GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'),
  153. GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'),
  154. GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'),
  155. GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'),
  156. GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'),
  157. GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'),
  158. GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades')
  159. );
  160. asort($options);
  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', 'grade_item_display', get_string('gradedisplaytype', 'grades'), $options);
  168. $mform->addHelpButton('grade_item_display', 'gradedisplaytype', 'grades');
  169. $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
  170. $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
  171. $mform->addElement('select', 'grade_item_decimals', get_string('decimalpoints', 'grades'), $options);
  172. $mform->addHelpButton('grade_item_decimals', 'decimalpoints', 'grades');
  173. $mform->setDefault('grade_item_decimals', -1);
  174. $mform->disabledIf('grade_item_decimals', 'grade_item_display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
  175. if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
  176. $mform->disabledIf('grade_item_decimals', 'grade_item_display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
  177. }
  178. /// hiding
  179. // advcheckbox is not compatible with disabledIf!
  180. $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
  181. $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
  182. $mform->addElement('date_time_selector', 'grade_item_hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  183. $mform->disabledIf('grade_item_hidden', 'grade_item_hiddenuntil[off]', 'notchecked');
  184. /// locking
  185. $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
  186. $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
  187. $mform->addElement('date_time_selector', 'grade_item_locktime', get_string('locktime', 'grades'), array('optional'=>true));
  188. $mform->disabledIf('grade_item_locktime', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
  189. /// parent category related settings
  190. $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  191. $options = array();
  192. $default = -1;
  193. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  194. foreach ($categories as $cat) {
  195. $cat->apply_forced_settings();
  196. $options[$cat->id] = $cat->get_name();
  197. if ($cat->is_course_category()) {
  198. $default = $cat->id;
  199. }
  200. }
  201. if (count($categories) > 1) {
  202. $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
  203. $mform->setDefault('parentcategory', $default);
  204. $mform->addElement('static', 'currentparentaggregation', get_string('currentparentaggregation', 'grades'));
  205. }
  206. // hidden params
  207. $mform->addElement('hidden', 'id', 0);
  208. $mform->setType('id', PARAM_INT);
  209. $mform->addElement('hidden', 'courseid', 0);
  210. $mform->setType('courseid', PARAM_INT);
  211. /// add return tracking info
  212. $gpr = $this->_customdata['gpr'];
  213. $gpr->add_mform_elements($mform);
  214. /// mark advanced according to site settings
  215. if (isset($CFG->grade_item_advanced)) {
  216. $advanced = explode(',', $CFG->grade_item_advanced);
  217. foreach ($advanced as $el) {
  218. $el = 'grade_item_'.$el;
  219. if ($mform->elementExists($el)) {
  220. $mform->setAdvanced($el);
  221. }
  222. }
  223. }
  224. //-------------------------------------------------------------------------------
  225. // buttons
  226. $this->add_action_buttons();
  227. //-------------------------------------------------------------------------------
  228. $this->set_data($category);
  229. }
  230. /// tweak the form - depending on existing data
  231. function definition_after_data() {
  232. global $CFG, $COURSE;
  233. $mform =& $this->_form;
  234. $somecat = new grade_category();
  235. foreach ($somecat->forceable as $property) {
  236. if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
  237. if ($mform->elementExists($property)) {
  238. if (empty($CFG->grade_hideforcedsettings)) {
  239. $mform->hardFreeze($property);
  240. } else {
  241. if ($mform->elementExists($property)) {
  242. $mform->removeElement($property);
  243. }
  244. }
  245. }
  246. }
  247. }
  248. if ($CFG->grade_droplow > 0) {
  249. if ($mform->elementExists('keephigh')) {
  250. $mform->removeElement('keephigh');
  251. }
  252. } else if ($CFG->grade_keephigh > 0) {
  253. if ($mform->elementExists('droplow')) {
  254. $mform->removeElement('droplow');
  255. }
  256. }
  257. if ($id = $mform->getElementValue('id')) {
  258. $grade_category = grade_category::fetch(array('id'=>$id));
  259. $grade_item = $grade_category->load_grade_item();
  260. // remove agg coef if not used
  261. if ($grade_category->is_course_category()) {
  262. if ($mform->elementExists('parentcategory')) {
  263. $mform->removeElement('parentcategory');
  264. }
  265. if ($mform->elementExists('currentparentaggregation')) {
  266. $mform->removeElement('currentparentaggregation');
  267. }
  268. } else {
  269. // if we wanted to change parent of existing category - we would have to verify there are no circular references in parents!!!
  270. if ($mform->elementExists('parentcategory')) {
  271. $mform->hardFreeze('parentcategory');
  272. }
  273. $parent_cat = $grade_category->get_parent_category();
  274. $mform->setDefault('currentparentaggregation', $this->aggregation_options[$parent_cat->aggregation]);
  275. }
  276. if ($grade_item->is_calculated()) {
  277. // following elements are ignored when calculation formula used
  278. if ($mform->elementExists('aggregation')) {
  279. $mform->removeElement('aggregation');
  280. }
  281. if ($mform->elementExists('keephigh')) {
  282. $mform->removeElement('keephigh');
  283. }
  284. if ($mform->elementExists('droplow')) {
  285. $mform->removeElement('droplow');
  286. }
  287. if ($mform->elementExists('aggregateonlygraded')) {
  288. $mform->removeElement('aggregateonlygraded');
  289. }
  290. if ($mform->elementExists('aggregateoutcomes')) {
  291. $mform->removeElement('aggregateoutcomes');
  292. }
  293. if ($mform->elementExists('aggregatesubcats')) {
  294. $mform->removeElement('aggregatesubcats');
  295. }
  296. }
  297. // If it is a course category, remove the "required" rule from the "fullname" element
  298. if ($grade_category->is_course_category()) {
  299. unset($mform->_rules['fullname']);
  300. $key = array_search('fullname', $mform->_required);
  301. unset($mform->_required[$key]);
  302. }
  303. // If it is a course category and its fullname is ?, show an empty field
  304. if ($grade_category->is_course_category() && $mform->getElementValue('fullname') == '?') {
  305. $mform->setDefault('fullname', '');
  306. }
  307. // remove unwanted aggregation options
  308. if ($mform->elementExists('aggregation')) {
  309. $allaggoptions = array_keys($this->aggregation_options);
  310. $agg_el =& $mform->getElement('aggregation');
  311. $visible = explode(',', $CFG->grade_aggregations_visible);
  312. if (!is_null($grade_category->aggregation)) {
  313. // current type is always visible
  314. $visible[] = $grade_category->aggregation;
  315. }
  316. foreach ($allaggoptions as $type) {
  317. if (!in_array($type, $visible)) {
  318. $agg_el->removeOption($type);
  319. }
  320. }
  321. }
  322. } else {
  323. // adding new category
  324. if ($mform->elementExists('currentparentaggregation')) {
  325. $mform->removeElement('currentparentaggregation');
  326. }
  327. // remove unwanted aggregation options
  328. if ($mform->elementExists('aggregation')) {
  329. $allaggoptions = array_keys($this->aggregation_options);
  330. $agg_el =& $mform->getElement('aggregation');
  331. $visible = explode(',', $CFG->grade_aggregations_visible);
  332. foreach ($allaggoptions as $type) {
  333. if (!in_array($type, $visible)) {
  334. $agg_el->removeOption($type);
  335. }
  336. }
  337. }
  338. }
  339. // no parent header for course category
  340. if (!$mform->elementExists('parentcategory')) {
  341. $mform->removeElement('headerparent');
  342. }
  343. /// GRADE ITEM
  344. if ($id = $mform->getElementValue('id')) {
  345. $grade_category = grade_category::fetch(array('id'=>$id));
  346. $grade_item = $grade_category->load_grade_item();
  347. $mform->setDefault('grade_item_hidden', (int) $grade_item->hidden);
  348. if ($grade_item->is_outcome_item()) {
  349. // we have to prevent incompatible modifications of outcomes if outcomes disabled
  350. $mform->removeElement('grade_item_grademax');
  351. $mform->removeElement('grade_item_grademin');
  352. $mform->removeElement('grade_item_gradetype');
  353. $mform->removeElement('grade_item_display');
  354. $mform->removeElement('grade_item_decimals');
  355. $mform->hardFreeze('grade_item_scaleid');
  356. }
  357. //remove the aggregation coef element if not needed
  358. if ($grade_item->is_course_item()) {
  359. if ($mform->elementExists('grade_item_aggregationcoef')) {
  360. $mform->removeElement('grade_item_aggregationcoef');
  361. }
  362. } else {
  363. if ($grade_item->is_category_item()) {
  364. $category = $grade_item->get_item_category();
  365. $parent_category = $category->get_parent_category();
  366. } else {
  367. $parent_category = $grade_item->get_parent_category();
  368. }
  369. $parent_category->apply_forced_settings();
  370. if (!$parent_category->is_aggregationcoef_used()) {
  371. if ($mform->elementExists('grade_item_aggregationcoef')) {
  372. $mform->removeElement('grade_item_aggregationcoef');
  373. }
  374. } else {
  375. $coefstring = $grade_item->get_coefstring();
  376. if ($coefstring == 'aggregationcoefextrasum') {
  377. // advcheckbox is not compatible with disabledIf!
  378. $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
  379. } else {
  380. $element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
  381. }
  382. $mform->insertElementBefore($element, 'parentcategory');
  383. $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
  384. }
  385. }
  386. }
  387. }
  388. /// perform extra validation before submission
  389. function validation($data, $files) {
  390. global $COURSE;
  391. $errors = parent::validation($data, $files);
  392. if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
  393. if (empty($data['grade_item_scaleid'])) {
  394. $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
  395. }
  396. }
  397. if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
  398. if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
  399. ($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
  400. $data['grade_item_grademax'] < $data['grade_item_grademin'])) {
  401. $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
  402. $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
  403. }
  404. }
  405. return $errors;
  406. }
  407. }