PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/tree/outcomeitem_form.php

https://bitbucket.org/moodle/moodle
PHP | 275 lines | 184 code | 50 blank | 41 comment | 61 complexity | 71f31e432cbfc5122ca792fdff00e50d 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 allow the creation and editing of outcome grade items
  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_outcomeitem_form extends moodleform {
  28. function definition() {
  29. global $COURSE, $CFG;
  30. $mform =& $this->_form;
  31. /// visible elements
  32. $mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades'));
  33. $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
  34. $mform->addRule('itemname', get_string('required'), 'required', null, 'client');
  35. $mform->setType('itemname', PARAM_TEXT);
  36. $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
  37. $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
  38. $mform->setType('iteminfo', PARAM_TEXT);
  39. $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
  40. $mform->addHelpButton('idnumber', 'idnumbermod');
  41. $mform->setType('idnumber', PARAM_RAW);
  42. // allow setting of outcomes on module items too
  43. $options = array();
  44. if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
  45. foreach ($outcomes as $outcome) {
  46. $options[$outcome->id] = $outcome->get_name();
  47. }
  48. }
  49. $mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null,
  50. array('link' => $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$COURSE->id, 'label' => get_string('outcomeassigntocourse', 'grades')));
  51. $mform->addHelpButton('outcomeid', 'outcome', 'grades');
  52. $mform->addRule('outcomeid', get_string('required'), 'required');
  53. $options = array(0=>get_string('none'));
  54. if ($coursemods = get_course_mods($COURSE->id)) {
  55. foreach ($coursemods as $coursemod) {
  56. if ($mod = get_coursemodule_from_id($coursemod->modname, $coursemod->id)) {
  57. $options[$coursemod->id] = format_string($mod->name);
  58. }
  59. }
  60. }
  61. $mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options);
  62. $mform->addHelpButton('cmid', 'linkedactivity', 'grades');
  63. $mform->setDefault('cmid', 0);
  64. /// hiding
  65. /// advcheckbox is not compatible with disabledIf !!
  66. $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
  67. $mform->addHelpButton('hidden', 'hidden', 'grades');
  68. $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  69. $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
  70. //locking
  71. $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
  72. $mform->addHelpButton('locked', 'locked', 'grades');
  73. $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
  74. /// parent category related settings
  75. $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  76. $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
  77. $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
  78. $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
  79. $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
  80. $mform->setType('aggregationcoef2', PARAM_RAW);
  81. $mform->disabledIf('aggregationcoef2', 'weightoverride');
  82. $options = array();
  83. $default = '';
  84. $coefstring = '';
  85. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  86. foreach ($categories as $cat) {
  87. $cat->apply_forced_settings();
  88. $options[$cat->id] = $cat->get_name();
  89. if ($cat->is_course_category()) {
  90. $default = $cat->id;
  91. }
  92. if ($cat->is_aggregationcoef_used()) {
  93. if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
  94. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
  95. } else if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  96. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
  97. } else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
  98. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextraweight') ? 'aggregationcoefextraweight' : 'aggregationcoef';
  99. } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
  100. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
  101. } else {
  102. $coefstring = 'aggregationcoef';
  103. }
  104. } else {
  105. $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id);
  106. }
  107. }
  108. if (count($categories) > 1) {
  109. $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
  110. $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
  111. }
  112. if ($coefstring !== '') {
  113. if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
  114. // advcheckbox is not compatible with disabledIf!
  115. $coefstring = 'aggregationcoefextrasum';
  116. $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
  117. } else {
  118. $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
  119. }
  120. $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
  121. }
  122. /// hidden params
  123. $mform->addElement('hidden', 'id', 0);
  124. $mform->setType('id', PARAM_INT);
  125. $mform->addElement('hidden', 'courseid', $COURSE->id);
  126. $mform->setType('courseid', PARAM_INT);
  127. /// add return tracking info
  128. $gpr = $this->_customdata['gpr'];
  129. $gpr->add_mform_elements($mform);
  130. /// mark advanced according to site settings
  131. if (isset($CFG->grade_item_advanced)) {
  132. $advanced = explode(',', $CFG->grade_item_advanced);
  133. foreach ($advanced as $el) {
  134. if ($mform->elementExists($el)) {
  135. $mform->setAdvanced($el);
  136. }
  137. }
  138. }
  139. //-------------------------------------------------------------------------------
  140. // buttons
  141. $this->add_action_buttons();
  142. }
  143. /// tweak the form - depending on existing data
  144. function definition_after_data() {
  145. global $CFG, $COURSE;
  146. $mform =& $this->_form;
  147. if ($id = $mform->getElementValue('id')) {
  148. $grade_item = grade_item::fetch(array('id'=>$id));
  149. //remove the aggregation coef element if not needed
  150. if ($grade_item->is_course_item()) {
  151. if ($mform->elementExists('parentcategory')) {
  152. $mform->removeElement('parentcategory');
  153. }
  154. if ($mform->elementExists('aggregationcoef')) {
  155. $mform->removeElement('aggregationcoef');
  156. }
  157. } else {
  158. // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
  159. if ($mform->elementExists('parentcategory')) {
  160. $mform->hardFreeze('parentcategory');
  161. }
  162. if ($grade_item->is_category_item()) {
  163. $category = $grade_item->get_item_category();
  164. $parent_category = $category->get_parent_category();
  165. } else {
  166. $parent_category = $grade_item->get_parent_category();
  167. }
  168. $parent_category->apply_forced_settings();
  169. if (!$parent_category->is_aggregationcoef_used() || !$parent_category->aggregateoutcomes) {
  170. if ($mform->elementExists('aggregationcoef')) {
  171. $mform->removeElement('aggregationcoef');
  172. }
  173. } else {
  174. //fix label if needed
  175. $agg_el =& $mform->getElement('aggregationcoef');
  176. $aggcoef = '';
  177. if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
  178. $aggcoef = 'aggregationcoefweight';
  179. } else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  180. $aggcoef = 'aggregationcoefextrasum';
  181. } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
  182. $aggcoef = 'aggregationcoefextraweight';
  183. } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
  184. $aggcoef = 'aggregationcoefextrasum';
  185. }
  186. if ($aggcoef !== '') {
  187. $agg_el->setLabel(get_string($aggcoef, 'grades'));
  188. $mform->addHelpButton('aggregationcoef', $aggcoef, 'grades');
  189. }
  190. }
  191. // Remove the natural weighting fields for other aggregations,
  192. // or when the category does not aggregate outcomes.
  193. if ($parent_category->aggregation != GRADE_AGGREGATE_SUM ||
  194. !$parent_category->aggregateoutcomes) {
  195. if ($mform->elementExists('weightoverride')) {
  196. $mform->removeElement('weightoverride');
  197. }
  198. if ($mform->elementExists('aggregationcoef2')) {
  199. $mform->removeElement('aggregationcoef2');
  200. }
  201. }
  202. }
  203. }
  204. // no parent header for course category
  205. if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
  206. $mform->removeElement('headerparent');
  207. }
  208. }
  209. /// perform extra validation before submission
  210. function validation($data, $files) {
  211. global $COURSE;
  212. $errors = parent::validation($data, $files);
  213. if (array_key_exists('idnumber', $data)) {
  214. if ($data['id']) {
  215. $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  216. } else {
  217. $grade_item = null;
  218. }
  219. if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
  220. $errors['idnumber'] = get_string('idnumbertaken');
  221. }
  222. }
  223. return $errors;
  224. }
  225. }