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
|
<?php
/**
* ParticipantRestrictedAdmission.php
*
* Specifies restricted number of participants for course admission.
*
* 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 ParticipantRestrictedAdmission extends AdmissionRule
{
// --- ATTRIBUTES ---
/**
* Timestamp for execution of seat distribution algorithm
*/
public $distributionTime = null;
public $first_come_first_served_allowed = false;
public $minimum_timespan_to_distribution_time = 120;
public $prio_exists = false;
// --- OPERATIONS ---
/**
* Standard constructor
*
* @param String $ruleId
* @param String $courseSetId
*/
public function __construct($ruleId = '', $courseSetId = '')
{
parent::__construct($ruleId, $courseSetId);
$this->first_come_first_served_allowed = (bool)Config::get()->ENABLE_COURSESET_FCFS;
$this->default_message = _('Es stehen keine weiteren Plätze zur Verfügung.');
if ($ruleId) {
$this->load();
} else {
$this->id = $this->generateId('participantrestrictedadmissions');
}
}
public function isFCFSallowed()
{
return $this->first_come_first_served_allowed;
}
/**
* Deletes the admission rule and all associated data.
*/
public function delete()
{
if ($this->prio_exists) {
$set_id = DBManager::get()->fetchColumn("SELECT set_id FROM courseset_rule WHERE rule_id = ? LIMIT 1", [$this->id]);
//Delete priorities
AdmissionPriority::unsetAllPriorities($set_id);
}
parent::delete();
// Delete rule data.
$stmt = DBManager::get()->prepare("DELETE FROM `participantrestrictedadmissions`
WHERE `rule_id`=?");
$stmt->execute([$this->id]);
}
/**
* Gets some text that describes what this AdmissionRule (or respective
* subclass) does.
*/
public static function getDescription()
{
return _("Anmelderegeln dieses Typs legen fest, ob die zugeordneten Veranstaltungen eine maximale Teilnehmendenanzahl haben. Die Platzverteilung erfolgt automatisiert.");
}
/**
* Gets the time for seat distribution algorithm.
*
* @return int
*/
public function getDistributionTime()
{
return $this->distributionTime;
}
/**
* Return this rule's name.
*/
public static function getName()
{
return _("Beschränkte Teilnehmendenanzahl");
}
/**
* Gets the template that provides a configuration GUI for this rule.
*
* @return String
*/
public function getTemplate()
{
$factory = new Flexi\Factory(dirname(__FILE__).'/templates/');
// Open specific template for this rule and insert base template.
$tpl = $factory->open('configure');
$tpl->set_attribute('rule', $this);
return $tpl->render();
}
/**
* Helper function for loading rule definition from database.
*/
public function load()
{
// Load data.
$stmt = DBManager::get()->prepare("SELECT *
FROM `participantrestrictedadmissions` WHERE `rule_id`=? LIMIT 1");
$stmt->execute([$this->id]);
if ($current = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->message = $current['message'];
$this->distributionTime = $current['distribution_time'];
if ($current['distribution_time'] > 0) {
$this->prio_exists = DBManager::get()->fetchColumn("SELECT 1 FROM courseset_rule INNER JOIN priorities USING(set_id) WHERE rule_id = ? LIMIT 1", [$this->id]);
}
}
}
/**
* 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);
if (!empty($data['distributiondate'])) {
if (!$data['distributiontime']) {
$data['distributiontime'] = '23:59';
}
$ddate = strtotime($data['distributiondate'] . ' ' . $data['distributiontime']);
$this->setDistributionTime($ddate);
}
if (!empty($data['enable_FCFS'])) {
$this->setDistributionTime(0);
}
if (!empty($data['startdate'])) {
$starttime = strtotime($data['startdate'] . ' ' . $data['starttime']);
if ($starttime > time()) {
$this->minimum_timespan_to_distribution_time = $this->minimum_timespan_to_distribution_time + (($starttime - time()) / 60);
}
}
return $this;
}
/**
* Sets a new timestamp for seat distribution algorithm execution.
*
* @param int $newDistributionTime
* @return ParticipantRestrictedAdmission
*/
public function setDistributionTime($newDistributionTime)
{
$this->distributionTime = $newDistributionTime;
return $this;
}
/**
* Store rule definition to database.
*/
public function store()
{
// Store data.
$stmt = DBManager::get()->prepare("INSERT INTO `participantrestrictedadmissions`
(`rule_id`, `message`, `distribution_time`,
`mkdate`, `chdate`) VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
`distribution_time`=VALUES(`distribution_time`),
message=VALUES(message), `chdate`=VALUES(`chdate`)");
$stmt->execute([$this->id, (string)$this->message,
(int)$this->distributionTime, time(), time()]);
}
/**
* 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 $data Request data
* @return Array Error messages.
*/
public function validate($data)
{
$errors = parent::validate($data);
if (!$data['distributiontime']) {
$data['distributiontime'] = '23:59';
}
$ddate = strtotime($data['distributiondate'] . ' ' . $data['distributiontime']);
if (empty($data['enable_FCFS']) && (empty($data['distributiondate']) || $ddate < (time() + $this->minimum_timespan_to_distribution_time * 60))) {
$errors[] = sprintf(_('Bitte geben Sie für die Platzverteilung ein Datum an, das weiter in der Zukunft liegt. Das frühestmögliche Datum ist %s.'), strftime('%x %R', time() + $this->minimum_timespan_to_distribution_time*60));
}
if (!empty($data['enable_FCFS']) && $data['distributiondate']) {
$errors[] = _('Sie können kein Datum für die automatische Platzverteilung einstellen und gleichzeitig die automatische Platzverteilung ausschalten.');
}
return $errors;
}
}
|