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
|
<?php
/**
* Course_StudyAreasController
*
* 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 Marcus Lunzenauer <mlunzena@uos.de>
* @author David Siegfried <david.siegfried@uni-vechta.de>
* @category Stud.IP
* @since 3.2
*/
class Course_StudyAreasController extends AuthenticatedController
{
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
// Search for course object
$this->course = Course::findCurrent();
$this->locked = LockRules::Check($this->course->id, 'sem_tree');
// check course object and perms
if (isset($this->course) && !$GLOBALS['perm']->have_studip_perm('tutor', $this->course->id)) {
$this->set_status(403);
return false;
}
// Init Studyareas-Step for
$lv_groups_enabled = CourseWizardStepRegistry::findOneBySQL("
`classname` = 'StudyAreasLVGroupsCombinedWizardStep'
AND `enabled` = 1");
if ($lv_groups_enabled) {
$this->step = new StudyAreasLVGroupsCombinedWizardStep();
} else {
$this->step = new StudyAreasWizardStep();
}
$this->values = [];
$this->values[get_class($this->step)]['studyareas'] = $this->get_area_ids($this->course->id);
$this->values[get_class($this->step)]['ajax_url'] = $this->url_for('course/study_areas/ajax');
$this->values[get_class($this->step)]['no_js_url'] = $this->url_for('course/study_areas/show');
PageLayout::setTitle($this->course->getFullName() . ' - ' . _('Studienbereiche'));
}
public function show_action()
{
Navigation::activateItem('course/admin/study_areas');
$this->url_params = [];
if (Request::get('from')) {
$this->url_params['from'] = Request::get('from');
}
if (Request::get('open_node')) {
$this->url_params['open_node'] = Request::get('open_node');
}
if ($this->course) {
if ($GLOBALS['perm']->have_studip_perm('admin', $this->course->id)) {
$widget = new CourseManagementSelectWidget();
Sidebar::Get()->addWidget($widget);
}
}
if (Request::get('open_node')) {
$this->values[get_class($this->step)]['open_node'] = Request::get('open_node');
}
$this->is_activated = $this->is_activated();
$this->is_required = $this->is_required();
$this->values[get_class($this->step)]['locked'] = $this->locked;
$this->tree = $this->step->getStepTemplate($this->values, 0, 0);
}
public function ajax_action()
{
$parameter = Request::getArray('parameter');
$method = Request::get('method');
switch ($method) {
case 'searchSemTree':
$json = $this->step->searchSemTree($parameter[0]);
break;
case 'getSemTreeLevel':
$json = $this->step->getSemTreeLevel($parameter[0]);
break;
default:
$json = $this->step->getAncestorTree($parameter[0]);
break;
}
$this->render_json($json);
}
public function save_action()
{
if($this->locked) {
throw new Trails\Exception(403);
}
$params = [];
if (Request::get('open_node')) {
$params['open_node'] = Request::get('open_node');
}
if (Request::get('from')) {
$url = $this->url_for(Request::get('from'));
} else {
$url = $this->url_for('course/study_areas/show/' . $this->course->id);
}
if (Request::submittedSome('assign', 'unassign')) {
if (Request::submitted('assign')) {
$msg = $this->assign();
}
if (Request::submitted('unassign')) {
$msg = $this->unassign();
}
} else {
$studyareas = Request::getArray('studyareas');
if (empty($studyareas) && $this->is_required()) {
PageLayout::postError(_('Sie müssen mindestens einen Studienbereich auswählen'));
$this->redirect($url);
return;
}
if (!empty($studyareas) && !$this->is_activated()) {
PageLayout::postError(_('Sie dürfen keine Studienbereiche zuweisen.'));
$this->redirect($url);
return;
}
try {
$this->course->setStudyAreas($studyareas);
} catch (UnexpectedValueException $e) {
PageLayout::postError($e->getMessage());
}
}
if (empty($msg)) {
PageLayout::postSuccess(_('Die Studienbereichszuordnung wurde übernommen.'));
} else {
PageLayout::postError($msg);
}
if (Request::isDialog()) {
$this->response->add_header('X-Dialog-Close', 1);
$this->response->add_header('X-Dialog-Execute', 'STUDIP.AdminCourses.App.loadCourse');
$this->render_text($this->course->id);
} else {
$this->redirect($url);
}
}
public function unassign()
{
$msg = null;
$assigned = $this->course->study_areas->pluck('sem_tree_id');
foreach (array_keys(Request::getArray('unassign')) as $remove) {
if (false !== ($pos = array_search($remove, $assigned))) {
unset($assigned[$pos]);
}
}
if (empty($assigned) && $this->is_required()) {
return _('Sie müssen mindestens einen Studienbereich auswählen');
}
$this->course->setStudyAreas($assigned);
return $msg;
}
public function assign()
{
$msg = null;
$assigned = array_keys(Request::getArray('assign'));
if ($this->course->study_areas) {
$assigned = array_unique(array_merge($assigned, $this->course->study_areas->pluck('sem_tree_id')));
}
$this->course->setStudyAreas($assigned);
return $msg;
}
public function get_area_ids($course_id)
{
$selection = StudipStudyArea::getStudyAreasForCourse($course_id);
return array_keys($selection->toGroupedArray('sem_tree_id'));
}
/**
* Check whether the assignmenet of study areas is required.
*
* @return boolean True if required.
*/
private function is_required()
{
$sem_class = $this->course->getSemClass();
if (get_class($this->step) === 'StudyAreasLVGroupsCombinedWizardStep') {
if ($sem_class['module']) {
$lv_gruppen = Lvgruppe::findBySeminar($this->course->id);
if (count($lv_gruppen)) {
return false;
}
}
}
return (bool) $sem_class['bereiche'];
}
private function is_activated()
{
$sem_class = $this->course->getSemClass();
return (bool) $sem_class['bereiche'];
}
}
|