Mnet: Update user's enrolment information on jump/land

This commit is contained in:
donal72 2007-01-23 02:38:54 +00:00
parent 3de34ccf68
commit 573f8b025c
3 changed files with 112 additions and 6 deletions

View File

@ -306,7 +306,7 @@ class auth_plugin_mnet
// check sso access control list for permission first
if (!$this->can_login_remotely($localuser->username, $remotehost->id)) {
error("Username '$localuser->username' is not permitted to login from '$remotehost->name'.");
print_error('sso_mnet_login_refused', 'mnet', '', array($localuser->username, $remotehost->name));
}
$session_gc_maxlifetime = 1440;
@ -423,6 +423,16 @@ class auth_plugin_mnet
$keys = array_keys($courses);
$defaultrolename = get_field('role', 'shortname', 'id', $CFG->defaultcourseroleid);
foreach ($keys AS $id) {
if ($courses[$id]->visible == 0) {
unset($courses[$id]);
continue;
}
$courses[$id]->cat_id = $extra[$id]->category;
$courses[$id]->defaultroleid = $extra[$id]->defaultrole;
unset($courses[$id]->category);
unset($courses[$id]->defaultrole);
unset($courses[$id]->visible);
$courses[$id]->cat_name = $extra[$id]->cat_name;
$courses[$id]->cat_description = $extra[$id]->cat_description;
if (!empty($extra[$id]->defaultrolename)) {
@ -461,7 +471,7 @@ class auth_plugin_mnet
* @returns bool
*/
function update_enrolments($username, $courses) {
global $MNET_REMOTE_CLIENT;
global $MNET_REMOTE_CLIENT, $CFG;
if (empty($username) || !is_array($courses)) {
return false;
@ -482,10 +492,99 @@ class auth_plugin_mnet
return true;
}
// add/update courses && enrolment entries
// remove stale enrolments (but not the courses)
// TODO
// IMPORTANT: Ask for remoteid as the first element in the query, so
// that the array that comes back is indexed on the same field as the
// array that we have received from the remote client
$sql = '
SELECT
c.remoteid,
c.id,
c.cat_id,
c.cat_name,
c.cat_description,
c.sortorder,
c.fullname,
c.shortname,
c.idnumber,
c.summary,
c.startdate,
c.cost,
c.currency,
c.defaultroleid,
c.defaultrolename,
a.id as assignmentid
FROM
'.$CFG->prefix.'mnet_enrol_course c
LEFT JOIN
'.$CFG->prefix.'mnet_enrol_assignments a
ON
(a.courseid = c.id AND
a.hostid = c.hostid AND
a.userid = \''.$userid.'\')
WHERE
c.hostid = \''.(int)$MNET_REMOTE_CLIENT->id.'\'';
$currentcourses = get_records_sql($sql);
$local_courseid_array = array();
foreach($courses as $course) {
$course['remoteid'] = $course['id'];
$course['hostid'] = (int)$MNET_REMOTE_CLIENT->id;
$userisregd = false;
// First up - do we have a record for this course?
if (!array_key_exists($course['remoteid'], $currentcourses)) {
// No record - we must create it
$course['id'] = insert_record('mnet_enrol_course', (object)$course);
$currentcourse = (object)$course;
} else {
// Pointer to current course:
$currentcourse =& $currentcourses[$course['remoteid']];
// We have a record - is it up-to-date?
$course['id'] = $currentcourse->id;
$saveflag = false;
foreach($course as $key => $value) {
if ($currentcourse->$key != $value) {
$saveflag = true;
$currentcourse->$key = $value;
}
}
if ($saveflag) {
update_record('mnet_enrol_course', $currentcourse);
}
if (isset($currentcourse->assignmentid) && is_numeric($currentcourse->assignmentid)) {
$userisregd = true;
}
}
// By this point, we should always have a $dataObj->id
$local_courseid_array[] = $course['id'];
// Do we have a record for this assignment?
if ($userisregd) {
// Yes - we know about this one already
// We don't want to do updates because the new data is probably
// 'less complete' than the data we have.
} else {
// No - create a record
$assignObj = new stdClass();
$assignObj->userid = $userid;
$assignObj->hostid = (int)$MNET_REMOTE_CLIENT->id;
$assignObj->courseid = $course['id'];
$assignObj->rolename = $course['defaultrolename'];
$assignObj->id = insert_record('mnet_enrol_assignments', $assignObj);
}
}
// Clean up courses that the user is no longer enrolled in.
$local_courseid_string = implode(', ', $local_courseid_array);
$whereclause = " userid = '$userid' AND hostid = '{$MNET_REMOTE_CLIENT->id}' AND courseid NOT IN ($local_courseid_string)";
delete_records_select('mnet_enrol_assignments', $whereclause);
}
/**

View File

@ -10,4 +10,5 @@ $string['sso_sp_description'] = 'Publish this service to allow authent
'<ul><li><em>Dependency</em>: You must also <strong>subscribe</strong> to the SSO (Identity Provider) service on $a.</li></ul><br />'.
'Subscribe to this service to allow your users to roam to the $a Moodle site without having to re-login there. '.
'<ul><li><em>Dependency</em>: You must also <strong>publish</strong> the SSO (Identity Provider) service to $a.</li></ul><br />';
$string['sso_mnet_login_refused'] = 'Username $a[0] is not permitted to login from $a[1].';
?>

View File

@ -253,7 +253,7 @@ function mnet_server_fault($code, $text, $param = null) {
* @return string $text The XML text of the error message
*/
function mnet_server_fault_xml($code, $text) {
global $MNET_REMOTE_CLIENT;
global $MNET_REMOTE_CLIENT, $CFG;
// Replace illegal XML chars - is this already in a lib somewhere?
$text = str_replace(array('<','>','&','"',"'"), array('&lt;','&gt;','&amp;','&quot;','&apos;'), $text);
@ -274,6 +274,12 @@ function mnet_server_fault_xml($code, $text) {
</value>
</fault>
</methodResponse>');
if (!empty($CFG->mnet_rpcdebug)) {
trigger_error("XMLRPC Error Response");
trigger_error(print_r($return,1));
}
return $return;
}