Newer
Older
<?php
/**
* This program 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 2 of
* the License, or (at your option) any later version.
*
* @author Nico Müller <nico.mueller@uni-oldenburg.de>
* @author Michael Riehemann <michael.riehemann@uni-oldenburg.de>
* @author Jan Hendrik Willms <jan.hendrik.willms@uni-oldenburg.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 2.1
*/
/**

Jan-Hendrik Willms
committed
* AutoInsert.php
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
* Provides functions required by StEP00216:
* - Assign seminars for automatic registration of certain user types
* - Maintenance of registration rules
*
* Example of use:
* @code
*
* # show all auto insert seminars
* $auto_sems = AutoInsert::getAllSeminars();
*
* # Save a new auto insert seminar with the user status
* AutoInsert::saveSeminar($sem_id, $rechte);
*
* @endcode
*/
class AutoInsert
{
private static $instance = null;
protected static $seminar_cache = null;
private $settings = [];
public static function instance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct()
{
$this->loadSettings();
}
private function loadSettings()
{
$query = "SELECT a.seminar_id, GROUP_CONCAT(a.status,IF(LENGTH(a.domain_id)=0,':keine',CONCAT(':',a.domain_id))) AS domain_status, s.Name, s.Schreibzugriff, s.start_time ";
$query .= "FROM auto_insert_sem a ";
$query .= "JOIN seminare AS s USING (Seminar_id) ";
$query .= "GROUP BY s.seminar_id ";
$query .= "ORDER BY s.Name";
$statement = DBManager::get()->query($query);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $result) {
if ($result['Schreibzugriff'] < 3) {
$domains = explode(',', $result['domain_status']);
foreach ($domains as $domain) {
$array = explode(':', $domain);
$key = $array[1] . '.' . $array[0];
$this->settings[$key][$result['seminar_id']] = ['Seminar_id' => $result['seminar_id'],
'name' => $result['Name'],
'Schreibzugriff' => $result['Schreibzugriff'],
'start_time' => $result['start_time']];
}
}
}
}
private function getUserSeminars($user_id, $seminare)
{
$statement = DBManager::get()->prepare("SELECT Seminar_id,s.name,s.Schreibzugriff,s.start_time,su.status
FROM seminar_user su
INNER JOIN seminare s USING(Seminar_id)
WHERE user_id = ? AND Seminar_id IN(?)");
$statement->execute([$user_id, $seminare]);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Trägt den Benutzer in den Eingestellten veranstaltungen automatisch ein.

Jan-Hendrik Willms
committed
* @param string|bool $status Wenn Status nicht angegeben wird, wird der Status des Users aus user_id genommen
* @return array 'added' Namen der Seminare in die der User eingetragen wurde
* array 'removed' Namen der Seminare aus denen der User ausgetragen wurde
*/
public function saveUser($user_id, $status = false)
{
$domains = [];
if (!$status) {
$status = $GLOBALS['perm']->get_perm($user_id);
}
foreach (UserDomain::getUserDomainsForUser($user_id) as $d) {
$domains [] = $d->id; //Domains des Users
}
if (count($domains) === 0) {
$domains [] = 'keine';
}
$settings = [];
$all_seminare = [];
foreach ($domains as $domain) {
$key = $domain . '.' . $status;
if (isset($this->settings[$key]) && is_array($this->settings[$key])) {
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
foreach ($this->settings[$key] as $id => $value) {
$settings[$id] = $value;
}
}
foreach ($this->settings as $key) {
foreach ($key as $id => $sem) {
$all_seminare[$id] = $sem;
}
}
}
$seminare = [];
$seminare_tutor_dozent = [];
foreach ($this->getUserSeminars($user_id, array_keys($all_seminare)) as $sem) {
$seminare[$sem['Seminar_id']] = $sem;
if (in_array($sem['status'], ['tutor', 'dozent'])) {
$seminare_tutor_dozent[$sem['Seminar_id']] = $sem;
}
}
$toAdd = array_diff_key($settings, $seminare);
$toRemove = array_diff_key($all_seminare, $toAdd, $settings, $seminare_tutor_dozent);
$added = [];
$removed = [];
foreach ($toAdd as $id => $seminar) {
if ($this->addUser($user_id, $seminar)) $added[] = $seminar['name'];
}
foreach ($toRemove as $id => $seminar) {
if ($this->removeUser($user_id, $seminar)) $removed[] = $seminar['name'];
}
return ['added' => $added, 'removed' => $removed];
}
private function addUser($user_id, $seminar)
{
return CourseMember::insertCourseMember($seminar['Seminar_id'], $user_id, 'autor');
}
private function removeUser($user_id, $seminar)
{
$rows = CourseMember::deleteBySQL(' user_id = ? AND Seminar_id = ?', [$user_id, $seminar['Seminar_id']]);
$statusgruppe_rows = StatusgruppeUser::deleteBySQL(
'user_id = ? AND statusgruppe_id IN (SELECT statusgruppe_id FROM statusgruppen WHERE range_id = ?)',
[$user_id, $seminar['Seminar_id']]
);
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
if ($rows > 0 || $statusgruppe_rows > 0) return true;
return false;
}
/**
* Tests if a seminar already has an autoinsert record
* @param string $seminar_id Id of the seminar
* @return bool Indicating whether the seminar already has an autoinsert record
*/
public static function checkSeminar($seminar_id, $domain_id = false)
{
$cached = self::getSeminarCache();
if (!isset($cached[$seminar_id])) {
$query = "SELECT domain_id, 1
FROM auto_insert_sem
WHERE seminar_id = ?";
$cached[$seminar_id] = DBManager::get()->fetchGroupedPairs(
$query,
[$seminar_id],
function ($value) {
return (bool) $value;
}
);
}
return array_key_exists($domain_id ?: '', $cached[$seminar_id])
? $cached[$seminar_id][$domain_id ?: '']
: false;
}
/**
* Enables a seminar for autoinsertion of users with the given status(ses)
* @param string $seminar_id Id of the seminar
* @param mixed $status Either a single string or an array of strings
* containing the status(ses) to enable for
* autoinsertion
*/
public static function saveSeminar($seminar_id, $status, $domain_id)
{
$query = "INSERT INTO auto_insert_sem (seminar_id, status,domain_id) VALUES (?, ?,?)";
$statement = DBManager::get()->prepare($query);
foreach ((array)$status as $s) {
$statement->execute([$seminar_id, $s, $domain_id]);
}
}
/**
* Updates an autoinsert record for a given seminar, dependent on the
* parameter $remove it either inserts or removes the record for the given
* parameters
*
* @param string $seminar_id Id of the seminar
* @param string $status Status for autoinsertion
* @param bool $remove Whether the record should be added or removed
*/
public static function updateSeminar($seminar_id, $domain, $status, $remove = false)
{
$query = $remove ? "DELETE FROM auto_insert_sem WHERE seminar_id = ? AND status= ? AND domain_id = ?" : "INSERT IGNORE INTO auto_insert_sem (seminar_id, status,domain_id) VALUES (?, ?, ?)";
$statement = DBManager::get()->prepare($query);
$statement->execute([$seminar_id, $status, $domain]);
if ($remove) {
unset(self::getSeminarCache()[$seminar_id]);
}
}
/**
* Removes a seminar from the autoinsertion process.
* @param string $seminar_id Id of the seminar
*/

Jan-Hendrik Willms
committed
public static function deleteSeminar($seminar_id): bool
{
$query = "DELETE FROM auto_insert_sem WHERE seminar_id = ?";
$statement = DBManager::get()->prepare($query);

Jan-Hendrik Willms
committed
$result = $statement->execute([$seminar_id]);
unset(self::getSeminarCache()[$seminar_id]);

Jan-Hendrik Willms
committed
return $result > 0;
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
}
/**
* Returns a list of all seminars enabled for autoinsertion
* @param bool Indicates whether only the seminar ids (true) or the full
* dataset shall be returned (false)
* @return array The list of all enabled seminars (format according to $only_sem_id)
*/
public static function getAllSeminars($only_sem_id = false)
{
if ($only_sem_id) {
$query = "SELECT DISTINCT seminar_id FROM auto_insert_sem";
$statement = DBManager::get()->query($query);
$results = $statement->fetchAll(PDO::FETCH_COLUMN);
} else {
$query = "SELECT a.seminar_id, GROUP_CONCAT(a.status,IF(LENGTH(a.domain_id)=0,':keine',CONCAT(':',a.domain_id))) AS domain_status, s.Name, s.Schreibzugriff, s.start_time ";
$query .= "FROM auto_insert_sem a ";
$query .= "JOIN seminare AS s USING (Seminar_id) ";
$query .= "GROUP BY s.seminar_id ";
$query .= "ORDER BY s.Name";
$statement = DBManager::get()->query($query);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $index => $result) {
$domains = explode(',', $result['domain_status']);
foreach ($domains as $domain) {
$array = explode(':', $domain);
$results[$index]['status'][$array[1]][] = $array[0];
}
}
}
return $results;
}
/**
* Returns a seminar's info for autoinsertion
* @param string $seminar_id Id of the seminar
* @return array The seminar's data as an associative array
*/
public static function getSeminar($seminar_id)
{
$query = "SELECT a.seminar_id, GROUP_CONCAT(a.status) AS status, s.Name ";
$query .= "FROM auto_insert_sem a ";
$query .= "JOIN seminare AS s USING (Seminar_id) ";
$query .= "WHERE a.seminar_id = ? ";
$query .= "GROUP BY s.seminar_id";
$statement = DBManager::get()->prepare($query);
$statement->execute([$seminar_id]);
$result = $statement->fetch(PDO::FETCH_ASSOC);
$result['status'] = explode(',', $result['status']);
return $result;
}
/**
* Store the user's automatic registration in a seminar redundantly to
* avoid an annoying reregistration although the user explicitely left the
* according seminar
* @param string $user_id Id of the user
* @param string $seminar_id Id of the seminar
*/
public static function saveAutoInsertUser($seminar_id, $user_id)
{
$query = "INSERT IGNORE INTO auto_insert_user (Seminar_id, user_id, mkdate)
SELECT ?, user_id, UNIX_TIMESTAMP()
FROM auth_user_md5
WHERE user_id = ? AND perms NOT IN ('root','admin')";
return DBManager::get()->execute($query, [$seminar_id, $user_id]);
}
/**
* Tests whether a user was already automatically registered for a certain
* seminar.
* @param string $seminar_id Id of the seminar
* @param string $user_id If of the user
* @return bool Indicates whether the user was already registered
*/
public static function checkAutoInsertUser($seminar_id, $user_id)
{
$query = "SELECT 1 FROM auto_insert_user WHERE seminar_id = ? AND user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$seminar_id, $user_id]);
$result = $statement->fetchColumn();
return $result > 0;
}
/**
* Returns the cache for seminars.
*/
protected static function getSeminarCache(): StudipCachedArray
{
if (self::$seminar_cache === null) {
self::$seminar_cache = new StudipCachedArray('AutoInsertSeminars');
}
return self::$seminar_cache;
}
}