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
|
<?php
/**
* Admission_RuleController - Admission rules
*
* 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
* @since 3.0
*/
class Admission_RuleController extends AuthenticatedController
{
/**
* @see AuthenticatedController::before_filter
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if ($GLOBALS['perm']->have_perm('admin') || ($GLOBALS['perm']->have_perm('dozent') && Config::get()->ALLOW_DOZENT_COURSESET_ADMIN)) {
Navigation::activateItem('/browse/coursesets');
}
PageLayout::setTitle(_('Anmeldesets'));
}
/**
* Gets the template for the rule configuration form.
*
* @param String $ruleType Class name of the rule to configure.
* @param String $ruleId Optional ID of an existing rule.
*/
public function configure_action($ruleType = '', $ruleId = '')
{
$this->ruleTypes = AdmissionRule::getAvailableAdmissionRules();
UserFilterField::getAvailableFilterFields();
$this->ruleType = $ruleType;
// Check if rule data has been given via request.
if (Request::getArray('rules')) {
$rule_siblings = [];
foreach (Request::getManyObjects('rules', 'AdmissionRule') as $rule) {
if ($ruleType == get_class($rule) && $rule->getId() == Request::get('ruleId')) {
$this->rule = $rule;
} else {
$rule_siblings[$rule->getId()] = $rule;
}
}
if (!$this->rule && in_array($ruleType, array_keys($this->ruleTypes))) {
$this->rule = new $ruleType($ruleId);
}
$this->rule->setSiblings($rule_siblings);
} elseif (Request::get('rule')) {
$rule = Request::getObject('rule', 'AdmissionRule');
if ($ruleType == get_class($rule)) {
$this->rule = $rule;
}
} elseif (in_array($ruleType, array_keys($this->ruleTypes))) {
$this->rule = new $ruleType($ruleId);
}
if ($this->rule) {
$this->ruleTemplate = $this->rule->getTemplate();
}
}
/**
* Shows a form for selecting which rule type to use.
*
* @param String $cs_id ID of a courseset the rule shall belong to.
*/
public function select_type_action($cs_id = '')
{
$this->ruleTypes = AdmissionRule::getAvailableAdmissionRules();
$this->courseset = new CourseSet($cs_id);
$this->courseset->clearAdmissionRules();
foreach (Request::getManyObjects('rules', 'AdmissionRule') as $rule) {
$this->courseset->addAdmissionRule($rule);
}
}
/**
* Saves the given rule.
*
* @param String $ruleType The class name of the configured rule.
* @param String $ruleId ID of the rule to save, or empty if this is a new rule.
*/
public function save_action($ruleType, $ruleId = '')
{
CSRFProtection::verifyUnsafeRequest();
$this->rule = $this->loadRule($ruleType, $ruleId);
$requestData = Request::getInstance();
// Check for start and end date and parse the String values to timestamps.
if (!empty($requestData['start_date'])) {
$parsed = date_parse($requestData['start_date']);
$timestamp = mktime($parsed['hour'], $parsed['minute'], 0,
$parsed['month'], $parsed['day'], $parsed['year']);
$requestData['start_time'] = $timestamp;
}
if (!empty($requestData['end_date'])) {
$parsed = date_parse($requestData['end_date']);
$timestamp = mktime($parsed['hour'], $parsed['minute'], 0,
$parsed['month'], $parsed['day'], $parsed['year']);
$requestData['end_time'] = $timestamp;
}
$this->rule->setAllData($requestData);
}
/**
* Validates if the values given in the current request are sufficient to
* configure a rule of the given type.
*
* @param String $ruleType Class name of the rule to check.
* @param String $ruleId ID of the rule to save, or empty if this is a new rule.
*/
public function validate_action($ruleType, $ruleId = '')
{
$rule = $this->loadRule($ruleType, $ruleId);
$this->errors = $rule->validate(Request::getInstance());
}
/**
* Loads a rule by string and ensures that it is a subclass of the abstract
* admission rule.
*
* @param string $rule_type
* @param string $rule_id
* @return AdmissionRule
*/
private function loadRule(string $rule_type, string $rule_id = ''): AdmissionRule
{
static $initialized = false;
if (!$initialized) {
// This is neccessary so that all admission rules are correctly
// loaded and known to the system
AdmissionRule::getAvailableAdmissionRules();
$initialized = true;
}
if (!is_a($rule_type, AdmissionRule::class, true)) {
throw new InvalidArgumentException('Rule type must be a subclass of ' . AdmissionRule::class);
}
return new $rule_type($rule_id);
}
}
|