aboutsummaryrefslogtreecommitdiff
path: root/lib/models/StgteilBezeichnung.php
blob: ba6ddd9053ccf550432c61895b79f03ca6868110 (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
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
<?php
/**
 * StgteilBezeichnung.php
 * Model class for Studiengangteil-Bezeichnungen (table mvv_stgteil_bez)
 *
 * 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>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.5
 *
 * @property string $id alias column for stgteil_bez_id
 * @property string $stgteil_bez_id database column
 * @property I18NString $name database column
 * @property I18NString $name_kurz database column
 * @property int $position database column
 * @property string $author_id database column
 * @property string $editor_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property mixed $count_stgteile additional field
 * @property mixed $count_studiengaenge additional field
 */

class StgteilBezeichnung extends ModuleManagementModel
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'mvv_stgteil_bez';

        $config['additional_fields']['count_stgteile']['get'] =
            function($stg_bez) { return $stg_bez->count_stgteile; };
        $config['additional_fields']['count_stgteile']['set'] = false;
        $config['additional_fields']['count_studiengaenge']['get'] =
            function($stg_bez) { return $stg_bez->count_studiengaenge; };
        $config['additional_fields']['count_studiengaenge']['set'] = false;

        $config['i18n_fields']['name'] = true;
        $config['i18n_fields']['name_kurz'] = true;

        parent::configure($config);
    }

    private $count_stgteile;
    private $count_studiengaenge;

    public function __construct($id = null)
    {
        parent::__construct($id);
        $this->object_real_name = _('Studiengangteil-Bezeichnung');
    }

    /**
     * Returns all or a specified (by row count and offset) number of
     * Studiengangteil-Bezeichnungen sorted by given parameters and enriched
     * with some additional fields. This function is mainly used
     * in the list view.
     *
     * @param string $sortby Field name to order by.
     * @param string $order ASC or DESC direction of order.
     * @param int $row_count The max number of objects to return.
     * @param int $offset The first object to return in a result set.
     * @return SimpleORMapCollection A collection of Studiengangteil-
     * Bezeichnungen.
     */
    public static function getAllEnriched($sortby = 'position', $order = 'ASC',
            $row_count = null, $offset = null)
    {
        $sortby = self::createSortStatement($sortby, $order, 'position',
                ['count_studiengaenge', 'count_stgteile']);
        return parent::getEnrichedByQuery('
            SELECT msb.*,
                COUNT(DISTINCT studiengang_id) AS `count_studiengaenge`,
                COUNT(DISTINCT stgteil_id) AS `count_stgteile`
            FROM mvv_stgteil_bez AS msb
                LEFT JOIN mvv_stg_stgteil USING (stgteil_bez_id)
                LEFT JOIN mvv_studiengang USING (studiengang_id)
            GROUP BY stgteil_bez_id
            ORDER BY ' . $sortby,
            [],
            $row_count,
            $offset
        );
    }

    /**
     * Returns all Studiengangteil-Bezeichnungen ordered by position.
     *
     * @return SimpleORMapCollection A collection of Studiengangteil-
     * Bezeichnungen.
     */
    public static function getAllSorted()
    {
        return parent::getEnrichedByQuery('
            SELECT * FROM mvv_stgteil_bez
            ORDER BY position
        ');
    }

    /**
     * Retrieves all Studienganteil-Bezeichnungen used by given Studiengang.
     *
     * @param type $studiengang_id The id of a Studiengang.
     * @return SimpleORMapCollection A collection of Studiengangteil-
     * Bezeichnungen.
     */
    public static function findByStudiengang($studiengang_id)
    {
        return parent::getEnrichedByQuery('
            SELECT msb.*,
                COUNT(DISTINCT stgteil_id) AS `count_stgteile`
            FROM mvv_stgteil_bez msb
                LEFT JOIN mvv_stg_stgteil mss USING(stgteil_bez_id)
            WHERE mss.studiengang_id = ?
            GROUP BY stgteil_bez_id
            ORDER BY position, mkdate',
            [$studiengang_id]
        );
    }

    /**
     * @see ModuleManagementModel::getClassDisplayName
     */
    public static function getClassDisplayName($long = false)
    {
        return _('Studiengangteil-Bezeichnung');
    }

    public function validate()
    {
        $ret = parent::validate();
        $messages = [];
        $rejected = false;

        // The name must not be empty
        if (!trim($this->name)) {
            $ret['name'] = true;
            $messages[] = _('Der Name der Studiengangteil-Bezeichnung darf nicht leer sein.');
            $rejected = true;
        } else {
            if ($this->isNew()) {
                // The name has to be unique
                $existing = $this->findBySql('name = ' . DBManager::get()->quote($this->name));
                if (sizeof($existing)) {
                    $ret['name'] = true;
                    $messages[] = sprintf(
                        _('Es existiert bereits eine Studiengangteil-Bezeichnung mit dem Namen "%s"!'),
                        $this->name
                    );
                    $rejected = true;
                }
            }
        }
        if ($rejected) {
            throw new InvalidValuesException(join("\n", $messages), $ret);
        }
        return $ret;
    }

}