Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / mnet / lib.php
blob0a507293f718790fe25178357deec22719a52d7c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * MNet enrolment plugin
21 * @package enrol
22 * @subpackage mnet
23 * @copyright 2010 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * MNet enrolment plugin implementation for Moodle 2.x enrolment framework
32 class enrol_mnet_plugin extends enrol_plugin {
34 /**
35 * Returns localised name of enrol instance
37 * @param object|null $instance enrol_mnet instance
38 * @return string
40 public function get_instance_name($instance) {
41 global $DB;
43 if (empty($instance)) {
44 $enrol = $this->get_name();
45 return get_string('pluginname', 'enrol_'.$enrol);
47 } else if (empty($instance->name)) {
48 $enrol = $this->get_name();
49 if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
50 $role = role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING));
51 } else {
52 $role = get_string('error');
54 if (empty($instance->customint1)) {
55 $host = get_string('remotesubscribersall', 'enrol_mnet');
56 } else {
57 $host = $DB->get_field('mnet_host', 'name', array('id'=>$instance->customint1));
59 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($host) . ' - ' . $role .')';
61 } else {
62 return format_string($instance->name);
66 /**
67 * Returns link to page which may be used to add new instance of enrolment plugin into the course
69 * The link is returned only if there are some MNet peers that we publish enrolment service to.
71 * @param int $courseid id of the course to add the instance to
72 * @return moodle_url|null page url or null if instance can not be created
74 public function get_newinstance_link($courseid) {
75 global $CFG, $DB;
76 require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
78 $service = mnetservice_enrol::get_instance();
79 if (!$service->is_available()) {
80 return null;
82 $coursecontext = context_course::instance($courseid);
83 if (!has_capability('moodle/course:enrolconfig', $coursecontext)) {
84 return null;
86 $subscribers = $service->get_remote_subscribers();
87 if (empty($subscribers)) {
88 return null;
91 return new moodle_url('/enrol/mnet/addinstance.php', array('id'=>$courseid));