aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Grading/Instance.php
blob: e152544021e0050aee7b844d889105ed7d9cc7b4 (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
<?php

namespace Grading;

use OAT\Library\Lti1p3Ags\Model\Result\Result;

/**
 * @license GPL2 or any later version
 *
 * @property array $id alias for pk
 * @property int $definition_id database column
 * @property string $user_id database column
 * @property float $rawgrade database column
 * @property string|null $feedback database column
 * @property int $passed database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property \User $user belongs_to \User
 * @property Definition $definition belongs_to Definition
 */
class Instance extends \SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'grading_instances';

        $config['belongs_to']['user'] = [
            'class_name' => \User::class,
            'foreign_key' => 'user_id',
        ];
        $config['belongs_to']['definition'] = [
            'class_name' => Definition::class,
            'foreign_key' => 'definition_id',
        ];

        parent::configure($config);
    }

    public static function findByCourse(\Course $course)
    {
        $definitionIds = Definition::findAndMapBySQL(
            function ($def) {
                return $def->id;
            },
            'course_id = ?',
            [$course->id]
        );

        if (!count($definitionIds)) {
            return [];
        }

        return self::findBySql('definition_id IN (?)', [$definitionIds]);
    }

    public static function findByCourseAndUser(\Course $course, \User $user)
    {
        $definitionIds = Definition::findAndMapBySQL(
            function ($def) {
                return $def->id;
            },
            'course_id = ?',
            [$course->id]
        );

        if (!count($definitionIds)) {
            return [];
        }

        return self::findBySql('definition_id IN (?) AND user_id = ?', [$definitionIds, $user->id]);
    }

    /**
     * setter for the rawgrade column. The database type is decimal(6,5) UNSIGNED, therefore
     * the setter mimics the database behaviour to get valid results from ::isFieldDirty()
     *
     * @param mixed $grade
     * @return string
     */
    public function setRawgrade($grade = 0): string
    {
        if ($grade < 0) {
            $grade = 0;
        }
        if ($grade >= 10) {
            $grade = 9.99999;
        }
        return $this->content['rawgrade'] = number_format($grade, 5, '.', '');
    }

    public function toResult() : Result
    {
        return new Result(
            $this->user_id,
            $this->definition_id,
            $this->user_id . '_' . $this->definition_id,
            $this->rawgrade,
            9.99999, //see above
            $this->feedback
        );
    }
}