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

namespace Grading;

use OAT\Library\Lti1p3Ags\Model\LineItem\LineItem;
use OAT\Library\Lti1p3Ags\Model\LineItem\LineItemInterface;
use OAT\Library\Lti1p3Ags\Model\LineItem\LineItemSubmissionReview;

/**
 * @license GPL2 or any later version
 *
 * @property int $id database column
 * @property string $course_id database column
 * @property string $item database column
 * @property string $name database column
 * @property string $tool database column
 * @property string $category database column
 * @property int $position database column
 * @property float $weight database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property \SimpleORMapCollection|Instance[] $instances has_many Instance
 * @property \Course $course belongs_to \Course
 */
class Definition extends \SimpleORMap
{
    const CUSTOM_DEFINITIONS_CATEGORY = 'xyzzy';

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

        $config['belongs_to']['course'] = [
            'class_name' => \Course::class,
            'foreign_key' => 'course_id',
        ];
        $config['has_many']['instances'] = [
            'class_name' => Instance::class,
            'assoc_foreign_key' => 'definition_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
        ];

        parent::configure($config);
    }

    public static function getCategoriesByCourse(\Course $course)
    {
        $query = 'SELECT category FROM grading_definitions
                  WHERE course_id = ?
                  GROUP BY category
                  ORDER BY category ASC';

        $stmt = \DBManager::get()->prepare($query);
        $stmt->execute([$course->id]);

        $categories = $stmt->fetchAll(\PDO::FETCH_COLUMN);

        $customIndex = array_search(self::CUSTOM_DEFINITIONS_CATEGORY, $categories);
        if (false !== $customIndex) {
            unset($categories[$customIndex]);
            array_unshift($categories, self::CUSTOM_DEFINITIONS_CATEGORY);
        }

        return $categories;
    }

    public static function findByCourse(\Course $course)
    {
        return Definition::findBySQL('course_id = ? ORDER BY position ASC, name ASC', [$course->id]);
    }

    public function toLineItem() : LineItemInterface
    {
        $deployment_id = explode('-', $this->tool)[1] ?? '';
        return new LineItem(
            PHP_FLOAT_MAX, //TODO
            $this->name,
            $this->id,
            $deployment_id
        );
    }

    public static function createFromLineItem(LineItemInterface $line_item) : ?Definition
    {
        $definition = new Definition();
        $definition->id = $line_item->getIdentifier();
        $definition->name = $line_item->getLabel();
        $definition->tool = 'lti-' . $line_item->getResourceIdentifier();
        if ($definition->store()) {
            return $definition;
        }
        return null;
    }
}