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

namespace Courseware;

use Course;
use Statusgruppen;
use User;

/**
 * Courseware's peer review instances.
 *
 * @since   Stud.IP 6.0
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 *
 * @property int $id database column
 * @property int $process_id database column
 * @property int $task_id database column
 * @property string $submitter_id database column
 * @property string $reviewer_id database column
 * @property string|null $reviewer_type database column
 * @property \JSONArrayObject|null $assessment database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property PeerReviewProcess $process belongs_to PeerReviewProcess
 * @property Task $task belongs_to Task
 * @property \User $submitter belongs_to \User
 * @property \User $reviewer belongs_to \User
 */
class PeerReview extends \SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_peer_reviews';

        $config['serialized_fields']['assessment'] = 'JSONArrayObject';

        $config['belongs_to']['process'] = [
            'class_name' => PeerReviewProcess::class,
            'foreign_key' => 'process_id',
        ];
        $config['belongs_to']['task'] = [
            'class_name' => Task::class,
            'foreign_key' => 'task_id',
        ];
        $config['belongs_to']['submitter'] = [
            'class_name' => User::class,
            'foreign_key' => 'submitter_id',
        ];
        $config['belongs_to']['reviewer'] = [
            'class_name' => User::class,
            'foreign_key' => 'reviewer_id',
        ];

        parent::configure($config);
    }

    public static function findByCourse(Course $course): iterable
    {
        $collections = [];
        foreach (PeerReviewProcess::findByCourse($course) as $process) {
            $collections[] = $process->getPeerReviews()->getArrayCopy();
        }

        return array_flatten($collections);
    }

    public function getCourse(): Course
    {
        return $this->process->getCourse();
    }

    public function isAnonymous(): bool
    {
        return $this->process->isAnonymous();
    }

    public function isReviewer(User $user): bool
    {
        return match($this->reviewer_type) {
            'autor' => $this->reviewer_id === $user->id,
            'group' => \Statusgruppen::isMemberOf($this->reviewer_id, $user->getId()),
        };
    }

    public function getReviewer(): User|Statusgruppen
    {
        return match($this->reviewer_type) {
            'autor' => User::find($this->reviewer_id),
            'group' => Statusgruppen::find($this->reviewer_id),
        };
    }

    public function isSubmitter(User $user): bool
    {
        return match (get_class($this->getSubmitter())) {
            Statusgruppen::class => \Statusgruppen::isMemberOf($this->submitter_id, $user->id),
            User::class => $this->submitter_id === $user->id
        };
    }

    public function getSubmitter(): User|Statusgruppen
    {
        return User::find($this->submitter_id)
            ?? Statusgruppen::find($this->submitter_id);
    }
}