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

namespace Grading;

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',
            'foreign_key' => 'course_id',
        ];
        $config['has_many']['instances'] = [
            'class_name' => '\\Grading\\Instance',
            '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]);
    }
}