aboutsummaryrefslogtreecommitdiff
path: root/lib/elearning/ConnectedCMS.class.php
blob: 3dd3bfba2620c14ecc931c62ec749994f895b43f (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
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
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
/**
* main-class for connected systems
*
* This class contains the main methods of the elearning-interface to connect content-management-systems.
*
* @author   Arne Schröder <schroeder@data-quest.de>
* @access   public
* @modulegroup  elearning_interface_modules
* @module       ConnectedCMS
* @package  ELearning-Interface
*/
class ConnectedCMS
{
    public $title;

    public $is_active;
    public $cms_type;
    public $name = null;
    public $ABSOLUTE_PATH_ELEARNINGMODULES = null;
    public $ABSOLUTE_PATH_SOAP = null;
    public $RELATIVE_PATH_DB_CLASSES = false;
    public $CLASS_PREFIX = null;
    public $auth_necessary = null;
    public $USER_AUTO_CREATE = null;
    public $USER_PREFIX = null;
    public $target_file = null;
    public $logo_file = null;
    public $db_classes;
    public $soap_data = null;
    public $soap_client;
    public $types = null;
    public $roles = null;

    public $db;
    public $db_class;
    public $link;
    public $user;
    public $permissions;
    public $content_module;

    /**
    * constructor
    *
    * init class. don't call directly but by extending class ("new Ilias3ConnectedCMS($cms)" for example), except for basic administration
    * @access
    * @param string $cms system-type
    */
    public function __construct($cms = "")
    {
        $this->cms_type = $cms;
        $this->is_active = (bool) Config::get()->getValue("ELEARNING_INTERFACE_{$cms}_ACTIVE");

        if ($cms) {
            $this->init($cms);
        }
    }

    /**
    * init settings
    *
    * gets settings from config-array and initializes db
    * @access private
    * @param string $cms system-type
    */
    public function init($cms)
    {
        global $ELEARNING_INTERFACE_MODULES;

        $this->name = $ELEARNING_INTERFACE_MODULES[$cms]['name'] ?? null;
        $this->ABSOLUTE_PATH_ELEARNINGMODULES = $ELEARNING_INTERFACE_MODULES[$cms]["ABSOLUTE_PATH_ELEARNINGMODULES"];
        $this->ABSOLUTE_PATH_SOAP = $ELEARNING_INTERFACE_MODULES[$cms]["ABSOLUTE_PATH_SOAP"];
        if (isset($ELEARNING_INTERFACE_MODULES[$cms]["RELATIVE_PATH_DB_CLASSES"])) {
            $this->RELATIVE_PATH_DB_CLASSES = $ELEARNING_INTERFACE_MODULES[$cms]["RELATIVE_PATH_DB_CLASSES"];
            $this->db_classes = $ELEARNING_INTERFACE_MODULES[$cms]["db_classes"];
        } else {
            $this->RELATIVE_PATH_DB_CLASSES = false;
        }
        $this->CLASS_PREFIX = $ELEARNING_INTERFACE_MODULES[$cms]['CLASS_PREFIX'];
        $this->auth_necessary = $ELEARNING_INTERFACE_MODULES[$cms]['auth_necessary'];
        $this->USER_AUTO_CREATE = $ELEARNING_INTERFACE_MODULES[$cms]['USER_AUTO_CREATE'] ?? null;
        $this->USER_PREFIX = $ELEARNING_INTERFACE_MODULES[$cms]['USER_PREFIX'] ?? null;
        $this->target_file = $ELEARNING_INTERFACE_MODULES[$cms]['target_file'] ?? null;
        $this->logo_file = $ELEARNING_INTERFACE_MODULES[$cms]['logo_file'] ?? null;
        $this->soap_data = $ELEARNING_INTERFACE_MODULES[$cms]['soap_data'] ?? null;
        $this->types = $ELEARNING_INTERFACE_MODULES[$cms]['types'] ?? null;
        $this->roles = $ELEARNING_INTERFACE_MODULES[$cms]['roles'] ?? null;
    }

    /**
    * init subclasses
    *
    * loads classes for user-functions
    * @access public
    */
    public function initSubclasses()
    {
        if ($this->auth_necessary) {
            require_once $this->CLASS_PREFIX . "ConnectedUser.class.php";
            $classname = $this->CLASS_PREFIX . "ConnectedUser";
            $this->user = new $classname($this->cms_type);

            require_once $this->CLASS_PREFIX  . "ConnectedPermissions.class.php";
            $classname = $this->CLASS_PREFIX  . "ConnectedPermissions";
            $this->permissions = new $classname($this->cms_type);
        }
        require_once $this->CLASS_PREFIX . "ConnectedLink.class.php";
        $classname = $this->CLASS_PREFIX . "ConnectedLink";
        $this->link = new $classname($this->cms_type);
    }

    /**
    * get connection status
    *
    * checks settings
    * @access public
    * @param string $cms system-type
    * @return array messages
    */
    public function getConnectionStatus($cms = "")
    {
        $msg = [
            'path' => [],
        ];

        if ($this->cms_type == "") {
            $this->init($cms);
        }

        // check connection to CMS
        if (!$this->auth_necessary) {
            $msg['auth'] = [
                'info' => _('Eine Authentifizierung ist für dieses System nicht vorgesehen.')
            ];
        }

        // check for SOAP-Interface
        if (in_array($this->CLASS_PREFIX, ['Ilias3','Ilias4','Ilias5'])) {
            $ch = curl_init($this->ABSOLUTE_PATH_ELEARNINGMODULES . 'login.php');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_exec($ch);

            if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) !== 200) {
                $msg['path']['error'] = sprintf(
                    _('Die Verbindung zum System "%s" konnte nicht hergestellt werden. Der Pfad "%s" ist ungültig.'),
                    $this->name,
                    $this->ABSOLUTE_PATH_ELEARNINGMODULES
                );

            } else {
                $msg['path']['info'] = sprintf(
                    _('Die %s-Installation wurde gefunden.'),
                    $this->name
                );
            }

            $msg['soap'] = [];
            if (!Config::get()->SOAP_ENABLE) {
                $msg['soap']['error'] = _('Das Stud.IP-Modul für die SOAP-Schnittstelle ist nicht aktiviert. Ändern Sie den entsprechenden Eintrag in der Konfigurationsdatei "local.inc".');
            } elseif (!is_array($this->soap_data)) {
                $msg['soap']['error'] = _('Die SOAP-Verbindungsdaten sind für dieses System nicht gesetzt. Ergänzen Sie die Einstellungen für dieses Systems um den Eintrag "soap_data" in der Konfigurationsdatei "local.inc".');
            } else {
                $this->soap_client = new StudipSoapClient($this->ABSOLUTE_PATH_SOAP);
                $msg['soap']['info'] = _('Das SOAP-Modul ist aktiv.');
            }
        } else {
            $file = fopen($this->ABSOLUTE_PATH_ELEARNINGMODULES, 'r');
            if ($file === false) {
                $msg['path']['error'] = sprintf(
                    _('Die Verbindung zum System "%s" konnte nicht hergestellt werden. Der Pfad "%s" ist ungültig.'),
                    $this->name,
                    $this->ABSOLUTE_PATH_ELEARNINGMODULES
                );
            } else {
                fclose($file);
                $msg['path']['info'] = sprintf(
                    _("Die %s-Installation wurde gefunden."),
                    $this->name
                );

                // check if target-file exists
                $msg['auth'] = [];

                $file = fopen($this->ABSOLUTE_PATH_ELEARNINGMODULES . $this->target_file, 'r');
                if ($file === false) {
                    $msg['auth']['error'] = sprintf(
                        _('Die Zieldatei "%s" liegt nicht im Hauptverzeichnis der %s-Installation.'),
                        $this->target_file,
                        $this->name
                    );
                } else {
                    fclose($file);
                    $msg['auth']['info'] = _('Die Zieldatei ist vorhanden.');
                }
            }
        }

        $el_path = $GLOBALS['STUDIP_BASE_PATH'] . '/lib/elearning';
        // check if needed classes exist
        $files = [
            'class_link'    => "{$el_path}/{$this->CLASS_PREFIX}ConnectedLink.class.php",
            'class_content' => "{$el_path}/{$this->CLASS_PREFIX}ContentModule.class.php",
            'class_cms'     => "{$el_path}/{$this->CLASS_PREFIX}ConnectedCMS.class.php",
        ];

        if ($this->auth_necessary) {
            $files['class_user'] = "{$el_path}/{$this->CLASS_PREFIX}ConnectedUser.class.php";
            $files['class_perm'] = "{$el_path}/{$this->CLASS_PREFIX}ConnectedPermissions.class.php";
        }

        $errors = 0;
        foreach ($files as $index => $file) {
            if (!file_exists($file)) {
                $msg[$index] = [
                    'error' => sprintf(_('Die Datei "%s" existiert nicht.'), $file),
                ];
                $errors += 1;
            }
        }

        $msg['classes'] = [];
        if ($errors === 0) {
            require_once $files['class_cms'];
            $msg['classes']['info'] = sprintf(
                _('Die Klassen der Schnittstelle zum System "%s" wurden geladen.'),
                $this->name
            );
        } else {
            $msg['classes']['error'] = sprintf(
                _('Die Klassen der Schnittstelle zum System "%s" wurden nicht geladen.'),
                $this->name
            );
        }

        return $msg;
    }

    /**
    * get preferences
    *
    * shows additional settings. can be overwritten by subclass.
    * @access public
    */
    public function getPreferences()
    {
        if ($this->types != "")
        {
            echo "<b>" . _("Angebundene Lernmodul-Typen: ") . "</b>";
            echo "<br>\n";
            foreach($this->types as $key => $type)
                echo Icon::create($type["icon"], Icon::ROLE_INACTIVE)->asImg() . $type["name"] . " ($key)<br>\n";
            echo "<br>\n";
        }

        if ($this->db_classes != "")
        {
            echo "<b>" . _("Verwendete DB-Zugriffs-Klassen: ") . "</b>";
            echo "<br>\n";
            foreach($this->db_classes as $key => $type) {
                echo $type["file"] . " ($key)<br>\n";
            }
            echo "<br>\n";
        }
    }

    /**
    * create new instance of subclass content-module with given values
    *
    * creates new instance of subclass content-module with given values
    * @access public
    * @param array $data module-data
    * @param boolean $is_connected is module connected to seminar?
    */
    public function setContentModule($data, $is_connected = false)
    {
        global $current_module;
        $current_module = $data["ref_id"];

        require_once($this->CLASS_PREFIX . "ContentModule.class.php");
        $classname = $this->CLASS_PREFIX  . "ContentModule";

        $this->content_module[$current_module] = new  $classname("", $data["type"], $this->cms_type);
        $this->content_module[$current_module]->setId($data["ref_id"]);
        $this->content_module[$current_module]->setTitle($data["title"]);
        $this->content_module[$current_module]->setDescription($data["description"]);
        $this->content_module[$current_module]->setConnectionType($is_connected);
    }

    /**
    * create new instance of subclass content-module
    *
    * creates new instance of subclass content-module
    * @access public
    * @param string $module_id module-id
    * @param string $module_type module-type
    * @param boolean $is_connected is module connected to seminar?
    */
    public function newContentModule($module_id, $module_type, $is_connected = false)
    {
        global $current_module;
        $current_module = $module_id;

        require_once($this->CLASS_PREFIX . "ContentModule.class.php");
        $classname = $this->CLASS_PREFIX  . "ContentModule";

        if ($is_connected == false)
        {
            $this->content_module[$module_id] = new  $classname("", $module_type, $this->cms_type);
            $this->content_module[$module_id]->setId($module_id);
        }
        else
        {
            $this->content_module[$module_id] = new  $classname($module_id, $module_type, $this->cms_type);
        }

        $this->content_module[$module_id]->setConnectionType($is_connected);
    }

    /**
    * get name of cms
    *
    * returns name of cms
    * @access public
    * @return string name
    */
    public function getName()
    {
        return $this->name;
    }

    /**
    * get type of cms
    *
    * returns type of cms
    * @access public
    * @return string type
    */
    public function getCMSType()
    {
        return $this->cms_type;
    }

    /**
    * get path of cms
    *
    * returns path of cms
    * @access public
    * @return string path
    */
    public function getAbsolutePath()
    {
        return $this->ABSOLUTE_PATH_ELEARNINGMODULES;
    }

    /**
    * get target file of cms
    *
    * returns target file of cms
    * @access public
    * @return string target file
    */
    public function getTargetFile()
    {
        return $this->target_file;
    }

    /**
    * get class prefix
    *
    * returns class prefix
    * @access public
    * @return string class prefix
    */
    public function getClassPrefix()
    {
        return $this->CLASS_PREFIX;
    }

    /**
    * get authentification-setting
    *
    * returns true, if authentification is necessary
    * @access public
    * @return boolean authentification-setting
    */
    public function isAuthNecessary()
    {
        return $this->auth_necessary;
    }

    /**
    * get active-setting
    *
    * returns true, if cms is active
    * @access public
    * @return boolean active-setting
    function isActive($cms = "")
    {
        return $this->is_active;
    }
    */

    /**
    * get user prefix
    *
    * returns user prefix
    * @access public
    * @return string user prefix
    */
    public function getUserPrefix()
    {
        return $this->USER_PREFIX;
    }

    /**
    * get logo-image
    *
    * returns logo-image
    * @access public
    * @return string logo-image
    */
    public function getLogo()
    {
        return "<img src=\"" . $this->logo_file . "\">";
    }

    /**
    * get user modules
    *
    * dummy-method. returns false. must be overwritten by subclass.
    * @access public
    * @return boolean returns false
    */
    public function getUserContentModules()
    {
        return false;
    }

    /**
    * search modules
    *
    * dummy-method. returns false. must be overwritten by subclass.
    * @access public
    * @return boolean returns false
    */
    public function searchContentModules($key)
    {
        return false;
    }

    /**
    * dummy-method. can be overwritten by subclass.
    */
    public function terminate()
    {
    }

    public function deleteConnectedModules($object_id){
        return ObjectConnections::DeleteAllConnections($object_id, $this->cms_type);
    }
}