enr_course_enrol.php 10.5 KB
Newer Older
martinlanghoff's avatar
martinlanghoff committed
1
2
3
4
5
6
<?PHP  // $Id$
       // enrol_config.php - allows admin to edit all enrollment variables
       //                    Yes, enrol is correct English spelling.

    require_once(dirname(__FILE__) . "/../../config.php");
    require_once($CFG->libdir.'/adminlib.php');
7
    include_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
martinlanghoff's avatar
martinlanghoff committed
8

9
    if (!confirm_sesskey()) {
10
        print_error('confirmsesskeybad', 'error');
11
12
    }

13
    admin_externalpage_setup('mnetenrol');
martinlanghoff's avatar
martinlanghoff committed
14
15
16
17

    require_once("$CFG->dirroot/enrol/enrol.class.php");   /// Open the factory class
    $enrolment = enrolment_factory::factory('mnet');

18
    $mnethostid = required_param('host', PARAM_INT);
martinlanghoff's avatar
martinlanghoff committed
19
20
    $courseid = required_param('courseid', PARAM_INT);

21
22
23
24
25
    $mnet_peer = new mnet_peer();
    if (!$mnet_peer->set_id($mnethostid)) {
        print_error('hostcoursenotfound','mnet');
    }

26
    $course = $DB->get_record('mnet_enrol_course', array('id'=>$courseid, 'hostid'=>$mnet_peer->id));
martinlanghoff's avatar
martinlanghoff committed
27

28
    if (empty($course)) {
29
        print_error('hostcoursenotfound','mnet');
martinlanghoff's avatar
martinlanghoff committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    }

    define("MAX_USERS_PER_PAGE", 5000);

    $add            = optional_param('add', 0, PARAM_BOOL);
    $remove         = optional_param('remove', 0, PARAM_BOOL);
    $showall        = optional_param('showall', 0, PARAM_BOOL);
    $searchtext     = optional_param('searchtext', '', PARAM_RAW); // search string
    $previoussearch = optional_param('previoussearch', 0, PARAM_BOOL);
    $userid         = optional_param('userid', 0, PARAM_INT); // needed for user tabs

    $errors = array();

    $previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;

45
    $baseurl = "remote_enrolment.php?courseid={$course->id}&amp;host={$mnet_peer->id}";
martinlanghoff's avatar
martinlanghoff committed
46
47
48
49
50
51
    if (!empty($userid)) {
        $baseurl .= '&amp;userid='.$userid;
    }

/// Process incoming role assignment

52
    if ($frm = data_submitted()) {
martinlanghoff's avatar
martinlanghoff committed
53
54
55
56
57
58
59
60
        if ($add and !empty($frm->addselect) and confirm_sesskey()) {
            $timemodified = time();

            foreach ($frm->addselect as $adduser) {
                if (!$adduser = clean_param($adduser, PARAM_INT)) {
                    continue;
                }
                if (! $enrolment->req_enrol_user($adduser, $course->id)) {
61
                    $errors[] = "Could not add user with id $adduser to course {$course->id}!";
martinlanghoff's avatar
martinlanghoff committed
62
63
64
65
66
67
                }
            }
        } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
            foreach ($frm->removeselect as $removeuser) {
                $removeuser = clean_param($removeuser, PARAM_INT);
                if (! $enrolment->req_unenrol_user($removeuser, $course->id)) {
68
                    $errors[] = "Could not remove user with id $removeuser from course {$course->id}!";
martinlanghoff's avatar
martinlanghoff committed
69
70
71
72
73
74
75
76
77
                }
            }
        } else if ($showall) {
            $searchtext = '';
            $previoussearch = 0;
        }
    }

/// Prepare data for users / enrolled users panes
78
79
80
81
82
83


/// Create a new request object
    $mnet_request = new mnet_xmlrpc_client();

/// Pass it the path to the method that we want to execute
84
    $mnet_request->set_method('enrol/mnet/enrol.php/course_enrolments');
85
86
    $mnet_request->add_param($course->remoteid, 'int');
    $mnet_request->send($mnet_peer);
87
    $raw_all_enrolled_users = $mnet_request->response;
88
    unset($mnet_request);
89
90
91
92
93
94
95
96
97
98
99
100
101

    $all_enrolled_users = array();
    if (!empty($raw_all_enrolled_users)) {
        // Try to repair keying of remote users array, numeric usernames get lost in the fracas
        foreach ($raw_all_enrolled_users as $username => $userdetails) {
            if (empty($userdetails['username']) || !is_numeric($username)) {
                //Not able to repair, or no need to repair
                $all_enrolled_users[$username] = $userdetails;
            } else {
                $all_enrolled_users[$userdetails['username']] = $userdetails;
            }
        }
    }
102
103
    
    $all_enrolled_usernames = '';
104
    $timemodified = array();
105
106
107
108
/// List all the users (homed on this server) who are enrolled on the course
/// This will include mnet-enrolled users, and those who have enrolled 
/// themselves, etc.
    if (is_array($all_enrolled_users) && count($all_enrolled_users)) {
109
110
111
        list($select, $params) = $DB->get_in_or_equal(array_keys($all_enrolled_users), SQL_PARAMS_NAMED, 'un0'); 
        $select = " u.username $select AND ";

112
113
    } else {
        $all_enrolled_users = array();
114
115
        $params = array();
        $select = '';
116
117
    }

118
/// Synch our mnet_enrol_assignments with remote server
119
120
121
122
123
    $sql = "
            SELECT
                u.id,
                u.firstname,
                u.lastname,
124
                u.username,
125
                u.email,
126
127
128
                a.enroltype,
                a.id as enrolid,
                COALESCE(a.hostid, 0) as wehaverecord,
129
130
                a.courseid
            FROM
131
                {user} u
132
            LEFT JOIN
133
                {mnet_enrol_assignments} a
134
            ON
135
                a.userid = u.id AND a.courseid=:courseid
136
137
138
139
            WHERE
                $select 
                u.deleted = 0 AND
                u.confirmed = 1 AND
140
                u.mnethostid = :mnetid
141
142
143
            ORDER BY
                u.firstname ASC,
                u.lastname ASC";
144
145
    $params['courseid'] = $courseid;
    $params['mnetid']   = $CFG->mnet_localhost_id;
146

147
    if (!$enrolledusers = $DB->get_records_sql($sql, $params)) {
martinlanghoff's avatar
martinlanghoff committed
148
149
150
        $enrolledusers = array();
    }

151
152
153
154
155
156
157
158
159
160
161
    foreach($enrolledusers as $user) {

        $dataobj = new stdClass();
        $dataobj->userid    = $user->id;
        $dataobj->hostid    = $mnet_peer->id;
        $dataobj->courseid  = $courseid;
        $dataobj->rolename  = $all_enrolled_users[$user->username]['name'];
        $dataobj->enroltype = $all_enrolled_users[$user->username]['enrol'];

        if ($user->wehaverecord == 0) {
            $dataobj->enroltime = $all_enrolled_users[$user->username]['timemodified'];
162
            $dataobj->id = $DB->insert_record('mnet_enrol_assignments', $dataobj);
163
164
        } elseif (array_key_exists($user->username, $all_enrolled_users)) {
            $dataobj->id    = $user->enrolid;
165
            $DB->update_record('mnet_enrol_assignments', $dataobj);
166
        } elseif (is_array($all_enrolled_users) && count($all_enrolled_users)) {
167
            $DB->delete_record('mnet_enrol_assignments', array('id'=>$user->enrolid));
168
169
        }
    }
170
    unset($enrolledusers);
171

172
173
174
175
    // Read about our remote enrolments in 2 sets
    // first, get the remote enrolments done via enrol/mnet      $mnetenrolledusers
    // second, get the remote enrolments done with other plugins $remtenrolledusers
    // NOTE: both arrays are keyed on the userid! 
176
177
178
179
180
    $sql = "
            SELECT
                u.id,
                u.firstname,
                u.lastname,
181
                a.rolename,
182
183
184
                a.enroltype,
                a.courseid
            FROM
185
186
                {user} u,
                {mnet_enrol_assignments} a
187
188
            WHERE
                a.userid = u.id AND 
189
                a.courseid=:courseid AND
190
                a.enroltype = 'mnet'   AND
191
192
                u.deleted = 0 AND
                u.confirmed = 1 AND
193
                u.mnethostid = :mnetid
194
195
196
            ORDER BY
                u.firstname ASC,
                u.lastname ASC";
197
198
199
    $params = array();
    $params['courseid'] = $courseid;
    $params['mnetid']   = $CFG->mnet_localhost_id;
200

201
    if (!$mnetenrolledusers = $DB->get_records_sql($sql, $params)) {
202
        $mnetenrolledusers = array();
203
    }
204
205
206
207
208
209
210
211
212
    $sql = "
            SELECT
                u.id,
                u.firstname,
                u.lastname,
                a.rolename,
                a.enroltype,
                a.courseid
            FROM
213
214
                {user} u,
                {mnet_enrol_assignments} a
215
216
            WHERE
                a.userid = u.id AND 
217
                a.courseid=:courseid AND
218
219
220
                a.enroltype != 'mnet'  AND
                u.deleted = 0 AND
                u.confirmed = 1 AND
221
                u.mnethostid = :mnetid
222
223
224
            ORDER BY
                u.firstname ASC,
                u.lastname ASC";
225
226
227
    $params = array();
    $params['courseid'] = $courseid;
    $params['mnetid']   = $CFG->mnet_localhost_id;
228

229
    if (!$remtenrolledusers = $DB->get_records_sql($sql, $params)) {
230
        $remtenrolledusers = array();
231
232
    }

233
234
    $params = array();

235
    $select = '';
236
237
238
239
240
241
    $exclude = array_merge(array_keys($mnetenrolledusers), array_keys($remtenrolledusers));
    $exclude[] = 0;
    $select = 'AND u.username!=\'guest\' AND u.id NOT IN ('. join(',',$exclude) .') ';
    unset($exclude);

    $searchtext = trim($searchtext);
martinlanghoff's avatar
martinlanghoff committed
242
243

    if ($searchtext !== '') {   // Search for a subset of remaining users
244
245
        $LIKE      = $DB->sql_ilike();
        $FULLNAME  = $DB->sql_fullname();
martinlanghoff's avatar
martinlanghoff committed
246

247
248
249
        $select  .= " AND ($FULLNAME $LIKE :search1 OR email $LIKE :search2) ";
        $params['search1'] = "%$searchtext%";
        $params['search2'] = "%$searchtext%";
martinlanghoff's avatar
martinlanghoff committed
250
    }
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    $sql = ("SELECT id, firstname, lastname, email 
               FROM {user} u
              WHERE deleted = 0 AND confirmed = 1 
                    AND mnethostid = :mnetid
                    $select
           ORDER BY lastname ASC, firstname ASC");
    $params['mnetid'] = $CFG->mnet_localhost_id;

    $availableusers = $DB->get_recordset_sql($sql, $params, 0, MAX_USERS_PER_PAGE);

    $sql = ("SELECT COUNT('x') 
               FROM {user} u
              WHERE deleted = 0 AND confirmed = 1 
                    AND mnethostid = :mnetid
                    $select");
    $availablecount = $DB->count_records_sql($sql, $params);
martinlanghoff's avatar
martinlanghoff committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286



/// Print the page

/// get language strings
$str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
/// Get some language strings

$strpotentialusers = get_string('potentialusers', 'role');
$strexistingusers = get_string('existingusers', 'role');
$straction = get_string('assignroles', 'role');
$strroletoassign = get_string('roletoassign', 'role');
$strcurrentcontext = get_string('currentcontext', 'role');
$strsearch = get_string('search');
$strshowall = get_string('showall');
$strparticipants = get_string('participants');
$strsearchresults = get_string('searchresults');

287
admin_externalpage_print_header();
martinlanghoff's avatar
martinlanghoff committed
288

289
print_box('<strong>' . s($mnet_peer->name) . ' : ' 
290
          . format_string($course->shortname) .' '. format_string($course->fullname) 
291
292
          . '</strong><br />'
          . get_string("enrolcourseenrol_desc", "mnet"));
martinlanghoff's avatar
martinlanghoff committed
293
294
295

echo "<hr />";

296
include(dirname(__FILE__).'/enr_course_enrol.html');
martinlanghoff's avatar
martinlanghoff committed
297

298
299
300
301
302
303
304
305
if (!empty($errors)) {
    $msg = '<p>';
    foreach ($errors as $e) {
        $msg .= $e.'<br />';
    }
    $msg .= '</p>';
    notify($msg);
}
martinlanghoff's avatar
martinlanghoff committed
306
307


308
echo $OUTPUT->footer();
martinlanghoff's avatar
martinlanghoff committed
309
310

?>