2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * Simple form to search for users and add them using a manual enrolment to this course.
22 * @package enrol_manual
23 * @copyright 2016 Damyon Wiese
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/formslib.php');
31 class enrol_manual_enrol_users_form extends moodleform {
37 public function definition() {
38 global $PAGE, $DB, $CFG;
41 require_once($CFG->dirroot . '/enrol/locallib.php');
43 $context = $this->_customdata->context;
45 // Get the course and enrolment instance.
46 $coursecontext = $context->get_course_context();
47 $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
48 $manager = new course_enrolment_manager($PAGE, $course);
51 foreach ($manager->get_enrolment_instances() as $tempinstance) {
52 if ($tempinstance->enrol == 'manual') {
53 if ($instance === null) {
54 $instance = $tempinstance;
60 $mform = $this->_form;
61 $mform->setDisableShortforms();
62 $mform->disable_form_change_checker();
63 // Build the list of options for the enrolment period dropdown.
64 $unlimitedperiod = get_string('unlimited');
65 $periodmenu = array();
66 $periodmenu[''] = $unlimitedperiod;
67 for ($i=1; $i<=365; $i++) {
68 $seconds = $i * 86400;
69 $periodmenu[$seconds] = get_string('numdays', '', $i);
71 // Work out the apropriate default settings.
72 $defaultperiod = $instance->enrolperiod;
73 if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
74 $periodmenu[$instance->enrolperiod] = format_time($instance->enrolperiod);
76 if (empty($extendbase)) {
77 if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
78 // Default to now if there is no system setting.
83 // Build the list of options for the starting from dropdown.
85 $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
86 $dateformat = get_string('strftimedatefullshort');
90 if ($course->startdate > 0) {
91 $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
93 $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
94 $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
96 $mform->addElement('header', 'main', get_string('enrolmentoptions', 'enrol'));
98 'ajax' => 'enrol_manual/form-potential-user-selector',
100 'courseid' => $course->id,
101 'enrolid' => $instance->id
103 $mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options);
105 $options = ['contextid' => $context->id, 'multiple' => true];
106 $mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
108 $roles = get_assignable_roles($context);
109 $mform->addElement('select', 'role', get_string('assignrole', 'enrol_manual'), $roles);
110 $keys = array_keys($roles);
111 $defaultrole = end($keys);
112 $mform->setDefault('role', $defaultrole);
114 $mform->addAdvancedStatusElement('main');
116 $mform->addElement('checkbox', 'recovergrades', get_string('recovergrades', 'enrol'));
117 $mform->setAdvanced('recovergrades');
118 $mform->addElement('select', 'duration', get_string('defaultperiod', 'enrol_manual'), $periodmenu);
119 $mform->setDefault('duration', $defaultperiod);
120 $mform->setAdvanced('duration');
121 $mform->addElement('select', 'startdate', get_string('startingfrom'), $basemenu);
122 $mform->setDefault('startdate', $extendbase);
123 $mform->setAdvanced('startdate');
125 $mform->addElement('hidden', 'id', $course->id);
126 $mform->setType('id', PARAM_INT);
127 $mform->addElement('hidden', 'action', 'enrol');
128 $mform->setType('action', PARAM_ALPHA);
129 $mform->addElement('hidden', 'enrolid', $instance->id);
130 $mform->setType('enrolid', PARAM_INT);