MDL-24322 grrr, fixing my previous commit, sorry Eloy
[moodle.git] / enrol / manual / lib.php
CommitLineData
df997f84
PS
1<?php
2
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.
14//
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/>.
17
18/**
19 * Manual enrolment plugin main library file.
20 *
eafb7a72
PS
21 * @package enrol
22 * @subpackage manual
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
25 */
26
97795859 27defined('MOODLE_INTERNAL') || die();
df997f84
PS
28
29class enrol_manual_plugin extends enrol_plugin {
30
31 public function roles_protected() {
32 // users may tweak the roles later
33 return false;
34 }
35
91b99e80
PS
36 public function allow_enrol(stdClass $instance) {
37 // users with enrol cap may unenrol other users manually manually
38 return true;
39 }
40
df997f84
PS
41 public function allow_unenrol(stdClass $instance) {
42 // users with unenrol cap may unenrol other users manually manually
43 return true;
44 }
45
46 public function allow_manage(stdClass $instance) {
47 // users with manage cap may tweak period and status
48 return true;
49 }
50
51 /**
52 * Returns link to manual enrol UI if exists.
53 * Does the access control tests automatically.
54 *
55 * @param object $instance
56 * @return moodle_url
57 */
58 public function get_manual_enrol_link($instance) {
59 $name = $this->get_name();
60 if ($instance->enrol !== $name) {
61 throw new coding_exception('invalid enrol instance!');
62 }
63
df997f84
PS
64 if (!enrol_is_enabled($name)) {
65 return NULL;
66 }
67
68 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
69
91b99e80 70 if (!has_capability('enrol/manual:manage', $context) or !has_capability('enrol/manual:enrol', $context) or !has_capability('enrol/manual:unenrol', $context)) {
df997f84
PS
71 return NULL;
72 }
73
74 return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid));
75 }
76
2d4b1f3e
PS
77 /**
78 * Returns enrolment instance manage link.
79 *
80 * By defaults looks for manage.php file and tests for manage capability.
81 *
82 * @param object $instance
83 * @return moodle_url;
84 */
85 public function add_course_navigation($instancesnode, stdClass $instance) {
eafb7a72 86 if ($instance->enrol !== 'manual') {
2d4b1f3e
PS
87 throw new coding_exception('Invalid enrol instance type!');
88 }
89
90 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
eafb7a72
PS
91 if (has_capability('enrol/manual:config', $context)) {
92 $managelink = new moodle_url('/enrol/manual/edit.php', array('courseid'=>$instance->courseid));
93 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
2d4b1f3e 94 }
2d4b1f3e
PS
95 }
96
97 /**
98 * Returns edit icons for the page with list of instances
99 * @param stdClass $instance
100 * @return array
101 */
102 public function get_action_icons(stdClass $instance) {
103 global $OUTPUT;
104
eafb7a72 105 if ($instance->enrol !== 'manual') {
2d4b1f3e
PS
106 throw new coding_exception('invalid enrol instance!');
107 }
2d4b1f3e 108 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
eafb7a72
PS
109
110 $icons = array();
111
112 if (has_capability('enrol/manual:manage', $context)) {
113 $managelink = new moodle_url("/enrol/manual/manage.php", array('enrolid'=>$instance->id));
114 $icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall')));
115 }
116 if (has_capability('enrol/manual:config', $context)) {
117 $editlink = new moodle_url("/enrol/manual/edit.php", array('courseid'=>$instance->courseid));
118 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
2d4b1f3e
PS
119 }
120
eafb7a72 121 return $icons;
2d4b1f3e
PS
122 }
123
df997f84
PS
124 /**
125 * Returns link to page which may be used to add new instance of enrolment plugin in course.
126 * @param int $courseid
127 * @return moodle_url page url
128 */
e25f2466 129 public function get_newinstance_link($courseid) {
df997f84
PS
130 global $DB;
131
eafb7a72
PS
132 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
133
134 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/manual:config', $context)) {
df997f84
PS
135 return NULL;
136 }
137
138 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'))) {
139 return NULL;
140 }
141
eafb7a72 142 return new moodle_url('/enrol/manual/edit.php', array('courseid'=>$courseid));
df997f84
PS
143 }
144
145 /**
146 * Add new instance of enrol plugin with default settings.
147 * @param object $course
0848a196 148 * @return int id of new instance, null if can not be created
df997f84
PS
149 */
150 public function add_default_instance($course) {
151 $fields = array('status'=>$this->get_config('status'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0));
152 return $this->add_instance($course, $fields);
153 }
154
0848a196
PS
155 /**
156 * Add new instance of enrol plugin.
157 * @param object $course
158 * @param array instance fields
159 * @return int id of new instance, null if can not be created
160 */
161 public function add_instance($course, array $fields = NULL) {
162 global $DB;
163
164 if ($DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) {
165 // only one instance allowed, sorry
166 return NULL;
167 }
168
169 return parent::add_instance($course, $fields);
170 }
df997f84
PS
171}
172