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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
|
<?php
/**
* LimitedAdmission.php
*
* Represents rules for admission to a limited number of courses.
*
* 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 Thomas Hackl <thomas.hackl@uni-passau.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class LimitedAdmission extends AdmissionRule
{
// --- ATTRIBUTES ---
/**
* Maximal number of courses that a user can register for.
*/
public $maxNumber = 1;
// --- OPERATIONS ---
/**
* Standard constructor.
*
* @param String ruleId
* @return LimitedAdmission
*/
public function __construct($ruleId='', $courseSetId = '')
{
parent::__construct($ruleId, $courseSetId);
$this->default_message = _('Sie haben sich bereits zur maximalen Anzahl von %s Veranstaltungen angemeldet.');
if ($ruleId) {
$this->load();
} else {
$this->id = $this->generateId('limitedadmissions');
}
}
/**
* Deletes the admission rule and all associated data.
*/
public function delete() {
parent::delete();
// Delete rule data.
$stmt = DBManager::get()->prepare("DELETE FROM `limitedadmissions`
WHERE `rule_id`=?");
$stmt->execute([$this->id]);
// Delete all custom max numbers.
$stmt = DBManager::get()->prepare("DELETE FROM `userlimits`
WHERE `rule_id`=?");
$stmt->execute([$this->id]);
}
/**
* Users can specify their own maximal number of courses they want
* to be registered for. This method gets the specified value for the
* given user or the max number that has been specified by the rule if no
* custom number was set.
*
* @param userId
* @return Integer
*/
public function getCustomMaxNumber($userId)
{
// Initially we use the number given per admission rule.
$maxNumber = $this->maxNumber;
$stmt = DBManager::get()->prepare("SELECT `maxnumber`
FROM `userlimits` WHERE rule_id=? AND user_id=?");
$stmt->execute([$this->id, $userId]);
// The user has given some custom number.
if ($current = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Custom number must be smaller than rule max number.
$maxNumber = min($maxNumber, $current['maxnumber']);
}
return $maxNumber;
}
/**
* Gets some text that describes what this AdmissionRule (or respective
* subclass) does.
*/
public static function getDescription() {
return _("Diese Art von Anmelderegel legt eine Maximalzahl von ".
"Veranstaltungen fest, an denen Nutzer im aktuellen ".
"Anmeldeset teilnehmen können.");
}
/**
* Gets the maximal number of courses that users can be registered for.
*
* @return Integer
*/
public function getMaxNumber()
{
return (int)$this->maxNumber;
}
public function getMaxNumberForUser($userId)
{
return min($this->maxNumber, $this->getCustomMaxNumber($userId));
}
/**
* Return this rule's name.
*/
public static function getName() {
return _("Anmeldung zu maximal n Veranstaltungen");
}
/**
* Gets the template that provides a configuration GUI for this rule.
*
* @return String
*/
public function getTemplate() {
// Open generic admission rule template.
$tpl = $GLOBALS['template_factory']->open('admission/rules/configure');
$tpl->set_attribute('rule', $this);
$factory = new Flexi\Factory(dirname(__FILE__).'/templates/');
// Now open specific template for this rule and insert base template.
$tpl2 = $factory->open('configure');
$tpl2->set_attribute('rule', $this);
$tpl2->set_attribute('tpl', $tpl->render());
return $tpl2->render();
}
/**
* Internal helper function for loading rule definition from database.
*/
public function load() {
$stmt = DBManager::get()->prepare("SELECT *
FROM `limitedadmissions` WHERE `rule_id`=? LIMIT 1");
$stmt->execute([$this->id]);
if ($current = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->message = $current['message'];
$this->startTime = $current['start_time'];
$this->endTime = $current['end_time'];
$this->maxNumber = $current['maxnumber'];
}
}
/**
* Does the current rule allow the given user to register as participant
* in the given course? That only happens when the user has no more than
* the given number of registrations at the other courses in the course set.
*
* @param String userId
* @param String courseId
* @return Array Any errors that occurred on admission.
*/
public function ruleApplies($userId, $courseId)
{
$errors = [];
// Check for rule validity time frame.
if ($this->checkTimeFrame()) {
// How many courses from this set has the user already registered for?
$db = DBManager::get();
$number = $db->fetchColumn("SELECT COUNT(*)
FROM `seminar_user` WHERE `user_id`=? AND `status` IN ('user', 'autor') AND `Seminar_id` IN (
SELECT `Seminar_id` FROM `seminar_courseset` WHERE `set_id`=?)", [$userId, $this->courseSetId]);
$number += $db->fetchColumn("SELECT COUNT(*)
FROM `admission_seminar_user` WHERE `user_id`=? AND `Seminar_id` IN (
SELECT `Seminar_id` FROM `seminar_courseset` WHERE `set_id`=?)", [$userId, $this->courseSetId]);
// Check if the number is smaller than admission rule limit
if (!($number <
$this->getMaxNumber())) {
$errors[] = $this->getMessage($this->getMaxNumber());
}
}
return $errors;
}
/**
* Uses the given data to fill the object values. This can be used
* as a generic function for storing data if the concrete rule type
* isn't known in advance.
*
* @param Array $data
* @return AdmissionRule This object.
*/
public function setAllData($data) {
parent::setAllData($data);
$this->maxNumber = intval($data['maxnumber']);
return $this;
}
/**
* Sets a new maximal number of courses that the given user can
* register for.
*
* @param String userId
* @param Integer maxNumber
* @return LimitedAdmission
*/
public function setCustomMaxNumber($userId, $maxNumber)
{
$stmt = DBManager::get()->prepare("INSERT INTO `userlimits`
(`rule_id`, `user_id`, `maxnumber`, `mkdate`, `chdate`)
VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE
`maxnumber`=VALUES(`maxnumber`), `chdate`=VALUES(`chdate`)");
$stmt->execute([$this->id, $userId,
min($this->maxNumber, $maxNumber), time(), time()]);
return $this;
}
/**
* Sets a new maximal number of courses for registration of the same user.
*
* @param Integer newMaxNumber
* @return LimitedAdmission
*/
public function setMaxNumber($newMaxNumber)
{
$this->maxNumber = $newMaxNumber;
return $this;
}
/**
* Helper function for storing data to DB.
*/
public function store() {
// Store data.
$stmt = DBManager::get()->prepare("INSERT INTO `limitedadmissions`
(`rule_id`, `message`, `start_time`, `end_time`, `maxnumber`,
`mkdate`, `chdate`)
VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE
`message`=VALUES(`message`), `start_time`=VALUES(`start_time`),
`end_time`=VALUES(`end_time`), `maxnumber`=VALUES(`maxnumber`),
`chdate`=VALUES(`chdate`)");
$stmt->execute([$this->id, $this->message, (int)$this->startTime,
(int)$this->endTime, $this->maxNumber, time(), time()]);
return $this;
}
/**
* A textual description of the current rule.
*
* @return String
*/
public function toString() {
$factory = new Flexi\Factory(dirname(__FILE__).'/templates/');
$tpl = $factory->open('info');
$tpl->set_attribute('rule', $this);
return $tpl->render();
}
/**
* Validates if the given request data is sufficient to configure this rule
* (e.g. if required values are present).
*
* @param Array Request data
* @return Array Error messages.
*/
public function validate($data)
{
$errors = parent::validate($data);
if (!$data['maxnumber']) {
$errors[] = _('Bitte geben Sie die maximale Anzahl erlaubter Anmeldungen an.');
}
return $errors;
}
public function getMessage($max_number = null)
{
$message = parent::getMessage();
if (isset($max_number)) {
return sprintf($message, $max_number);
} else {
return $message;
}
}
} /* end of class LimitedAdmission */
?>
|