aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/StudIPPlugin.php
blob: 6642dbdc989ab4ea2dcf8fae935dcb402ce0bcc9 (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
<?php
/**
 * StudIPPlugin.php - generic plugin base class
 *
 * @author    Elmar Ludwig <ludwig@uos.de>
 * @copyright 2009 Authors
 * @license   GPL2 or any later version
 */
abstract class StudIPPlugin
{
    use PluginAssetsTrait;
    use TranslatablePluginTrait;

    /**
     * plugin meta data
     */
    protected $plugin_info;

    /**
     * Plugin manifest
     */
    protected $manifest = null;

    /**
     * plugin constructor
     */
    public function __construct()
    {
        $plugin_manager = PluginManager::getInstance();
        $this->plugin_info = $plugin_manager->getPluginInfo(static::class);
    }

    /**
     * Return the ID of this plugin.
     */
    public function getPluginId()
    {
        return $this->plugin_info['id'];
    }

    public function isEnabled()
    {
        return $this->plugin_info['enabled'];
    }

    /**
     * Return the name of this plugin.
     */
    public function getPluginName()
    {
        return $this->plugin_info['name'];
    }

    /**
     * Return the filesystem path to this plugin.
     */
    public function getPluginPath()
    {
        return "plugins_packages/{$this->plugin_info['path']}";
    }

    /**
     * Return the URL of this plugin. Can be used to refer to resources
     * (images, style sheets, etc.) inside the installed plugin package.
     */
    public function getPluginURL()
    {
        return $GLOBALS['ABSOLUTE_URI_STUDIP'] . $this->getPluginPath();
    }

    /**
     * Return metadata stored in the manifest of this plugin.
     */
    public function getMetadata()
    {
        if ($this->manifest === null) {
            $plugin_manager = PluginManager::getInstance();
            $this->manifest = $plugin_manager->getPluginManifest($this->getPluginPath());
        }
        return $this->manifest;
    }

    /**
     * Returns the description of the plugin either from plugins-table or from the manifest's descriptionlong
     * or description attribute.
     * @return string|null
     */
    public function getPluginDescription()
    {
        $metadata = $this->getMetadata();
        $language = getUserLanguage(User::findCurrent()->id);
        if (!empty($metadata['descriptionlong_' . $language])) {
            return $metadata['descriptionlong_' . $language];
        }
        if (!empty($metadata['description_' . $language])) {
            return $metadata['description_' . $language];
        }
        $description = $metadata['descriptionlong'] ?? $metadata['description'] ?? '';

        if ($this->plugin_info['description_mode'] === 'override_description') {
            return $this->plugin_info['description'];
        } else {
            return $description . $this->plugin_info['description'];
        }
    }

    public function getDescriptionMode()
    {
        return $this->plugin_info['description_mode'];
    }

    public function isHighlighted()
    {
        return $this->plugin_info['highlight_until'] > time();
    }

    public function getHighlightText()
    {
        return $this->plugin_info['highlight_text'];
    }

    /**
     * Returns the version of this plugin as defined in manifest.
     * @return string
     */
    public function getPluginVersion()
    {
        return $this->getMetadata()['version'];
    }

    /**
     * Checks if the plugin is a core-plugin. Returns true if this is the case.
     *
     * @return boolean
     */
    public function isCorePlugin()
    {
       return $this->plugin_info['core'];
    }

    /**
     * Get the activation status of this plugin in the given context.
     * This also checks the plugin default activations.
     *
     * @param $context   context range id (optional)
     * @param $type      type of activation (optional), can be set to 'user'
     *                   in order to point to a homepage plugin
     */
    public function isActivated($context = null, $type = 'sem')
    {
        $plugin_id = $this->getPluginId();
        $plugin_manager = PluginManager::getInstance();

        /*
         * Context can be a Seminar ID or the current user ID if not set.
         * Identification is done via the "username" parameter.
         */
        if (!isset($context)) {
            if ($type === 'user') {
                $context = get_userid(Request::username('username', $GLOBALS['user']->username));
            } else {
                $context = Context::getId();
            }
        }

        if ($type === 'user') {
            $activated = $plugin_manager->isPluginActivatedForUser($plugin_id, $context);
        } else {
            $activated = $plugin_manager->isPluginActivated($plugin_id, $context);
        }

        return $activated;
    }

    /**
     * Returns whether the plugin may be activated in a certain context.
     *
     * @param Range $context
     * @return bool
     */
    public function isActivatableForContext(Range $context)
    {
        return true;
    }

    /**
     * This method dispatches all actions.
     *
     * @param string   part of the dispatch path that was not consumed
     *
     * @return void
     */
    public function perform($unconsumed_path)
    {
        $args = explode('/', $unconsumed_path);
        $action = $args[0] !== '' ? array_shift($args).'_action' : 'show_action';

        if (!method_exists($this, $action)) {
            $dispatcher = app(\Trails\Dispatcher::class);
            $dispatcher->trails_root = $this->getPluginPath();
            $dispatcher->trails_uri = rtrim(PluginEngine::getLink($this, [], null, true), '/');
            $dispatcher->default_controller = 'index';
            $dispatcher->current_plugin = $this;
            try {
                $dispatcher->dispatch($unconsumed_path);
            } catch (Trails\Exceptions\UnknownAction $exception) {
                if (count($args) > 0) {
                    throw $exception;
                } else {
                    throw new Exception(_('unbekannte Plugin-Aktion: ') . $unconsumed_path);
                }
            }
        } else {
            call_user_func_array([$this, $action], $args);
        }
    }

    /**
     * Callback function called after enabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just enabled.
     */
    public static function onEnable($plugin_id)
    {
    }

    /**
     * Callback function called after disabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just disabled.
     */
    public static function onDisable($plugin_id)
    {
    }

    /**
     * Callback function called after enabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just enabled.
     */
    public static function onActivation($plugin_id, $range_id)
    {
    }

    /**
     * Callback function called after disabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just disabled.
     */
    public static function onDeactivation($plugin_id, $range_id)
    {
    }
}