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
 * Cohort related management functions, this file needs to be included manually.
19
 *
20
 * @package    core_cohort
21
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once($CFG->dirroot . '/lib/formslib.php');
28
 
29
class cohort_edit_form extends moodleform {
30
 
31
    /**
32
     * Define the cohort edit form
33
     */
34
    public function definition() {
35
        global $CFG;
36
 
37
        $mform = $this->_form;
38
        $editoroptions = $this->_customdata['editoroptions'];
39
        $cohort = $this->_customdata['data'];
40
 
41
        $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
42
        $mform->addRule('name', get_string('required'), 'required', null, 'client');
43
        $mform->setType('name', PARAM_TEXT);
44
 
45
        $options = $this->get_category_options($cohort->contextid);
46
        $mform->addElement('autocomplete', 'contextid', get_string('context', 'role'), $options);
47
        $mform->addRule('contextid', null, 'required', null, 'client');
48
 
49
        $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
50
        $mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
51
 
52
        $mform->addElement('advcheckbox', 'visible', get_string('visible', 'cohort'));
53
        $mform->setDefault('visible', 1);
54
        $mform->addHelpButton('visible', 'visible', 'cohort');
55
 
56
        $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
57
        $mform->setType('description_editor', PARAM_RAW);
58
 
59
        if (!empty($CFG->allowcohortthemes)) {
60
            $themes = array_merge(array('' => get_string('forceno')), cohort_get_list_of_themes());
61
            $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
62
        }
63
 
64
        $mform->addElement('hidden', 'id');
65
        $mform->setType('id', PARAM_INT);
66
 
67
        if (isset($this->_customdata['returnurl'])) {
68
            $mform->addElement('hidden', 'returnurl', $this->_customdata['returnurl']->out_as_local_url());
69
            $mform->setType('returnurl', PARAM_LOCALURL);
70
        }
71
 
72
        $handler = core_cohort\customfield\cohort_handler::create();
73
        $handler->instance_form_definition($mform, empty($cohort->id) ? 0 : $cohort->id);
74
 
75
        $this->add_action_buttons();
76
 
77
        $handler->instance_form_before_set_data($cohort);
78
        $this->set_data($cohort);
79
    }
80
 
81
    public function validation($data, $files) {
82
        global $DB;
83
 
84
        $errors = parent::validation($data, $files);
85
 
86
        $idnumber = trim($data['idnumber']);
87
        if ($idnumber === '') {
88
            // Fine, empty is ok.
89
 
90
        } else if ($data['id']) {
91
            $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
92
            if ($current->idnumber !== $idnumber) {
93
                if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
94
                    $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
95
                }
96
            }
97
 
98
        } else {
99
            if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
100
                $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
101
            }
102
        }
103
 
104
        $handler = core_cohort\customfield\cohort_handler::create();
105
        $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
106
 
107
        return $errors;
108
    }
109
 
110
    protected function get_category_options($currentcontextid) {
111
        $displaylist = core_course_category::make_categories_list('moodle/cohort:manage');
112
        $options = array();
113
        $syscontext = context_system::instance();
114
        if (has_capability('moodle/cohort:manage', $syscontext)) {
115
            $options[$syscontext->id] = $syscontext->get_context_name();
116
        }
117
        foreach ($displaylist as $cid=>$name) {
118
            $context = context_coursecat::instance($cid);
119
            $options[$context->id] = $name;
120
        }
121
        // Always add current - this is not likely, but if the logic gets changed it might be a problem.
122
        if (!isset($options[$currentcontextid])) {
123
            $context = context::instance_by_id($currentcontextid, MUST_EXIST);
124
            $options[$context->id] = $syscontext->get_context_name();
125
        }
126
        return $options;
127
    }
128
 
129
    /**
130
     *  Apply a logic after data is set.
131
     */
132
    public function definition_after_data() {
133
        $cohortid = $this->_form->getElementValue('id');
134
        $handler = core_cohort\customfield\cohort_handler::create();
135
        $handler->instance_form_definition_after_data($this->_form, empty($cohortid) ? 0 : $cohortid);
136
    }
137
}
138