Skip to content

Commit

Permalink
MDL-22787 MNet: fetch the information about remote enrolments of our …
Browse files Browse the repository at this point in the history
…users (work in progress)
  • Loading branch information
mudrd8mz committed Jul 17, 2010
1 parent de5a205 commit 3ce65bf
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 19 deletions.
77 changes: 77 additions & 0 deletions mnet/service/enrol/course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Page to enrol our users into remote courses
*
* @package plugintype
* @subpackage pluginname
* @copyright 2010 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');

require_sesskey();

$hostid = required_param('host', PARAM_INT); // remote host id in our mnet_host table
$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');
$service = mnetservice_enrol::get_instance();

if (!$service->is_available()) {
echo $OUTPUT->box(get_string('mnetdisabled','mnet'), 'noticebox');
echo $OUTPUT->footer();
die();
}

// remote hosts that may publish remote enrolment service and we are subscribed to it
$hosts = $service->get_remote_publishers();

if (empty($hosts[$hostid])) {
print_error('wearenotsubscribedtothishost', 'mnetservice_enrol');
}
$host = $hosts[$hostid];
$course = $DB->get_record('mnetservice_enrol_courses', array('id'=>$courseid, 'hostid'=>$host->id), '*', MUST_EXIST);

echo $OUTPUT->header();

// course name
$icon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/course'), 'alt' => get_string('category')));
echo $OUTPUT->heading($icon . s($course->fullname));

// collapsible course summary
if (!empty($course->summary)) {
unset($options);
$options->trusted = false;
$options->para = false;
$options->filter = false;
$options->noclean = false;
print_collapsible_region_start('remotecourse summary', 'remotecourse-summary', get_string('coursesummary'), false, true);
echo format_text($course->summary, $course->summaryformat, $options, $course->id);
print_collapsible_region_end();
}

// form to enrol our students
$enrolments = $service->get_remote_course_enrolments($host->id, $course->remoteid, $usecache);

print_object($enrolments); // DONOTCOMMIT
echo $OUTPUT->footer();
119 changes: 100 additions & 19 deletions mnet/service/enrol/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ public function get_remote_publishers() {
* @return array|string returned list or serialized array of mnet error messages
*/
public function get_remote_courses($mnethostid, $usecache=true) {
global $CFG, $DB;
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
global $CFG, $DB; // $CFG needed!

$lastfetchcourses = get_config('mnetservice_enrol', 'lastfetchcourses');
if (empty($lastfetchcourses) or (time()-$lastfetchcourses > DAYSECS)) {
Expand All @@ -163,6 +162,7 @@ public function get_remote_courses($mnethostid, $usecache=true) {
}

// do not use cache - fetch fresh list from remote MNet host
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
$peer = new mnet_peer();
if (!$peer->set_id($mnethostid)) {
return serialize(array('unknown mnet peer'));
Expand Down Expand Up @@ -250,31 +250,112 @@ public function get_remote_courses($mnethostid, $usecache=true) {
* @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
*/
public function get_remote_course_enrolments($mnethostid, $remotecourseid) {
global $CFG, $DB;
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';

$peer = new mnet_peer();
if (!$peer->set_id($mnethostid)) {
return serialize(array('unknown mnet peer'));
}
public function get_remote_course_enrolments($mnethostid, $remotecourseid, $usecache=true) {
global $CFG, $DB; // $CFG needed!

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

$request = new mnet_xmlrpc_client();
$request->set_method('enrol/mnet/enrol.php/course_enrolments');
$request->add_param($remotecourseid, 'int');
$lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
if (empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) {
// force XML-RPC every 10 minutes
$usecache = false;
}

if ($request->send($peer)) {
$list = array();
$response = $request->response;
return $response;
if (!$usecache) {
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';

} else {
return serialize($request->error);
$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');

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'];
}
list($usql, $params) = $DB->get_in_or_equal($usernames, SQL_PARAMS_NAMED);
$params['mnethostid'] = $CFG->mnet_localhost_id;
$sql = "SELECT username,id
FROM {user}
WHERE mnethostid = :mnethostid
AND username $usql
AND deleted = 0
AND confirmed = 1
ORDER BY lastname,firstname,email";
$usersbyusername = $DB->get_records_sql($sql, $params);

// 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, 'rolename'=>$enrolment->rolename,
'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($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);

return $DB->get_records_sql($sql, $params);
}

/**
Expand Down

0 comments on commit 3ce65bf

Please sign in to comment.