aboutsummaryrefslogtreecommitdiff
path: root/lib/ilias_interface/IliasObjectConnections.php
blob: 0c6a8a9721f8247cda8748b30075f193bcbdea61 (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
<?php
# Lifter003: TEST
# Lifter007: TODO
# Lifter010: TODO
/**
* class to handle object connections
*
* This class contains methods to handle connections between stud.ip-objects and external content.
*
* @author   Arne Schröder <schroeder@data-quest.de>
* @access   public
* @modulegroup  ilias_interface_modules
* @module       IliasObjectConnections
* @package  Ilias-Interface
*/
class IliasObjectConnections
{
    public $id;
    public $object_connections;
    /**
    * constructor
    *
    * init class.
    * @access public
    * @param string $object_id object-id
    */
    function __construct($object_id = "")
    {
        $this->id = $object_id;
        if ($object_id != "")
            $this->readData();
    }

    /**
    * read object connections
    *
    * gets object connections from database
    * @access public
    */
    function readData()
    {
        $this->object_connections = [];

        $query = "SELECT system_type, module_type, module_id, chdate
                  FROM object_contentmodules
                  WHERE object_id = ?
                  ORDER BY chdate DESC";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([$this->id]);

        $module_count = 0;
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
            $module_count += 1;
            $d_system_type = $row['system_type'];
            $d_module_type = $row['module_type'];
            $d_module_id   = $row['module_id'];

            $reference = $d_system_type . '_' . $d_module_type . '_' . $d_module_id;
            $reference = $d_module_id;
            $this->object_connections[$d_system_type][$reference]['index']    = $d_system_type;
            $this->object_connections[$d_system_type][$reference]['type']   = $d_module_type;
            $this->object_connections[$d_system_type][$reference]['id']     = $d_module_id;
            $this->object_connections[$d_system_type][$reference]['chdate'] = $row['chdate'];
        }

        if ($module_count == 0) {
            $this->object_connections = false;
        }
    }

    /**
    * get object connections
    *
    * returns object connections
    * @access public
    * @return array object connections
    */
    function getConnections()
    {
        return $this->object_connections;
    }

    /**
    * get connection-status
    *
    * returns true, if object has connections
    * @access public
    * @return boolean connection-status
    */
    function isConnected($ilias_index = '')
    {
        if ($ilias_index) {
            return (boolean) $this->object_connections[$ilias_index];
        } else {
            return (boolean) $this->object_connections;
        }
    }

    /**
     * get connection-status
     *
     * returns true, if object has connections
     * @access public
     * @param string $object_id object-id (optional)
     * @return boolean connection-status
     */
    public static function isObjectConnected($index, $object_id)
    {
        if ($object_id && $index) {
            $query = "SELECT 1 FROM object_contentmodules WHERE object_id = ? AND system_type = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([$object_id, $index]);
            return (bool) $statement->fetchColumn();
        }

        return false;
    }

    /**
     * get connection-status
     *
     * returns true, if course has connections to ILIAS courses
     * @access public
     * @param string $object_id object-id (optional)
     * @return boolean connection-status
     */
    public static function isCourseConnected($object_id)
    {
        if ($object_id) {
            $query = "SELECT 1 FROM object_contentmodules WHERE object_id = ? AND module_type = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([$object_id, 'crs']);
            return (bool)$statement->fetchColumn();
        } else {
            return false;
        }
    }

    /**
    * get module-id
    *
    * returns module-id of given connection
    * @access public
    * @param string $connection_object_id object-id
    * @param string $connection_module_type module-type
    * @param string $connection_cms system-type
    * @return string module-id
    */
    public static function getConnectionModuleId($connection_object_id, $connection_module_type, $connection_cms)
    {
        $query = "SELECT module_id
                  FROM object_contentmodules
                  WHERE object_id = ? AND system_type = ? AND module_type = ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $connection_object_id,
            $connection_cms,
            $connection_module_type
        ]);
        return $statement->fetchColumn() ?: false;
    }

    /**
    * set connection
    *
    * sets connection with object
    * @access public
    * @param string $connection_object_id object-id
    * @param string $connection_module_id module-id
    * @param string $connection_module_type module-type
    * @param string $connection_cms system-type
    * @return boolean successful
    */
    public static function setConnection($connection_object_id, $connection_module_id, $connection_module_type, $connection_cms)
    {
        $query = "SELECT 1
                  FROM object_contentmodules
                  WHERE object_id = ? AND module_id = ? AND system_type = ?
                    AND module_type = ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $connection_object_id,
            $connection_module_id,
            $connection_cms,
            $connection_module_type
        ]);
        $check = $statement->fetchColumn();

        if ($check) {
            $query = "UPDATE object_contentmodules
                      SET module_type = ?, chdate = UNIX_TIMESTAMP()
                      WHERE object_id = ? AND module_id = ? AND system_type = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                $connection_module_type,
                $connection_object_id,
                $connection_module_id,
                $connection_cms
            ]);
        } else {
            $query = "INSERT INTO object_contentmodules
                        (object_id, module_id, system_type, module_type, mkdate, chdate)
                      VALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                $connection_object_id,
                $connection_module_id,
                $connection_cms,
                $connection_module_type
            ]);
        }
        return true;
    }

    /**
    * unset connection
    *
    * deletes connection with object
    * @access public
    * @param string $connection_object_id object-id
    * @param string $connection_module_id module-id
    * @param string $connection_module_type module-type
    * @param string $connection_cms system-type
    * @return boolean successful
    */
    public static function unsetConnection($connection_object_id, $connection_module_id, $connection_module_type, $connection_cms)
    {
        $query = "SELECT 1
                  FROM object_contentmodules
                  WHERE object_id = ? AND module_id = ? AND system_type = ?
                    AND module_type = ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $connection_object_id,
            $connection_module_id,
            $connection_cms,
            $connection_module_type
        ]);
        $check = $statement->fetchColumn();


        if ($check) {
            $query = "DELETE FROM object_contentmodules
                      WHERE object_id = ? AND module_id = ? AND system_type = ?
                        AND module_type = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                $connection_object_id,
                $connection_module_id,
                $connection_cms,
                $connection_module_type
            ]);
            return true;
        }
        return false;
    }

    public static function GetConnectedSystems($object_id)
    {
        $query = "SELECT DISTINCT system_type
                  FROM object_contentmodules
                  WHERE object_id = ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([$object_id]);
        return $statement->fetchAll(PDO::FETCH_COLUMN);
    }

    public static function DeleteAllConnections($object_id, $cms_type)
    {
        $query = "DELETE FROM object_contentmodules
                  WHERE object_id = ? AND system_type = ?";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([$object_id, $cms_type]);
        return $statement->rowCount();
    }

    /**
     * @param Course $course
     * @return int
     */
    public static function importIliasResultsForCourse(Course $course): int
    {
        $connected_ilias = [];
        $students = new SimpleCollection($course->getMembersWithStatus('autor'));
        $num = 0;
        foreach (Grading\Definition::findBySQL("course_id = ? AND tool='ILIAS'", [$course->id]) as $definition) {
            [$index, $module_id, $import_type] = explode('-', $definition->item);
            if (!isset($connected_ilias[$index])) {
                $connected_ilias[$index] = new ConnectedIlias($index);
            }
            $test_result = $connected_ilias[$index]->soap_client->getTestResults($module_id);
            foreach ($test_result as $result) {
                $ilias_user = $connected_ilias[$index]->getConnectedUser($result['user_id']);
                if ($ilias_user) {
                    $member = $students->findOneBy('user_id', $ilias_user->getStudipId());
                    if ($member) {
                        $grade = Grading\Instance::import([
                                'definition_id' => $definition->id,
                                'user_id'       => $member->user_id,
                                'rawgrade'      => $import_type & 1 && $result['maximum_points'] ? $result['received_points'] / $result['maximum_points'] : 0,
                                'passed'        => $import_type & 2 ? $result['passed'] : 0
                            ]
                        );
                        $num += $grade->store();
                    }
                }
            }
        }
        return $num;
    }
}