Passed
Push — master ( 18111a...f46ab8 )
by Julito
12:19
created

SkillRelGradebookModel   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 132
rs 10
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A existsGradeBookSkill() 0 18 2
A __construct() 0 3 1
C updateGradeBookListBySkill() 0 42 12
A updateBySkill() 0 17 3
A getSkillInfo() 0 20 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class SkillRelGradebookModel extends Model
6
{
7
    public $columns = ['id', 'gradebook_id', 'skill_id'];
8
9
    public function __construct()
10
    {
11
        $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
12
    }
13
14
    /**
15
     * @param int $gradebookId
16
     * @param int $skillId
17
     *
18
     * @return bool
19
     */
20
    public function existsGradeBookSkill($gradebookId, $skillId)
21
    {
22
        $result = $this->find(
23
            'all',
24
            [
25
                'where' => [
26
                    'gradebook_id = ? AND skill_id = ?' => [
27
                        $gradebookId,
28
                        $skillId,
29
                    ],
30
                ],
31
            ]
32
        );
33
        if (!empty($result)) {
34
            return true;
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Gets an element.
42
     */
43
    public function getSkillInfo($skill_id, $gradebookId)
44
    {
45
        if (empty($skill_id)) {
46
            return [];
47
        }
48
        $result = Database::select(
49
            '*',
50
            $this->table,
51
            [
52
                'where' => [
53
                    'skill_id = ? AND gradebook_id = ? ' => [
54
                        $skill_id,
55
                        $gradebookId,
56
                    ],
57
                ],
58
            ],
59
            'first'
60
        );
61
62
        return $result;
63
    }
64
65
    /**
66
     * @param int   $skill_id
67
     * @param array $gradebook_list
68
     */
69
    public function updateGradeBookListBySkill($skill_id, $gradebook_list)
70
    {
71
        $original_gradebook_list = $this->find(
72
            'all',
73
            ['where' => ['skill_id = ?' => [$skill_id]]]
74
        );
75
        $gradebooks_to_remove = [];
76
        $gradebooks_to_add = [];
77
        $original_gradebook_list_ids = [];
78
79
        if (!empty($original_gradebook_list)) {
80
            foreach ($original_gradebook_list as $gradebook) {
81
                if (!in_array($gradebook['gradebook_id'], $gradebook_list)) {
82
                    $gradebooks_to_remove[] = $gradebook['id'];
83
                }
84
            }
85
            foreach ($original_gradebook_list as $gradebook_item) {
86
                $original_gradebook_list_ids[] = $gradebook_item['gradebook_id'];
87
            }
88
        }
89
90
        if (!empty($gradebook_list)) {
91
            foreach ($gradebook_list as $gradebook_id) {
92
                if (!in_array($gradebook_id, $original_gradebook_list_ids)) {
93
                    $gradebooks_to_add[] = $gradebook_id;
94
                }
95
            }
96
        }
97
98
        if (!empty($gradebooks_to_remove)) {
99
            foreach ($gradebooks_to_remove as $id) {
100
                $this->delete($id);
101
            }
102
        }
103
104
        if (!empty($gradebooks_to_add)) {
105
            foreach ($gradebooks_to_add as $gradebook_id) {
106
                $attributes = [
107
                    'skill_id' => $skill_id,
108
                    'gradebook_id' => $gradebook_id,
109
                ];
110
                $this->save($attributes);
111
            }
112
        }
113
    }
114
115
    /**
116
     * @param array $params
117
     *
118
     * @return bool|void
119
     */
120
    public function updateBySkill($params)
121
    {
122
        $skillInfo = $this->existsGradeBookSkill(
123
            $params['gradebook_id'],
124
            $params['skill_id']
125
        );
126
127
        if ($skillInfo) {
128
            return;
129
        } else {
130
            $result = $this->save($params);
131
        }
132
        if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
133
            return true;
134
        }
135
136
        return false;
137
    }
138
}
139