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
 * The trait for adding eventtype fields to a form.
19
 *
20
 * @package     core_calendar
21
 * @copyright   2017 Andrew Nicols <andrew@nicols.co.uk>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core_calendar\local\event\forms;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
/**
29
 * The trait for adding eventtype fields to a form.
30
 *
31
 * @copyright   2017 Andrew Nicols <andrew@nicols.co.uk>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
trait eventtype {
35
 
36
    /**
37
     * Add the appropriate elements for the available event types.
38
     *
39
     * If the only event type available is 'user' then we add a hidden
40
     * element because there is nothing for the user to choose.
41
     *
42
     * If more than one type is available then we add the elements as
43
     * follows:
44
     *      - Always add the event type selector
45
     *      - Elements per type:
46
     *          - course: add an additional select element with each
47
     *                    course as an option.
48
     *          - group: add a select element for the course (different
49
     *                   from the above course select) and a select
50
     *                   element for the group.
51
     *
52
     * @param MoodleQuickForm $mform
53
     * @param array $eventtypes The available event types for the user
54
     */
55
    protected function add_event_type_elements($mform, $eventtypes) {
56
        global $CFG, $DB;
57
        $options = [];
58
 
59
        if (!empty($eventtypes['user'])) {
60
            $options['user'] = get_string('user', 'calendar');
61
        }
62
        if (!empty($eventtypes['group'])) {
63
            $options['group'] = get_string('group', 'calendar');
64
        }
65
        if (!empty($eventtypes['course'])) {
66
            $options['course'] = get_string('course', 'calendar');
67
        }
68
        if (!empty($eventtypes['category'])) {
69
            $options['category'] = get_string('category', 'calendar');
70
        }
71
        if (!empty($eventtypes['site'])) {
72
            $options['site'] = get_string('site', 'calendar');
73
        }
74
 
75
        // If we only have one event type and it's 'user' event then don't bother
76
        // rendering the select boxes because there is no choice for the user to
77
        // make.
78
        if (!empty($eventtypes['user']) && count($options) == 1) {
79
            $mform->addElement('hidden', 'eventtype');
80
            $mform->setType('eventtype', PARAM_TEXT);
81
            $mform->hardFreeze('eventtype');
82
            $mform->setConstant('eventtype', 'user');
83
            return;
84
        } else {
85
            $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
86
        }
87
 
88
        if (!empty($eventtypes['category'])) {
89
            $categoryoptions = [];
90
            foreach (\core_course_category::make_categories_list('moodle/category:manage') as $id => $category) {
91
                $categoryoptions[$id] = $category;
92
            }
93
 
94
            $mform->addElement('autocomplete', 'categoryid', get_string('category'), $categoryoptions);
95
            $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
96
        }
97
 
98
        $showall = is_siteadmin() && !empty($CFG->calendar_adminseesall);
99
        if (!empty($eventtypes['course'])) {
100
            $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => !$showall]);
101
            $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
102
        }
103
 
104
        if (!empty($eventtypes['group'])) {
105
            $groups = !(empty($this->_customdata['groups'])) ? $this->_customdata['groups'] : null;
106
            // Get the list of courses without groups to filter on the course selector.
107
            $sql = "SELECT c.id
108
                      FROM {course} c
109
                     WHERE c.id NOT IN (
110
                            SELECT DISTINCT courseid FROM {groups}
111
                           )";
112
            $coursesnogroup = $DB->get_records_sql($sql);
113
            $mform->addElement('course', 'groupcourseid', get_string('course'),  ['limittoenrolled' => !$showall,
114
                    'exclude' => array_keys($coursesnogroup)]);
115
            $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
116
 
117
            $mform->addElement('select', 'groupid', get_string('group'), $groups);
118
            $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
119
            // We handle the group select hide/show actions on the event_form module.
120
        }
121
    }
122
}