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 * Various enrol UI forms
19 *
20 * @package    core_enrol
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
25defined('MOODLE_INTERNAL') || die();
26
27require_once("$CFG->libdir/formslib.php");
28
29class enrol_users_assign_form extends moodleform {
30    function definition() {
31        global $CFG, $DB;
32
33        $mform = $this->_form;
34
35        $user       = $this->_customdata['user'];
36        $course     = $this->_customdata['course'];
37        $context    = context_course::instance($course->id);
38        $assignable = $this->_customdata['assignable'];
39        $assignable = array_reverse($assignable, true); // students first
40
41        $ras = get_user_roles($context, $user->id, true);
42        foreach ($ras as $ra) {
43            unset($assignable[$ra->roleid]);
44        }
45
46        $mform->addElement('header','general', fullname($user));
47
48        $mform->addElement('select', 'roleid', get_string('addrole', 'role'), $assignable);
49
50        $mform->addElement('hidden', 'id');
51        $mform->setType('id', PARAM_INT);
52
53        $mform->addElement('hidden', 'user');
54        $mform->setType('user', PARAM_INT);
55
56        $mform->addElement('hidden', 'action');
57        $mform->setType('action', PARAM_ALPHANUMEXT);
58
59        $mform->addElement('hidden', 'ifilter');
60        $mform->setType('ifilter', PARAM_ALPHA);
61
62        $mform->addElement('hidden', 'page');
63        $mform->setType('page', PARAM_INT);
64
65        $mform->addElement('hidden', 'perpage');
66        $mform->setType('perpage', PARAM_INT);
67
68        $mform->addElement('hidden', 'sort');
69        $mform->setType('sort', PARAM_ALPHA);
70
71        $mform->addElement('hidden', 'dir');
72        $mform->setType('dir', PARAM_ALPHA);
73
74        $this->add_action_buttons();
75
76        $this->set_data(array('action'=>'assign', 'user'=>$user->id));
77    }
78}
79
80class enrol_users_addmember_form extends moodleform {
81    function definition() {
82        global $CFG, $DB;
83
84        $mform = $this->_form;
85
86        $user     = $this->_customdata['user'];
87        $course   = $this->_customdata['course'];
88        $context  = context_course::instance($course->id, IGNORE_MISSING);
89        $allgroups = $this->_customdata['allgroups'];
90        $usergroups = groups_get_all_groups($course->id, $user->id, 0, 'g.id');
91
92        $options = array();
93        foreach ($allgroups as $group) {
94            if (isset($usergroups[$group->id])) {
95                continue;
96            }
97            $options[$group->id] = $group->name;
98        }
99
100        $mform->addElement('header','general', fullname($user));
101
102        $mform->addElement('select', 'groupids', get_string('addgroup', 'group'), $options, array('multiple' => 'multiple'));
103        $mform->addRule('groupids', null, 'required');
104
105        $mform->addElement('hidden', 'id');
106        $mform->setType('id', PARAM_INT);
107
108        $mform->addElement('hidden', 'user');
109        $mform->setType('user', PARAM_INT);
110
111        $mform->addElement('hidden', 'action');
112        $mform->setType('action', PARAM_ALPHANUMEXT);
113
114        $mform->addElement('hidden', 'ifilter');
115        $mform->setType('ifilter', PARAM_ALPHA);
116
117        $mform->addElement('hidden', 'page');
118        $mform->setType('page', PARAM_INT);
119
120        $mform->addElement('hidden', 'perpage');
121        $mform->setType('perpage', PARAM_INT);
122
123        $mform->addElement('hidden', 'sort');
124        $mform->setType('sort', PARAM_ALPHA);
125
126        $mform->addElement('hidden', 'dir');
127        $mform->setType('dir', PARAM_ALPHA);
128
129        $this->add_action_buttons();
130
131        $this->set_data(array('action'=>'addmember', 'user'=>$user->id));
132    }
133}
134
135
136/**
137 * Form that lets users filter the enrolled user list.
138 */
139class enrol_users_filter_form extends moodleform {
140    function definition() {
141        global $CFG, $DB;
142
143        $manager = $this->_customdata['manager'];
144
145        $mform = $this->_form;
146
147        // Text search box.
148        $mform->addElement('text', 'search', get_string('search'));
149        $mform->setType('search', PARAM_RAW);
150
151        // Filter by enrolment plugin type.
152        $mform->addElement('select', 'ifilter', get_string('enrolmentinstances', 'enrol'),
153                array(0 => get_string('all')) + (array)$manager->get_enrolment_instance_names());
154
155        // Role select dropdown includes all roles, but using course-specific
156        // names if applied. The reason for not restricting to roles that can
157        // be assigned at course level is that upper-level roles display in the
158        // enrolments table so it makes sense to let users filter by them.
159        $visibleroles = $manager->get_viewable_roles();
160        $rolenames = array();
161        foreach ($visibleroles as $id => $role) {
162            $rolenames[$id] = $role;
163        }
164        $mform->addElement('select', 'role', get_string('role'),
165                array(0 => get_string('all')) + $rolenames);
166
167        // Filter by group.
168        $allgroups = $manager->get_all_groups();
169        $groupsmenu[0] = get_string('allparticipants');
170        $groupsmenu[-1] = get_string('nogroup', 'enrol');
171        foreach($allgroups as $gid => $unused) {
172            $groupsmenu[$gid] = $allgroups[$gid]->name;
173        }
174        if (count($groupsmenu) > 1) {
175            $mform->addElement('select', 'filtergroup', get_string('group'), $groupsmenu);
176        }
177
178        // Status active/inactive.
179        $mform->addElement('select', 'status', get_string('status'),
180                array(-1 => get_string('all'),
181                    ENROL_USER_ACTIVE => get_string('active'),
182                    ENROL_USER_SUSPENDED => get_string('inactive')));
183
184        // Submit button does not use add_action_buttons because that adds
185        // another fieldset which causes the CSS style to break in an unfixable
186        // way due to fieldset quirks.
187        $group = array();
188        $group[] = $mform->createElement('submit', 'submitbutton', get_string('filter'));
189        $group[] = $mform->createElement('submit', 'resetbutton', get_string('reset'));
190        $mform->addGroup($group, 'buttons', '', ' ', false);
191
192        // Add hidden fields required by page.
193        $mform->addElement('hidden', 'id', $this->_customdata['id']);
194        $mform->setType('id', PARAM_INT);
195        $mform->addElement('hidden', 'newcourse', $this->_customdata['newcourse']);
196        $mform->setType('newcourse', PARAM_BOOL);
197    }
198}
199