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

namespace Courseware;

/**
 * Courseware's user progresses.
 *
 * @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               $block_id database column
 * @property string            $grade    database column
 * @property string            $mkdate   database column
 * @property string            $chdate   database column
 * @property \Courseware\Block $block    belongs_to Courseware\Block
 * @property \User             $user     belongs_to User
 */
class UserProgress extends \SimpleORMap implements \PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_user_progresses';

        $config['belongs_to']['block'] = [
            'class_name' => Block::class,
            'foreign_key' => 'block_id',
        ];

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

        parent::configure($config);
    }

    /**
     * Returns either an existing UserProgress of this user and block or creates a new one.
     *
     * @param \User $user  the user of the existing or new UserProgress
     * @param Block $block the block of the existing or new UserProgress
     *
     * @return UserProgress the either already existing or new UserProgress
     */
    public static function getUserProgress(\User $user, Block $block): UserProgress
    {
        /** @var ?UserProgress $progress */
        $progress = UserProgress::find([$user->id, $block->id]);
        if (!$progress) {
            /** @var UserProgress $progress */
            $progress = UserProgress::build([
                'user_id' => $user->id,
                'block_id' => $block->id,
                'grade' => 0,
            ]);
        }

        return $progress;
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(\StoredUserData $storage)
    {
        $userProgresses = \DBManager::get()->fetchAll(
            'SELECT * FROM cw_user_progresses WHERE user_id = ?',
            [$storage->user_id]
        );
        if ($userProgresses) {
            $storage->addTabularData(_('Courseware Fortschritt'), 'cw_user_progresses', $userProgresses);
        }
    }
}