Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * Edit form for grade scales
19
 *
20
 * @package   core_grades
21
 * @copyright 2007 Petr Skoda
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
if (!defined('MOODLE_INTERNAL')) {
26
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
27
}
28
 
29
require_once $CFG->libdir.'/formslib.php';
30
 
31
class edit_scale_form extends moodleform {
32
    function definition() {
33
        global $CFG;
34
        $mform =& $this->_form;
35
 
36
        // visible elements
37
        $mform->addElement('header', 'general', get_string('scale'));
38
 
39
        $mform->addElement('text', 'name', get_string('name'), 'size="40"');
40
        $mform->addRule('name', get_string('required'), 'required', null, 'client');
41
        $mform->setType('name', PARAM_TEXT);
42
 
43
        $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
44
        $mform->addHelpButton('standard', 'scalestandard');
45
 
46
        $mform->addElement('static', 'used', get_string('used'));
47
 
48
        $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
49
        $mform->addHelpButton('scale', 'scale');
50
        $mform->addRule('scale', get_string('required'), 'required', null, 'client');
51
        $mform->setType('scale', PARAM_TEXT);
52
 
53
        $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
54
 
55
        // hidden params
56
        $mform->addElement('hidden', 'id', 0);
57
        $mform->setType('id', PARAM_INT);
58
 
59
        $mform->addElement('hidden', 'courseid', 0);
60
        $mform->setType('courseid', PARAM_INT);
61
 
62
/// add return tracking info
63
        $gpr = $this->_customdata['gpr'];
64
        $gpr->add_mform_elements($mform);
65
 
66
//-------------------------------------------------------------------------------
67
        // buttons
68
        $this->add_action_buttons();
69
    }
70
 
71
 
72
/// tweak the form - depending on existing data
73
    function definition_after_data() {
74
        global $CFG;
75
 
76
        $mform =& $this->_form;
77
 
78
        $courseid = $mform->getElementValue('courseid');
79
 
80
        if ($id = $mform->getElementValue('id')) {
81
            $scale = grade_scale::fetch(array('id'=>$id));
82
            $used = $scale->is_used();
83
 
84
            if ($used) {
85
                $mform->hardFreeze('scale');
86
            }
87
 
88
            if (empty($courseid)) {
89
                $mform->hardFreeze('standard');
90
 
91
            } else if (!has_capability('moodle/course:managescales', context_system::instance())) {
92
                //if they dont have managescales at system level the shouldnt be allowed to make scales standard (or not standard)
93
                $mform->hardFreeze('standard');
94
 
95
            } else if ($used and !empty($scale->courseid)) {
96
                $mform->hardFreeze('standard');
97
            }
98
 
99
            $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
100
            $used_el =& $mform->getElement('used');
101
            $used_el->setValue($usedstr);
102
 
103
        } else {
104
            $mform->removeElement('used');
105
            if (empty($courseid) or !has_capability('moodle/course:managescales', context_system::instance())) {
106
                $mform->hardFreeze('standard');
107
            }
108
        }
109
    }
110
 
111
/// perform extra validation before submission
112
    function validation($data, $files) {
113
        global $CFG, $COURSE, $DB;
114
 
115
        $errors = parent::validation($data, $files);
116
 
117
        // we can not allow 2 scales with the same exact scale as this creates
118
        // problems for backup/restore
119
 
120
        $old = grade_scale::fetch(array('id'=>$data['id']));
121
 
122
        if (array_key_exists('standard', $data)) {
123
            if (empty($data['standard'])) {
124
                $courseid = $COURSE->id;
125
            } else {
126
                $courseid = 0;
127
            }
128
 
129
        } else {
130
            $courseid = $old->courseid;
131
        }
132
 
133
        if (array_key_exists('scale', $data)) {
134
            $scalearray = explode(',', $data['scale']);
135
            $scalearray = array_map('trim', $scalearray);
136
            $scaleoptioncount = count($scalearray);
137
 
138
            if (count($scalearray) < 1) {
139
                $errors['scale'] = get_string('badlyformattedscale', 'grades');
140
            } else {
141
                $thescale = implode(',',$scalearray);
142
 
143
                //this check strips out whitespace from the scale we're validating but not from those already in the DB
144
                $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', core_text::strlen($thescale)).'=:scale',
145
                    array('courseid'=>$courseid, 'scale'=>$thescale));
146
 
147
                if ($count) {
148
                    //if this is a new scale but we found a duplice in the DB
149
                    //or we found a duplicate in another course report the error
150
                    if (empty($old->id) or $old->courseid != $courseid) {
151
                        $errors['scale'] = get_string('duplicatescale', 'grades');
152
                    } else if ($old->scale !== $thescale and $old->scale !== $data['scale']) {
153
                        //if the old scale from DB is different but we found a duplicate then we're trying to modify a scale to be a duplicate
154
                        $errors['scale'] = get_string('duplicatescale', 'grades');
155
                    }
156
                }
157
            }
158
        }
159
 
160
        return $errors;
161
    }
162
}
163
 
164