aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Institute.class.php
blob: 04430ee72c84b00411ead53578f05fcd380ce064 (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
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
372
<?php
/**
 * Institute.class.php - model class for table Institute
 *
 * 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      Rasmus Fuhse <fuhse@data-quest>
 * @author      Suchi & Berg GmbH <info@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       2.0
 *
 * @property string institut_id database column
 * @property string id alias column for institut_id
 * @property string name database column
 * @property string fakultaets_id database column
 * @property string strasse database column
 * @property string plz database column
 * @property string url database column
 * @property string telefon database column
 * @property string email database column
 * @property string fax database column
 * @property string type database column
 * @property string modules database column
 * @property string mkdate database column
 * @property string chdate database column
 * @property string lit_plugin_name database column
 * @property string srienabled database column
 * @property string lock_rule database column
 * @property string is_fak computed column
 * @property SimpleORMapCollection members has_many InstituteMember
 * @property SimpleORMapCollection home_courses has_many Course
 * @property SimpleORMapCollection sub_institutes has_many Institute
 * @property SimpleORMapCollection datafields has_many DatafieldEntryModel
 * @property Institute faculty belongs_to Institute
 * @property SimpleORMapCollection courses has_and_belongs_to_many Course
 *
 * @property ConsultationBlock[]|SimpleORMapCollection $consultation_blocks
 * @property ConsultationResponsibility[]|SimpleORMapCollection $consultation_responsibilities
 */

class Institute extends SimpleORMap implements Range
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'Institute';
        $config['additional_fields']['is_fak']['get'] = 'isFaculty';

        $config['has_many']['members'] = [
            'class_name' => InstituteMember::class,
            'assoc_func' => 'findByInstitute',
            'on_delete' => 'delete',
            'on_store' => 'store',
        ];
        $config['has_many']['home_courses'] = [
            'class_name' => Course::class,
            'on_delete' => 'delete',
            'on_store' => 'store',
        ];
        $config['has_many']['sub_institutes'] = [
            'class_name' => Institute::class,
            'assoc_foreign_key' => 'fakultaets_id',
            'assoc_func' => 'findByFaculty',
            'on_delete' => 'delete',
            'on_store' => 'store',
        ];
        $config['has_many']['datafields'] = [
            'class_name' => DatafieldEntryModel::class,
            'assoc_foreign_key' =>
                function($model,$params) {
                    $model->setValue('range_id', $params[0]->id);
                },
            'assoc_func' => 'findByModel',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'foreign_key' =>
                function($i) {
                    return [$i];
                }
        ];
        $config['belongs_to']['faculty'] = [
            'class_name' => Institute::class,
            'foreign_key' => 'fakultaets_id',
        ];
        $config['has_and_belongs_to_many']['courses'] = [
            'class_name' => Course::class,
            'thru_table' => 'seminar_inst',
            'on_delete' => 'delete',
            'on_store' => 'store',
        ];
        $config['has_many']['scm'] = [
            'class_name'        => StudipScmEntry::class,
            'assoc_foreign_key' => 'range_id',
            'on_delete'         => 'delete',
            'on_store'          => 'store',
        ];
        $config['has_many']['status_groups'] = [
            'class_name'        => Statusgruppen::class,
            'assoc_foreign_key' => 'range_id',
            'on_delete'         => 'delete',
            'on_store'          => 'store',
            'order_by'          => 'ORDER BY position ASC',
        ];
        $config['has_many']['blubberthreads'] = [
            'class_name' => BlubberThread::class,
            'assoc_func' => 'findByInstitut',
            'on_delete'  => 'delete',
            'on_store'   => 'store',
        ];
        $config['has_many']['consultation_blocks'] = [
            'class_name'        => ConsultationBlock::class,
            'assoc_foreign_key' => 'range_id',
            'on_delete'         => 'delete',
        ];
        $config['has_many']['consultation_responsibilities'] = [
            'class_name'        => ConsultationResponsibility::class,
            'assoc_func'        => 'findByInstituteId',
            'on_delete'         => 'delete',
        ];
        $config['has_many']['tools'] = [
            'class_name'        => ToolActivation::class,
            'assoc_foreign_key' => 'range_id',
            'order_by'          => 'ORDER BY position',
            'on_delete'         => 'delete',
        ];
        $config['additional_fields']['all_status_groups']['get'] = function ($institute) {
            return Statusgruppen::findAllByRangeId($institute->id, true);
        };

        $config['i18n_fields']['name'] = true;
        $config['registered_callbacks']['after_create'][] = 'setDefaultTools';

        parent::configure($config);
    }

    /**
    * Returns the currently active course or false if none is active.
    *
    * @return Institute object of currently active institute
    * @since 3.0
    */
    public static function findCurrent()
    {
        if (Context::isInstitute()) {
            return Context::get();
        }
        return null;
    }

    /**
     * returns array of instances of Institutes belonging to given faculty
     * @param string $fakultaets_id
     * @return array
     */
    public static function findByFaculty($fakultaets_id)
    {
        return self::findBySQL("fakultaets_id=? AND fakultaets_id <> institut_id ORDER BY Name ASC", [$fakultaets_id]);
    }

    /**
     * returns an array of all institutes ordered by faculties and name
     * @return array
     */
    public static function getInstitutes()
    {
        $db = DBManager::get();
        $result = $db->query("SELECT Institute.Institut_id, Institute.Name, IF(Institute.Institut_id=Institute.fakultaets_id,1,0) AS is_fak " .
                "FROM Institute " .
                    "LEFT JOIN Institute as fakultaet ON (Institute.fakultaets_id = fakultaet.Institut_id) " .
                "ORDER BY fakultaet.Name ASC, is_fak DESC, Institute.Name ASC")->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

    /**
     * returns an array of all institutes to which the given user belongs,
     * ordered by faculties and name. The user role for each institute is included
     *
     * @param string $user_id if omitted, the current user is used
     *
     * @return array
     */
    public static function getMyInstitutes($user_id = NULL)
    {
        global $perm, $user;
        if (!$user_id) {
            $user_id = $user->id;
        }
        $db = DBManager::get();
        if (!$perm->have_perm("admin", $user_id)) {
            $result = $db->query("SELECT user_inst.Institut_id, Institute.Name, Institute.fakultaets_id, IF(user_inst.Institut_id=Institute.fakultaets_id,1,0) AS is_fak, user_inst.inst_perms " .
                "FROM user_inst " .
                    "LEFT JOIN Institute USING (institut_id) " .
                "WHERE (user_id = ".$db->quote($user_id)." " .
                    "AND (inst_perms = 'dozent' OR inst_perms = 'tutor')) " .
                "ORDER BY Institute.Name ASC")->fetchAll(PDO::FETCH_ASSOC);
        } else if (!$perm->have_perm("root", $user_id)) {
            $result = $db->query("SELECT user_inst.Institut_id, Institute.Name, Institute.fakultaets_id, IF(user_inst.Institut_id=Institute.fakultaets_id,1,0) AS is_fak, user_inst.inst_perms " .
                "FROM user_inst " .
                    "LEFT JOIN Institute USING (institut_id) " .
                "WHERE (user_id = ".$db->quote($user_id)." AND inst_perms = 'admin') " .
                "ORDER BY Institute.Name ASC")->fetchAll(PDO::FETCH_ASSOC);
            if ($perm->is_fak_admin($user_id)) {
                foreach($result as $fak) {
                    $combined_result[] = $fak;
                    $institutes = $db->query("SELECT Institut_id, Name, fakultaets_id, 0 as is_fak, 'admin' as inst_perms
                                              FROM Institute WHERE Institut_id <> fakultaets_id AND fakultaets_id = " . $db->quote($fak['Institut_id'])
                                             . " ORDER BY Institute.Name ASC")->fetchAll(PDO::FETCH_ASSOC);
                    $combined_result = array_merge($combined_result, $institutes);
                }
                $result = $combined_result;
            }

        } else {
            $result = $db->query("SELECT Institute.Institut_id, Institute.Name, Institute.fakultaets_id, IF(Institute.Institut_id=Institute.fakultaets_id,1,0) AS is_fak, 'admin' AS inst_perms " .
                "FROM Institute " .
                    "LEFT JOIN Institute as fakultaet ON (Institute.fakultaets_id = fakultaet.Institut_id) " .
                "ORDER BY fakultaet.Name ASC, is_fak DESC, Institute.Name ASC")->fetchAll(PDO::FETCH_ASSOC);
        }
        return $result;
    }

    public function isFaculty()
    {
        return $this->fakultaets_id === $this->institut_id;
    }

    /**
     * Returns the full name of an institute.
     *
     * @param string formatting template name
     * @return string Fullname
     */
    public function getFullname($format = 'default'): string
    {
        $template['type-name'] = '%2$s: %1$s';
        if ($format === 'default' || !isset($template[$format])) {
           $format = 'type-name';
        }
        $type = $GLOBALS['INST_TYPE'][$this['type']]['name'];
        if (!$type) {
            $type = _('Einrichtung');
        }
        $data[0] = $this['name'];
        $data[1] = $type;
        return trim(vsprintf($template[$format], array_map('trim', $data)));
    }

    /**
     * Returns a descriptive text for the range type.
     *
     * @return string
     */
    public function describeRange(): string
    {
        return _('Einrichtung');
    }

    /**
     * Returns a unique identificator for the range type.
     *
     * @return string
     */
    public function getRangeType(): string
    {
        return 'institute';
    }

    /**
     * Returns the id of the current range
     *
     * @return mixed (string|int)
     */
    public function getRangeId()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getConfiguration()
    {
        return InstituteConfig::get($this);
    }

    /**
     * Decides whether the user may access the range.
     *
     * @param string|null $user_id Optional id of a user, defaults to current user
     * @return bool
     * @todo Check permissions
     */
    public function isAccessibleToUser($user_id = null): bool
    {
        return true;
    }

    /**
     * Decides whether the user may edit/alter the range.
     *
     * @param string|null $user_id Optional id of a user, defaults to current user
     * @return bool
     * @todo Check permissions
     */
    public function isEditableByUser($user_id = null): bool
    {
        if ($user_id === null) {
            $user = User::findCurrent();
        } else {
            $user = User::find($user_id);
        }

        if (!$user) {
            return false;
        }

        $member = $this->members->findOneBy('user_id', $user->id);
        return ($member && $member->inst_perms === 'admin')
            || $user->perms === 'root';
    }

    /**
     * @return SemClass
     */
    public function getSemClass()
    {
        return SemClass::getDefaultInstituteClass($this->type);
    }

    /**
     *
     */
    public function setDefaultTools()
    {
       $this->tools = [];
       foreach (array_values($this->getSemClass()->getActivatedModuleObjects()) as $module) {
           PluginManager::getInstance()->setPluginActivated($module->getPluginId(), $this->id, true);
           $this->tools[] = ToolActivation::find([$this->id, $module->getPluginId()]);
       }
    }

    /**
     * @param $name string name of tool / plugin
     * @return bool
     */
    public function isToolActive($name): bool
    {
        $plugin = PluginEngine::getPlugin($name);
        return $plugin && $this->tools->findOneby('plugin_id', $plugin->getPluginId());
    }

    /**
     * returns all activated plugins/modules for this course
     * @return StudipModule[]
     */
    public function getActivatedTools()
    {
        return array_filter($this->tools->getStudipModule());
    }


    /**
     * @see Range::__toString()
     */
    public function __toString() : string
    {
        return $this->getFullName();
    }
}