3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Page to enrol our users into remote courses
22 * @subpackage pluginname
23 * @copyright 2010 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require(__DIR__.'/../../../config.php');
28 require_once($CFG->libdir.'/adminlib.php');
29 require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
33 $hostid = required_param('host', PARAM_INT); // remote host id in our mnet_host table
34 $courseid = required_param('course', PARAM_INT); // id of the course in our cache table
35 $usecache = optional_param('usecache', true, PARAM_BOOL); // use cached list of enrolments
37 admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1, 'sesskey'=>sesskey()),
38 new moodle_url('/mnet/service/enrol/course.php'));
40 $service = mnetservice_enrol::get_instance();
42 if (!$service->is_available()) {
43 echo $OUTPUT->box(get_string('mnetdisabled','mnet'), 'noticebox');
44 echo $OUTPUT->footer();
48 // remote hosts that may publish remote enrolment service and we are subscribed to it
49 $hosts = $service->get_remote_publishers();
51 if (empty($hosts[$hostid])) {
52 print_error('wearenotsubscribedtothishost', 'mnetservice_enrol');
54 $host = $hosts[$hostid];
55 $course = $DB->get_record('mnetservice_enrol_courses', array('id'=>$courseid, 'hostid'=>$host->id), '*', MUST_EXIST);
57 echo $OUTPUT->header();
60 $icon = $OUTPUT->pix_icon('i/course', get_string('category'));
61 echo $OUTPUT->heading($icon . s($course->fullname));
63 // collapsible course summary
64 if (!empty($course->summary)) {
65 $options = new stdClass();
66 $options->trusted = false;
67 $options->para = false;
68 $options->filter = false;
69 $options->noclean = false;
70 $options->overflowdiv = true;
71 print_collapsible_region_start('remotecourse summary', 'remotecourse-summary', get_string('coursesummary'), false, true);
72 echo format_text($course->summary, $course->summaryformat, $options);
73 print_collapsible_region_end();
78 $lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
79 if (!$usecache or empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) {
80 // fetch fresh data from remote if we just came from the course selection screen
81 // or every 10 minutes
83 $result = $service->req_course_enrolments($host->id, $course->remoteid, $usecache);
84 if ($result !== true) {
85 $error .= $service->format_error_message($result);
90 $currentuserselector = new mnetservice_enrol_existing_users_selector('removeselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
91 $potentialuserselector = new mnetservice_enrol_potential_users_selector('addselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
93 // process incoming enrol request
94 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
95 $userstoassign = $potentialuserselector->get_selected_users();
96 if (!empty($userstoassign)) {
97 foreach($userstoassign as $adduser) {
98 $user = $DB->get_record('user', array('id'=>$adduser->id));
99 $result = $service->req_enrol_user($user, $course);
100 if ($result !== true) {
101 $error .= $service->format_error_message($result);
105 $potentialuserselector->invalidate_selected_users();
106 $currentuserselector->invalidate_selected_users();
110 // process incoming unenrol request
111 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
112 $userstounassign = $currentuserselector->get_selected_users();
113 if (!empty($userstounassign)) {
114 foreach($userstounassign as $removeuser) {
115 $user = $DB->get_record('user', array('id'=>$removeuser->id));
116 $result = $service->req_unenrol_user($user, $course);
117 if ($result !== true) {
118 $error .= $service->format_error_message($result);
122 $potentialuserselector->invalidate_selected_users();
123 $currentuserselector->invalidate_selected_users();
127 if (!empty($error)) {
128 echo $OUTPUT->box($error, 'generalbox error');
131 // print form to enrol our students
133 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
135 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
136 <input type="hidden" name="hostid" value="<?php echo $host->id ?>" />
137 <input type="hidden" name="courseid" value="<?php echo $course->id ?>" />
139 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
141 <td id="existingcell">
142 <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
143 <?php $currentuserselector->display() ?>
145 <td id="buttonscell">
146 <div id="addcontrols">
147 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().' '.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
149 <div class="enroloptions">
150 <p><?php echo get_string('assignrole', 'role') .': '. s($course->rolename); ?></p>
155 <div id="removecontrols">
156 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').' '.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
159 <td id="potentialcell">
160 <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
161 <?php $potentialuserselector->display() ?>
169 // eventually display other enrolments of our users (manual, self etc.) in the remote course
170 list($sort, $params) = users_order_by_sql('u');
171 $sql = "SELECT e.id,e.enroltype AS plugin, u.firstname, u.lastname, u.email, u.id AS userid,
172 e.enroltime AS timemodified, e.rolename
173 FROM {mnetservice_enrol_enrolments} e
174 JOIN {user} u ON u.id = e.userid
175 WHERE e.hostid = :hostid AND e.remotecourseid = :remotecourseid AND e.enroltype != 'mnet'
177 $params['hostid'] = $host->id;
178 $params['remotecourseid'] = $course->remoteid;
180 if ($enrolments = $DB->get_records_sql($sql, $params)) {
181 echo $OUTPUT->heading(get_string('otherenrolledusers', 'mnetservice_enrol'), 3);
183 $table = new html_table();
184 $table->attributes['class'] = 'generaltable otherenrolledusers';
185 $table->head = array(get_string('fullnameuser'), get_string('role'), get_string('plugin'));
186 foreach ($enrolments as $enrolleduser) {
187 $table->data[] = array(fullname($enrolleduser), s($enrolleduser->rolename), s($enrolleduser->plugin));
189 echo html_writer::table($table);
193 echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
194 get_string('refetch', 'mnetservice_enrol'), 'get');
197 echo $OUTPUT->single_button(new moodle_url('/mnet/service/enrol/host.php', array('id'=>$host->id)),
198 get_string('availablecourseson', 'mnetservice_enrol', s($host->hostname)), 'get');
200 echo $OUTPUT->footer();