PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/outcome/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 160 lines | 94 code | 36 blank | 30 comment | 18 complexity | 159806720f32fd62af4b851fd72808d1 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. * Edit form for grade outcomes
  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_outcome_form extends moodleform {
  28. public function definition() {
  29. global $CFG, $COURSE;
  30. $mform =& $this->_form;
  31. // visible elements
  32. $mform->addElement('header', 'general', get_string('outcomes', 'grades'));
  33. $mform->addElement('text', 'fullname', get_string('outcomefullname', 'grades'), 'size="40"');
  34. $mform->addRule('fullname', get_string('required'), 'required');
  35. $mform->setType('fullname', PARAM_TEXT);
  36. $mform->addElement('text', 'shortname', get_string('outcomeshortname', 'grades'), 'size="20"');
  37. $mform->addRule('shortname', get_string('required'), 'required');
  38. $mform->setType('shortname', PARAM_NOTAGS);
  39. $mform->addElement('advcheckbox', 'standard', get_string('outcomestandard', 'grades'));
  40. $mform->addHelpButton('standard', 'outcomestandard', 'grades');
  41. $options = array();
  42. $mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
  43. array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
  44. $mform->addHelpButton('scaleid', 'typescale', 'grades');
  45. $mform->addRule('scaleid', get_string('required'), 'required');
  46. $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
  47. // hidden params
  48. $mform->addElement('hidden', 'id', 0);
  49. $mform->setType('id', PARAM_INT);
  50. $mform->addElement('hidden', 'courseid', 0);
  51. $mform->setType('courseid', PARAM_INT);
  52. /// add return tracking info
  53. $gpr = $this->_customdata['gpr'];
  54. $gpr->add_mform_elements($mform);
  55. //-------------------------------------------------------------------------------
  56. // buttons
  57. $this->add_action_buttons();
  58. }
  59. /// tweak the form - depending on existing data
  60. function definition_after_data() {
  61. global $CFG;
  62. $mform =& $this->_form;
  63. // first load proper scales
  64. if ($courseid = $mform->getElementValue('courseid')) {
  65. $options = array();
  66. if ($scales = grade_scale::fetch_all_local($courseid)) {
  67. $options[-1] = '--'.get_string('scalescustom');
  68. foreach($scales as $scale) {
  69. $options[$scale->id] = $scale->get_name();
  70. }
  71. }
  72. if ($scales = grade_scale::fetch_all_global()) {
  73. $options[-2] = '--'.get_string('scalesstandard');
  74. foreach($scales as $scale) {
  75. $options[$scale->id] = $scale->get_name();
  76. }
  77. }
  78. $scale_el =& $mform->getElement('scaleid');
  79. $scale_el->load($options);
  80. } else {
  81. $options = array();
  82. if ($scales = grade_scale::fetch_all_global()) {
  83. foreach($scales as $scale) {
  84. $options[$scale->id] = $scale->get_name();
  85. }
  86. }
  87. $scale_el =& $mform->getElement('scaleid');
  88. $scale_el->load($options);
  89. }
  90. if ($id = $mform->getElementValue('id')) {
  91. $outcome = grade_outcome::fetch(array('id'=>$id));
  92. $itemcount = $outcome->get_item_uses_count();
  93. $coursecount = $outcome->get_course_uses_count();
  94. if ($itemcount) {
  95. $mform->hardFreeze('scaleid');
  96. }
  97. if (empty($courseid)) {
  98. $mform->hardFreeze('standard');
  99. } else if (!has_capability('moodle/grade:manage', context_system::instance())) {
  100. $mform->hardFreeze('standard');
  101. } else if ($coursecount and empty($outcome->courseid)) {
  102. $mform->hardFreeze('standard');
  103. }
  104. } else {
  105. if (empty($courseid) or !has_capability('moodle/grade:manage', context_system::instance())) {
  106. $mform->hardFreeze('standard');
  107. }
  108. }
  109. }
  110. /// perform extra validation before submission
  111. function validation($data, $files) {
  112. $errors = parent::validation($data, $files);
  113. if ($data['scaleid'] < 1) {
  114. $errors['scaleid'] = get_string('required');
  115. }
  116. if (!empty($data['standard']) and $scale = grade_scale::fetch(array('id'=>$data['scaleid']))) {
  117. if (!empty($scale->courseid)) {
  118. //TODO: localize
  119. $errors['scaleid'] = 'Can not use custom scale in global outcome!';
  120. }
  121. }
  122. return $errors;
  123. }
  124. }