aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/PeerReviewProcess.php
blob: 51c3c848aeeb1770f677472239009030370e5d99 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

namespace Courseware;

use Course;
use DBManager;
use SimpleORMapCollection;
use User;

/**
 * A PeerReviewProcess groups a set of PeerReviews.
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 *
 * @since   Stud.IP 5.5
 */
class PeerReviewProcess extends \SimpleORMap
{
    public const DEFAULT_DURATION = 7;

    public const STATE_BEFORE = 'before';
    public const STATE_ACTIVE = 'active';
    public const STATE_AFTER = 'after';

    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_peer_review_processes';

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

        $config['belongs_to']['task_group'] = [
            'class_name' => TaskGroup::class,
            'foreign_key' => 'task_group_id',
        ];
        $config['belongs_to']['owner'] = [
            'class_name' => User::class,
            'foreign_key' => 'owner_id',
        ];

        $config['additional_fields']['peer_reviews'] = [
            'get' => 'getPeerReviews',
            'set' => false,
        ];

        $config['has_many']['_peer_reviews'] = [
            'class_name' => PeerReview::class,
            'assoc_foreign_key' => 'process_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY mkdate',
        ];

        parent::configure($config);
    }

    public static function findByCourse(Course $course): iterable
    {
        return self::findBySQL('task_group_id IN (?) ORDER BY mkdate', [
            DBManager::get()->fetchFirst('SELECT id FROM `cw_task_groups` WHERE seminar_id = ?', [$course->getId()]),
        ]);
    }

    public static function findByUser(User $user): iterable
    {
        return self::findMany(
            DBManager::get()->fetchFirst(
                'SELECT id FROM cw_peer_review_processes
                   WHERE task_group_id IN (
                     SELECT id FROM cw_task_groups
                       WHERE cw_task_groups.seminar_id IN (
                         SELECT seminar_id FROM seminar_user WHERE user_id = ?))',
                [$user->getId()]
            )
        );
    }

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

    public function getPeerReviews(): SimpleORMapCollection
    {
        $this->checkAutomaticPairing();

        return SimpleORMapCollection::createFromArray(
            PeerReview::findBySql('process_id = ? ORDER BY mkdate', [$this->getId()])
        );
    }

    public function getDuration(): int
    {
        if (!isset($this->configuration['duration'])) {
            return self::DEFAULT_DURATION;
        }

        return (int) $this->configuration['duration'];
    }

    public function isAnonymous(): bool
    {
        if (!isset($this->configuration['anonymous'])) {
            return true;
        }

        return (bool) $this->configuration['automaticPairing'];
    }

    public function isAutomaticPairing(): bool
    {
        if (!isset($this->configuration['automaticPairing'])) {
            return true;
        }

        return (bool) $this->configuration['automaticPairing'];
    }

    public function getCurrentState(int $date = null): string
    {
        if (is_null($date)) {
            $date = time();
        }

        if ($this->review_end < $date) {
            return self::STATE_AFTER;
        }

        if ($date < $this->review_start) {
            return self::STATE_BEFORE;
        }

        return self::STATE_ACTIVE;
    }

    public function checkAutomaticPairing(): void
    {
        if ($this->isAutomaticPairing() && !$this->paired_at) {
            $now = time();
            if ($now > $this->review_start) {
                $this->createAutomaticPairings();
                $this->content['paired_at'] = $now;
                $this->content_db['paired_at'] = $now;
                $stmt = \DBManager::get()->prepare(
                    'UPDATE `' . $this->db_table() . '` SET `paired_at` = ? WHERE id = ?'
                );
                $stmt->execute([$now, $this->getId()]);
            }
        }
    }

    public function createAutomaticPairings(): iterable
    {
        $taskGroup = $this->task_group;
        $submitters = $taskGroup->getSubmitters();

        if (count($submitters) < 2) {
            return [];
        }

        shuffle($submitters);
        $copy = $submitters;
        array_push($copy, array_shift($copy));
        $pairings = array_map(null, $submitters, $copy);

        return array_map(function ($pairing) use ($taskGroup) {
            list($submitter, $reviewer) = $pairing;
            $task = $taskGroup->findTaskBySolver($submitter);

            return PeerReview::create([
                'process_id' => $this->getId(),
                'task_id' => $task->getId(),
                'submitter_id' => $submitter->getId(),
                'reviewer_id' => $reviewer->getId(),
                'reviewer_type' => $reviewer instanceof User ? 'autor' : 'group',
            ]);
        }, $pairings);
    }

    public function rescheduleTo(int $newStartDate): void
    {
        $newEndDate = $newStartDate + $this->getDuration() * (24 * 60 * 60);
        $this->setData([
            "review_start" => $newStartDate,
            "review_end" => $newEndDate,
        ]);
        $this->store();
    }
}