Newer
Older
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
<?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 implements ArrayAccess
{
protected $data = [];
static protected $studygroup_forbidden_modules = [
'CoreAdmin',
'CoreParticipants',
'CoreSchedule'
static protected $sem_classes = null;
static public function getDefaultSemClass() {
$data = [
'name' => "Fehlerhafte Seminarklasse!",
'modules' => '{"CoreOverview":{"activated":1,"sticky":1},"CoreAdmin":{"activated":1,"sticky":1}}',
'visible' => 1,
'is_group' => false
];
return new SemClass($data);
}
/**
* Generates a dummy SemClass for institutes of this type (as defined in config.inc.php).
* @param integer $type institute type
* @return SemClass
*/
static public 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,
'overview' => 'CoreOverview', // always available
'admin' => 'CoreAdmin' // always available
];
$slots = [
'documents' => 'CoreDocuments',
'scm' => 'CoreScm',
'wiki' => 'CoreWiki',
'calendar' => 'CoreCalendar',
'elearning_interface' => 'CoreElearningInterface',
'personal' => 'CorePersonal'
];
$modules = [
'CoreOverview' => ['activated' => 1, 'sticky' => 1],
'CoreAdmin' => ['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'] = json_encode($modules);
return new SemClass($data);
}
/**
* Constructor can be set with integer of sem_class_id or an array of
* the old $SEM_CLASS style.
* @param integer | array $data
*/
public function __construct($data)
{
$db = DBManager::get();
if (is_int($data)) {
$statement = $db->prepare("SELECT * FROM sem_classes WHERE id = :id ");
$statement->execute(['id' => $data]);
$this->data = $statement->fetch(PDO::FETCH_ASSOC);
} else {
$this->data = $data;
}
if (!empty($this->data['modules'])) {
$this->data['modules'] = self::object2array(json_decode($this->data['modules']));
} else {
$this->data['modules'] = [];
}
if (!empty($this->data['studygroup_mode'])) {
if (!isset($this->data['modules']['CoreStudygroupAdmin'])) {
$this->data['modules']['CoreStudygroupAdmin'] = ['activated' => 1, 'sticky' => 1];
}
} else {
if (!isset($this->data['modules']['CoreAdmin'])) {
$this->data['modules']['CoreAdmin'] = ['activated' => 1, 'sticky' => 1];
}
}
foreach (array_keys($this->data['modules']) as $modulename) {
if ($this->isModuleForbidden($modulename)) {
unset($this->data['modules'][$modulename]);
}
}
}
/**
* @param string $module
* @return false|int
*/
public function activateModuleInCourses($module)
{
$plugin = PluginManager::getInstance()->getPlugin($module);
if ($plugin) {
return Course::findEachBySQL(function ($course) use ($plugin) {

André Noack
committed
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) {

André Noack
committed
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()
{
$db = DBManager::get();
$sum = 0;
foreach ($GLOBALS['SEM_TYPE'] as $sem_type) {
if ($sem_type['class'] == $this->data['id']) {
$sum += $sem_type->countSeminars();
}
}
return $sum;
}
/**
* @param string $modulename
* @return bool
*/
public function isModuleForbidden($modulename)
{
return in_array($modulename, self::$studygroup_forbidden_modules);
} else {
return strpos($modulename, 'Studygroup') !== false;
}
}
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
/**
* 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->data['modules'][$modulename];
}
/**
* Sets the metadata for each module at once.
* @param array $module_array: array($module_name => array('sticky' => (bool), 'activated' => (bool)), ...)
*/
public function setModules($module_array)
{
$this->data['modules'] = $module_array;
}
/**
* Returns all metadata of the modules at once.
* @return array: array($module_name => array('sticky' => (bool), 'activated' => (bool)), ...)
*/
public function getModules()
{
return $this->data['modules'];
}
/**
* @return StudipModule[]
*/
public function getModuleObjects()
{
$result = [];
foreach (array_keys($this->getModules()) 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->data['modules'], 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()
{
$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)
{

André Noack
committed
return isset($this->data['modules'][$modulename])
&& $this->data['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)
{

André Noack
committed
return !$this->isModuleForbidden($modulename)
&& (empty($this->data['modules'][$modulename])
|| !$this->data['modules'][$modulename]['sticky']

André Noack
committed
|| $this->data['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->data['modules'][$module])
&& $this->data['modules'][$module]['sticky']
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
&& $this->data['modules'][$module]['activated'];
}
public function getSemTypes()
{
$types = [];
foreach (SemType::getTypes() as $id => $type) {
if ($type['class'] == $this->data['id']) {
$types[$id] = $type;
}
}
return $types;
}
/**
* Checks if the current sem class is usable for course grouping.
*/
public function isGroup()
{
return $this->data['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);
}
/**
* stores all data in the database
* @return boolean success
*/
public function store()
{
$db = DBManager::get();
$statement = $db->prepare(
"UPDATE sem_classes " .
"SET name = :name, " .
"description = :description, " .
"create_description = :create_description, " .
"studygroup_mode = :studygroup_mode, " .
"only_inst_user = :only_inst_user, " .
"default_read_level = :default_read_level, " .
"default_write_level = :default_write_level, " .
"bereiche = :bereiche, " .
"module = :module, " .
"show_browse = :show_browse, " .
"write_access_nobody = :write_access_nobody, " .
"topic_create_autor = :topic_create_autor, " .
"visible = :visible, " .
"course_creation_forbidden = :course_creation_forbidden, " .
"modules = :modules, " .
"title_dozent = :title_dozent, " .
"title_dozent_plural = :title_dozent_plural, " .
"title_tutor = :title_tutor, " .
"title_tutor_plural = :title_tutor_plural, " .
"title_autor = :title_autor, " .
"title_autor_plural = :title_autor_plural, " .
"admission_prelim_default = :admission_prelim_default, " .
"admission_type_default = :admission_type_default, " .
"show_raumzeit = :show_raumzeit, " .
"is_group = :is_group, " .
"chdate = UNIX_TIMESTAMP() " .
"WHERE id = :id ".
"");
StudipCacheFactory::getCache()->expire('DB_SEM_CLASSES_ARRAY');
return $statement->execute([
'id' => $this->data['id'],
'name' => $this->data['name'],
'description' => $this->data['description'],
'create_description' => $this->data['create_description'],
'studygroup_mode' => (int) $this->data['studygroup_mode'],
'only_inst_user' => (int) $this->data['only_inst_user'],
'default_read_level' => (int) $this->data['default_read_level'],
'default_write_level' => (int) $this->data['default_write_level'],
'bereiche' => (int) $this->data['bereiche'],
'module' => (int) $this->data['module'],
'show_browse' => (int) $this->data['show_browse'],
'write_access_nobody' => (int) $this->data['write_access_nobody'],
'topic_create_autor' => (int) $this->data['topic_create_autor'],
'visible' => (int) $this->data['visible'],
'course_creation_forbidden' => (int) $this->data['course_creation_forbidden'],
'modules' => json_encode((object) $this->data['modules']),
'title_dozent' => $this->data['title_dozent']
? $this->data['title_dozent']
: null,
'title_dozent_plural' => $this->data['title_dozent_plural']
? $this->data['title_dozent_plural']
: null,
'title_tutor' => $this->data['title_tutor']
? $this->data['title_tutor']
: null,
'title_tutor_plural' => $this->data['title_tutor_plural']
? $this->data['title_tutor_plural']
: null,
'title_autor' => $this->data['title_autor']
? $this->data['title_autor']
: null,
'title_autor_plural' => $this->data['title_autor_plural']
? $this->data['title_autor_plural']
: null,
'admission_prelim_default' => (int)$this->data['admission_prelim_default'],
'admission_type_default' => (int)$this->data['admission_type_default'],
'show_raumzeit' => (int) $this->data['show_raumzeit'],
'is_group' => (int) $this->data['is_group']
]);
}
/**
* Deletes the sem_class-object and all its sem_types. Will only delete,
* if there are no seminars in this sem_class.
* Remember to refresh the global $SEM_CLASS and $SEM_TYPE array.
* @return boolean : success of deletion
*/
public function delete()
{
if ($this->countSeminars() === 0) {
foreach ($GLOBALS['SEM_TYPE'] as $sem_type) {
if ($sem_type['class'] == $this->data['id']) {
$sem_type->delete();
}
}
$GLOBALS['SEM_TYPE'] = SemType::getTypes();
$db = DBManager::get();
$statement = $db->prepare("
DELETE FROM sem_classes
WHERE id = :id
");
StudipCacheFactory::getCache()->expire('DB_SEM_CLASSES_ARRAY');
return $statement->execute([
'id' => $this->data['id']
]);
} else {
return false;
}
}
/**
* Sets an attribute of sem_class->data
* @param string $offset
* @param mixed $value
*/
public function set($offset, $value)
{
$this->data[$offset] = $value;
}
/***************************************************************************
* ArrayAccess methods *
***************************************************************************/
/**
* deprecated, does nothing, should not be used
* @param string $offset
* @param mixed $value
*
* @todo Add void return type when Stud.IP requires PHP8 minimal
public function offsetSet($offset, $value)
{
}
/**
* Compatibility function with old $SEM_CLASS variable for plugins. Maps the
* new array-structure to the old boolean values.
* @param integer $offset: name of attribute
* @return boolean|(localized)string
*
* @todo Add mixed return type when Stud.IP requires PHP8 minimal
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
public function offsetGet($offset)
{
switch ($offset) {
case "name":
return gettext($this->data['name']);
case "only_inst_user":
return (bool) $this->data['only_inst_user'];
case "bereiche":
return (bool) $this->data['bereiche'];
case "show_browse":
return (bool) $this->data['show_browse'];
case "write_access_nobody":
return (bool) $this->data['write_access_nobody'];
case "topic_create_autor":
return (bool) $this->data['topic_create_autor'];
case "visible":
return (bool) $this->data['visible'];
case "studygroup_mode":
return (bool) $this->data['studygroup_mode'];
case "admission_prelim_default":
return (int) $this->data['admission_prelim_default'];
case "admission_type_default":
return (int) $this->data['admission_type_default'];
case "is_group":
return (bool) $this->data['is_group'];
}
//ansonsten
return $this->data[$offset];
}
/**
* ArrayAccess method to check if an attribute exists.
* @param int $offset
* @return bool
*
* @todo Add bool return type when Stud.IP requires PHP8 minimal
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
/**
* deprecated, does nothing, should not be used
* @param string $offset
*
* @todo Add void return type when Stud.IP requires PHP8 minimal
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
public function offsetUnset($offset)
{
}
/***************************************************************************
* static methods *
***************************************************************************/
/**
* 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
*/
static public function getClasses()
{
if (!is_array(self::$sem_classes)) {
$db = DBManager::get();
self::$sem_classes = [];
$cache = StudipCacheFactory::getCache();
$class_array = unserialize($cache->read('DB_SEM_CLASSES_ARRAY'));
if (!$class_array) {
try {
$statement = $db->prepare(
"SELECT * FROM sem_classes ORDER BY id ASC "
);
$statement->execute();
$class_array = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($class_array) {
$cache = StudipCacheFactory::getCache();
$cache->write('DB_SEM_CLASSES_ARRAY', serialize($class_array));
}
} catch (PDOException $e) {
//for use without or before migration 92
$class_array = $GLOBALS['SEM_CLASS_OLD_VAR'];
if (is_array($class_array)) {
ksort($class_array);
foreach ($class_array as $id => $class) {
self::$sem_classes[$id] = new SemClass($class);
}
} else {
self::$sem_classes[1] = self::getDefaultSemClass();
}
}
}
foreach ($class_array as $sem_class) {
self::$sem_classes[$sem_class['id']] = new SemClass($sem_class);
}
}
return self::$sem_classes;
}
/**
* Refreshes the internal $sem_classes cache-variable.
* @return array of SemClass
*/
static public function refreshClasses()
{
StudipCacheFactory::getCache()->expire('DB_SEM_CLASSES_ARRAY');
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
self::$sem_classes = null;
return self::getClasses();
}
/**
* Static method to recursively transform an object into an associative array.
* @param mixed $obj: should be of class StdClass
* @return array
*/
static public function object2array($obj)
{
$arr_raw = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($arr_raw as $key => $val) {
$val = (is_array($val) || is_object($val)) ? self::object2array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
/**
* 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.");
}
}