From bf1a3d20102eaadedfe478122eb23f38ee7af7bd Mon Sep 17 00:00:00 2001
From: martinlanghoff
Date: Thu, 4 Jan 2007 03:05:48 +0000
Subject: [PATCH] mnet: new enrolment plugin
---
enrol/mnet/allowed_courses.php | 240 ++++++++++++++
enrol/mnet/config.html | 36 ++
enrol/mnet/enrol.php | 541 +++++++++++++++++++++++++++++++
enrol/mnet/remote_courses.php | 52 +++
enrol/mnet/remote_enrolment.html | 94 ++++++
enrol/mnet/remote_enrolment.php | 146 +++++++++
enrol/mnet/remote_hosts.php | 64 ++++
lang/en_utf8/enrol_mnet.php | 22 ++
8 files changed, 1195 insertions(+)
create mode 100644 enrol/mnet/allowed_courses.php
create mode 100644 enrol/mnet/config.html
create mode 100644 enrol/mnet/enrol.php
create mode 100644 enrol/mnet/remote_courses.php
create mode 100755 enrol/mnet/remote_enrolment.html
create mode 100644 enrol/mnet/remote_enrolment.php
create mode 100644 enrol/mnet/remote_hosts.php
create mode 100644 lang/en_utf8/enrol_mnet.php
diff --git a/enrol/mnet/allowed_courses.php b/enrol/mnet/allowed_courses.php
new file mode 100644
index 00000000000..2407dc930b5
--- /dev/null
+++ b/enrol/mnet/allowed_courses.php
@@ -0,0 +1,240 @@
+libdir . '/adminlib.php';
+include_once $CFG->dirroot . '/mnet/lib.php';
+
+require_login();
+$adminroot = admin_get_root();
+admin_externalpage_setup('ssoaccesscontrol', $adminroot);
+admin_externalpage_print_header($adminroot);
+
+$addcategory = optional_param('addcategory', 0, PARAM_BOOL);
+$removecategory = optional_param('removecategory', 0, PARAM_BOOL);
+$addcourse = optional_param('addcourse', 0, PARAM_BOOL);
+$removecourse = optional_param('removecourse', 0, PARAM_BOOL);
+
+$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
+$sesskey = sesskey();
+$formerror = array();
+
+require_capability('moodle/user:delete', $sitecontext);
+
+// process returned form data
+if ($form = data_submitted() and confirm_sesskey()) {
+
+ // add and remove categories as needed
+ if (!empty($CFG->enrol_mnet_allowed_categories)) {
+ $allowedcategories = explode(',', $CFG->enrol_mnet_allowed_categories);
+ }
+ if ($addcategory and !empty($form->addcategories)) {
+ foreach ($form->addcategories as $category) {
+ if (!$category = clean_param($category, PARAM_INT)) {
+ continue;
+ }
+ $allowedcategories[] = $category;
+ }
+ }
+ if ($removecategory and !empty($form->removecategories)) {
+ foreach ($form->removecategories as $category) {
+ if ($category = clean_param($category, PARAM_INT)) {
+ $removedcategories[] = $category;
+ }
+ }
+ $allowedcategories = array_diff($allowedcategories, $removedcategories);
+ }
+
+ // add and remove courses as needed
+ if (!empty($CFG->enrol_mnet_allowed_courses)) {
+ $allowedcourses = explode(',', $CFG->enrol_mnet_allowed_courses);
+ }
+ if ($addcourse and !empty($form->addcourses)) {
+ foreach ($form->addcourses as $course) {
+ if ($course = clean_param($course, PARAM_INT)) {
+ $allowedcourses[] = $course;
+ }
+ }
+ }
+ if ($removecourse and !empty($form->removecourses)) {
+ foreach ($form->removecourses as $course) {
+ if (!$course = clean_param($course, PARAM_INT)) {
+ continue;
+ }
+ $removedcourses[] = $course;
+ }
+ $allowedcourses = array_diff($allowedcourses, $removedcourses);
+ }
+
+ // save config
+ $cfg = empty($allowedcategories) ? '' : implode(',', $allowedcategories);
+ set_config('enrol_mnet_allowed_categories', $cfg);
+ $cfg = empty($allowedcourses) ? '' : implode(',', $allowedcourses);
+ set_config('enrol_mnet_allowed_courses', $cfg);
+
+ // redirect('allowed_courses.php', get_string('changessaved'));
+}
+
+
+
+// setup arrays for allowed categories and courses
+$categories = array();
+if ($categories = get_records('course_categories', '', '', 'name', 'id, name')) {
+ if (empty($CFG->enrol_mnet_allowed_categories)) {
+ $allowedcategories = array();
+ $potentialcategories = $categories;
+ } else {
+ $allowedcategories = array_intersect_key($categories, array_flip(explode(',', $CFG->enrol_mnet_allowed_categories)));
+ $potentialcategories = array_diff_key($categories, array_flip(explode(',', $CFG->enrol_mnet_allowed_categories)));
+ }
+}
+$courses = array();
+if ($courses = get_records('course', '', '', 'shortname', 'id, shortname')) {
+ if (empty($CFG->enrol_mnet_allowed_courses)) {
+ $allowedcourses = array();
+ $potentialcourses = $courses;
+ } else {
+ $allowedcourses = array_intersect_key($courses, array_flip(explode(',', $CFG->enrol_mnet_allowed_courses)));
+ $potentialcourses = array_diff_key($courses, array_flip(explode(',', $CFG->enrol_mnet_allowed_courses)));
+ }
+}
+
+
+
+// output the form
+print_simple_box_start('center','90%','','20');
+
+?>
+
+
diff --git a/enrol/mnet/config.html b/enrol/mnet/config.html
new file mode 100644
index 00000000000..987bc4ec716
--- /dev/null
+++ b/enrol/mnet/config.html
@@ -0,0 +1,36 @@
+
diff --git a/enrol/mnet/enrol.php b/enrol/mnet/enrol.php
new file mode 100644
index 00000000000..d2346fcb530
--- /dev/null
+++ b/enrol/mnet/enrol.php
@@ -0,0 +1,541 @@
+allow_allcourses: expose all courses to external enrolment
+// $config->allowed_categories: serialised array of courses allowed
+// $config->allowed_courses: serialised array of courses allowed
+
+class enrolment_plugin_mnet {
+
+ /// Override the base config_form() function
+ function config_form($frm) {
+ global $CFG;
+
+ $vars = array('enrol_mnet_allow_allcourses',
+ 'enrol_mnet_allowed_categories',
+ 'enrol_mnet_allowed_courses');
+
+ foreach ($vars as $var) {
+ if (!isset($frm->$var)) {
+ $frm->$var = '';
+ }
+ }
+
+ $mnethosts = $this->list_remote_servers();
+
+ include ("$CFG->dirroot/enrol/mnet/config.html");
+ }
+
+
+ /// Override the base process_config() function
+ function process_config($config) {
+
+ if (!isset($config->enrol_mnet_allow_allcourses)) {
+ $config->enrol_mnet_allow_allcourses = false;
+ }
+ set_config('enrol_mnet_allow_allcourses', $config->enrol_mnet_allow_allcourses);
+
+ if (!isset($config->enrol_mnet_allowed_categories)) {
+ $config->enrol_mnet_allowed_categories = '';
+ }
+ set_config('enrol_mnet_allowed_categories', $config->enrol_mnet_allowed_categories);
+
+ if (!isset($config->enrol_mnet_allowed_courses)) {
+ $config->enrol_mnet_allowed_courses = '';
+ }
+ set_config('enrol_mnet_allowed_courses', $config->enrol_mnet_allowed_courses);
+
+ return true;
+
+ }
+
+ /// Override the get_access_icons() function
+ function get_access_icons($course) {
+ }
+
+ /**
+ * Override the base cron() function
+ */
+ //function cron() {
+ //
+ //} // end of cron()
+
+
+
+ /***
+ *** MNET functions
+ ***
+ ***/
+ function mnet_publishes() {
+
+ $enrol = array();
+ $enrol['name'] = 'mnet_enrol'; // Name & Description go in lang file
+ $enrol['apiversion'] = 1;
+ $enrol['methods'] = array('available_courses','user_enrolments', 'enrol_user', 'unenrol_user' );
+
+ return array($enrol);
+ }
+
+ /**
+ * Does Foo
+ *
+ * @param string $username The username
+ * @param int $mnethostid The id of the remote mnethost
+ * @return bool Whether the user can login from the remote host
+ */
+ function available_courses() {
+ global $CFG;
+
+ if (!empty($CFG->enrol_mnet_allow_allcourses)) {
+
+ $query =
+ "SELECT
+ co.id as remoteid,
+ ca.id as cat_id,
+ ca.name as cat_name,
+ ca.description as cat_description,
+ co.sortorder,
+ co.fullname,
+ co.shortname,
+ co.idnumber,
+ co.summary,
+ co.startdate,
+ co.cost,
+ co.currency,
+ co.defaultrole as defaultroleid,
+ r.name
+ FROM
+ {$CFG->prefix}course_categories ca
+ JOIN
+ {$CFG->prefix}course co ON
+ ca.id = co.category
+ LEFT JOIN
+ {$CFG->prefix}role r ON
+ r.id = co.defaultrole
+ WHERE
+ co.visible = '1' AND
+ co.enrollable = '1'
+ ORDER BY
+ sortorder ASC
+ ";
+
+ return get_records_sql($query);
+
+ } elseif (!empty($CFG->enrol_mnet_allowed_categories)) {
+
+ $cats = preg_split('/\s*,\s*/', $CFG->enrol_mnet_allowed_categories);
+ for ($n=0;$n < count($cats); $n++) {
+ $cats[$n] = " ca.path LIKE '%/" . (int)$cats[$n] . "/%' ";
+ }
+ $cats = join(' OR ', $cats);
+
+ $query =
+ "SELECT
+ id, name
+ FROM
+ {$CFG->prefix}course_categories ca
+ WHERE
+ ca.id IN ({$CFG->enrol_mnet_allowed_categories})
+ OR ( $cats )
+ ORDER BY
+ path ASC,
+ depth ASC
+ ";
+ unset($cats);
+
+ error_log($query);
+
+ $rs = get_records_sql($query);
+
+ if (!empty($rs)) {
+ $cats = array_keys($rs);
+ }
+ $where = ' AND ( ca.id IN (' . join(',', $cats) . ') ';
+
+
+ if (!empty($CFG->enrol_mnet_allowed_courses)) {
+ $where .= " OR co.id in ('{$CFG->enrol_mnet_allowed_courses}') ";
+ }
+
+ $where .= ')';
+
+ $query =
+ "SELECT
+ co.id as remoteid,
+ ca.id as cat_id,
+ ca.name as cat_name,
+ ca.description as cat_description,
+ co.sortorder,
+ co.fullname,
+ co.shortname,
+ co.idnumber,
+ co.summary,
+ co.startdate,
+ co.cost,
+ co.currency,
+ co.defaultrole as defaultroleid,
+ r.name
+ FROM
+ {$CFG->prefix}course_categories ca
+ JOIN
+ {$CFG->prefix}course co ON
+ ca.id = co.category
+ LEFT JOIN
+ {$CFG->prefix}role r ON
+ r.id = co.defaultrole
+ WHERE
+ co.visible = '1' AND
+ co.enrollable = '1' $where
+ ORDER BY
+ sortorder ASC
+ ";
+
+ error_log($query);
+
+ return get_records_sql($query);
+
+ } elseif (!empty($CFG->enrol_mnet_allowed_courses)) {
+
+ $query =
+ "SELECT
+ co.id as remoteid,
+ ca.id as cat_id,
+ ca.name as cat_name,
+ ca.description as cat_description,
+ co.sortorder,
+ co.fullname,
+ co.shortname,
+ co.idnumber,
+ co.summary,
+ co.startdate,
+ co.cost,
+ co.currency,
+ co.defaultrole as defaultroleid,
+ r.name
+ FROM
+ {$CFG->prefix}course_categories ca
+ JOIN
+ {$CFG->prefix}course co ON
+ ca.id = co.category
+ LEFT JOIN
+ {$CFG->prefix}role r ON
+ r.id = co.defaultrole
+ WHERE
+ co.visible = '1' AND
+ co.enrollable = '1' AND
+ co.id in ({$CFG->enrol_mnet_allowed_courses})
+ ORDER BY
+ sortorder ASC
+ ";
+
+ return get_records_sql($query);
+
+ }
+
+ return array();
+ }
+
+ /**
+ * Does Foo
+ *
+ * @param string $username The username
+ * @return array Whether the user can login from the remote host
+ */
+ function user_enrolments() {
+
+ return array();
+ }
+
+ /**
+ * Enrols user to course with the default role
+ *
+ * @param string $username The username of the remote use
+ * @param int $courseid The id of the local course
+ * @return bool Whether the enrolment has been successful
+ */
+ function enrol_user($user, $courseid) {
+ global $MNET, $MNET_REMOTE_CLIENT;
+
+ $userrecord = get_record('user','username',addslashes($user['username']), 'mnethostid',$MNET_REMOTE_CLIENT->id);
+
+ if ($userrecord == false) {
+ $userrecord = new stdClass();
+ $userrecord->username = addslashes($user['username']);
+ $userrecord->email = addslashes($user['email']);
+ $userrecord->firstname = addslashes($user['firstname']);
+ $userrecord->lastname = addslashes($user['lastname']);
+ $userrecord->mnethostid = $MNET_REMOTE_CLIENT->id;
+
+ if ($userrecord->id = insert_record('user', $userrecord)) {
+ $userrecord = get_record('user','id', $userrecord->id);
+ } else {
+ // TODO: Error out
+ return false;
+ }
+ }
+
+ if (! $course = get_record('course', 'id', $courseid) ) {
+ // TODO: Error out
+ return false;
+ }
+
+ $courses = $this->available_courses();
+
+ if (!empty($courses[$courseid])) {
+ error_log("remote enrolling $courseid, $userrecord->id,");
+ //TODO: rewire enrol_into_course
+ if (enrol_into_course($course, $userrecord, 'mnet')) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Unenrol a user from a course
+ *
+ * @param string $username The username
+ * @param int $courseid The id of the local course
+ * @return bool Whether the user can login from the remote host
+ */
+ function unenrol_user($user, $courseid) {
+ global $MNET_REMOTE_CLIENT;
+
+ $userrecord = get_record('user','username',$user['username'], 'mnethostid',$MNET_REMOTE_CLIENT->id);
+
+ if ($userrecord == false) {
+ // TODO: Error out
+ }
+
+ if (! $course = get_record('course', 'id', $courseid) ) {
+ // TODO: Error out
+ }
+
+ if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
+ // TODO: Error out (Invalid context)
+ }
+
+ // Are we a *real* user or the shady MNET Daemon?
+ // require_capability('moodle/role:assign', $context, NULL, false);
+
+ if (! role_unassign(0, $userrecord->id, 0, $context->id)) {
+ error("An error occurred while trying to unenrol that person.");
+ }
+
+ return true;
+ }
+
+ /***
+ *** Client RPC behaviour
+ ***
+ ***
+ ***/
+
+ /**
+ * Lists remote servers we use 'enrol' services from.
+ *
+ * @return array
+ */
+ function list_remote_servers() {
+ global $CFG;
+
+ $sql = "
+ SELECT DISTINCT
+ h.id,
+ h.name
+ FROM
+ {$CFG->prefix}mnet_host h,
+ {$CFG->prefix}mnet_host2service h2s,
+ {$CFG->prefix}mnet_service s
+ WHERE
+ h.id = h2s.hostid AND
+ h2s.serviceid = s.id AND
+ s.name = 'mnet_enrol'";
+
+ return get_records_sql($sql);
+ }
+
+ /**
+ * Does Foo
+ *
+ * @param int $mnethostid The id of the remote mnethost
+ * @return array Whether the user can login from the remote host
+ */
+ function fetch_remote_courses($mnethostid) {
+ global $CFG;
+ global $USER;
+ global $MNET;
+ require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
+
+ // get the Service Provider info
+ $mnet_sp = new mnet_peer();
+ $mnet_sp->set_id($mnethostid);
+
+ // set up the RPC request
+ $mnetrequest = new mnet_xmlrpc_client();
+ $mnetrequest->set_method('enrol/mnet/enrol.php/available_courses');
+
+ // TODO: cache for a while (10 minutes?)
+
+ // Thunderbirds are go! Do RPC call and store response
+ if ($mnetrequest->send($mnet_sp) === true) {
+ $courses = $mnetrequest->response;
+
+ // get the cached courses key'd on remote id - only need remoteid and id fields
+ $cachedcourses = get_records('mnet_enrol_course',
+ 'hostid', $mnethostid,
+ 'remoteid', 'remoteid, id' );
+
+ // Update cache and transform $courses into objects
+ // in-place for the benefit of our caller...
+ for ($n=0;$nremoteid = (int)$course->remoteid;
+ $course->hostid = $mnethostid;
+ $course->categoryid = (int)$course->categoryid;
+ $course->categoryname = addslashes($course->categoryname);
+ $course->description = addslashes($course->description);
+ $course->sortorder = (int)$course->sortorder ;
+ $course->fullname = addslashes($course->fullname);
+ $course->shortname = addslashes($course->shortname);
+ $course->idnumber = addslashes($course->idnumber);
+ $course->summary = addslashes($course->summary);
+ $course->startdate = (int)$course->startdate;
+ $course->cost = (int)$course->cost;
+ $course->currency = addslashes($course->currency);
+ $course->defaultroleid = (int)$course->defaultroleid;
+ $course->defaultrolename = addslashes($course->defaultrolename);
+
+ // insert or update
+ if (empty($cachedcourses[$course->remoteid])) {
+ $course->id = insert_record('mnet_enrol_course', $course);
+ } else {
+ $course->id = $cachedcourses[$course->remoteid]->id;
+ $cachedcourses[$course->remoteid]->seen=true;
+ update_record('mnet_enrol_course', $course);
+ }
+ }
+
+ // prune stale data from cache
+ if (!empty($cachedcourses)) {
+ $stale = array();
+ foreach ($cachedcourses as $id => $cc) {
+ // TODO: maybe set a deleted flag instead
+ if (empty($cc->seen)) {
+ $stale[] = $cc->id;
+ }
+ }
+ if (!empty($stale)) {
+ delete_records_select('mnet_enrol_course', 'id IN ('.join(',',$stale).')');
+ }
+ }
+
+ return $courses;
+ } else {
+ foreach ($mnetrequest->error as $code => $errormessage) {
+ $message .= "ERROR $code:
$errormessage
";
+ }
+ error("RPC enrol/mnet/available_courses:
$message");
+ }
+ return false;
+ }
+
+ /**
+ * Does Foo
+ *
+ * @param int $mnethostid The id of the remote mnethost
+ * @return array Whether the user can login from the remote host
+ */
+ function req_enrol_user($userid, $courseid) {
+ global $CFG;
+ global $USER;
+ global $MNET;
+ require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
+
+ // Prepare a basic user record
+ // in case the remote host doesn't have it
+ $user = get_record('user', 'id', $userid, '','','','', 'username, email, firstname, lastname');
+ $user = (array)$user;
+
+ $course = get_record('mnet_enrol_course', 'id', $courseid);
+
+ // get the Service Provider info
+ $mnet_sp = new mnet_peer();
+ $mnet_sp->set_id($course->hostid);
+
+ // set up the RPC request
+ $mnetrequest = new mnet_xmlrpc_client();
+ $mnetrequest->set_method('enrol/mnet/enrol.php/enrol_user');
+ $mnetrequest->add_param($user);
+ $mnetrequest->add_param($course->remoteid);
+
+ // Thunderbirds are go! Do RPC call and store response
+ if ($mnetrequest->send($mnet_sp) === true) {
+ if ($mnetrequest->response == true) {
+ // now store it in the mnet_enrol_assignments table
+ $assignment = new StdClass;
+ $assignment->userid = $userid;
+ $assignment->hostid = $course->hostid;
+ $assignment->courseid = $course->id;
+ $assignment->enroltype = 'mnet';
+ // TODO: other fields
+ if (insert_record('mnet_enrol_assignments', $assignment)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Does Foo
+ *
+ * @param int $mnethostid The id of the remote mnethost
+ * @return array Whether the user can login from the remote host
+ */
+ function req_unenrol_user($userid, $courseid) {
+ global $CFG;
+ global $USER;
+ global $MNET;
+ require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
+
+ // in case the remote host doesn't have it
+ $user = get_record('user', 'id', $userid, '','','','', 'username, email');
+ $user = $user->username;
+
+ $course = get_record('mnet_enrol_course', 'id', $courseid);
+
+ // get the Service Provider info
+ $mnet_sp = new mnet_peer();
+ $mnet_sp->set_id($course->hostid);
+
+ // set up the RPC request
+ $mnetrequest = new mnet_xmlrpc_client();
+ $mnetrequest->set_method('enrol/mnet/enrol.php/unenrol_user');
+ $mnetrequest->add_param($user);
+ $mnetrequest->add_param($course->remoteid);
+
+ // TODO - prevent removal of enrolments that are not of
+ // type mnet...
+
+
+ // Thunderbirds are go! Do RPC call and store response
+ if ($mnetrequest->send($mnet_sp) === true) {
+ if ($mnetrequest->response == true) {
+ // remove enrolment cached in mnet_enrol_assignments
+ delete_records_select('mnet_enrol_assignments',
+ "userid={$userid} AND courseid={$course->id}");
+
+ return true;
+ }
+ }
+ return false;
+ }
+
+} // end of class
+
+?>
diff --git a/enrol/mnet/remote_courses.php b/enrol/mnet/remote_courses.php
new file mode 100644
index 00000000000..f9b60dce353
--- /dev/null
+++ b/enrol/mnet/remote_courses.php
@@ -0,0 +1,52 @@
+libdir.'/adminlib.php');
+
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('enrolment', $adminroot);
+
+ $CFG->pagepath = 'enrol/mnet';
+ require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class
+ $enrolment = enrolment_factory::factory('mnet');
+
+ $mnethost = required_param('host', PARAM_INT);
+
+ $courses = $enrolment->fetch_remote_courses($mnethost);
+
+/// Print the page
+
+ /// get language strings
+ $str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
+
+ admin_externalpage_print_header($adminroot);
+
+ print_simple_box_start("center", "80%");
+
+ print_simple_box_start("center", "60%", '', 5, 'informationbox');
+ print_string("description", "enrol_mnet");
+ print_simple_box_end();
+
+ echo "
";
+
+ print ('');
+
+ print_simple_box_end();
+
+ admin_externalpage_print_footer($adminroot);
+
+?>
diff --git a/enrol/mnet/remote_enrolment.html b/enrol/mnet/remote_enrolment.html
new file mode 100755
index 00000000000..4016c76ffcf
--- /dev/null
+++ b/enrol/mnet/remote_enrolment.html
@@ -0,0 +1,94 @@
+
+
diff --git a/enrol/mnet/remote_enrolment.php b/enrol/mnet/remote_enrolment.php
new file mode 100644
index 00000000000..7b14eacb3aa
--- /dev/null
+++ b/enrol/mnet/remote_enrolment.php
@@ -0,0 +1,146 @@
+libdir.'/adminlib.php');
+
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('enrolment', $adminroot);
+
+ $CFG->pagepath = 'enrol/mnet';
+ require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class
+ $enrolment = enrolment_factory::factory('mnet');
+
+ $mnethost = 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);
+
+ if (empty($mnethost) || empty($course)) {
+ error("Host or course not found");
+ }
+
+ define("MAX_USERS_PER_PAGE", 5000);
+
+ $add = optional_param('add', 0, PARAM_BOOL);
+ $remove = optional_param('remove', 0, PARAM_BOOL);
+ $showall = optional_param('showall', 0, PARAM_BOOL);
+ $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
+ $previoussearch = optional_param('previoussearch', 0, PARAM_BOOL);
+ $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
+
+ $errors = array();
+
+ $previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;
+
+ $baseurl = "remote_enrolment.php?courseid={$course->id}&host={$mnethost->id}";
+ if (!empty($userid)) {
+ $baseurl .= '&userid='.$userid;
+ }
+
+/// Process incoming role assignment
+
+ if ($frm = data_submitted()) {
+ if ($add and !empty($frm->addselect) and confirm_sesskey()) {
+ $timemodified = time();
+
+ foreach ($frm->addselect as $adduser) {
+ if (!$adduser = clean_param($adduser, PARAM_INT)) {
+ continue;
+ }
+ if (! $enrolment->req_enrol_user($adduser, $course->id)) {
+ $errors[] = "Could not add user with id $adduser to this role!";
+ }
+ }
+ } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
+ foreach ($frm->removeselect as $removeuser) {
+ $removeuser = clean_param($removeuser, PARAM_INT);
+ if (! $enrolment->req_unenrol_user($removeuser, $course->id)) {
+ $errors[] = "Could not remove user with id $removeuser from this role!";
+ }
+ }
+ } else if ($showall) {
+ $searchtext = '';
+ $previoussearch = 0;
+ }
+ }
+
+/// 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";
+ 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);
+
+ if ($searchtext !== '') { // Search for a subset of remaining users
+ $LIKE = sql_ilike();
+ $FULLNAME = sql_fullname();
+
+ $select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
+ }
+ $availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
+ FROM '.$CFG->prefix.'user
+ WHERE '.$select.'
+ ORDER BY lastname ASC, firstname ASC');
+
+
+
+/// Print the page
+
+/// get language strings
+$str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
+/// Get some language strings
+
+$strassignusers = get_string('assignusers', 'role');
+$strpotentialusers = get_string('potentialusers', 'role');
+$strexistingusers = get_string('existingusers', 'role');
+$straction = get_string('assignroles', 'role');
+$strroletoassign = get_string('roletoassign', 'role');
+$strcurrentcontext = get_string('currentcontext', 'role');
+$strsearch = get_string('search');
+$strshowall = get_string('showall');
+$strparticipants = get_string('participants');
+$strsearchresults = get_string('searchresults');
+
+admin_externalpage_print_header($adminroot);
+
+print_simple_box_start("center", "80%");
+
+print_simple_box_start("center", "60%", '', 5, 'informationbox');
+print "Enrolling in course " . s($course->shortname) . ' on host ' . s($mnethost->name) ."
";
+print_string("description", "enrol_mnet");
+print_simple_box_end();
+
+echo "
";
+
+ print_simple_box_start('center');
+ include('remote_enrolment.html');
+ print_simple_box_end();
+
+ if (!empty($errors)) {
+ $msg = '';
+ foreach ($errors as $e) {
+ $msg .= $e.'
';
+ }
+ $msg .= '
';
+ print_simple_box_start('center');
+ notify($msg);
+ print_simple_box_end();
+ }
+
+
+
+admin_externalpage_print_footer($adminroot);
+
+?>
diff --git a/enrol/mnet/remote_hosts.php b/enrol/mnet/remote_hosts.php
new file mode 100644
index 00000000000..8a4c688a591
--- /dev/null
+++ b/enrol/mnet/remote_hosts.php
@@ -0,0 +1,64 @@
+libdir.'/adminlib.php');
+
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('enrolment', $adminroot);
+
+ $CFG->pagepath = 'enrol/mnet';
+
+
+ require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class
+
+ $enrolment = enrolment_factory::factory('mnet');
+
+/// If data submitted, then process and store.
+
+ if ($frm = data_submitted()) {
+
+ }
+
+/// Otherwise fill and print the form.
+
+ /// get language strings
+ $str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
+
+ admin_externalpage_print_header($adminroot);
+
+
+/// Print current enrolment type description
+ print_simple_box_start("center", "80%");
+ print_heading($options[$enrol]);
+
+ print_simple_box_start("center", "60%", '', 5, 'informationbox');
+ print_string("description", "enrol_$enrol");
+ print_simple_box_end();
+
+ echo "
";
+
+ print ('');
+
+ print_simple_box_end();
+
+ admin_externalpage_print_footer($adminroot);
+
+?>
diff --git a/lang/en_utf8/enrol_mnet.php b/lang/en_utf8/enrol_mnet.php
new file mode 100644
index 00000000000..de6b0a488a5
--- /dev/null
+++ b/lang/en_utf8/enrol_mnet.php
@@ -0,0 +1,22 @@
+here.';
+$string['allcourses'] = '$a potential courses';
+$string['allowedcourses'] = '$a allowed courses';
+$string['nocoursesdefined'] = 'No courses found. Define new courses here.';
+$string['allowedcourseslinktext'] = 'Edit allowed courses and categories here.';
+$string['mnet_enrol_name'] = 'Moodle Networked Enrolment';
+$string['mnet_enrol_description'] = 'Publish this service to allow administrators at $a to enrol their students in courses you have created on your server.
' .
+ '- Dependency: You must also publish the SSO (Service Provider) service to $a.
'.
+ '- Dependency: You must also subscribe to the SSO (Identity Provider) service on $a.
'.
+ 'Subscribe to this service to be able to enrol your students in courses on $a.
'.
+ '- Dependency: You must also subscribe to the SSO (Service Provider) service on $a.
'.
+ '- Dependency: You must also publish the SSO (Identity Provider) service to $a.
';
+
+?>