aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ToolActivation.php
blob: 8aebda0099b638f492c4868385bbf5f7303e3710 (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
<?php
/**
 * ToolActivation.php
 * model class for table tools_activated
 *
 * 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      André Noack <noack@data-quest.de>
 * @copyright   2021 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 *
 * @property array $id alias for pk
 * @property string $range_id database column
 * @property string $range_type database column
 * @property int $plugin_id database column
 * @property int $position database column
 * @property JSONArrayObject|null $metadata database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property Institute $institute belongs_to Institute
 * @property Course $course belongs_to Course
 */
class ToolActivation extends SimpleORMap
{
    public const VISIBILITY_PERMISSION_STUDENTS = 'students';
    public const VISIBILITY_PERMISSION_TEACHERS = 'teachers';

    protected static function configure($config = [])
    {
        $config['db_table'] = 'tools_activated';

        $config['belongs_to']['institute'] = [
            'class_name'  => Institute::class,
            'foreign_key' => 'range_id',
        ];
        $config['belongs_to']['course'] = [
            'class_name'  => Course::class,
            'foreign_key' => 'range_id',
        ];

        $config['serialized_fields']['metadata'] = JSONArrayObject::class;

        $config['registered_callbacks']['before_create'][] = 'setMaxPosition';

        parent::configure($config);
    }

    public function setSerializedValue($field, $value)
    {
        if ($value === null) {
            $value = [];
        }
        return parent::setSerializedValue($field, $value);
    }

    public function getStudipModule()
    {
        return PluginManager::getInstance()->getPluginById($this->plugin_id);
    }

    public function setStudipModule(StudipModule $module)
    {
        $this->plugin_id = $module->getPluginId();
    }

    public function setMaxPosition()
    {
        $max_position = DBManager::get()->fetchColumn(
            "SELECT MAX(position) FROM tools_activated WHERE range_id = ?",
            [$this->range_id]
        );
        $this->position = $max_position === null ? 0 : $max_position + 1;
    }

    public function getDisplayname()
    {
        if (isset($this->metadata['displayname'])) {
            return $this->metadata['displayname'];
        }
        return $this->getDefaultDisplayname();
    }

    public function getDefaultDisplayname()
    {
        $module = $this->getStudipModule();
        if ($module) {
            $metadata = $module->getMetadata();
            return $metadata['displayname'] ?? $module->getPluginName();
        }
    }

    public function setVisibilityPermission(string $permission)
    {
        if (!in_array($permission, [self::VISIBILITY_PERMISSION_STUDENTS, self::VISIBILITY_PERMISSION_TEACHERS])) {
            throw new InvalidArgumentException("Invalid permission setting {$permission}");
        }

        if ($permission === self::VISIBILITY_PERMISSION_STUDENTS) {
            unset($this->metadata['visibility']);
        } else {
            $this->metadata['visibility'] = 'tutor';
        }
    }

    public function getVisibilityPermission(): string
    {
        if (
            empty($this->metadata['visibility'])
            || $this->metadata['visibility'] !== 'tutor'
        ) {
            return 'nobody';
        }

        return 'tutor';
    }
}