Merge branch 'MDL-62660-master' of git://github.com/junpataleta/moodle
[moodle.git] / enrol / mnet / lib.php
blobbc33fe4db6e45e873edeb9492f7f2f20439079d5
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * MNet enrolment plugin
20 * @package enrol_mnet
21 * @copyright 2010 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * MNet enrolment plugin implementation for Moodle 2.x enrolment framework
30 class enrol_mnet_plugin extends enrol_plugin {
32 /**
33 * Returns localised name of enrol instance
35 * @param object|null $instance enrol_mnet instance
36 * @return string
38 public function get_instance_name($instance) {
39 global $DB;
41 if (empty($instance)) {
42 $enrol = $this->get_name();
43 return get_string('pluginname', 'enrol_'.$enrol);
45 } else if (empty($instance->name)) {
46 $enrol = $this->get_name();
47 if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
48 $role = role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING));
49 } else {
50 $role = get_string('error');
52 if (empty($instance->customint1)) {
53 $host = get_string('remotesubscribersall', 'enrol_mnet');
54 } else {
55 $host = $DB->get_field('mnet_host', 'name', array('id'=>$instance->customint1));
57 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($host) . ' - ' . $role .')';
59 } else {
60 return format_string($instance->name);
64 /**
65 * Returns true if a new instance can be added to this course.
67 * The link is returned only if there are some MNet peers that we publish enrolment service to.
69 * @param int $courseid id of the course to add the instance to
70 * @return boolean
72 public function can_add_instance($courseid) {
73 global $CFG, $DB;
74 require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
76 $service = mnetservice_enrol::get_instance();
77 if (!$service->is_available()) {
78 return false;
80 $coursecontext = context_course::instance($courseid);
81 if (!has_capability('moodle/course:enrolconfig', $coursecontext)) {
82 return false;
84 $subscribers = $service->get_remote_subscribers();
85 if (empty($subscribers)) {
86 return false;
89 return true;
92 /**
93 * Is it possible to delete enrol instance via standard UI?
95 * @param stdClass $instance
96 * @return bool
98 public function can_delete_instance($instance) {
99 $context = context_course::instance($instance->courseid);
100 return has_capability('enrol/mnet:config', $context);
104 * Is it possible to hide/show enrol instance via standard UI?
106 * @param stdClass $instance
107 * @return bool
109 public function can_hide_show_instance($instance) {
110 $context = context_course::instance($instance->courseid);
111 return has_capability('enrol/mnet:config', $context);
115 * Return an array of valid options for the hosts property.
117 * @return array
119 protected function get_valid_hosts_options() {
120 global $CFG;
121 require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
123 $service = mnetservice_enrol::get_instance();
125 $subscribers = $service->get_remote_subscribers();
126 $hosts = array(0 => get_string('remotesubscribersall', 'enrol_mnet'));
127 foreach ($subscribers as $hostid => $subscriber) {
128 $hosts[$hostid] = $subscriber->appname.': '.$subscriber->hostname.' ('.$subscriber->hosturl.')';
130 return $hosts;
134 * Return an array of valid options for the roles property.
136 * @param context $context
137 * @return array
139 protected function get_valid_roles_options($context) {
140 $roles = get_assignable_roles($context);
141 return $roles;
145 * Add elements to the edit instance form.
147 * @param stdClass $instance
148 * @param MoodleQuickForm $mform
149 * @param context $context
150 * @return bool
152 public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
153 global $CFG;
155 $hosts = $this->get_valid_hosts_options();
156 $mform->addElement('select', 'customint1', get_string('remotesubscriber', 'enrol_mnet'), $hosts);
157 $mform->addHelpButton('customint1', 'remotesubscriber', 'enrol_mnet');
158 $mform->addRule('customint1', get_string('required'), 'required', null, 'client');
160 $roles = $this->get_valid_roles_options($context);
161 $mform->addElement('select', 'roleid', get_string('roleforremoteusers', 'enrol_mnet'), $roles);
162 $mform->addHelpButton('roleid', 'roleforremoteusers', 'enrol_mnet');
163 $mform->addRule('roleid', get_string('required'), 'required', null, 'client');
164 $mform->setDefault('roleid', $this->get_config('roleid'));
166 $mform->addElement('text', 'name', get_string('instancename', 'enrol_mnet'));
167 $mform->addHelpButton('name', 'instancename', 'enrol_mnet');
168 $mform->setType('name', PARAM_TEXT);
172 * We are a good plugin and don't invent our own UI/validation code path.
174 * @return boolean
176 public function use_standard_editing_ui() {
177 return true;
181 * Perform custom validation of the data used to edit the instance.
183 * @param array $data array of ("fieldname"=>value) of submitted data
184 * @param array $files array of uploaded files "element_name"=>tmp_file_path
185 * @param object $instance The instance loaded from the DB
186 * @param context $context The context of the instance we are editing
187 * @return array of "element_name"=>"error_description" if there are errors,
188 * or an empty array if everything is OK.
189 * @return void
191 public function edit_instance_validation($data, $files, $instance, $context) {
192 global $DB;
193 $errors = array();
195 $validroles = array_keys($this->get_valid_roles_options($context));
196 $validhosts = array_keys($this->get_valid_hosts_options());
198 $params = array('enrol' => 'mnet', 'courseid' => $instance->courseid, 'customint1' => $data['customint1']);
199 if ($DB->record_exists('enrol', $params)) {
200 $errors['customint1'] = get_string('error_multiplehost', 'enrol_mnet');
203 $tovalidate = array(
204 'customint1' => $validhosts,
205 'roleid' => $validroles,
206 'name' => PARAM_TEXT
208 $typeerrors = $this->validate_param_types($data, $tovalidate);
209 $errors = array_merge($errors, $typeerrors);
211 return $errors;