MDL-22787 MNet: Finished client side of remote enrolment service

This commit is contained in:
David Mudrak 2010-07-17 22:35:18 +00:00
parent 8123245f7b
commit c24cf44291
2 changed files with 102 additions and 5 deletions

View File

@ -72,12 +72,17 @@ if (!empty($course->summary)) {
print_collapsible_region_end();
}
$error = '';
$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;
$result = $service->req_course_enrolments($host->id, $course->remoteid, $usecache);
if ($result !== true) {
$error .= $service->format_error_message($result);
}
}
// user selectors
@ -89,8 +94,11 @@ 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!
$user = $DB->get_record('user', array('id'=>$adduser->id));
$result = $service->req_enrol_user($user, $course);
if ($result !== true) {
$error .= $service->format_error_message($result);
}
}
$potentialuserselector->invalidate_selected_users();
@ -103,8 +111,11 @@ 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!
$user = $DB->get_record('user', array('id'=>$removeuser->id));
$result = $service->req_unenrol_user($user, $course);
if ($result !== true) {
$error .= $service->format_error_message($result);
}
}
$potentialuserselector->invalidate_selected_users();
@ -112,6 +123,10 @@ if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
}
}
if (!empty($error)) {
echo $OUTPUT->box($error, 'generalbox error');
}
// print form to enrol our students
?>
<form id="assignform" method="post" action="<?php echo $PAGE->url ?>">

View File

@ -350,6 +350,88 @@ class mnetservice_enrol {
}
}
/**
* Send request to enrol our user to the remote course
*
* Updates our remote enrolments cache if the enrolment was successful.
*
* @uses mnet_xmlrpc_client Invokes XML-RPC request
* @param object $user our user
* @param object $remotecourse record from mnetservice_enrol_courses table
* @return true|string true if success, error message from the remote host otherwise
*/
public function req_enrol_user(stdclass $user, stdclass $remotecourse) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
$peer = new mnet_peer();
$peer->set_id($remotecourse->hostid);
$request = new mnet_xmlrpc_client();
$request->set_method('enrol/mnet/enrol.php/enrol_user');
$request->add_param(mnet_strip_user((array)$user, mnet_fields_to_send($peer)));
$request->add_param($remotecourse->remoteid);
if ($request->send($peer) === true) {
if ($request->response === true) {
// cache the enrolment information in our table
$enrolment = new stdclass();
$enrolment->hostid = $peer->id;
$enrolment->userid = $user->id;
$enrolment->remotecourseid = $remotecourse->remoteid;
$enrolment->enroltype = 'mnet';
// $enrolment->rolename not known now, must be re-fetched
// $enrolment->enroltime not known now, must be re-fetched
$DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
return true;
} else {
return serialize(array('invalid response: '.print_r($request->response, true)));
}
} else {
return serialize($request->error);
}
}
/**
* Send request to unenrol our user from the remote course
*
* Updates our remote enrolments cache if the unenrolment was successful.
*
* @uses mnet_xmlrpc_client Invokes XML-RPC request
* @param object $user our user
* @param object $remotecourse record from mnetservice_enrol_courses table
* @return true|string true if success, error message from the remote host otherwise
*/
public function req_unenrol_user(stdclass $user, stdclass $remotecourse) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
$peer = new mnet_peer();
$peer->set_id($remotecourse->hostid);
$request = new mnet_xmlrpc_client();
$request->set_method('enrol/mnet/enrol.php/unenrol_user');
$request->add_param($user->username);
$request->add_param($remotecourse->remoteid);
if ($request->send($peer) === true) {
if ($request->response === true) {
// clear the cached information
$DB->delete_records('mnetservice_enrol_enrolments',
array('hostid'=>$peer->id, 'userid'=>$user->id, 'remotecourseid'=>$remotecourse->remoteid, 'enroltype'=>'mnet'));
return true;
} else {
return serialize(array('invalid response: '.print_r($request->response, true)));
}
} else {
return serialize($request->error);
}
}
/**
* Prepares error messages returned by our XML-RPC requests to be send as debug info to {@link print_error()}
*