aboutsummaryrefslogtreecommitdiff
path: root/lib/models/StgteilAbschnitt.php
blob: 77f8aef001d3c2a826ba323d0b6726341fd5fd45 (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
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
<?php
/**
 * StgteilAbschnitt.php
 * Model class for Studiengangsabschnitte (table mvv_stgteilabschnitt)
 *
  * 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 abschnitt_id
 * @property string $abschnitt_id database column
 * @property string $version_id database column
 * @property int $position database column
 * @property I18NString $name database column
 * @property I18NString|null $kommentar database column
 * @property float|null $kp database column
 * @property I18NString|null $ueberschrift 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 SimpleORMapCollection|StgteilabschnittModul[] $modul_zuordnungen has_many StgteilabschnittModul
 * @property SimpleORMapCollection|ModulteilStgteilabschnitt[] $modulteil_abschnitte has_many ModulteilStgteilabschnitt
 * @property StgteilVersion $version belongs_to StgteilVersion
 * @property SimpleORMapCollection|Modul[] $module has_and_belongs_to_many Modul
 * @property mixed $count_module additional field
 */

class StgteilAbschnitt extends ModuleManagementModelTreeItem
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'mvv_stgteilabschnitt';

        $config['has_and_belongs_to_many']['module'] = [
            'class_name' => Modul::class,
            'thru_table' => 'mvv_stgteilabschnitt_modul',
            'thru_key' => 'abschnitt_id',
            'thru_assoc_key' => 'modul_id',
            'order_by' => 'ORDER BY position,mkdate'
        ];
        $config['belongs_to']['version'] = [
            'class_name' => StgteilVersion::class,
            'foreign_key' => 'version_id',
            'assoc_func' => 'findCached',
        ];
        $config['has_many']['modul_zuordnungen'] = [
            'class_name' => StgteilabschnittModul::class,
            'assoc_foreign_key' => 'abschnitt_id',
            'order_by' => 'ORDER BY position,mkdate',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_many']['modulteil_abschnitte'] = [
            'class_name' => ModulteilStgteilabschnitt::class,
            'assoc_foreign_key' => 'abschnitt_id',
            'order_by' => 'ORDER BY fachsemester',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];

        $config['additional_fields']['count_module']['get'] = function ($fach) {
            return $fach->count_module;
        };

        $config['i18n_fields']['name'] = true;
        $config['i18n_fields']['kommentar'] = true;
        $config['i18n_fields']['ueberschrift'] = true;

        parent::configure($config);
    }

    private $count_module;

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

    public static function getEnriched($abschnitt_id)
    {
        $abschnitte = parent::getEnrichedByQuery(
                'SELECT msa.*, COUNT(modul_id) AS count_module '
                . 'FROM mvv_stgteilabschnitt msa '
                . 'LEFT JOIN mvv_stgteilabschnitt_modul msm USING(abschnitt_id) '
                . 'WHERE msa.abschnitt_id = ? '
                . 'GROUP BY abschnitt_id',
                [$abschnitt_id]);
        if (sizeof($abschnitte)) {
            return $abschnitte[$abschnitt_id];
        }
        return self::get();
    }

    /**
     * Retrieves all Studienganteil-Abschnitte for the given
     * Studienganteil-Version.
     *
     * @param type $version_id The id of a Studiengangteil-Version.
     * @return SimpleORMapCollection A collection of Studiengangteil-Abschnitte.
     */
    public static function findByStgteilVersion($version_id)
    {
        return parent::getEnrichedByQuery('SELECT msa.*, '
                . 'COUNT(modul_id) AS count_module '
                . 'FROM mvv_stgteilabschnitt msa '
                . 'LEFT JOIN mvv_stgteilabschnitt_modul msm USING(abschnitt_id) '
                . 'WHERE msa.version_id = ? '
                . 'GROUP BY abschnitt_id '
                . 'ORDER BY position, chdate', [$version_id]);
    }

    /**
     * Retrieves all Studienganteil-Abschnitte the given Modul is assigned to.
     *
     * @param type $modul_id The id of a Modul.
     * @return SimpleORMapCollection A collection of Studiengangteil-Abschnitte.
     */
    public static function findByModul($modul_id)
    {
        return parent::getEnrichedByQuery('SELECT msa.* '
                . 'FROM mvv_stgteilabschnitt msa '
                . 'LEFT JOIN mvv_stgteilabschnitt_modul msm USING(abschnitt_id) '
                . 'WHERE msm.modul_id = ? '
                . 'ORDER BY position, chdate', [$modul_id]);
    }

    /**
     * Assignes a Modul to this Studiengangteil-Abschnitt.
     *
     * @param type $modul The id of a Modul or the Modul as object.
     * @return boolean True if the assignemnt was successful.
     */
    public function addModul($modul)
    {
        if (is_object($modul)) {
            if (!($modul instanceof Modul) || $modul->isNew()) {
                return false;
            }
        } else {
            $modul = Modul::findCached($modul);
            if (!$modul) {
                return false;
            }
        }
        $abschnitt_modul = StgteilabschnittModul::findOneBySQL(
            'abschnitt_id = ? AND modul_id = ?',
            [$this->id, $modul->id]
        );
        if (!$abschnitt_modul) {
            $abschnitt_modul = new StgteilabschnittModul();
            $abschnitt_modul->abschnitt_id = $this->id;
            $abschnitt_modul->modul_id = $modul->id;
        }
        if (!$this->modul_zuordnungen) {
            $this->modul_zuordnungen = SimpleORMapCollection::createFromArray(
                    [$abschnitt_modul]);
        } else {
            $this->modul_zuordnungen->append($abschnitt_modul);
        }
        return true;
    }

    /**
     * Removes (deletes the assignment of) a Modul to this Studiengangteil-
     * Abschnitt.
     *
     * @param type $modul The id of a Modul or the Modul as object.
     * @return boolean True if the Modul was successfully removed.
     */
    public function removeModul($modul)
    {
        if (is_object($modul)) {
            if (!($modul instanceof Modul) || $modul->isNew()) {
                return false;
            }
        } else {
            $modul = Modul::findCached($modul);
            if (!$modul) {
                return false;
            }
        }
        if (!$this->modul_zuordnungen) {
            return false;
        }
        $modul_zuordnung = StgteilabschnittModul::findOneBySQL(
                'abschnitt_id = ? AND modul_id = ?',
                [$this->id, $modul->id]);
        $removed = $this->modul_zuordnungen->unsetByPk($modul_zuordnung->id);
        return $removed !== false;
    }

    /**
     * Returns the Version this Studiengangteilabschnitt is assigned to.
     *
     * @return null|object The Version.
     */
    public function getVersion()
    {
        return StgteilVersion::findByStgteilAbschnitt($this->getId());
    }

    /**
     * @see MvvTreeItem::getTrailParentId()
     */
    public function getTrailParentId()
    {
        return $this->version_id;
    }

    /**
     * @see MvvTreeItem::getTrailParent()
     */
    public function getTrailParent()
    {
        return $this->getVersion();
    }

    /**
     * @see MvvTreeItem::getParents()
     */
    public function getParents($mode = null)
    {
        return [StgteilVersion::findByStgteilAbschnitt($this->getId())];
    }

    /**
     * @see MvvTreeItem::getChildren()
     */
    public function getChildren()
    {
        $_SESSION['MVV/Modul/trail_parent_id'] =  $this->getId();
        $filter = ['mvv_modul.stat' => Modul::getPublicStatus()];
        return Modul::findByStgteilAbschnitt($this->getId(), $filter);
    }

    /**
     * Returns all assignments of Module to this Studiengangteil-Abschnitt.
     *
     * @return SimpleORMapCollection A collection of Module.
     */
    public function getModulAssignments()
    {
        return $this->modul_zuordnungen;
    }

    /**
     * Inherits the status of the parent version.
     *
     * @return string the status of parent version
     */
    public function getStatus()
    {
        if ($this->getVersion()) {
            return $this->getVersion()->getStatus();
        } elseif ($this->isNew()) {
            return $GLOBALS['MVV_STGTEILVERSION']['STATUS']['default'];
        }
        return parent::getStatus();
    }

    /**
     * Returns the responsible institutes.
     * Inherits the responsible institutes from Studiengangteil-Version
     *
     * @return array Array of institute objects.
     */
    public function getResponsibleInstitutes()
    {
        if ($this->version) {
            return $this->version->getResponsibleInstitutes();
        } else {
            //In case no responsible institutes can be
            //determined we must return an empty array:
            return [];
        }
    }

}