aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Task.php
blob: 7842830050d45156180786e19eba5b9653c2e28f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php

namespace Courseware;

use User;

/**
 * Courseware's tasks.
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.1
 *
 * @property int $id database column
 * @property int $task_group_id database column
 * @property int $structural_element_id database column
 * @property string $solver_id database column
 * @property string|null $solver_type database column
 * @property int $submission_date database column
 * @property int $submitted database column
 * @property string|null $renewal database column
 * @property int $renewal_date database column
 * @property int $visible database column
 * @property int|null $feedback_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property TaskGroup $task_group belongs_to TaskGroup
 * @property StructuralElement $structural_element belongs_to StructuralElement
 * @property \User $lecturer belongs_to \User
 * @property \User $user belongs_to \User
 * @property \Statusgruppen $group belongs_to \Statusgruppen
 * @property \Course $course belongs_to \Course
 * @property TaskFeedback|null $task_feedback belongs_to TaskFeedback
 * @property-read \User|\Statusgruppen|null $solver additional field
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class Task extends \SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_tasks';

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

        $config['belongs_to']['structural_element'] = [
            'class_name' => StructuralElement::class,
            'foreign_key' => 'structural_element_id',
        ];

        $config['belongs_to']['lecturer'] = [
            'class_name' => User::class,
            'foreign_key' => 'lecturer_id',
        ];

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

        $config['belongs_to']['group'] = [
            'class_name' => \Statusgruppen::class,
            'foreign_key' => 'solver_id',
            'assoc_foreign_key' => 'statusgruppe_id',
        ];

        $config['belongs_to']['course'] = [
            'class_name' => \Course::class,
            'foreign_key' => 'seminar_id',
        ];

        $config['belongs_to']['task_feedback'] = [
            'class_name' => TaskFeedback::class,
            'foreign_key' => 'feedback_id',
        ];

        $config['additional_fields']['solver'] = [
            'get' => 'getSolver',
        ];
        $config['additional_fields']['submission_date'] = [
            'get' => 'getSubmissionDate',
        ];

        parent::configure($config);
    }

    public function getTaskGroup(): TaskGroup
    {
        return $this->task_group;
    }

    /**
     * Returns the structural element of this task.
     * This structural element and all its children are part of the task.
     *
     * @return StructuralElement the structural element
     */
    public function getStructuralElement(): StructuralElement
    {
        return $this->structural_element;
    }

    /**
     * Returns the feedback for this task.
     *
     * @return TaskFeedback the task feedback
     */
    public function getFeedback(): ?TaskFeedback
    {
        return $this->task_feedback;
    }

    /**
     * @return bool true if task is submitted
     */
    public function isSubmitted(): bool
    {
        return 1 === (int) $this->submitted;
    }

    /**
     * @param \User|\Seminar_User $user
     */
    public function canUpdate($user): bool
    {
        $perm = false;
        switch ($this->solver_type) {
            case 'autor':
                if ($this->solver_id === $user->id) {
                    return true;
                }

                if ($this->visible) {
                    $solvers = $this->getTaskGroup()->getSolvers();
                    foreach ($solvers as $solver) {
                        if ($solver->id === $user->id) {
                            return true;
                        }
                    }
                }

                break;

            case 'group':
                $group = \Statusgruppen::find($this->solver_id);
                if (isset($group) && $group->isMember($user->id)) {
                    return true;
                }
                break;
        }

        return $this->getStructuralElement()->hasEditingPermission($user);
    }

    /**
     * @param \User|\Seminar_User $user
     */
    public function userIsASolver($user): bool
    {
        switch ($this->solver_type) {
            case 'autor':
                return $this->solver_id === $user->id;

            case 'group':
                $group = $this->getSolver();

                return $group->isMember($user->id);
        }

        return false;
    }

    /**
     * @return \User|\Statusgruppen|null the solver
     */
    public function getSolver()
    {
        switch ($this->solver_type) {
            case 'autor':
                return \User::find($this->solver_id);

            case 'group':
                return \Statusgruppen::find($this->solver_id);
        }

        return null;
    }

    public function getSubmissionDate(): int
    {
        return $this->task_group['end_date'];
    }

    public function getTaskProgress(): float
    {
        $children = $this->structural_element->findDescendants();

        $element_counter = 1;
        $progress = $this->getStructuralElementProgress($this->structural_element);
        foreach ($children as $child) {
            ++$element_counter;
            $progress = ($this->getStructuralElementProgress($child) + $progress) / $element_counter;
        }

        return $progress * 100;
    }

    public function canSubmit(): bool
    {
        return !$this->submitted
            && time() <= ('granted' === $this->renewal ? $this->renewal_date : $this->submission_date);
    }

    public function submitTask(): void
    {
        $this->submitted = 1;
        if ('pending' === $this->renewal) {
            $this->renewal = '';
        }
        $this->store();
    }

    public function isRenewed(): bool
    {
        return $this->renewal === 'granted';
    }

    public function requestRenewal(): void
    {
        $this->renewal = 'pending';
        $this->store();
    }

    public function declineRenewalRequest(): void
    {
        $this->renewal = 'declined';
        $this->store();
    }

    public function grantRenewalRequest(\DateTime $renewalDate): void
    {
        $this->renewal = 'granted';
        $this->renewal_date = $renewalDate->getTimestamp();
        $this->store();
    }

    public function setVisibility(bool $visibility): void
    {
        $this->visible = (int) $visibility;
        $this->store();
    }

    private function getStructuralElementProgress(StructuralElement $structural_element): float
    {
        $containers = Container::findBySQL('structural_element_id = ?', [intval($structural_element->id)]);
        $counter = 0;
        $progress = 0;
        $b = [];
        foreach ($containers as $container) {
            $blockCount = $container->countBlocks();

            $counter += $blockCount;
            if ($blockCount > 0) {
                $blocks = Block::findBySQL('container_id = ?', [$container->id]);
                foreach ($blocks as $block) {
                    $b[] = $block->id;
                    if ($this->task_group->lecturer_id === $block->owner_id && $block->owner_id !== $block->editor_id) {
                        ++$progress;
                    }
                }
            }
        }
        if ($counter > 0) {
            return $progress / $counter;
        }

        return 0;
    }
}