Skip to content

Commit

Permalink
MDL-22787 MNet: WIP prototype of the remote enrolment page based on u…
Browse files Browse the repository at this point in the history
…ser selectors
  • Loading branch information
mudrd8mz committed Jul 17, 2010
1 parent a722e49 commit 26f194d
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 6 deletions.
52 changes: 48 additions & 4 deletions mnet/service/enrol/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
$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');
admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1),
new moodle_url('/mnet/service/enrol/course.php'));

$service = mnetservice_enrol::get_instance();

if (!$service->is_available()) {
Expand Down Expand Up @@ -70,8 +72,50 @@
print_collapsible_region_end();
}

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

// user selectors
$currentuserselector = new mnetservice_enrol_existing_users_selector('removeselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
$potentialuserselector = new mnetservice_enrol_potential_users_selector('addselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));

// process incoming data

// print form to enrol our students
?>
<form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
<div>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
<input type="hidden" name="hostid" value="<?php echo $host->id ?>" />
<input type="hidden" name="courseid" value="<?php echo $course->id ?>" />

<table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
<tr>
<td id="existingcell">
<p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
<?php $currentuserselector->display() ?>
</td>
<td id="buttonscell">
<div id="addcontrols">
<input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />

<div class="enroloptions">
<p><?php echo get_string('assignrole', 'role') .': '. s($course->rolename); ?></p>
</div>

</div>

<div id="removecontrols">
<input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
</div>
</td>
<td id="potentialcell">
<p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
<?php $potentialuserselector->display() ?>
</td>
</tr>
</table>
</div>
</form>
<?php

print_object($enrolments); // DONOTCOMMIT
echo $OUTPUT->footer();
156 changes: 154 additions & 2 deletions mnet/service/enrol/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/user/selector/lib.php');

/**
* Singleton providing various functionality usable by plugin(s) implementing this MNet service
*/
Expand Down Expand Up @@ -318,8 +320,7 @@ public function get_remote_course_enrolments($mnethostid, $remotecourseid, $usec
$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');
'remotecourseid'=>$enrolment->remotecourseid, 'enroltype'=>$enrolment->enroltype), 'id, enroltime');
if (empty($current)) {
$enrolment->id = $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
} else {
Expand Down Expand Up @@ -374,5 +375,156 @@ public function format_error_message($errormsg) {
}
return $output;
}
}

/**
* Selector of our users enrolled into remote course via enrol_mnet plugin
*/
class mnetservice_enrol_existing_users_selector extends user_selector_base {
/** @var id of the MNet peer */
protected $hostid;
/** @var id of the course at the remote server */
protected $remotecourseid;

public function __construct($name, $options) {
$this->hostid = $options['hostid'];
$this->remotecourseid = $options['remotecourseid'];
parent::__construct($name, $options);
}

/**
* Find our users currently enrolled into the remote course
*
* @param string $search
* @return array
*/
public function find_users($search) {
global $DB;

list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['hostid'] = $this->hostid;
$params['remotecourseid'] = $this->remotecourseid;

$fields = "SELECT ".$this->required_fields_sql("u");
$countfields = "SELECT COUNT(1)";

$sql = " FROM {user} u
JOIN {mnetservice_enrol_enrolments} e ON e.userid = u.id
WHERE e.hostid = :hostid AND e.remotecourseid = :remotecourseid
AND e.enroltype = 'mnet'
AND $wherecondition";
$order = " ORDER BY u.lastname ASC, u.firstname ASC";

if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}

$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);

if (empty($availableusers)) {
return array();
}

if ($search) {
$groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
} else {
$groupname = get_string('enrolcandidates', 'enrol');
}

return array($groupname => $availableusers);
}

protected function get_options() {
$options = parent::get_options();
$options['hostid'] = $this->hostid;
$options['remotecourseid'] = $this->remotecourseid;
$options['file'] = 'mnet/service/enrol/locallib.php';
return $options;
}
}

/**
* Selector of our users who could be enrolled into a remote course via their enrol_mnet
*/
class mnetservice_enrol_potential_users_selector extends user_selector_base {
/** @var id of the MNet peer */
protected $hostid;
/** @var id of the course at the remote server */
protected $remotecourseid;

public function __construct($name, $options) {
$this->hostid = $options['hostid'];
$this->remotecourseid = $options['remotecourseid'];
parent::__construct($name, $options);
}

/**
* Find our users who could be enrolled into the remote course
*
* Our users must have 'moodle/site:mnetlogintoremote' capability assigned.
* Remote users, guests, deleted and not confirmed users are not returned.
*
* @param string $search
* @return array
*/
public function find_users($search) {
global $CFG, $DB;

$systemcontext = get_system_context();
$userids = get_users_by_capability($systemcontext, 'moodle/site:mnetlogintoremote', 'u.id');

list($usql, $uparams) = $DB->get_in_or_equal(array_keys($userids), SQL_PARAMS_NAMED, 'uid0000');

list($wherecondition, $params) = $this->search_sql($search, 'u');

$params = array_merge($params, $uparams);
$params['hostid'] = $this->hostid;
$params['remotecourseid'] = $this->remotecourseid;
$params['mnetlocalhostid'] = $CFG->mnet_localhost_id;

$fields = "SELECT ".$this->required_fields_sql("u");
$countfields = "SELECT COUNT(1)";

$sql = " FROM {user} u
WHERE $wherecondition
AND u.mnethostid = :mnetlocalhostid
AND u.id $usql
AND u.id NOT IN (SELECT e.userid
FROM {mnetservice_enrol_enrolments} e
WHERE (e.hostid = :hostid AND e.remotecourseid = :remotecourseid))";

$order = " ORDER BY u.lastname ASC, u.firstname ASC";

if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}

$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);

if (empty($availableusers)) {
return array();
}

if ($search) {
$groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
} else {
$groupname = get_string('enrolcandidates', 'enrol');
}

return array($groupname => $availableusers);
}

protected function get_options() {
$options = parent::get_options();
$options['hostid'] = $this->hostid;
$options['remotecourseid'] = $this->remotecourseid;
$options['file'] = 'mnet/service/enrol/locallib.php';
return $options;
}
}
3 changes: 3 additions & 0 deletions mnet/service/enrol/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ table.remotecourses th.categoryname img {
.collapsibleregion.remotecourse.summary {
margin: 0px 10em;
}
.roleassigntable {
margin: 1em auto;
}

0 comments on commit 26f194d

Please sign in to comment.