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

namespace Courseware;

use Course;
use DBManager;
use Statusgruppen;
use User;

/**
 * Courseware's peer review instances.
 *
 * @since   Stud.IP 5.5
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
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
    {
        switch ($this->reviewer_type) {
            case 'autor':
                return $this->reviewer_id === $user->getId();
            case 'group':
                return \Statusgruppen::isMemberOf($this->reviewer_id, $user->getId());
        }
    }

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

    public function isSubmitter(User $user): bool
    {
        return $this->submitter_id === $user->id;
    }

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

        $statusGroup = Statusgruppen::find($this->submitter_id);
        return $statusGroup;
    }
}