weekly release 5.0dev
[moodle.git] / grade / edit / outcome / edit_form.php
blobfd06079b8094d571c4c0c1e14c9bf7f660266336
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.
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/>.
17 /**
18 * Edit form for grade outcomes
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once $CFG->libdir.'/formslib.php';
31 class edit_outcome_form extends moodleform {
32 public function definition() {
33 global $CFG, $COURSE;
34 $mform =& $this->_form;
36 // visible elements
37 $mform->addElement('header', 'general', get_string('outcomes', 'grades'));
39 $mform->addElement('text', 'fullname', get_string('outcomefullname', 'grades'), 'size="40"');
40 $mform->addRule('fullname', get_string('required'), 'required');
41 $mform->setType('fullname', PARAM_TEXT);
43 $mform->addElement('text', 'shortname', get_string('outcomeshortname', 'grades'), 'size="20"');
44 $mform->addRule('shortname', get_string('required'), 'required');
45 $mform->setType('shortname', PARAM_NOTAGS);
47 $mform->addElement('advcheckbox', 'standard', get_string('outcomestandard', 'grades'));
48 $mform->addHelpButton('standard', 'outcomestandard', 'grades');
50 $options = array();
52 $mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
53 array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
54 $mform->addHelpButton('scaleid', 'typescale', 'grades');
55 $mform->addRule('scaleid', get_string('required'), 'required');
57 $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
60 // hidden params
61 $mform->addElement('hidden', 'id', 0);
62 $mform->setType('id', PARAM_INT);
64 $mform->addElement('hidden', 'courseid', 0);
65 $mform->setType('courseid', PARAM_INT);
67 /// add return tracking info
68 $gpr = $this->_customdata['gpr'];
69 $gpr->add_mform_elements($mform);
71 //-------------------------------------------------------------------------------
72 // buttons
73 $this->add_action_buttons();
77 /// tweak the form - depending on existing data
78 function definition_after_data() {
79 global $CFG;
81 $mform =& $this->_form;
83 // first load proper scales
84 if ($courseid = $mform->getElementValue('courseid')) {
85 $options = array();
86 if ($scales = grade_scale::fetch_all_local($courseid)) {
87 $options[-1] = '--'.get_string('scalescustom');
88 foreach($scales as $scale) {
89 $options[$scale->id] = $scale->get_name();
92 if ($scales = grade_scale::fetch_all_global()) {
93 $options[-2] = '--'.get_string('scalesstandard');
94 foreach($scales as $scale) {
95 $options[$scale->id] = $scale->get_name();
98 $scale_el =& $mform->getElement('scaleid');
99 $scale_el->load($options);
101 } else {
102 $options = array();
103 if ($scales = grade_scale::fetch_all_global()) {
104 foreach($scales as $scale) {
105 $options[$scale->id] = $scale->get_name();
108 $scale_el =& $mform->getElement('scaleid');
109 $scale_el->load($options);
112 if ($id = $mform->getElementValue('id')) {
113 $outcome = grade_outcome::fetch(array('id'=>$id));
114 $itemcount = $outcome->get_item_uses_count();
115 $coursecount = $outcome->get_course_uses_count();
117 if ($itemcount) {
118 $mform->hardFreeze('scaleid');
121 if (empty($courseid)) {
122 $mform->hardFreeze('standard');
124 } else if (!has_capability('moodle/grade:manage', context_system::instance())) {
125 $mform->hardFreeze('standard');
127 } else if ($coursecount and empty($outcome->courseid)) {
128 $mform->hardFreeze('standard');
132 } else {
133 if (empty($courseid) or !has_capability('moodle/grade:manage', context_system::instance())) {
134 $mform->hardFreeze('standard');
139 /// perform extra validation before submission
140 function validation($data, $files) {
141 $errors = parent::validation($data, $files);
143 if ($data['scaleid'] < 1) {
144 $errors['scaleid'] = get_string('required');
147 if (!empty($data['standard']) and $scale = grade_scale::fetch(array('id'=>$data['scaleid']))) {
148 if (!empty($scale->courseid)) {
149 //TODO: localize
150 $errors['scaleid'] = 'Can not use custom scale in global outcome!';
154 return $errors;