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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
<?php
/**
* admin/overlapping.php - controller to check for overlapping
* courses in Stud.IP
*
* 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 Peter Thienel <thienel@data-quest.de>
* @copyright 2018 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 4.4
*/
class Admin_OverlappingController extends AuthenticatedController
{
/**
* Common before filter for all actions.
*
* @param String $action Called actions
* @param Array $args Passed arguments
*/
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
Navigation::activateItem('/browse/my_courses/overlapping');
if (Request::option('sem_select')) {
$GLOBALS['user']->cfg->store('MY_COURSE_SELECTED_CYCLE', Request::option('sem_select'));
}
$this->selected_semester = Semester::find($GLOBALS['user']->cfg->MY_COURSE_SELECTED_CYCLE);
if (!$this->selected_semester) {
$this->selected_semester = Semester::findCurrent();
}
PageLayout::setTitle(_('Überschneidung von Veranstaltungen'));
}
/**
* Main view: Shows selection form and result.
*/
public function index_action()
{
$this->setSidebar();
$selections = SimpleORMapCollection::createFromArray(
MvvOverlappingSelection::findBySQL('`selection_id` = ? AND `user_id` = ?', [
Request::option('selection'),
$GLOBALS['user']->id
])
);
$this->selection_id = null;
if (count($selections)) {
$this->base_version = StgteilVersion::find($selections->first()->base_version_id);
$this->fachsems = (array) explode(',', $selections->first()->fachsems);
$this->semtypes = (array) explode(',', $selections->first()->semtypes);
$this->comp_versions = StgteilVersion::findMany($selections->pluck('comp_version_id'));
$this->selection_id = $selections->first()->selection_id;
if (Request::int('show_hidden') !== null) {
$_SESSION['MVV_OVL_HIDDEN'] = Request::int('show_hidden');
}
} else {
$this->base_version = StgteilVersion::find(Request::option('base_version'));
$this->comp_versions = StgteilVersion::findMany(Request::optionArray('comp_versions'));
$this->fachsems = Request::intArray('fachsems');
$this->semtypes = Request::intArray('semtypes');
}
$this->conflicts = MvvOverlappingSelection::getConflictsBySelection(
$this->selection_id,
empty($_SESSION['MVV_OVL_HIDDEN'])
);
}
/**
* Resets form and shows index view.
*/
public function reset_action()
{
$this->setSidebar();
$_SESSION['MVV_OVL_HIDDEN'] = 0;
$this->conflicts = [];
$this->render_action('index');
}
/**
* Calculates the conflicts and redirects to index view.
*/
public function check_action()
{
$this->setSidebar();
$this->base_version = StgteilVersion::find(Request::option('base_version'));
if ($this->base_version) {
$this->comp_versions = [];
foreach (Request::optionArray('comp_versions') as $comp_version_id) {
$this->comp_versions[] = StgteilVersion::find($comp_version_id);
}
// if no comparison version, check base version for internal conflicts
if (count($this->comp_versions) == 0) {
$this->comp_versions[$this->base_version->id] = $this->base_version;
}
$this->fachsems = Request::intArray('fachsems');
$this->semtypes = Request::intArray('semtypes');
if (Request::submitted('compare')) {
$selection_id = MvvOverlappingSelection::createSelectionId(
$this->base_version,
$this->comp_versions,
$this->fachsems,
$this->semtypes
);
// refresh conflicts
MvvOverlappingConflict::deleteBySelection($selection_id);
foreach ($this->comp_versions as $comp_version) {
$selection[$comp_version->id] = MvvOverlappingSelection::findOneBySQL(
'`selection_id` = ? AND `comp_version_id` = ?', [
$selection_id,
$comp_version->id
]);
if (!$selection[$comp_version->id]) {
$selection[$comp_version->id] = new MvvOverlappingSelection();
$selection[$comp_version->id]->semester_id = $this->selected_semester->id;
$selection[$comp_version->id]->selection_id = $selection_id;
$selection[$comp_version->id]->base_version_id = $this->base_version->id;
$selection[$comp_version->id]->comp_version_id = $comp_version->id;
$selection[$comp_version->id]->setFachsemester($this->fachsems);
$selection[$comp_version->id]->setCoursetypes($this->semtypes);
$selection[$comp_version->id]->user_id = $GLOBALS['user']->id;
$selection[$comp_version->id]->store();
}
$selection[$comp_version->id]->storeConflicts();
}
$conflicts = MvvOverlappingSelection::getConflictsBySelection($selection_id);
$visible_conflicts = MvvOverlappingSelection::getConflictsBySelection($selection_id, true);
if (count($conflicts)) {
if (count($conflicts) != count($visible_conflicts)) {
PageLayout::postSuccess(
sprintf(
ngettext('1 Konflikt gefunden (1 ausgeblendet)',
'%s Konflikte gefunden (%s ausgeblendet).', count($conflicts)),
count($conflicts),
count($conflicts) - count($visible_conflicts)
)
);
} else {
PageLayout::postSuccess(
sprintf(
ngettext('1 Konflikt gefunden.',
'%s Konflikte gefunden.', count($conflicts)),
count($conflicts)
)
);
}
} else {
PageLayout::postSuccess(_('Keine Konflikte gefunden.'));
}
}
} else {
PageLayout::postError('Die Basis-Version muss angegeben werden!');
}
$_SESSION['MVV_OVL_HIDDEN'] = Request::int('show_hidden');
$this->redirect($this->indexURL(['selection' => $selection_id]));
}
/**
* Shows the responsible admin of the course.
*
* @param type $course_id The id of the course.
*/
public function admin_info_action($course_id)
{
$this->course = Course::find($course_id);
if ($this->course) {
$this->admins = InstituteMember::findByInstituteAndStatus($this->course->institut_id, 'admin');
} else {
PageLayout::postMessage(MessageBox::error(_('Unbekannte Veranstaltung.')));
}
}
/**
* Shows the course details.
*
* @param type $course_id The id of the course.
*/
public function course_info_action($course_id)
{
$course = Course::find($course_id);
if ($course) {
Request::set('sem_id', $course->id);
$this->redirect('course/details' . '?sem_id=' . $course->id);
} else {
PageLayout::postMessage(MessageBox::error(_('Unbekannte Veranstaltung.')));
}
}
/**
* Sets a course as hidden.
*/
public function set_exclude_action()
{
$selection = MvvOverlappingSelection::find(Request::int('selection_id'));
if ($selection->user_id == $GLOBALS['user']->id) {
$exclude = new MvvOverlappingExclude([$selection->selection_id, Request::option('course_id')]);
if (Request::int('excluded')) {
$success = $exclude->delete();
} else {
$success = $exclude->store();
}
$this->set_status($success ? 204 : 400);
} else {
$this->set_status(403);
}
$this->render_nothing();
}
/**
* Shows detailed information about the studiengangteil version.
*
* @param type $version_id The id of the studiengangteil version.
*/
public function version_info_action($version_id)
{
$version = StgteilVersion::find($version_id);
if ($version) {
Request::set('version', $version->id);
$this->redirect($this->url_for('search/studiengaenge/verlauf/' . $version->stgteil_id,
['semester' => $this->selected_semester,
'version' => $version->id]));
return;
} else {
PageLayout::postMessage(MessageBox::error(_('Unbekannte Studiengangteil-Version.')));
}
}
/**
* Init the sidebar.
*/
private function setSidebar()
{
$sidebar = Sidebar::Get();
$widget = new SelectWidget(
_('Semesterauswahl'),
$this->url_for('admin/overlapping/index'),
'sem_select'
);
foreach (array_reverse(Semester::getAll()) as $semester) {
$widget->addElement(new SelectElement(
$semester->id,
$semester->name,
$semester->id === $this->selected_semester->id
), 'sem_select-' . $semester->id
);
}
$sidebar->addWidget($widget);
}
/**
* Search for base version by given search term.
*/
public function base_version_action()
{
$sword = Request::get('term');
$this->render_text(json_encode($this->getResult($sword)));
}
/**
* Search für comparison version by given search term.
*/
public function comp_versions_action()
{
$sword = Request::get('term');
$version_id = Config::get()->MVV_OVERLAPPING_SHOW_VERSIONS_INSIDE_MULTIPLE_STUDY_COURSES
? Request::option('version_id')
: null;
$version_ids = $this->getRelatedVersions($version_id);
$this->render_text(json_encode($this->getResult($sword, $version_ids)));
}
/**
* Returns versions related to the base version.
*
* @param type $version_id
* @return type
*/
private function getRelatedVersions($version_id)
{
$version_ids = [];
$version = StgteilVersion::find($version_id);
if ($version) {
$studiengaenge = Studiengang::findByStgTeil($version->stgteil_id);
} else {
return null;
}
foreach ($studiengaenge as $studiengang) {
if ($studiengang->typ == 'mehrfach') {
foreach ($studiengang->studiengangteile as $studiengangteil) {
$version_ids = array_merge(
$version_ids,
$studiengangteil->versionen->pluck('version_id')
);
}
}
}
return count($version_ids) ? array_diff($version_ids, [$version_id]) : null;
}
/**
* Search for studiengangteil versionen by given keyword. The result can be
* filtered by version ids.
*
* @param string $keyword The keyword to search for.
* @param array $version_ids An array of version ids.
* @return array An array of studiengangteil versionen.
*/
private function getResult($keyword, $version_ids = null) {
$version_query = '';
if (!is_null($version_ids)) {
$version_query = ' AND `mvv_stgteilversion`.`version_id` IN (:version_ids) ';
}
$query = "SELECT `version_id`, `fach`.`name`, `mvv_stgteil`.`kp`
FROM `fach`
INNER JOIN `mvv_stgteil` USING(`fach_id`)
INNER JOIN `mvv_stgteilversion` USING(`stgteil_id`)
INNER JOIN `semester_data` AS `start_sem`
ON (`mvv_stgteilversion`.`start_sem` = `start_sem`.`semester_id`)
LEFT JOIN `semester_data` AS `end_sem`
ON (`mvv_stgteilversion`.`end_sem` = `end_sem`.`semester_id`)
WHERE (`fach`.`name` LIKE :keyword
OR `mvv_stgteil`.`zusatz` LIKE :keyword
OR `mvv_stgteilversion`.`code` LIKE :keyword)
AND (`start_sem`.`beginn` <= :sem_end
OR ISNULL(`start_sem`.`beginn`))
AND (`end_sem`.`ende` >= :sem_start
OR ISNULL(`end_sem`.`ende`))
" . $version_query . "
ORDER BY `name` ASC, `kp` ASC";
$stat = array_keys(array_filter(
$GLOBALS['MVV_STGTEILVERSION']['STATUS']['values'],
function ($v) {
return $v['public'];
}
));
$stmt = DBManager::get()->prepare($query);
$stmt->execute([
':keyword' => '%' . $keyword . '%',
':stat' => $stat,
':sem_start' => $this->selected_semester->beginn,
':sem_end' => $this->selected_semester->ende,
':version_ids' => $version_ids
]);
$res = ['results' => []];
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $version_id) {
$version = StgteilVersion::find($version_id);
$res['results'][] = [
'id' => $version->id,
'text' => $version->getDisplayName()
];
}
return $res;
}
}
|