aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/userfilter/filter.php
blob: 87a7d872996a53cf2c0fe5b2df369320ed5853be (plain)
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
<?php

/**
 * Userfilter_FilterController
 *
 * 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 Userfilter_FilterController 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');
        }
        $this->conditionFields = UserFilterField::getAvailableFilterFields();

        PageLayout::setTitle(_('Auswahlbedingungen'));
        PageLayout::addScript('studip-userfilter.js');
    }

    /**
     * Show configuration for a given UserFilter.
     *
     * @param String $containerId Target HTML element
     * @param String $conditionId ID of an existiting UserFilter object
     */
    public function configure_action($containerId, $conditionId = '')
    {
        $this->containerId = $containerId;
        if ($conditionId) {
            $this->condition = new UserFilter($conditionId);
        }
    }

    /**
     * Adds a condition.
     */
    public function add_action()
    {
        $condition = new UserFilter();
        $fields = Request::getArray('field');
        $compareOps = Request::getArray('compare_operator');
        $values = Request::getArray('value');
        for ($i=0 ; $i<sizeof($fields) ; $i++) {
            $current = $fields[$i];
            if ($this->conditionFields[$current]) {
                $parts = explode('_', $current);
                $fieldType = $parts[0];
                $param = $parts[1] ?? null;
                $field = new $fieldType($param);
                $field->setCompareOperator($compareOps[$i]);
                $field->setValue($values[$i]);
                $condition->addField($field);
                $condition->show_user_count = true;
            }
        }
        $this->condition = $condition;
    }

    /**
     * Deletes the given UserFilter object.
     *
     * @param String $conditionId the UserFilter to delete.
     */
    public function delete_action($conditionId)
    {
        $condition = new UserFilter($conditionId);
        $condition->delete();
        $this->render_nothing();
    }

}