aboutsummaryrefslogtreecommitdiff
path: root/lib/models/MvvFile.php
blob: 44d908e349cc80bc6048c1e1c779d75c1ee7cc91 (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php
/**
 * MvvFile.php
 *
 * 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      Timo Hartge <hartge@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.5
 *
 * @property string $id alias column for mvvfile_id
 * @property string $mvvfile_id database column
 * @property int|null $year database column
 * @property string|null $type database column
 * @property string|null $category database column
 * @property string|null $tags database column
 * @property int|null $extern_visible database column
 * @property string|null $author_id database column
 * @property string|null $editor_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property SimpleORMapCollection|MvvFileFileref[] $file_refs has_many MvvFileFileref
 * @property SimpleORMapCollection|MvvFileRange[] $ranges has_many MvvFileRange
 * @property-read mixed $count_relations additional field
 */

class MvvFile extends ModuleManagementModel
{
    /**
     * @param array $config
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'mvv_files';

        $config['has_many']['file_refs'] = [
            'class_name'  => MvvFileFileref::class,
            'foreign_key' => 'mvvfile_id'
        ];
        $config['has_many']['ranges'] = [
            'class_name'  => MvvFileRange::class,
            'foreign_key' => 'mvvfile_id'
        ];
        $config['additional_fields']['count_relations']['get'] = 'countRelations';

        parent::configure($config);
    }

    /**
     * Finds all documents related to the given object.
     *
     * @param string $object A MVV object
     * @return array Array of documents.
     */
    public static function findByObject(SimpleORMap $object)
    {
        $condition = self::getFilterSql(['mdz.range_type' => get_class($object)]);
        $query = "SELECT md.*, mdz.position, mdz.mkdate, mdz.chdate
                  FROM mvv_files md
                  INNER JOIN mvv_files_ranges mdz USING(mvvfile_id)
                  WHERE mdz.range_id = ? {$condition}
                  ORDER BY mdz.position";
        return parent::getEnrichedByQuery($query, [$object->id]);
    }

    public static function findByRange_id($range_id)
    {
        return self::findBySQL('JOIN mvv_files_ranges USING (mvvfile_id) WHERE range_id =? ORDER BY position ASC', [$range_id]);
    }

    /**
     * Returns the name of the object to display in a specific context.
     *
     * @return string The name.
     */
    public function getDisplayName()
    {
        if ($this->file_refs) {
            return $this->file_refs[0]->name;
        }
        return '';
    }

    /**
     * Returns the name of the rangetype this mvvfile is bound to.
     *
     * @return string type of range
     */
    public function getRangeType()
    {
        if ($this->ranges) {
            return $this->ranges[0]->range_type;
        }
        return '';
    }

    /**
     * Returns the number of assignments to other MVV objects.
     *
     * @return int Number of assignments.
     */
    public function countRelations()
    {
        return MvvFileRange::countBySql('mvvfile_id = ?', [$this->mvvfile_id]);
    }

    /**
     * Returns the position in given range.
     *
     * @return int position.
     */
    public function getPositionInRange($range_id)
    {
        return MvvFileRange::find([$this->mvvfile_id, $range_id])->position;
    }

    /**
     * Returns the ids of related ranges.
     *
     * @return array Ids of related ranges.
     */
    public function getRangesArray()
    {
        return MvvFileRange::findAndMapBySQL(
            function ($range) {
                return $range->range_id;
            },
            'mvvfile_id = ?',
            [$this->mvvfile_id]
        );
    }

    /**
     * Returns the filenames of related filerefs.
     *
     * @return string available filenames.
     */
    public function getFilenames()
    {
        return MvvFileFileref::findAndMapBySQL(
            function ($ref) {
                return $ref->getFilename();
            },
            'mvvfile_id = ?',
            [$this->mvvfile_id]
        );
    }

    /**
     * Returns the filetypes of related filerefs.
     *
     * @return string available filetypes.
     */
    public function getFiletypes()
    {
        return MvvFileFileref::findAndMapBySQL(
            function ($ref) {
                return $ref->getFileType();
            },
            'mvvfile_id = ?',
            [$this->mvvfile_id]
        );
    }


    /**
     * Returns all or a specified (by row count and offset) number of
     * documents sorted and filtered by given parameters and enriched with
     * some additional fields. This function is mainly used in the list view.
     *
     * @param string $sortby Field name to order by.
     * @param string $order ASC or DESC direction of order.
     * @param array $filter Key-value pairs of filed names and values
     * to filter the result set.
     * @param int $row_count The max number of objects to return.
     * @param int $offset The first object to return in a result set.
     * @return object A SimpleORMapCollection of Dokument objects.
     */
    public static function getAllEnriched($sortby = 'chdate', $order = 'DESC',
            $row_count = null, $offset = null, $filter = null)
    {
        $sortby = self::createSortStatement(
            $sortby,
            $order,
            'mvvfile_id',
            ['count_zuordnungen', 'mvv_files_filerefs.name', 'file_refs.name']
        );

        $parameters = [];

        $name_filter_sql = '';
        if (!empty($filter['searchnames'])) {
            $name_filter_sql = " AND CONCAT_WS(' ', `file_refs`.`name`, `mvv_files_filerefs`.`name`, `mvv_files`.`category`,`mvv_files`.`tags`) LIKE :needle";
            $parameters[':needle'] = "%{$filter['searchnames']}%";
        }
        unset($filter['searchnames']);

        $parameters[':ranges'] = self::getIdsFiltered($filter);

        $query = "SELECT `mvv_files`.*,
                         COUNT(`mvv_files_ranges`.`range_id`) AS `count_relations`,
                         `file_refs`.`name` AS `filename`,
                         `mvv_files_ranges`.`range_type` AS `range_type`
                  FROM `mvv_files`
                  LEFT JOIN `mvv_files_ranges` USING (`mvvfile_id`)
                  LEFT JOIN `mvv_files_filerefs` USING (`mvvfile_id`)
                  INNER JOIN `file_refs` ON (`fileref_id` = `file_refs`.`id`)
                  INNER JOIN `folders` ON (file_refs.folder_id = folders.id)
                  WHERE `mvv_files_ranges`.`range_id` IN (:ranges) {$name_filter_sql}
                  GROUP BY `mvvfile_id`
                  ORDER BY {$sortby}";
        return parent::getEnrichedByQuery($query, $parameters, $row_count, $offset);
    }

    /**
     * Returns all relations of the documents specified by the given ids.
     * The returned array is ordered by the types of the referenced objects.
     *
     * @param array $dokument_ids Ids of the documents.
     * @return array References ordered by object types.
     */
    public static function getAllRelations($dokument_ids = [])
    {
        $files = [];
        if ($dokument_ids) {
            foreach ($dokument_ids as $dokument_id) {
                foreach (self::findBySQL('mvvfile_id = ?', [$dokument_id]) as $mvv_file) {
                    $files[] = $mvv_file;
                }
            }
        } else {
            $files = self::findBySQL('1');
        }
        $zuordnungen = [];
        foreach ($files as $file) {
            foreach ($file->ranges as $file_range) {
                $zuordnungen[$file_range['range_type']][$file_range['range_id']] = $file;
            }
        }
        return $zuordnungen;
    }

    /**
     * Returns all relations of this document grouped by object types.
     *
     * @return Array Relations ordered by object types
     */
    public function getRelations()
    {
        $zuordnungen = [];
        foreach (MvvFileRange::findBySQL('mvvfile_id =?', [$this->mvvfile_id]) as $range) {
            $zuordnungen[$range['range_type']][$range['range_id']] = $range;
        }
        return $zuordnungen;
    }

    /**
     * Find Documents by given search term.
     * Used as search function in list view.
     *
     * @param type $term The search term.
     * @param type $filter Optional filter parameters.
     * @return array An array of Dokument ids.
     */
    public static function findBySearchTerm($term, $filter = null)
    {
        $sql = "LEFT JOIN mvv_files_ranges USING (mvvfile_id)
                LEFT JOIN mvv_files_filerefs USING (mvvfile_id)
                LEFT JOIN file_refs ON (fileref_id = file_refs.id)
                LEFT JOIN folders ON (file_refs.folder_id = folders.id)
                WHERE (mvv_files_filerefs.name LIKE CONCAT('%', :term, '%') OR file_refs.name LIKE CONCAT('%', :term, '%')) GROUP BY mvvfile_id";
        $params['term'] = $term;

        /* if ($filter) {
            foreach ($filter as $column => $val) {
                if ($column == null || $val == null) {
                    continue;
                }
                if (is_array($val)) {
                    $sql .= ' AND '. $column . ' IN('
                    . join(',', array_map(
                        function ($val) {
                            return DBManager::get()->quote($val);
                        }, $val))
                    . ') ';
                } else {
                    $sql .= ' AND '.$column.' = ? ';
                    //$params[] = $column;
                    $params[] = $val;
                }
            }
        } */

        return SimpleORMapCollection::createFromArray(self::findBySQL($sql, $params));
    }



    /**
     * Returns the number of Documents comply with the given filter parameters.
     *
     * @param array $filter Array of filter parameters
     * @see ModuleManagementModel::getFilterSql()
     * @return int The number of Documents.
     */
    public static function getCount($filter = null)
    {
        if (empty($filter)) {
            return parent::getCount();
        }
        $searchnames = $filter['searchnames'] ?? '';
        $name_filter_sql = '';
        if ($searchnames) {
            $name_filter_sql = " AND CONCAT_WS(' ', `file_refs`.`name`, `mvv_files_filerefs`.`name`, `mvv_files`.`category`,`mvv_files`.`tags`) LIKE ". DBManager::get()->quote('%' . $searchnames . '%');
        }
        unset($filter['searchnames']);
        $ids = self::getIdsFiltered($filter);
        return parent::getCountBySql("
            SELECT COUNT(DISTINCT `mvv_files`.`mvvfile_id`)
            FROM `mvv_files_ranges`
            INNER JOIN `mvv_files` USING(`mvvfile_id`)
            INNER JOIN `mvv_files_filerefs` USING(`mvvfile_id`)
            INNER JOIN `file_refs` ON `fileref_id` = `file_refs`.`id`
            WHERE `mvv_files_ranges`.`range_id` IN ('" . implode("','", $ids) . "') $name_filter_sql");
    }

    /**
     * Returns a ready to use quick search widget.
     *
     * @param array $exclude Ids of documents excluded from search.
     * @return array Array with quick search id and quick search html.
     */
    public static function getQuickSearch($exclude = array())
    {
        $query = "
            SELECT mvv_files.mvvfile_id, mvv_files_filerefs.name
            FROM mvv_files
            WHERE (mvv_files_filerefs.name LIKE :input OR file_refs.name LIKE :input)
                AND mvv_files.fileref_id NOT IN ('"
            . implode("','", $exclude) . "')
            ORDER BY name ASC";
        $search = new SQLSearch($query, _('Dokument suchen'));
        $qs_id = md5(serialize($search));
        $qs_html = QuickSearch::get('dokumente', $search)
                    ->fireJSFunctionOnSelect('STUDIP.MVV.Search.addSelected')
                    ->noSelectbox();
        return ['id' => $qs_id, 'html' => $qs_html];
    }

    /**
     * Returns the highest current sorting position.
     *
     * @param sting $range_id Id of the mvv object.
     * @return int Number of the highest current sorting position.
     */
    public static function getMaxSortingPos($range_id)
    {
        $ret = DBManager::get()->fetchOne("
            SELECT MAX(`position`)
            FROM `mvv_files_ranges`
            JOIN mvv_files USING (`mvvfile_id`) WHERE `range_id` = ?;", [$range_id]);
        return $ret['MAX(`position`)'];
    }

    /**
     * Adds this mvvfile to given range.
     *
     * @param string $range_id Id of the mvv object.
     */
    public function addToRange($range_id, $range_type)
    {
        $mvvfile_range = new MvvFileRange([$this->mvvfile_id, $range_id]);
        $mvvfile_range->range_type = $range_type;
        if ($mvvfile_range->isNew()) {
            $mvvfile_range->position = self::getMaxSortingPos($range_id) + 1;
        }
        $mvvfile_range->store();
    }

    /**
     * Removes this mvvfile from given range.
     *
     * @param sting $range_id Id of the mvv object.
     */
    public function removeFromRange($range_id)
    {
        if ($mvvfile_range = MvvFileRange::find([$this->mvvfile_id, $range_id])) {
            $vacant = $mvvfile_range->position;
            if ($mvvfile_range->delete()) {
                foreach (MvvFileRange::findBySQL('range_id = ? ORDER BY position ASC',[$range_id]) as $other_range) {
                    if ($other_range->position > $vacant) {
                        $tmp = $other_range->position;
                        $other_range->position = $vacant;
                        $other_range->store();
                        $vacant = $tmp;
                    }
                }
            }
        }
    }

    public function validate()
    {
        $ret = parent::validate();
        if ($this->isDirty()) {
            $messages = array();
            $rejected = false;

            /* if (!$this->name) {
                $ret['name'] = true;
                $messages[] = _('Es muss ein Anzeigename für das Dokument angegeben werden.');
                $rejected = true;
            } */

            if ($rejected) {
                throw new InvalidValuesException(join("\n", $messages), $ret);
            }
        }
        return $ret;
    }

    /**
     * Returns all institutes assigned to files. Sorted and filtered by
     * optional parameters.
     *
     * @param string $sortby DB field to sort by.
     * @param string $order ASC or DESC
     * @param array $filter Array of filter.
     * @return array Array of found Fachbereiche.
     */
    public static function getAllAssignedInstitutes($sortby = 'name',
            $order = 'ASC', $filter = null, $row_count = null, $offset = null)
    {
        $sortby = Fachbereich::createSortStatement($sortby, $order, 'name',
                ['count_objects']);

        return Fachbereich::getEnrichedByQuery("
            SELECT `Institute`.*,
                `Institute`.`Name` as `name`,
                `Institute`.`Institut_id` AS `institut_id`,
                COUNT(DISTINCT `mvv_files`.`mvvfile_id`) as `count_objects`
            FROM `mvv_files`
                LEFT JOIN `mvv_files_ranges` USING(`mvvfile_id`)
                LEFT JOIN `mvv_abschl_zuord` ON (`mvv_abschl_zuord`.`kategorie_id` = `mvv_files_ranges`.`range_id`)
                INNER JOIN `abschluss` USING(`abschluss_id`)
                LEFT JOIN `mvv_studiengang` ON (`mvv_studiengang`.`abschluss_id` = `abschluss`.`abschluss_id`
                    OR `mvv_studiengang`.`studiengang_id` = `mvv_files_ranges`.`range_id`)
                INNER JOIN `Institute` USING(`institut_id`)
                LEFT JOIN `semester_data` `start_sem`
                    ON (`mvv_studiengang`.`start` = `start_sem`.`semester_id`)
                LEFT JOIN `semester_data` `end_sem`
                    ON (`mvv_studiengang`.`end` = `end_sem`.`semester_id`)"
            . Fachbereich::getFilterSql($filter, true) . "
            GROUP BY `institut_id` ORDER BY " . $sortby, [], $row_count, $offset
        );
    }

    /**
     * Returns range_ids (ids of Studiengang/Abschluss-Kategorie) of assigned files.
     *
     * @staticvar array $ids The range_ids of assigned files after filtration.
     * @param array $filter An array with keys  (table_name.column_name) and
     * values (skalar or array) used in where clause.
     * @param boolean $file_ids Return the file ids instead of the range ids.
     * @return array An array with range_ids of assigned files.
     */
    public static function getIdsFiltered($filter, $file_ids = false)
    {
        $id_type = $file_ids ? 'mvvfile_id' : 'range_id';
        $sql = "SELECT DISTINCT `mvv_files_ranges`.`{$id_type}`
                FROM `mvv_files`
                    LEFT JOIN `mvv_files_ranges` USING (`mvvfile_id`)
                    LEFT JOIN `mvv_abschl_zuord` ON (`mvv_abschl_zuord`.`kategorie_id` = `mvv_files_ranges`.`range_id`)
                    LEFT JOIN `abschluss` USING(`abschluss_id`)
                    LEFT JOIN `mvv_studiengang` ON (`mvv_studiengang`.`abschluss_id` = `abschluss`.`abschluss_id`
                        OR `mvv_studiengang`.`studiengang_id` = `mvv_files_ranges`.`range_id`)
                    LEFT JOIN `semester_data` `start_sem`
                        ON (`mvv_studiengang`.`start` = `start_sem`.`semester_id`)
                    LEFT JOIN `semester_data` `end_sem`
                        ON (`mvv_studiengang`.`end` = `end_sem`.`semester_id`)"
                    . self::getFilterSql($filter, true);

        $stm = DBManager::get()->prepare($sql);
        $stm->execute();
        return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
    }

    protected function logChanges($action = null)
    {
        $log_action = 'MVV_FILE_' . mb_strtoupper($action);
        $affected = $this->id;
        $info = ['mvv_files.*'];
        $debug_info = $this->getDisplayName();
        if ($action === 'update') {
            $logged_fields = [
                'year',
                'type',
                'category',
                'tags',
                'extern_visible',
            ];
            foreach ($logged_fields as $logged_field) {
                if ($this->isFieldDirty($logged_field)) {
                    $info[] = $logged_field
                        . ': ' . ($this->getValue($logged_field) ?? '-')
                        . ' (' . ($this->getPristineValue($logged_field) ?? '-')
                        . ')';
                }
            }
        }
        StudipLog::log($log_action, $affected, null, implode(' | ', $info), $debug_info);
    }

}