Skip to content

Commit

Permalink
MDL-22787 MNet: displays our users enrolled by other plugin, too, XML…
Browse files Browse the repository at this point in the history
…-RPC fetching improved

AMOS BEGIN
 MOV [otherenrolledusers,core_mnet],[otherenrolledusers,mnetservice_enrol]
AMOS END
  • Loading branch information
mudrd8mz committed Jul 17, 2010
1 parent 26f194d commit ef75ea6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 85 deletions.
1 change: 0 additions & 1 deletion lang/en/mnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@
$string['off'] = 'Off';
$string['on'] = 'On';
$string['options'] = 'Options';
$string['otherenrolledusers'] = 'Other enrolled users';
$string['peerprofilefielddesc'] = 'Here you can override the global settings for which profile fields to send and import when new users are created';
$string['permittedtransports'] = 'Permitted transports';
$string['phperror'] = 'An internal PHP error prevented your request being fulfilled.';
Expand Down
64 changes: 61 additions & 3 deletions mnet/service/enrol/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
$courseid = required_param('course', PARAM_INT); // id of the course in our cache table
$usecache = optional_param('usecache', true, PARAM_BOOL); // use cached list of enrolments

admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1),
admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1, 'sesskey'=>sesskey()),
new moodle_url('/mnet/service/enrol/course.php'));

$service = mnetservice_enrol::get_instance();
Expand Down Expand Up @@ -72,13 +72,45 @@
print_collapsible_region_end();
}

//$enrolments = $service->get_remote_oourse_enrolments($host->id, $course->remoteid, $usecache);
$lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
if (!$usecache or empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) {
// fetch fresh data from remote if we just came from the course selection screen
// or every 10 minutes
$service->req_course_enrolments($host->id, $course->remoteid, $usecache);
$usecache = false;
}

// user selectors
$currentuserselector = new mnetservice_enrol_existing_users_selector('removeselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
$potentialuserselector = new mnetservice_enrol_potential_users_selector('addselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));

// process incoming data
// process incoming enrol request
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
$userstoassign = $potentialuserselector->get_selected_users();
if (!empty($userstoassign)) {
foreach($userstoassign as $adduser) {
//$enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
}

$potentialuserselector->invalidate_selected_users();
$currentuserselector->invalidate_selected_users();
}
}

// process incoming unenrol request
if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
$userstounassign = $currentuserselector->get_selected_users();
if (!empty($userstounassign)) {
foreach($userstounassign as $removeuser) {
//$enrol_manual->unenrol_user($instance, $removeuser->id);
add_to_log($course->id, 'course', 'unenrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
}

$potentialuserselector->invalidate_selected_users();
$currentuserselector->invalidate_selected_users();
}
}

// print form to enrol our students
?>
Expand Down Expand Up @@ -118,4 +150,30 @@
</form>
<?php

// eventually display other enrolments of our users (manual, self etc.) in the remote course
$sql = "SELECT e.id,e.enroltype AS plugin, u.firstname, u.lastname, u.email, u.id AS userid,
e.enroltime AS timemodified, e.rolename
FROM {mnetservice_enrol_enrolments} e
JOIN {user} u ON u.id = e.userid
WHERE e.hostid = ? AND e.remotecourseid = ? AND e.enroltype != 'mnet'
ORDER BY u.lastname, u.firstname";
$params = array($host->id, $course->remoteid);

if ($enrolments = $DB->get_records_sql($sql, $params)) {
echo $OUTPUT->heading(get_string('otherenrolledusers', 'mnetservice_enrol'), 3);

$table = new html_table();
$table->attributes['class'] = 'generaltable otherenrolledusers';
$table->head = array(get_string('fullnameuser'), get_string('role'), get_string('plugin'));
foreach ($enrolments as $enrolleduser) {
$table->data[] = array(fullname($enrolleduser), s($enrolleduser->rolename), s($enrolleduser->plugin));
}
echo html_writer::table($table);
}

if ($usecache) {
echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
get_string('refetch', 'mnetservice_enrol'), 'get');
}

echo $OUTPUT->footer();
2 changes: 1 addition & 1 deletion mnet/service/enrol/host.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
$prevcat = $course->categoryid;
}
$editbtn = $OUTPUT->single_button(new moodle_url('/mnet/service/enrol/course.php',
array('host'=>$host->id, 'course'=>$course->id, 'sesskey'=>sesskey())),
array('host'=>$host->id, 'course'=>$course->id, 'usecache'=>0, 'sesskey'=>sesskey())),
get_string('editenrolments', 'mnetservice_enrol'), 'get');
$row = new html_table_row();
$row->cells = array(
Expand Down
1 change: 1 addition & 0 deletions mnet/service/enrol/lang/en/mnetservice_enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
$string['hostname'] = 'Host name';
$string['hosturl'] = 'Remote host URL';
$string['nopublishers'] = 'No remote peers available.';
$string['otherenrolledusers'] = 'Other enrolled users';
$string['pluginname'] = 'Remote enrolment service';
$string['refetch'] = 'Re-fetch up to date state from remote hosts';
151 changes: 71 additions & 80 deletions mnet/service/enrol/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function get_remote_courses($mnethostid, $usecache=true) {
}

/**
* Returns the information about enrolments of our users in remote courses
* Updates local cache about enrolments of our users in remote courses
*
* The remote course must allow enrolments via our Remote enrolment service client.
* Because of legacy design of data structure returned by XML-RPC code, only one
Expand All @@ -249,114 +249,105 @@ public function get_remote_courses($mnethostid, $usecache=true) {
* @param id $mnethostid MNet remote host id
* @param int $remotecourseid ID of the course at the remote host
* @param bool $usecache use cached data or invoke new XML-RPC?
* @uses mnet_xmlrpc_client Invokes XML-RPC request if the cache is not used
* @return array|string returned list or serialized array of mnet error messages
* @uses mnet_xmlrpc_client Invokes XML-RPC request
* @return bool|string true if success or serialized array of mnet error messages
*/
public function get_remote_course_enrolments($mnethostid, $remotecourseid, $usecache=true) {
public function req_course_enrolments($mnethostid, $remotecourseid) {
global $CFG, $DB; // $CFG needed!
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';

if (!$DB->record_exists('mnetservice_enrol_courses', array('hostid'=>$mnethostid, 'remoteid'=>$remotecourseid))) {
return serialize(array('course not available for remote enrolments'));
}

$lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
if (empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) {
// force XML-RPC every 10 minutes
$usecache = false;
$peer = new mnet_peer();
if (!$peer->set_id($mnethostid)) {
return serialize(array('unknown mnet peer'));
}

if (!$usecache) {
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';

$peer = new mnet_peer();
if (!$peer->set_id($mnethostid)) {
return serialize(array('unknown mnet peer'));
}

$request = new mnet_xmlrpc_client();
$request->set_method('enrol/mnet/enrol.php/course_enrolments');
$request->add_param($remotecourseid, 'int');
$request = new mnet_xmlrpc_client();
$request->set_method('enrol/mnet/enrol.php/course_enrolments');
$request->add_param($remotecourseid, 'int');

if ($request->send($peer)) {
$list = array();
$response = $request->response;
if ($request->send($peer)) {
$list = array();
$response = $request->response;

// prepare a table mapping usernames of our users to their ids
$usernames = array();
foreach ($response as $unused => $remote) {
if (!isset($remote['username'])) {
// see MDL-19219
return serialize(array('remote host running old version of mnet server - does not return username attribute'));
}
if ($remote['username'] == 'guest') {
// do not try nasty things you bastard!
continue;
}
$usernames[$remote['username']] = $remote['username'];
// prepare a table mapping usernames of our users to their ids
$usernames = array();
foreach ($response as $unused => $remote) {
if (!isset($remote['username'])) {
// see MDL-19219
return serialize(array('remote host running old version of mnet server - does not return username attribute'));
}
if ($remote['username'] == 'guest') {
// do not try nasty things you bastard!
continue;
}
$usernames[$remote['username']] = $remote['username'];
}

if (!empty($usernames)) {
list($usql, $params) = $DB->get_in_or_equal($usernames, SQL_PARAMS_NAMED);
$params['mnethostid'] = $CFG->mnet_localhost_id;
$params['mnetlocalhostid'] = $CFG->mnet_localhost_id;
$sql = "SELECT username,id
FROM {user}
WHERE mnethostid = :mnethostid
WHERE mnethostid = :mnetlocalhostid
AND username $usql
AND deleted = 0
AND confirmed = 1
ORDER BY lastname,firstname,email";
$usersbyusername = $DB->get_records_sql($sql, $params);
} else {
$usersbyusername = array();
}

// populate the returned list and update local cache of enrolment records
foreach ($response as $remote) {
if (empty($usersbyusername[$remote['username']])) {
// we do not know this user or she is deleted or not confirmed or is 'guest'
continue;
}
$enrolment = new stdclass();
$enrolment->hostid = $mnethostid;
$enrolment->userid = $usersbyusername[$remote['username']]->id;
$enrolment->remotecourseid = $remotecourseid;
$enrolment->rolename = $remote['name']; // $remote['shortname'] not used
$enrolment->enroltime = $remote['timemodified'];
$enrolment->enroltype = $remote['enrol'];

$current = $DB->get_record('mnetservice_enrol_enrolments', array('hostid'=>$enrolment->hostid, 'userid'=>$enrolment->userid,
'remotecourseid'=>$enrolment->remotecourseid, 'enroltype'=>$enrolment->enroltype), 'id, enroltime');
if (empty($current)) {
$enrolment->id = $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
} else {
$enrolment->id = $current->id;
if ($current->enroltime != $enrolment->enroltime) {
$DB->update_record('mnetservice_enrol_enrolments', $enrolment);
}
// populate the returned list and update local cache of enrolment records
foreach ($response as $remote) {
if (empty($usersbyusername[$remote['username']])) {
// we do not know this user or she is deleted or not confirmed or is 'guest'
continue;
}
$enrolment = new stdclass();
$enrolment->hostid = $mnethostid;
$enrolment->userid = $usersbyusername[$remote['username']]->id;
$enrolment->remotecourseid = $remotecourseid;
$enrolment->rolename = $remote['name']; // $remote['shortname'] not used
$enrolment->enroltime = $remote['timemodified'];
$enrolment->enroltype = $remote['enrol'];

$current = $DB->get_record('mnetservice_enrol_enrolments', array('hostid'=>$enrolment->hostid, 'userid'=>$enrolment->userid,
'remotecourseid'=>$enrolment->remotecourseid, 'enroltype'=>$enrolment->enroltype), 'id, enroltime');
if (empty($current)) {
$enrolment->id = $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
} else {
$enrolment->id = $current->id;
if ($current->enroltime != $enrolment->enroltime) {
$DB->update_record('mnetservice_enrol_enrolments', $enrolment);
}

$list[$enrolment->id] = $enrolment;
}

// prune stale enrolment records
$list[$enrolment->id] = $enrolment;
}

// prune stale enrolment records
if (empty($list)) {
$DB->delete_records('mnetservice_enrol_enrolments', array('hostid'=>$mnethostid));
} else {
list($isql, $params) = $DB->get_in_or_equal(array_keys($list), SQL_PARAMS_NAMED, 'param0000', false);
$params['hostid'] = $mnethostid;
$select = "hostid = :hostid AND id $isql";
$DB->delete_records_select('mnetservice_enrol_enrolments', $select, $params);

// store the timestamp of the recent fetch
set_config('lastfetchenrolments', time(), 'mnetservice_enrol');

} else {
return serialize($request->error);
}
}

// get the information from cache
$sql = "SELECT e.id,e.enroltype AS plugin, u.firstname, u.lastname, u.email, u.id AS userid,
e.enroltime AS timemodified, e.rolename
FROM {mnetservice_enrol_enrolments} e
LEFT JOIN {user} u ON u.id = e.userid
WHERE e.hostid = ? AND e.remotecourseid = ?
ORDER BY e.enroltype, u.lastname, u.firstname";
$params = array($mnethostid, $remotecourseid);
// store the timestamp of the recent fetch, can be used for cache invalidate purposes
set_config('lastfetchenrolments', time(), 'mnetservice_enrol');
// local cache successfully updated
return true;

return $DB->get_records_sql($sql, $params);
} else {
return serialize($request->error);
}
}

/**
Expand Down Expand Up @@ -429,9 +420,9 @@ public function find_users($search) {
}

if ($search) {
$groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
$groupname = get_string('enrolledusersmatching', 'enrol', $search);
} else {
$groupname = get_string('enrolcandidates', 'enrol');
$groupname = get_string('enrolledusers', 'enrol');
}

return array($groupname => $availableusers);
Expand Down
1 change: 1 addition & 0 deletions mnet/service/enrol/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
text-align: center;
}
table.remotehosts,
table.otherenrolledusers,
table.remotecourses {
margin: 0px auto 1em auto;
}
Expand Down

0 comments on commit ef75ea6

Please sign in to comment.