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

SkillRelSkillModel::getSkillParents()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 22
rs 9.4888
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class SkillRelSkillModel extends Model
6
{
7
    public $columns = ['skill_id', 'parent_id', 'relation_type', 'level'];
8
9
    /**
10
     * Constructor.
11
     */
12
    public function __construct()
13
    {
14
        $this->table = Database::get_main_table(TABLE_MAIN_SKILL_REL_SKILL);
15
        $this->tableSkill = Database::get_main_table(TABLE_MAIN_SKILL);
16
    }
17
18
    /**
19
     * Gets an element.
20
     *
21
     * @param int $id
22
     *
23
     * @return array
24
     */
25
    public function getSkillInfo($id)
26
    {
27
        $id = (int) $id;
28
29
        if (empty($id)) {
30
            return [];
31
        }
32
33
        $result = Database::select(
34
            '*',
35
            $this->table,
36
            ['where' => ['skill_id = ?' => $id]],
37
            'first'
38
        );
39
40
        return $result;
41
    }
42
43
    /**
44
     * @param int  $skillId
45
     * @param bool $add_child_info
46
     *
47
     * @return array
48
     */
49
    public function getSkillParents($skillId, $add_child_info = true)
50
    {
51
        $skillId = (int) $skillId;
52
        $sql = 'SELECT child.* FROM '.$this->table.' child
53
                LEFT JOIN '.$this->table.' parent
54
                ON child.parent_id = parent.skill_id
55
                WHERE child.skill_id = '.$skillId.' ';
56
        $result = Database::query($sql);
57
        $skill = Database::store_result($result, 'ASSOC');
58
        $skill = isset($skill[0]) ? $skill[0] : null;
59
60
        $parents = [];
61
        if (!empty($skill)) {
62
            if (null != $skill['parent_id']) {
63
                $parents = self::getSkillParents($skill['parent_id']);
0 ignored issues
show
Bug Best Practice introduced by
The method SkillRelSkillModel::getSkillParents() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                /** @scrutinizer ignore-call */ 
64
                $parents = self::getSkillParents($skill['parent_id']);
Loading history...
64
            }
65
            if ($add_child_info) {
66
                $parents[] = $skill;
67
            }
68
        }
69
70
        return $parents;
71
    }
72
73
    /**
74
     * @param int $skillId
75
     *
76
     * @return array
77
     */
78
    public function getDirectParents($skillId)
79
    {
80
        $skillId = (int) $skillId;
81
        $sql = 'SELECT parent_id as skill_id
82
                FROM '.$this->table.'
83
                WHERE skill_id = '.$skillId;
84
        $result = Database::query($sql);
85
        $skill = Database::store_result($result, 'ASSOC');
86
        $skill = isset($skill[0]) ? $skill[0] : null;
87
        $parents = [];
88
        if (!empty($skill)) {
89
            $parents[] = $skill;
90
        }
91
92
        return $parents;
93
    }
94
95
    /**
96
     * @param int  $skill_id
97
     * @param bool $load_user_data
98
     * @param bool $user_id
99
     *
100
     * @return array
101
     */
102
    public function getChildren(
103
        $skill_id,
104
        $load_user_data = false,
105
        $user_id = false,
106
        $order = ''
107
    ) {
108
        $skill_id = (int) $skill_id;
109
        $sql = 'SELECT parent.* FROM '.$this->tableSkill.' skill
110
                INNER JOIN '.$this->table.' parent
111
                ON parent.id = skill.id
112
                WHERE parent_id = '.$skill_id.'
113
                ORDER BY skill.name ASC';
114
        $result = Database::query($sql);
115
        $skills = Database::store_result($result, 'ASSOC');
116
117
        $skill_obj = new SkillModel();
118
        $skill_rel_user = new SkillRelUserModel();
119
120
        if ($load_user_data) {
121
            $passed_skills = $skill_rel_user->getUserSkills($user_id);
122
            $done_skills = [];
123
            foreach ($passed_skills as $done_skill) {
124
                $done_skills[] = $done_skill['skill_id'];
125
            }
126
        }
127
128
        if (!empty($skills)) {
129
            foreach ($skills as &$skill) {
130
                $skill['data'] = $skill_obj->get($skill['skill_id']);
131
                if (isset($skill['data']) && !empty($skill['data'])) {
132
                    if (!empty($done_skills)) {
133
                        $skill['data']['passed'] = 0;
134
                        if (in_array($skill['skill_id'], $done_skills)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $done_skills does not seem to be defined for all execution paths leading up to this point.
Loading history...
135
                            $skill['data']['passed'] = 1;
136
                        }
137
                    }
138
                } else {
139
                    $skill = null;
140
                }
141
            }
142
        }
143
144
        return $skills;
145
    }
146
147
    /**
148
     * @param array $params
149
     *
150
     * @return bool
151
     */
152
    public function updateBySkill($params)
153
    {
154
        $result = Database::update(
155
            $this->table,
156
            $params,
157
            ['skill_id = ? ' => $params['skill_id']]
158
        );
159
        if ($result) {
160
            return true;
161
        }
162
163
        return false;
164
    }
165
166
    /**
167
     * @param int $skill_id
168
     * @param int $parent_id
169
     *
170
     * @return bool
171
     */
172
    public function relationExists($skill_id, $parent_id)
173
    {
174
        $result = $this->find(
175
            'all',
176
            [
177
                'where' => [
178
                    'skill_id = ? AND parent_id = ?' => [
179
                        $skill_id,
180
                        $parent_id,
181
                    ],
182
                ],
183
            ]
184
        );
185
186
        if (!empty($result)) {
187
            return true;
188
        }
189
190
        return false;
191
    }
192
}
193