aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Bookmark.php
blob: e11954825cc6d656d78ad9de00cf987b7acd9ca4 (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
<?php

namespace Courseware;

/**
 * Courseware's bookmarks.
 *
 * @author  Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>
 * @author  Till Glöggler <gloeggler@elan-ev.de>
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 *
 * @property array                         $id         computed column read/write
 * @property string                        $user_id    database column
 * @property int                           $element_id database column
 * @property int                           $mkdate     database column
 * @property int                           $chdate     database column
 * @property \User                         $user       belongs_to User
 * @property \Courseware\StructuralElement $element    belongs_to Courseware\StructuralElement
 */
class Bookmark extends \SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_bookmarks';

        $config['belongs_to']['user'] = [
            'class_name' => \User::class,
            'foreign_key' => 'user_id',
        ];

        $config['belongs_to']['element'] = [
            'class_name' => StructuralElement::class,
            'foreign_key' => 'element_id',
        ];

        parent::configure($config);
    }

    /**
     * Returns the range object this bookmark belongs to.
     *
     * @return \Range the range object of this object
     */
    public function getRange(): \Range
    {
        $rangeType = $this->element['range_type'];

        return $this->element->$rangeType;
    }

    /**
     * Returns all bookmarks of a user.
     *
     * @param \User $user the user for whom to search for bookmarks
     *
     * @return Bookmark[] the list of bookmarks
     */
    public static function findUsersBookmarks($user): array
    {
        return self::findBySQL('user_id = ? ORDER BY chdate', [$user->id]);
    }
}