From 82195aaefe5ace633c25eeda3285502592193456 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 17 Jul 2010 22:17:10 +0000 Subject: [PATCH] MDL-22787 Instances of enrol_mnet can be added to the course --- enrol/mnet/addinstance.php | 68 +++++++++++++++++++ enrol/mnet/addinstance_form.php | 87 ++++++++++++++++++++++++ enrol/mnet/db/access.php | 37 ---------- enrol/mnet/lang/en/enrol_mnet.php | 8 +++ enrol/mnet/lib.php | 109 ++++++++++++++++++++++++++++++ enrol/mnet/version.php | 2 +- 6 files changed, 273 insertions(+), 38 deletions(-) create mode 100644 enrol/mnet/addinstance.php create mode 100644 enrol/mnet/addinstance_form.php delete mode 100644 enrol/mnet/db/access.php diff --git a/enrol/mnet/addinstance.php b/enrol/mnet/addinstance.php new file mode 100644 index 00000000000..5a7fb868177 --- /dev/null +++ b/enrol/mnet/addinstance.php @@ -0,0 +1,68 @@ +. + +/** + * Adds new instance of enrol_mnet into the specified course + * + * @package enrol + * @subpackage mnet + * @copyright 2010 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require(dirname(dirname(dirname(__FILE__))).'/config.php'); +require_once("$CFG->dirroot/enrol/mnet/addinstance_form.php"); + +$id = required_param('id', PARAM_INT); // course id + +$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); +$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); + +require_login($course); +require_capability('moodle/course:enrolconfig', $context); // todo shall the own capability be used? + +$PAGE->set_url('/enrol/mnet/addinstance.php', array('id'=>$course->id)); +$PAGE->set_pagelayout('standard'); + +// Try and make the manage instances node on the navigation active +$courseadmin = $PAGE->settingsnav->get('courseadmin'); +if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) { + $courseadmin->get('users')->get('manageinstances')->make_active(); +} + +$enrol = enrol_get_plugin('mnet'); +// make sure we were allowed to get here form the Enrolment methods page +if (!$enrol->get_candidate_link($course->id)) { + redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id))); +} + +$mform = new enrol_mnet_addinstance_form(null, array('course'=>$course, 'enrol'=>$enrol)); + +if ($mform->is_cancelled()) { + redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id))); + +} else if ($data = $mform->get_data()) { + $enrol->add_instance($course, array('customint1'=>$data->hostid, 'roleid'=>$data->roleid, 'name'=>$data->name)); + redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id))); +} + +$PAGE->set_heading($course->fullname); +$PAGE->set_title(get_string('pluginname', 'enrol_mnet')); + +echo $OUTPUT->header(); +$mform->display(); +echo $OUTPUT->footer(); diff --git a/enrol/mnet/addinstance_form.php b/enrol/mnet/addinstance_form.php new file mode 100644 index 00000000000..3e5547b35ab --- /dev/null +++ b/enrol/mnet/addinstance_form.php @@ -0,0 +1,87 @@ +. + +/** + * Form to add an instance of enrol_mnet plugin + * + * @package enrol + * @subpackage mnet + * @copyright 2010 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +require_once("$CFG->libdir/formslib.php"); + +class enrol_mnet_addinstance_form extends moodleform { + function definition() { + global $CFG, $DB; + + $mform = $this->_form; + $course = $this->_customdata['course']; + $enrol = $this->_customdata['enrol']; + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + + $subscribers = $enrol->get_remote_subscribers(); + $hosts = array(0 => get_string('remotesubscribersall', 'enrol_mnet')); + foreach ($subscribers as $hostid => $subscriber) { + $hosts[$hostid] = $subscriber->appname.': '.$subscriber->hostname.' ('.$subscriber->hosturl.')'; + } + $roles = get_assignable_roles($coursecontext); + + $mform->addElement('header','general', get_string('pluginname', 'enrol_mnet')); + + $mform->addElement('select', 'hostid', get_string('remotesubscriber', 'enrol_mnet'), $hosts); + $mform->addHelpButton('hostid', 'remotesubscriber', 'enrol_mnet'); + $mform->addRule('hostid', get_string('required'), 'required', null, 'client'); + + $mform->addElement('select', 'roleid', get_string('roleforremoteusers', 'enrol_mnet'), $roles); + $mform->addHelpButton('roleid', 'roleforremoteusers', 'enrol_mnet'); + $mform->addRule('roleid', get_string('required'), 'required', null, 'client'); + $mform->setDefault('roleid', $enrol->get_config('roleid')); + + $mform->addElement('text', 'name', get_string('instancename', 'enrol_mnet')); + $mform->addHelpButton('name', 'instancename', 'enrol_mnet'); + $mform->setType('name', PARAM_TEXT); + + $mform->addElement('hidden', 'id', null); + $mform->setType('id', PARAM_INT); + + $this->add_action_buttons(); + + $this->set_data(array('id'=>$course->id)); + } + + /** + * Do not allow multiple instances for single remote host + * + * @param array $data raw form data + * @return array + */ + function validation($data) { + global $DB; + + $errors = array(); + + if ($DB->record_exists('enrol', array('enrol' => 'mnet', 'courseid' => $data['id'], 'customint1' => $data['hostid']))) { + $errors['hostid'] = get_string('error_multiplehost', 'enrol_mnet'); + } + + return $errors; + } +} diff --git a/enrol/mnet/db/access.php b/enrol/mnet/db/access.php deleted file mode 100644 index 96c9cf6068a..00000000000 --- a/enrol/mnet/db/access.php +++ /dev/null @@ -1,37 +0,0 @@ -. - -/** - * Capabilities for MNet enrolment plugin - * - * @package enrol - * @subpackage mnet - * @copyright 2010 David Mudrak - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -$capabilities = array( - // configure the mnet plugin settings in the course - // users with this capability open the course for the remote users - 'enrol/mnet:config' => array( - 'captype' => 'write', - 'contextlevel' => CONTEXT_COURSE, - 'legacy' => array( - 'manager' => CAP_ALLOW, - ) - ), -); diff --git a/enrol/mnet/lang/en/enrol_mnet.php b/enrol/mnet/lang/en/enrol_mnet.php index 0d6c93d0b11..4e77a092ba4 100644 --- a/enrol/mnet/lang/en/enrol_mnet.php +++ b/enrol/mnet/lang/en/enrol_mnet.php @@ -24,7 +24,15 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['error_multiplehost'] = 'Some instance of MNet enrolment plugin already exists for this host. Only one instance per host and/or one instance for \'All hosts\' is allowed.'; +$string['instancename'] = 'Enrolment method name'; +$string['instancename_help'] = 'You can optionally rename this instance of the MNet enrolment method. If you leave this field empty, the default instance name will be used, containing the name of the remote host and the assigned role for their users.'; $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}.

'; $string['mnet_enrol_name'] = 'Moodle Networked Enrolment'; $string['pluginname'] = 'MNet remote enrolments'; $string['pluginname_desc'] = 'Allows remote MNet host to enrol their users into our courses.'; +$string['remotesubscriber'] = 'Remote host'; +$string['remotesubscriber_help'] = 'Select \'All hosts\' to open this course for all MNet peers we are offering the remote enrolment service to. Or choose a single host to make this course available for their users only.'; +$string['remotesubscribersall'] = 'All hosts'; +$string['roleforremoteusers'] = 'Role for their users'; +$string['roleforremoteusers_help'] = 'What role will the remote users from the selected host get.'; diff --git a/enrol/mnet/lib.php b/enrol/mnet/lib.php index 814f548bfb2..1cc39d5ade2 100644 --- a/enrol/mnet/lib.php +++ b/enrol/mnet/lib.php @@ -31,4 +31,113 @@ defined('MOODLE_INTERNAL') || die; */ class enrol_mnet_plugin extends enrol_plugin { + /** @var caches the result of {@link get_remote_subscribers()} */ + protected $cachesubscribers = null; + + //////////////////////////////////////////////////////////////////////////// + // Enrolment plugin API - methods required by Moodle core + //////////////////////////////////////////////////////////////////////////// + + /** + * Returns localised name of enrol instance + * + * @param object|null $instance enrol_mnet instance + * @return string + */ + public function get_instance_name($instance) { + global $DB; + + if (empty($instance)) { + $enrol = $this->get_name(); + return get_string('pluginname', 'enrol_'.$enrol); + + } else if (empty($instance->name)) { + $enrol = $this->get_name(); + if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) { + $role = role_get_name($role, get_context_instance(CONTEXT_COURSE, $instance->courseid)); + } else { + $role = get_string('error'); + } + if (empty($instance->customint1)) { + $host = get_string('remotesubscribersall', 'enrol_mnet'); + } else { + $host = $DB->get_field('mnet_host', 'name', array('id'=>$instance->customint1)); + } + return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($host) . ' - ' . $role .')'; + + } else { + return format_string($instance->name); + } + } + + /** + * Returns link to page which may be used to add new instance of enrolment plugin into the course + * + * The link is returned only if there are some MNet peers that we publish enrolment service to. + * + * @param int $courseid id of the course to add the instance to + * @return moodle_url|null page url or null if instance can not be created + */ + public function get_candidate_link($courseid) { + global $DB; + + if (!$this->mnet_available()) { + return null; + } + $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); + if (!has_capability('moodle/course:enrolconfig', $coursecontext)) { + return null; + } + $subscribers = $this->get_remote_subscribers(); + if (empty($subscribers)) { + return null; + } + + return new moodle_url('/enrol/mnet/addinstance.php', array('id'=>$courseid)); + } + + //////////////////////////////////////////////////////////////////////////// + // Internal methods used by the plugin itself only + //////////////////////////////////////////////////////////////////////////// + + /** + * Returns a list of remote servers that can enrol their users into our courses + * + * We must publish MNet service 'mnet_enrol' for the peers to allow them to enrol + * their users into our courses. + * + * @return array + */ + public function get_remote_subscribers() { + global $DB; + + if (is_null($this->cachesubscribers)) { + $sql = "SELECT DISTINCT h.id, h.name AS hostname, h.wwwroot AS hosturl, + a.display_name AS appname + FROM {mnet_host} h + JOIN {mnet_host2service} hs ON h.id = hs.hostid + JOIN {mnet_service} s ON hs.serviceid = s.id + JOIN {mnet_application} a ON h.applicationid = a.id + WHERE s.name = 'mnet_enrol' + AND hs.publish = 1"; + $this->cachesubscribers = $DB->get_records_sql($sql); + } + + return $this->cachesubscribers; + } + + /** + * Is MNet networking enabled at this server? + * + * @return bool + */ + protected function mnet_available() { + global $CFG; + + if (empty($CFG->mnet_dispatcher_mode) || $CFG->mnet_dispatcher_mode !== 'strict') { + return false; + } + return true; + } + } diff --git a/enrol/mnet/version.php b/enrol/mnet/version.php index 20d3b0c1b39..23307785678 100644 --- a/enrol/mnet/version.php +++ b/enrol/mnet/version.php @@ -24,4 +24,4 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$plugin->version = 2010071400; +$plugin->version = 2010071401;