on-demand release 4.0dev+
[moodle.git] / mnet / service / enrol / course.php
CommitLineData
3ce65bfd
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Page to enrol our users into remote courses
20 *
21 * @package plugintype
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
25 */
26
1fcf0ca8 27require(__DIR__.'/../../../config.php');
3ce65bfd
DM
28require_once($CFG->libdir.'/adminlib.php');
29require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
30
31require_sesskey();
32
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
36
ef75ea6c 37admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1, 'sesskey'=>sesskey()),
26f194d2
DM
38 new moodle_url('/mnet/service/enrol/course.php'));
39
3ce65bfd
DM
40$service = mnetservice_enrol::get_instance();
41
42if (!$service->is_available()) {
43 echo $OUTPUT->box(get_string('mnetdisabled','mnet'), 'noticebox');
44 echo $OUTPUT->footer();
45 die();
46}
47
48// remote hosts that may publish remote enrolment service and we are subscribed to it
49$hosts = $service->get_remote_publishers();
50
51if (empty($hosts[$hostid])) {
52 print_error('wearenotsubscribedtothishost', 'mnetservice_enrol');
53}
54$host = $hosts[$hostid];
55$course = $DB->get_record('mnetservice_enrol_courses', array('id'=>$courseid, 'hostid'=>$host->id), '*', MUST_EXIST);
56
57echo $OUTPUT->header();
58
59// course name
b9b409cf 60$icon = $OUTPUT->pix_icon('i/course', get_string('category'));
3ce65bfd
DM
61echo $OUTPUT->heading($icon . s($course->fullname));
62
63// collapsible course summary
64if (!empty($course->summary)) {
93288f22 65 $options = new stdClass();
3ce65bfd
DM
66 $options->trusted = false;
67 $options->para = false;
68 $options->filter = false;
69 $options->noclean = false;
367a75fa 70 $options->overflowdiv = true;
3ce65bfd 71 print_collapsible_region_start('remotecourse summary', 'remotecourse-summary', get_string('coursesummary'), false, true);
4841fda1 72 echo format_text($course->summary, $course->summaryformat, $options);
3ce65bfd
DM
73 print_collapsible_region_end();
74}
75
c24cf442
DM
76$error = '';
77
ef75ea6c
DM
78$lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
79if (!$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
ef75ea6c 82 $usecache = false;
c24cf442
DM
83 $result = $service->req_course_enrolments($host->id, $course->remoteid, $usecache);
84 if ($result !== true) {
85 $error .= $service->format_error_message($result);
86 }
ef75ea6c 87}
26f194d2
DM
88
89// user selectors
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));
92
ef75ea6c
DM
93// process incoming enrol request
94if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
95 $userstoassign = $potentialuserselector->get_selected_users();
96 if (!empty($userstoassign)) {
97 foreach($userstoassign as $adduser) {
c24cf442
DM
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);
102 }
ef75ea6c
DM
103 }
104
105 $potentialuserselector->invalidate_selected_users();
106 $currentuserselector->invalidate_selected_users();
107 }
108}
109
110// process incoming unenrol request
111if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
112 $userstounassign = $currentuserselector->get_selected_users();
113 if (!empty($userstounassign)) {
114 foreach($userstounassign as $removeuser) {
c24cf442
DM
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);
119 }
ef75ea6c
DM
120 }
121
122 $potentialuserselector->invalidate_selected_users();
123 $currentuserselector->invalidate_selected_users();
124 }
125}
26f194d2 126
c24cf442
DM
127if (!empty($error)) {
128 echo $OUTPUT->box($error, 'generalbox error');
129}
130
26f194d2
DM
131// print form to enrol our students
132?>
133<form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
134<div>
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 ?>" />
138
139 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
140 <tr>
141 <td id="existingcell">
142 <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
143 <?php $currentuserselector->display() ?>
144 </td>
145 <td id="buttonscell">
146 <div id="addcontrols">
147 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
148
149 <div class="enroloptions">
150 <p><?php echo get_string('assignrole', 'role') .': '. s($course->rolename); ?></p>
151 </div>
152
153 </div>
154
155 <div id="removecontrols">
156 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
157 </div>
158 </td>
159 <td id="potentialcell">
160 <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
161 <?php $potentialuserselector->display() ?>
162 </td>
163 </tr>
164 </table>
165</div>
166</form>
167<?php
3ce65bfd 168
ef75ea6c 169// eventually display other enrolments of our users (manual, self etc.) in the remote course
9695ff81 170list($sort, $params) = users_order_by_sql('u');
ef75ea6c
DM
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
9695ff81
TH
175 WHERE e.hostid = :hostid AND e.remotecourseid = :remotecourseid AND e.enroltype != 'mnet'
176 ORDER BY $sort";
177$params['hostid'] = $host->id;
178$params['remotecourseid'] = $course->remoteid;
ef75ea6c
DM
179
180if ($enrolments = $DB->get_records_sql($sql, $params)) {
181 echo $OUTPUT->heading(get_string('otherenrolledusers', 'mnetservice_enrol'), 3);
182
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));
188 }
189 echo html_writer::table($table);
190}
191
192if ($usecache) {
193 echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
194 get_string('refetch', 'mnetservice_enrol'), 'get');
195}
196
538dcf89
DM
197echo $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');
199
3ce65bfd 200echo $OUTPUT->footer();