Mnet: Show non-mnet enrolments in the mnet/enrolment page

This commit is contained in:
donal72 2007-01-17 12:12:11 +00:00
parent 069a820a7e
commit 5291907c45
4 changed files with 143 additions and 36 deletions

View File

@ -235,14 +235,52 @@ class enrolment_plugin_mnet {
}
/**
* Does Foo
*
* @param string $username The username
* @return array Whether the user can login from the remote host
*/
function user_enrolments() {
* Get a list of users from the client server who are enrolled in a course
*
* @param int $courseid The Course ID
* @param string $roles Comma-separated list of role shortnames
* @return array Array of usernames who are homed on the
* client machine
*/
function user_enrolments($courseid, $roles = '') {
global $MNET_REMOTE_CLIENT, $CFG;
return array();
if (! $course = get_record('course', 'id', $courseid) ) {
return 'no course';
//error("That's an invalid course id");
}
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$sql = "
SELECT
u.id,
u.username
FROM
{$CFG->prefix}role_assignments a,
{$CFG->prefix}user u
WHERE
a.contextid = '{$context->id}' AND
a.userid = u.id AND
u.mnethostid = '{$MNET_REMOTE_CLIENT->id}'
";
if(!empty($roles)) {
// $default_role = get_default_course_role($course); ???
$sql .= " AND
a.roleid in ('".str_replace(',', "', '", $roles)."')";
}
$f = fopen('/tmp/sql.sql', 'w');
fwrite($f, $sql);
$enrolments = get_records_sql($sql);
$returnarray = array();
foreach($enrolments as $user) {
$returnarray[] = $user->username;
}
return $returnarray;
}
/**

View File

@ -2,7 +2,7 @@
<form id="assignform" method="post" action="">
<input type="hidden" name="previoussearch" value="<?php p($previoussearch) ?>" />
<input type="hidden" name="courseid" value="<?php p($courseid) ?>" />
<input type="hidden" name="host" value="<?php p($mnethost->id) ?>" />
<input type="hidden" name="host" value="<?php p($mnet_peer->id) ?>" />
<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
<table align="center" border="0" cellpadding="5" cellspacing="0">
<tr>
@ -11,7 +11,7 @@
</td>
<td></td>
<td valign="top">
<?php print_string('potentialusers', 'role', $usercount); ?>
<?php print_string('potentialusers', 'role', $availableusers->_numOfRows); ?>
</td>
</tr>
<tr>
@ -21,10 +21,12 @@
getElementById('assignform').remove.disabled=false;
getElementById('assignform').addselect.selectedIndex=-1;">
<?php
foreach ($enrolledusers as $enrolleduser) {
$fullname = fullname($enrolleduser, true);
echo "<option value=\"$enrolleduser->id\">".$fullname.", ".$enrolleduser->email."</option>\n";
}
foreach ($enrolledusers as $enrolleduser) {
if(!empty($enrolleduser->ismnetenrolment)) {
$fullname = fullname($enrolleduser, true);
echo "<option value=\"$enrolleduser->id\">".$fullname.", ".$enrolleduser->email."</option>\n";
}
}
?>
</select></td>
@ -55,16 +57,14 @@
echo "</optgroup>\n";
} else {
if ($usercount > MAX_USERS_PER_PAGE) {
if ($availableusers->_numOfRows > MAX_USERS_PER_PAGE) {
echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
.'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
} else {
while (! $availableusers->EOF) {
$user = $availableusers->FetchObj();
if (!isset($enrolledusers[$user->id])) {
$fullname = fullname($user, true);
echo "<option value=\"$user->id\">".$fullname.", ".$user->email."</option>\n";
}
$fullname = fullname($user, true);
echo "<option value=\"$user->id\">".$fullname.", ".$user->email."</option>\n";
$availableusers->MoveNext();
}
}
@ -90,5 +90,18 @@
?>
</td>
</tr>
<tr>
<td valign="top"><?php print_string('otherenrolledusers', 'mnet'); ?>:<br />
<?php
foreach ($enrolledusers as $enrolleduser) {
if(empty($enrolleduser->ismnetenrolment)) {
$fullname = fullname($enrolleduser, true);
echo $fullname.', '.$enrolleduser->email."<br />\n";
}
}
?></td>
<td valign="top"></td>
<td valign="top"></td>
</tr>
</table>
</form>

View File

@ -4,6 +4,7 @@
require_once(dirname(__FILE__) . "/../../config.php");
require_once($CFG->libdir.'/adminlib.php');
include_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
$adminroot = admin_get_root();
admin_externalpage_setup('enrolment', $adminroot);
@ -12,13 +13,17 @@
require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class
$enrolment = enrolment_factory::factory('mnet');
$mnethost = required_param('host', PARAM_INT);
$mnethostid = required_param('host', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
$mnethost = get_record('mnet_host', 'id', $mnethost);
$course = get_record('mnet_enrol_course', 'id', $courseid, 'hostid', $mnethost->id);
$mnet_peer = new mnet_peer();
if (!$mnet_peer->set_id($mnethostid)) {
print_error('hostcoursenotfound','mnet');
}
if (empty($mnethost) || empty($course)) {
$course = get_record('mnet_enrol_course', 'id', $courseid, 'hostid', $mnet_peer->id);
if (empty($course)) {
print_error('hostcoursenotfound','mnet');
}
@ -35,7 +40,7 @@
$previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;
$baseurl = "remote_enrolment.php?courseid={$course->id}&amp;host={$mnethost->id}";
$baseurl = "remote_enrolment.php?courseid={$course->id}&amp;host={$mnet_peer->id}";
if (!empty($userid)) {
$baseurl .= '&amp;userid='.$userid;
}
@ -68,20 +73,62 @@
}
/// Prepare data for users / enrolled users panes
$sql = "SELECT u.id, u.firstname, u.lastname, u.email
FROM {$CFG->prefix}mnet_enrol_assignments a
JOIN {$CFG->prefix}user u ON a.userid=u.id
WHERE a.courseid={$courseid}
ORDER BY u.id";
/// Create a new request object
$mnet_request = new mnet_xmlrpc_client();
/// Pass it the path to the method that we want to execute
$mnet_request->set_method('enrol/mnet/enrol.php/user_enrolments');
$mnet_request->add_param($course->remoteid, 'int');
$mnet_request->send($mnet_peer);
$all_enrolled_users = $mnet_request->response;
unset($mnet_request);
$select = '';
$all_enrolled_usernames = '';
/// List all the users (homed on this server) who are enrolled on the course
/// This will include mnet-enrolled users, and those who have enrolled
/// themselves, etc.
if (is_array($all_enrolled_users) && count($all_enrolled_users)) {
foreach($all_enrolled_users as $user) {
$all_enrolled_usernames .= "'{$user}', ";
}
$select = ' u.username IN (' .substr($all_enrolled_usernames, 0, -2) .') AND';
}
/// Pseudocode for query - get records for all users that are enrolled in the
/// course, and if they were enrolled via mnet, ismnetenrolment will be > 0
$sql = "
SELECT
u.id,
u.firstname,
u.lastname,
u.email,
coalesce ( a.hostid , 0) as ismnetenrolment,
a.courseid
FROM
{$CFG->prefix}user u
LEFT JOIN
{$CFG->prefix}mnet_enrol_assignments a
ON
a.userid = u.id AND a.courseid={$courseid}
WHERE
$select
u.deleted = 0 AND
u.confirmed = 1 AND
u.mnethostid = {$CFG->mnet_localhost_id}
ORDER BY
u.firstname ASC,
u.lastname ASC";
if (!$enrolledusers = get_records_sql($sql)) {
$enrolledusers = array();
}
$select = "username != 'guest' AND username != 'changeme' AND deleted = 0 AND confirmed = 1 AND mnethostid = {$CFG->mnet_localhost_id}";
$usercount = count_records_select('user', $select) - count($enrolledusers);
$searchtext = trim($searchtext);
$select = '';
if ($searchtext !== '') { // Search for a subset of remaining users
$LIKE = sql_ilike();
@ -89,10 +136,19 @@
$select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
}
/**** start of NOT IN block ****/
$select .= " AND username NOT IN ('guest', 'changeme', ";
$select .= $all_enrolled_usernames;
$select = substr($select, 0, -2) .') ';
/**** end of NOT IN block ****/
$availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
FROM '.$CFG->prefix.'user
WHERE '.$select.'
ORDER BY lastname ASC, firstname ASC');
WHERE deleted = 0 AND confirmed = 1 AND mnethostid = '.$CFG->mnet_localhost_id.' '.$select.'
ORDER BY lastname ASC, firstname ASC', 0, MAX_USERS_PER_PAGE);
@ -118,7 +174,7 @@ admin_externalpage_print_header($adminroot);
print_simple_box_start("center", "80%");
print_simple_box_start("center", "60%", '', 5, 'informationbox');
print_string('enrollingincourse', 'mnet', array(s($course->shortname), s($mnethost->name)));
print_string('enrollingincourse', 'mnet', array(s($course->shortname), s($mnet_peer->name)));
print_string("description", "enrol_mnet");
print_simple_box_end();

View File

@ -22,7 +22,7 @@ $string['trustedhostsexplain'] = 'Please enter a list of IP addresses o
'Any host whatsoever:<br />'.
'192.168.0.0/0<br />'.
'Obviously the last example is not a recommended configuration.';
$string['otherenrolledusers'] = 'Other enrolled users';
$string['hideremote'] = 'Hide remote users';
$string['showremote'] = 'Show remote users';
$string['hidelocal'] = 'Hide local users';