Skip to content

Commit

Permalink
MDL-22787 MNet: Fixing regressions in auth_mnet caused by changes in …
Browse files Browse the repository at this point in the history
…enrol_mnet

This smells - IMHO auth_enrol should not do anything with the enrolment
related information. These two plugins seem to be tied too much...
  • Loading branch information
mudrd8mz committed Jul 17, 2010
1 parent 8ed6e35 commit 152a227
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 69 deletions.
2 changes: 1 addition & 1 deletion admin/mnet/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
if ('verify' == $step) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deleteaserver', 'mnet'));
if ($mnet_peer->count_live_sessions() > 0) {
if ($live_users = $mnet_peer->count_live_sessions() > 0) {
echo $OUTPUT->notification(get_string('usersareonline', 'mnet', $live_users));
}
$yesurl = new moodle_url('/admin/mnet/delete.php', array('hostid' => $mnet_peer->id, 'step' => 'delete'));
Expand Down
87 changes: 30 additions & 57 deletions auth/mnet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,14 @@ function user_authorise($token, $useragent) {
$userdata['myhosts'][] = array('name'=> $SITE->shortname, 'url' => $CFG->wwwroot, 'count' => count($courses));
}

$sql = "
SELECT
h.name as hostname,
h.wwwroot,
h.id as hostid,
count(c.id) as count
FROM
{mnet_enrol_course} c,
{mnet_enrol_assignments} a,
{mnet_host} h
WHERE
c.id = a.courseid AND
c.hostid = h.id AND
a.userid = ? AND
c.hostid != ?
GROUP BY
h.name,
h.id,
h.wwwroot";
$sql = "SELECT h.name AS hostname, h.wwwroot, h.id AS hostid,
COUNT(c.id) AS count
FROM {mnetservice_enrol_courses} c
JOIN {mnetservice_enrol_enrolments} e ON (e.hostid = c.hostid AND e.remotecourseid = c.remoteid)
JOIN {mnet_host} h ON h.id = c.hostid
WHERE e.userid = ? AND c.hostid = ?
GROUP BY h.name, h.wwwroot, h.id";

if ($courses = $DB->get_records_sql($sql, array($user->id, $remoteclient->id))) {
foreach($courses as $course) {
$userdata['myhosts'][] = array('name'=> $course->hostname, 'url' => $CFG->wwwroot.'/auth/mnet/jump.php?hostid='.$course->hostid, 'count' => $course->count);
Expand Down Expand Up @@ -139,7 +128,7 @@ function start_jump_session($mnethostid, $wantsurl, $wantsurlbackhere=false) {
require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';

// check remote login permissions
if (! has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM))
if (! has_capability('moodle/site:mnetlogintoremote', get_system_context())
or is_mnet_remote_user($USER)
or isguestuser()
or !isloggedin()) {
Expand Down Expand Up @@ -359,7 +348,7 @@ function confirm_mnet_session($token, $remotepeer) {
$mnetrequest->set_method('auth/mnet/auth.php/update_enrolments');

// pass username and an assoc array of "my courses"
// with info so that the IDP can maintain mnet_enrol_assignments
// with info so that the IDP can maintain mnetservice_enrol_enrolments
$mnetrequest->add_param($remoteuser->username);
$fields = 'id, category, sortorder, fullname, shortname, idnumber, summary, startdate, visible';
$courses = enrol_get_users_courses($localuser->id, false, $fields, 'visible DESC,sortorder ASC');
Expand All @@ -374,7 +363,8 @@ function confirm_mnet_session($token, $remotepeer) {
$extra = $DB->get_records_sql($sql);

$keys = array_keys($courses);
$defaultrole = get_default_course_role($ccache[$shortname]); //TODO: rewrite this completely, there is no default course role any more!!!
$defaultrole = reset(get_archetype_roles('student'));
//$defaultrole = get_default_course_role($ccache[$shortname]); //TODO: rewrite this completely, there is no default course role any more!!!
foreach ($keys AS $id) {
if ($courses[$id]->visible == 0) {
unset($courses[$id]);
Expand Down Expand Up @@ -452,7 +442,7 @@ public function update_mnet_session($user, $token, $remotepeer) {
* Normally called by the SP after calling user_authorise()
*
* @param string $username The username
* @param string $courses Assoc array of courses following the structure of mnet_enrol_course
* @param string $courses Assoc array of courses following the structure of mnetservice_enrol_courses
* @return bool
*/
function update_enrolments($username, $courses) {
Expand All @@ -470,52 +460,32 @@ function update_enrolments($username, $courses) {
}

if (empty($courses)) { // no courses? clear out quickly
$DB->delete_records('mnet_enrol_assignments', array('hostid'=>$remoteclient->id, 'userid'=>$userid));
$DB->delete_records('mnetservice_enrol_enrolments', array('hostid'=>$remoteclient->id, 'userid'=>$userid));
return true;
}

// 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,
a.id as assignmentid
FROM
{mnet_enrol_course} c
LEFT JOIN {mnet_enrol_assignments} a
ON
(a.courseid = c.id AND
a.hostid = c.hostid AND
a.userid = ?)
WHERE
c.hostid = ?';
$sql = "SELECT c.remoteid, c.id, c.categoryid AS cat_id, c.categoryname AS cat_name, c.sortorder,
c.fullname, c.shortname, c.idnumber, c.summary, c.summaryformat, c.startdate,
e.id AS enrolmentid
FROM {mnetservice_enrol_courses} c
LEFT JOIN {mnetservice_enrol_enrolments} e ON (e.hostid = c.hostid AND e.remotecourseid = c.remoteid)
WHERE e.userid = ? AND c.hostid = ?";

$currentcourses = $DB->get_records_sql($sql, array($userid, $remoteclient->id));

$local_courseid_array = array();
foreach($courses as $course) {
foreach($courses as $ix => $course) {

$course['remoteid'] = $course['id'];
$course['hostid'] = (int)$remoteclient->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'] = $DB->insert_record('mnet_enrol_course', (object)$course);
$currentcourse = (object)$course;
} else {
// if we do not have the the information about the remote course, it is not available
// to us for remote enrolment - skip
if (array_key_exists($course['remoteid'], $currentcourses)) {
// Pointer to current course:
$currentcourse =& $currentcourses[$course['remoteid']];
// We have a record - is it up-to-date?
Expand All @@ -531,12 +501,15 @@ function update_enrolments($username, $courses) {
}

if ($saveflag) {
$DB->update_record('mnet_enrol_course', $currentcourse);
$DB->update_record('mnetervice_enrol_courses', $currentcourse);
}

if (isset($currentcourse->assignmentid) && is_numeric($currentcourse->assignmentid)) {
if (isset($currentcourse->enrolmentid) && is_numeric($currentcourse->enrolmentid)) {
$userisregd = true;
}
} else {
unset ($courses[$ix]);
continue;
}

// By this point, we should always have a $dataObj->id
Expand All @@ -552,7 +525,7 @@ function update_enrolments($username, $courses) {
$assignObj = new stdClass();
$assignObj->userid = $userid;
$assignObj->hostid = (int)$remoteclient->id;
$assignObj->courseid = $course['id'];
$assignObj->remotecourseid = $course['remoteid'];
$assignObj->rolename = $course['defaultrolename'];
$assignObj->id = $DB->insert_record('mnet_enrol_assignments', $assignObj);
}
Expand Down
2 changes: 1 addition & 1 deletion course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,7 @@ function print_remote_course($course, $width="100%") {
$options = NULL;
$options->noclean = true;
$options->para = false;
echo format_text($course->summary, FORMAT_MOODLE, $options);
echo format_text($course->summary, $course->summaryformat, $options);
echo '</div>';
echo '</div>';
}
Expand Down
69 changes: 65 additions & 4 deletions enrol/mnet/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,72 @@ public function unenrol_user() {
}

/**
* TODO: short description.
* Returns a list of users from the client server who are enrolled in our course
*
* @return TODO
* Suitable instance of enrol_mnet must be created in the course. This method will not
* return any information about the enrolments in courses that are not available for
* remote enrolment, even if their users are enrolled into them via other plugin
* (note the difference from {@link self::user_enrolments()}).
*
* This method will return enrolment information for users from hosts regardless
* the enrolment plugin. It does not matter if the user was enrolled remotely by
* their admin or locally. Once the course is available for remote enrolments, we
* will tell them everything about their users.
*
* In Moodle 1.x the returned array used to be indexed by username. The side effect
* of MDL-19219 fix is that we do not need to use such index and therefore we can
* return all enrolment records. MNet clients 1.x will only use the last record for
* the student, if she is enrolled via multiple plugins.
*
* @uses mnet_remote_client Callable via XML-RPC only
* @param int $courseid ID of our course
* @return array
*/
public function course_enrolments() {
return array();
public function course_enrolments($courseid) {
global $DB;

if (!$client = get_mnet_remote_client()) {
die('Callable via XML-RPC only');
}

// todo 'guest' nevracet

return array(
0 => array(
'username' => 'admina',
'shortname' => 'student',
'name' => 'Student',
'enrol' => 'mnet',
'timemodified' => time(),
),
1 => array(
'username' => 'admina',
'shortname' => 'teacher',
'name' => 'Teacher',
'enrol' => 'manual',
'timemodified' => time(),
),
2 => array(
'username' => 'guest',
'shortname' => 'admin',
'name' => 'Admin',
'enrol' => 'crack',
'timemodified' => time(),
),
3 => array(
'username' => 'usera',
'shortname' => 'student',
'name' => 'Student',
'enrol' => 'mnet',
'timemodified' => time(),
),
4 => array(
'username' => 'doesnotexist',
'shortname' => 'student',
'name' => 'Student',
'enrol' => 'mnet',
'timemodified' => time(),
),
);
}
}
12 changes: 6 additions & 6 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,13 +1172,13 @@ function get_my_remotecourses($userid=0) {
$userid = $USER->id;
}

$sql = "SELECT c.id, c.remoteid, c.shortname, c.fullname,
c.hostid, c.summary, c.cat_name,
$sql = "SELECT DISTINCT c.id, c.remoteid, c.shortname, c.fullname,
c.hostid, c.summary, c.summaryformat, c.categoryname AS cat_name,
h.name AS hostname
FROM {mnet_enrol_course} c
JOIN {mnet_enrol_assignments} a ON c.id=a.courseid
JOIN {mnet_host} h ON c.hostid=h.id
WHERE a.userid=?";
FROM {mnetservice_enrol_courses} c
JOIN {mnetservice_enrol_enrolments} e ON (e.hostid = c.hostid AND e.remotecourseid = c.remoteid)
JOIN {mnet_host} h ON h.id = c.hostid
WHERE e.userid = ?";

return $DB->get_records_sql($sql, array($userid));
}
Expand Down

0 comments on commit 152a227

Please sign in to comment.