-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.php
166 lines (138 loc) · 5.9 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* User role assignment plugin.
*
* This plugin synchronises user roles with external database table.
*
* @package enrol
* @subpackage dbuserrel
* @copyright Segun Babalola <[email protected]>
* @copyright Penny Leach <[email protected]>
* @copyright Maxime Pelletier <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class enrol_dbuserrel_plugin extends enrol_plugin {
private $externaldataport;
private $internaldataport;
private $syncstrategy;
var $log;
private function setup() {
try {
// Set data ports (internal, i.e. Moodle) and external.
$this->set_external_dataport(enrol_dbuserrel_dataport_factory::create('EXTERNAL', [
'dbhost' => $this->get_config('dbhost'), 'dbname' => $this->get_config('dbname'),
'dbtype' => $this->get_config('dbtype'), 'dbuser' => $this->get_config('dbuser'),
'dbpass' => $this->get_config('dbpass'), 'dbsetupsql' => $this->get_config('dbsetupsql'),
'table' => $this->get_config('remoteenroltable'),
'remotesubject' => $this->get_config('remotesubjectuserfield'),
'remoteobject' => $this->get_config('remoteobjectuserfield'),
'remoterole' => $this->get_config('remoterolefield'),
'debugdb' => $this->get_config('debugdb')
]));
$this->set_internal_dataport(enrol_dbuserrel_dataport_factory::create('INTERNAL', [
'localsubject' => $this->get_config('localsubjectuserfield'),
'localobject' => $this->get_config('localobjectuserfield'),
'localrole' => $this->get_config('localrolefield')
]));
// Configure strategy (only default exists for now), hopefully others will be added in future.
$this->syncstrategy = new enrol_dbuserrel\syncstrategy\defaultstrategy(
$this->get_external_dataport(),
$this->get_internal_dataport()
);
} catch (\Exception $e) {
error_log(get_string('failure_initialisation', 'enrol_dbuserrel', $e->getMessage()));
throw new \Exception(get_string('failure_initialisation', 'enrol_dbuserrel', $e->getMessage()));
}
}
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param object $instance
* @return bool
*/
public function instance_deleteable($instance) {
if (!enrol_is_enabled('dbuserrel')) {
return true;
}
if (!$this->get_config('dbtype') or !$this->get_config('dbhost') or !$this->get_config('remoteenroltable') or !$this->get_config('remotecoursefield') or !$this->get_config('remoteuserfield')) {
return true;
}
//TODO: connect to external system and make sure no users are to be enrolled in this course.
return false;
}
/**
* Does this plugin allow manual unenrolment of a specific user?
* Yes, but only if user suspended...
*
* @param stdClass $instance course enrol instance
* @param stdClass $ue record from user_enrolments table
*
* @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
*/
public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
if ($ue->status == ENROL_USER_SUSPENDED) {
return true;
}
return false;
}
/**
* MAIN FUNCTION
* For the given user, let's go out and look in an external database
* for an authoritative list of relationships, and then adjust the
* local Moodle assignments to match.
*
* @param bool $verbose
* @param mixed $user
*
* @return int 0 means success, 1 db connect failure, 2 db read failure
* @throws \Exception
*/
function setup_enrolments($verbose = false, &$user=null) {
try {
$this->setup();
if ($verbose) {
mtrace('Starting user enrolment synchronisation...');
mtrace("Starting db_init()");
}
// We may need a lot of memory here.
@set_time_limit(0);
raise_memory_limit(MEMORY_HUGE);
// Assume no user initially.
$userid = null;
if ($user && isset($user->id) && intval($user->id, 10) > 0) {
$userid = $user->id;
}
$this->syncstrategy->sync_relationships($userid, $verbose);
} catch(\Exception $e) {
mtrace(get_string('failure_sync_operation', 'enrol_dbuserrel', $e->getMessage()));
}
}
// Data ports.
private function get_internal_dataport(): enrol_dbuserrel_dataport_interface {
return $this->internaldataport;
}
private function set_internal_dataport(enrol_dbuserrel_dataport_interface $dp) {
$this->internaldataport = $dp;
}
private function get_external_dataport(): enrol_dbuserrel_dataport_interface {
return $this->externaldataport;
}
private function set_external_dataport(enrol_dbuserrel_dataport_interface $dp) {
$this->externaldataport = $dp;
}
} // End of class.