-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.php
196 lines (171 loc) · 7.47 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?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/>.
/**
* Class to identify the courses to be deleted since they miss a person in charge.
*
* @package lifecycletrigger_byrole
* @copyright 2017 Tobias Reischmann WWU Nina Herrmann WWU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lifecycle\trigger;
use tool_lifecycle\local\manager\settings_manager;
use tool_lifecycle\local\response\trigger_response;
use tool_lifecycle\settings_type;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
/**
* Class for processing courses if or if not roles are missing.
*/
class byrole extends base_automatic {
/**
* Extends the where clause by a statement which selects all entries of the byrole table,
* which reached a specific age. That means they are longer than the max delay time without a responsible person.
* Further, we update the byrole table in this function to refresh the records of the stored courses.
* @param int $triggerid
* @return array
* @throws \coding_exception
* @throws \dml_exception
*/
public function get_course_recordset_where($triggerid) {
$this->update_courses($triggerid);
$delay = settings_manager::get_settings($triggerid, settings_type::TRIGGER)['delay'];
$maxtime = time() - $delay;
$sql = "{course}.id in (SELECT DISTINCT courseid
FROM {lifecycletrigger_byrole} WHERE triggerid = $triggerid AND timecreated < $maxtime)";
return array($sql, array());
}
/**
* Always triggers a course that got past the where clause.
* @param \stdClass $course
* @param int $triggerid id of the trigger instance
* @return trigger_response one of next() or trigger()
*/
public function check_course($course, $triggerid) {
return trigger_response::trigger();
}
/**
* Return the roles that were set in the config.
* @param int $triggerid id of the trigger instance
* @return array
* @throws \coding_exception
*/
private function get_roles($triggerid) {
$roles = settings_manager::get_settings($triggerid, settings_type::TRIGGER)['roles'];
if ($roles === "") {
throw new \coding_exception('No Roles defined');
} else {
$roles = explode(",", $roles);
}
return $roles;
}
/**
* Updates the current state of the courses
* There are two cases:
* 1. a course has no responsible user and no entry in the table, then the course should be inserted in the table,
* 2. a course has an entry in the table but has a responsible person, then the course should be deleted from the table,
* @param int $triggerid id of the trigger instance
* @throws \coding_exception
* @throws \dml_exception
*/
private function update_courses($triggerid) {
global $DB;
$coursesintable = $DB->get_records('lifecycletrigger_byrole',
array('triggerid' => $triggerid), '', 'courseid');
$coursesintable = array_map(function($elem) {
return $elem->courseid;
}, $coursesintable);
list($insql, $inparams) = $DB->get_in_or_equal($this->get_roles($triggerid), SQL_PARAMS_NAMED);
$sql = "SELECT c.id
FROM {course} c
WHERE c.id NOT IN (
SELECT e.courseid FROM {context} coursectx
JOIN {enrol} e ON coursectx.contextlevel = 50 AND e.courseid = coursectx.instanceid AND e.status = 0
JOIN {user_enrolments} ue ON e.id = ue.enrolid AND ue.status = 0
JOIN {context} ccctx ON ccctx.id = coursectx.id
OR (ccctx.contextlevel = 40 AND coursectx.path LIKE {$DB->sql_concat("ccctx.path", "'/%'")})
JOIN {role_assignments} ra ON ra.contextid = ccctx.id AND ra.roleid {$insql} AND ra.userid = ue.userid
)";
$courseswithoutteacher = $DB->get_records_sql($sql, $inparams);
$courseswithoutteacher = array_map(function($elem) {
return $elem->id;
}, $courseswithoutteacher);
// Insert new entries without the specified role assignments.
$insertcourses = array_diff($courseswithoutteacher, $coursesintable);
$records = array();
foreach ($insertcourses as $courseid) {
$dataobject = new \stdClass();
$dataobject->courseid = $courseid;
$dataobject->triggerid = $triggerid;
$dataobject->timecreated = time();
$records[] = $dataobject;
}
$DB->insert_records('lifecycletrigger_byrole', $records);
// Delete old entries, which now have an assigment of the specified role, again.
$deletecourses = array_diff($coursesintable, $courseswithoutteacher);
list($insqltrigger, $inparamstrigger) = $DB->get_in_or_equal($triggerid, SQL_PARAMS_NAMED);
if (!empty($deletecourses)) {
list($insqlcourseids, $inparamscourseids) = $DB->get_in_or_equal($deletecourses, SQL_PARAMS_NAMED);
$DB->delete_records_select('lifecycletrigger_byrole',
"courseid {$insqlcourseids} AND triggerid {$insqltrigger}",
array_merge($inparamscourseids, $inparamstrigger));
}
}
/**
* The return value should be equivalent with the name of the subplugin folder.
* @return string technical name of the subplugin
*/
public function get_subpluginname() {
return 'byrole';
}
/**
* Settings for the trigger.
* @return array|instance_setting[]
*/
public function instance_settings() {
return array(
new instance_setting('roles', PARAM_SEQUENCE),
new instance_setting('delay', PARAM_INT),
);
}
/**
* Form for the instance.
* @param \MoodleQuickForm $mform
* @return void
* @throws \coding_exception
* @throws \dml_exception
*/
public function extend_add_instance_form_definition($mform) {
global $DB;
$allroles = $DB->get_records('role', null, 'sortorder DESC');
$rolenames = array();
foreach ($allroles as $role) {
$rolenames[$role->id] = empty($role->name) ? $role->shortname : $role->name;
}
$options = array(
'multiple' => true,
);
$mform->addElement('autocomplete', 'roles',
get_string('responsibleroles', 'lifecycletrigger_byrole'),
$rolenames, $options);
$mform->addHelpButton('roles', 'responsibleroles', 'lifecycletrigger_byrole');
$mform->setType('roles', PARAM_SEQUENCE);
$mform->addRule('roles', 'Test', 'required');
$elementname = 'delay';
$mform->addElement('duration', $elementname, get_string('delay', 'lifecycletrigger_byrole'));
$mform->addHelpButton('delay', 'delay', 'lifecycletrigger_byrole');
$mform->setType($elementname, PARAM_INT);
}
}