aboutsummaryrefslogtreecommitdiff
path: root/lib/models/SemClass.php
blob: 6eb2029d1915373e0c6f7855e682b571d50dd0e3 (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
373
374
375
<?php

/*
 *  Copyright (c) 2012  Rasmus Fuhse <fuhse@data-quest.de>
 *
 *  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.
 */

/**
 * Class to define and manage attributes of seminar classes (or seminar categories).
 * Usually all sem-classes are stored in a global variable $SEM_CLASS which is
 * an array of SemClass objects.
 *
 * SemClass::getClasses() gets you all seminar classes in an array.
 *
 * You can access the attributes of a sem-class like an associative
 * array with $sem_class['default_read_level']. The uinderlying data is stored
 * in the database in the table sem_classes.
 *
 * If you want to have a name of a sem-class like "Lehre", please use
 * $sem_class['name'] and you will get a fully localized name and not the pure
 * database entry.
 *
 * This class manages also which modules are contained in which course-slots,
 * like "what module is used as a forum in my seminars". In the database stored
 * is the name of the module like "CoreForum" or a classname of a plugin or null
 * if the forum is completely disabled by root for this sem-class. Core-modules
 * can only be used within a standard slot. Plugins may also be used as optional
 * modules not contained in a slot.
 *
 * In the field 'modules' in the database is for each modules stored in a json-string
 * if the module is activatable by the teacher or not and if it is activated as
 * a default. Please use the methods SemClass::isSlotModule, SemClass::getSlotModule,
 * SemClass::isModuleAllowed, SemClass::isModuleMandatory, SemClass::isSlotMandatory
 * or even more simple SemClass::getNavigationForSlot (see documentation there).
 */
class SemClass extends SimpleORMap
{
    static protected $sem_classes = null;

    static protected $studygroup_forbidden_modules = [
        'CoreAdmin',
        'CoreParticipants',
    ];

    /**
     * Configure the database mapping.
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'sem_classes';

        $config['serialized_fields']['modules'] = 'JSONArrayObject';

        $config['has_many']['sem_types'] = [
            'class_name'        => SemType::class,
            'assoc_foreign_key' => 'class',
            'on_delete'         => 'delete'
        ];

        $config['registered_callbacks']['after_store'][] = 'SemClass::refreshClasses';
        $config['registered_callbacks']['after_delete'][] = 'SemClass::refreshClasses';

        parent::configure($config);
    }

    public static function getDefaultSemClass() {
        return self::build([
            'name' => _('Fehlerhafte Seminarklasse'),
            'modules' => [
                'CoreOverview' => ['activated' => 1, 'sticky' => 1],
                'CoreAdmin'    => ['activated' => 1, 'sticky' => 1]
            ],
            'visible' => 1,
            'is_group' => false
        ]);
    }

    /**
     * Generates a dummy SemClass for institutes of this type (as defined in config.inc.php).
     * @param integer $type   institute type
     * @return SemClass
     */
    public static function getDefaultInstituteClass($type)
    {
        global $INST_MODULES;

        // fall back to 'default' if modules are not defined
        $type = isset($INST_MODULES[$type]) ? $type : 'default';

        $data = [
            'name'                => _('Generierte Standardinstitutsklasse'),
            'visible'             => 1,
            'admin'               => 'CoreAdmin',     // always available
            'overview'            => 'CoreOverview'   // always available
        ];
        $slots = [
            'forum'               => 'CoreForum',
            'documents'           => 'CoreDocuments',
            'scm'                 => 'CoreScm',
            'wiki'                => 'CoreWiki',
            'calendar'            => 'CoreCalendar',
            'elearning_interface' => 'CoreElearningInterface',
            'personal'            => 'CorePersonal'
        ];
        $modules = [
            'CoreAdmin'           => ['activated' => 1, 'sticky' => 1],
            'CoreOverview'        => ['activated' => 1, 'sticky' => 1],
        ];

        foreach ($slots as $slot => $module) {
            $data[$slot] = $module;
            $modules[$module] = ['activated' => (int) ($INST_MODULES[$type][$slot] ?? 0), 'sticky' => 0];
        }
        $data['modules'] = $modules;

        return self::build($data);
    }

    /**
     * @param string $module
     * @return false|int
     */
    public function activateModuleInCourses($module)
    {
        $plugin = PluginManager::getInstance()->getPlugin($module);
        if ($plugin) {
            return Course::findEachBySQL(function ($course) use ($plugin) {
                return PluginManager::getInstance()->setPluginActivated($plugin->getPluginId(), $course->id, true);
            },
                "seminare.status IN (?)",
                [array_keys($this->getSemTypes())]);
        } else {
            return false;
        }
    }

    /**
     * @param string $module
     * @return false|int
     */
    public function deActivateModuleInCourses($module)
    {
        $plugin = PluginManager::getInstance()->getPlugin($module);
        if ($plugin) {
            return Course::findEachBySQL(function ($course) use ($plugin) {
                return PluginManager::getInstance()->setPluginActivated($plugin->getPluginId(), $course->id, false);
            },
                "seminare.status IN (?)",
                [array_keys($this->getSemTypes())]);
        } else {
            return false;
        }

    }

    /**
     * Returns the number of seminars of this sem_class in Stud.IP
     * @return integer
     */
    public function countSeminars()
    {
        $sum = 0;
        foreach ($this->sem_types as $sem_type) {
            $sum += $sem_type->countSeminars();
        }
        return $sum;
    }


    /**
     * @param string $modulename
     * @return bool
     */
    public function isModuleForbidden($modulename)
    {
        if (!empty($this->studygroup_mode)) {
            return in_array($modulename, self::$studygroup_forbidden_modules);
        } else {
            return strpos($modulename, 'Studygroup') !== false;
        }
    }

    /**
     * Returns the metadata of a module regarding this sem_class object.
     * @param string $modulename
     * @return array('sticky' => (bool), 'activated' => (bool))
     */
    public function getModuleMetadata($modulename)
    {
        return $this->modules[$modulename];
    }

    /**
     * @return StudipModule[]
     */
    public function getModuleObjects()
    {
        $result = [];
        foreach (array_keys($this->modules->getArrayCopy()) as $module) {
            $plugin = PluginManager::getInstance()->getPlugin($module);
            if ($plugin) {
                $result[$plugin->getPluginId()] = $plugin;
            }
        }
        return $result;
    }

    /**
     * @return string[]
     */
    public function getActivatedModules()
    {
        return array_keys(array_filter($this->modules->getArrayCopy(), function ($meta) {
            return $meta['activated'];
        }));
    }

    /**
     * @return StudipModule[]
     */
    public function getActivatedModuleObjects()
    {
        $result = [];
        foreach ($this->getActivatedModules() as $module) {
            $plugin = PluginManager::getInstance()->getPlugin($module);
            if ($plugin) {
                $result[$plugin->getPluginId()] = $plugin;
            }
        }
        return $result;
    }

    /**
     * @return mixed|object
     */
    public function getAdminModuleObject()
    {
        if ($this->studygroup_mode) {
            $module = 'CoreStudygroupAdmin';
        } else {
            $module = 'CoreAdmin';
        }
        return PluginManager::getInstance()->getPlugin($module);
    }

    /**
     * Returns true if a module is activated on default for this sem_class.
     * @param string $modulename
     * @return boolean
     */
    public function isModuleActivated($modulename)
    {
        return isset($this->modules[$modulename])
            && $this->modules[$modulename]['activated'];
    }

    /**
     * Returns if a module is allowed to be displayed for this sem_class.
     * @param string $modulename
     * @return boolean
     */
    public function isModuleAllowed($modulename)
    {
        return !$this->isModuleForbidden($modulename)
            && (
                empty($this->modules[$modulename])
                || empty($this->modules[$modulename]['sticky'])
                || !empty($this->modules[$modulename]['activated'])
            );
    }

    /**
     * Returns if a module is mandatory for this sem_class.
     * @param string $module
     * @return boolean
     */
    public function isModuleMandatory($module)
    {
        return isset($this->modules[$module])
            && !empty($this->modules[$module]['sticky'])
            && !empty($this->modules[$module]['activated']);
    }

    public function getSemTypes()
    {
        $types = [];
        foreach ($this->sem_types as $type) {
            $types[$type->id] = $type;
        }
        return $types;
    }

    /**
     * Checks if the current sem class is usable for course grouping.
     */
    public function isGroup()
    {
        return $this->is_group;
    }

    /**
     * Checks if any SemClasses exist that provide grouping functionality.
     * @return SimpleCollection
     */
    public static function getGroupClasses()
    {
        return SimpleCollection::createFromArray(self::getClasses())->findBy('is_group', true);
    }

    /**
     * Returns an array of all SemClasses in Stud.IP. Equivalent to global
     * $SEM_CLASS variable. This variable is statically stored in this class.
     * @return SemClass[] of SemClass
     */
    public static function getClasses()
    {
        if (!is_array(self::$sem_classes)) {
            $cache = \Studip\Cache\Factory::getCache();
            $class_array = $cache->read('DB_SEM_CLASSES_ARRAY');

            if (!is_array($class_array)) {
                $class_array = self::findBySQL('1 ORDER BY id');
                $cache->write('DB_SEM_CLASSES_ARRAY', $class_array);
            }

            foreach ($class_array as $sem_class) {
                self::$sem_classes[$sem_class->id] = $sem_class;
            }
        }

        return self::$sem_classes;
    }

    /**
     * Refreshes the internal $sem_classes cache-variable.
     * @return array of SemClass
     */
    public static function refreshClasses()
    {
        \Studip\Cache\Factory::getCache()->expire('DB_SEM_CLASSES_ARRAY');
        self::$sem_classes = null;
        return self::getClasses();
    }

    /**
     * Static method only to keep the translationstrings of the values. It is
     * never used within the system.
     */
    static private function localization()
    {
        _("Lehre");
        _("Forschung");
        _("Organisation");
        _("Community");
        _("Arbeitsgruppen");
        _("importierte Kurse");
        _("Hauptveranstaltungen");

        _("Hier finden Sie alle in Stud.IP registrierten Lehrveranstaltungen");
        _("Verwenden Sie diese Kategorie, um normale Lehrveranstaltungen anzulegen");
        _("Hier finden Sie virtuelle Veranstaltungen zum Thema Forschung an der Universität");
        _("In dieser Kategorie können Sie virtuelle Veranstaltungen für Forschungsprojekte anlegen.");
        _("Hier finden Sie virtuelle Veranstaltungen zu verschiedenen Gremien an der Universität");
        _("Um virtuelle Veranstaltungen für Uni-Gremien anzulegen, verwenden Sie diese Kategorie");
        _("Hier finden Sie virtuelle Veranstaltungen zu unterschiedlichen Themen");
        _("Wenn Sie Veranstaltungen als Diskussiongruppen zu unterschiedlichen Themen anlegen möchten, verwenden Sie diese Kategorie.");
        _("Hier finden Sie verschiedene Arbeitsgruppen an der %s");
        _("Verwenden Sie diese Kategorie, um unterschiedliche Arbeitsgruppen anzulegen.");
        _("Veranstaltungen dieser Kategorie dienen als Gruppierungselement, um die Zusammengehörigkeit von Veranstaltungen anderer Kategorien abzubilden.");
    }
}