PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/tree/outcomeitem_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 254 lines | 168 code | 47 blank | 39 comment | 55 complexity | ef7568c3cbb6a6fa46e56bd7dbfb80c0 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 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. $options = array();
  77. $default = '';
  78. $coefstring = '';
  79. $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
  80. foreach ($categories as $cat) {
  81. $cat->apply_forced_settings();
  82. $options[$cat->id] = $cat->get_name();
  83. if ($cat->is_course_category()) {
  84. $default = $cat->id;
  85. }
  86. if ($cat->is_aggregationcoef_used()) {
  87. if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
  88. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
  89. } else if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  90. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
  91. } else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
  92. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextraweight') ? 'aggregationcoefextraweight' : 'aggregationcoef';
  93. } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
  94. $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
  95. } else {
  96. $coefstring = 'aggregationcoef';
  97. }
  98. } else {
  99. $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id);
  100. }
  101. }
  102. if (count($categories) > 1) {
  103. $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
  104. $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
  105. }
  106. if ($coefstring !== '') {
  107. if ($coefstring == 'aggregationcoefextrasum') {
  108. // advcheckbox is not compatible with disabledIf!
  109. $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
  110. } else {
  111. $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
  112. }
  113. $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
  114. }
  115. /// hidden params
  116. $mform->addElement('hidden', 'id', 0);
  117. $mform->setType('id', PARAM_INT);
  118. $mform->addElement('hidden', 'courseid', $COURSE->id);
  119. $mform->setType('courseid', PARAM_INT);
  120. /// add return tracking info
  121. $gpr = $this->_customdata['gpr'];
  122. $gpr->add_mform_elements($mform);
  123. /// mark advanced according to site settings
  124. if (isset($CFG->grade_item_advanced)) {
  125. $advanced = explode(',', $CFG->grade_item_advanced);
  126. foreach ($advanced as $el) {
  127. if ($mform->elementExists($el)) {
  128. $mform->setAdvanced($el);
  129. }
  130. }
  131. }
  132. //-------------------------------------------------------------------------------
  133. // buttons
  134. $this->add_action_buttons();
  135. }
  136. /// tweak the form - depending on existing data
  137. function definition_after_data() {
  138. global $CFG, $COURSE;
  139. $mform =& $this->_form;
  140. if ($id = $mform->getElementValue('id')) {
  141. $grade_item = grade_item::fetch(array('id'=>$id));
  142. //remove the aggregation coef element if not needed
  143. if ($grade_item->is_course_item()) {
  144. if ($mform->elementExists('parentcategory')) {
  145. $mform->removeElement('parentcategory');
  146. }
  147. if ($mform->elementExists('aggregationcoef')) {
  148. $mform->removeElement('aggregationcoef');
  149. }
  150. } else {
  151. // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
  152. if ($mform->elementExists('parentcategory')) {
  153. $mform->hardFreeze('parentcategory');
  154. }
  155. if ($grade_item->is_category_item()) {
  156. $category = $grade_item->get_item_category();
  157. $parent_category = $category->get_parent_category();
  158. } else {
  159. $parent_category = $grade_item->get_parent_category();
  160. }
  161. $parent_category->apply_forced_settings();
  162. if (!$parent_category->is_aggregationcoef_used() or $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
  163. if ($mform->elementExists('aggregationcoef')) {
  164. $mform->removeElement('aggregationcoef');
  165. }
  166. } else {
  167. //fix label if needed
  168. $agg_el =& $mform->getElement('aggregationcoef');
  169. $aggcoef = '';
  170. if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
  171. $aggcoef = 'aggregationcoefweight';
  172. } else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  173. $aggcoef = 'aggregationcoefextrasum';
  174. } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
  175. $aggcoef = 'aggregationcoefextraweight';
  176. } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
  177. $aggcoef = 'aggregationcoefextrasum';
  178. }
  179. if ($aggcoef !== '') {
  180. $agg_el->setLabel(get_string($aggcoef, 'grades'));
  181. $mform->addHelpButton('aggregationcoef', $aggcoef, 'grades');
  182. }
  183. }
  184. }
  185. }
  186. // no parent header for course category
  187. if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
  188. $mform->removeElement('headerparent');
  189. }
  190. }
  191. /// perform extra validation before submission
  192. function validation($data, $files) {
  193. global $COURSE;
  194. $errors = parent::validation($data, $files);
  195. if (array_key_exists('idnumber', $data)) {
  196. if ($data['id']) {
  197. $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  198. } else {
  199. $grade_item = null;
  200. }
  201. if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
  202. $errors['idnumber'] = get_string('idnumbertaken');
  203. }
  204. }
  205. return $errors;
  206. }
  207. }