aboutsummaryrefslogtreecommitdiff
path: root/lib/models/vips/VipsBlock.php
blob: 217925481e732893c27815901361e83fc536c726 (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
<?php
/*
 * VipsBlock.php - Vips block class for Stud.IP
 * Copyright (c) 2016  Elmar Ludwig
 *
 * 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.
 */

/**
 * @license GPL2 or any later version
 *
 * @property int $id database column
 * @property string $name database column
 * @property string $range_id database column
 * @property string|null $group_id database column
 * @property int $visible database column
 * @property float|null $weight database column
 * @property SimpleORMapCollection|VipsAssignment[] $assignments has_many VipsAssignment
 * @property Course $course belongs_to Course
 * @property Statusgruppen|null $group belongs_to Statusgruppen
 */
class VipsBlock extends SimpleORMap
{
    /**
     * Configure the database mapping.
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'etask_blocks';

        $config['has_many']['assignments'] = [
            'class_name'        => VipsAssignment::class,
            'assoc_foreign_key' => 'block_id'
        ];

        $config['belongs_to']['course'] = [
            'class_name'  => Course::class,
            'foreign_key' => 'range_id'
        ];
        $config['belongs_to']['group'] = [
            'class_name'  => Statusgruppen::class,
            'foreign_key' => 'group_id'
        ];

        parent::configure($config);
    }

    /**
     * Delete entry from the database.
     */
    public function delete()
    {
        foreach ($this->assignments as $assignment) {
            $assignment->block_id = null;
            $assignment->store();
        }

        return parent::delete();
    }

    /**
     * Check if this block is visible to this user.
     */
    public function isVisible(string $user_id): bool
    {
        $visible = $this->visible;

        if ($visible && $this->group_id) {
            $visible = StatusgruppeUser::exists([$this->group_id, $user_id]);
        }

        return $visible;
    }

    /**
     * Get the first assignment attempt of the given user for this block.
     * Returns null if there is no assignment attempt for this user.
     *
     * @param string $user_id   user id
     */
    public function getAssignmentAttempt(string $user_id): ?VipsAssignmentAttempt
    {
        $assignment_ids = $this->assignments->pluck('id');

        return VipsAssignmentAttempt::findOneBySQL(
            'assignment_id IN (?) AND user_id = ? ORDER BY start', [$assignment_ids, $user_id]
        );
    }
}