Gradebook::is_active()   B
last analyzed

Complexity

Conditions 8
Paths 72

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 72
nop 1
dl 0
loc 30
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class Gradebook.
6
 * This class provides methods for the notebook management.
7
 * Include/require it in your code to use its features.
8
 */
9
class Gradebook extends Model
10
{
11
    public $columns = [
12
        'id',
13
        'name',
14
        'description',
15
        'course_code',
16
        'parent_id',
17
        'grade_model_id',
18
        'session_id',
19
        'weight',
20
        'user_id',
21
    ];
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        parent::__construct();
29
        $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
30
        $this->table_skill = Database::get_main_table(TABLE_MAIN_SKILL);
31
        $this->table_skill_rel_gradebook = Database::get_main_table(TABLE_MAIN_SKILL_REL_GRADEBOOK);
32
    }
33
34
    /**
35
     * Returns true if the gradebook is active and visible in a course, false
36
     * otherwise.
37
     *
38
     * @param int $c_id Course integer id, defaults to the current course
39
     *
40
     * @return bool
41
     */
42
    public static function is_active($c_id = null)
43
    {
44
        $name = 'gradebook';
45
        $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
46
        $sql = "SELECT * from $table
47
                WHERE variable='course_hide_tools' AND subkey='$name'
48
                LIMIT 1";
49
        $result = Database::query($sql);
50
        $setting = Database::store_result($result);
51
        $setting = isset($setting[0]) ? $setting[0] : null;
52
        $setting = $setting ? $setting : [];
53
        $inactive = isset($setting['selected_value']) && 'true' == $setting['selected_value'];
54
55
        if ($inactive) {
56
            return false;
57
        }
58
59
        $c_id = $c_id ? intval($c_id) : api_get_course_int_id();
60
        $table = Database::get_course_table(TABLE_TOOL_LIST);
61
        $sql = "SELECT * from $table
62
                WHERE c_id = $c_id and name='$name'
63
                LIMIT 1";
64
        $result = Database::query($sql);
65
        $item = Database::store_result($result, 'ASSOC');
66
        $item = isset($item[0]) ? $item[0] : null;
67
        if (empty($item)) {
68
            return true;
69
        }
70
71
        return '1' == $item['visibility'];
72
    }
73
74
    /**
75
     * @param array $options
76
     *
77
     * @return array
78
     */
79
    public function get_all($options = [])
80
    {
81
        $gradebooks = parent::get_all($options);
82
        foreach ($gradebooks as &$gradebook) {
83
            if (empty($gradebook['name'])) {
84
                $gradebook['name'] = $gradebook['course_code'];
85
            }
86
        }
87
88
        return $gradebooks;
89
    }
90
91
    /**
92
     * @param int   $gradebook_id
93
     * @param array $skill_list
94
     * @param bool  $deleteSkillNotInList
95
     *
96
     * @return bool
97
     */
98
    public function updateSkillsToGradeBook(
99
        $gradebook_id,
100
        $skill_list,
101
        $deleteSkillNotInList = true
102
    ) {
103
        $skill_gradebook = new SkillRelGradebook();
104
        $skill_gradebooks_source = $skill_gradebook->get_all(
105
            ['where' => ['gradebook_id = ?' => $gradebook_id]]
106
        );
107
        $clean_gradebook = [];
108
109
        if (!empty($skill_gradebooks_source)) {
110
            foreach ($skill_gradebooks_source as $source) {
111
                $clean_gradebook[] = $source['skill_id'];
112
            }
113
        }
114
115
        //Cleaning skills
116
        if (!empty($skill_list)) {
117
            $skill_list = array_map('intval', $skill_list);
118
            $skill_list = array_filter($skill_list);
119
        }
120
121
        if (!empty($skill_list)) {
122
            if (!empty($clean_gradebook)) {
123
                $skill_to_remove = array_diff($clean_gradebook, $skill_list);
124
            }
125
126
            foreach ($skill_list as $skill_id) {
127
                $params = [];
128
                $params['gradebook_id'] = $gradebook_id;
129
                $params['skill_id'] = $skill_id;
130
                if (!$skill_gradebook->existsGradeBookSkill($gradebook_id, $skill_id)) {
131
                    $skill_gradebook->save($params);
132
                }
133
            }
134
        } else {
135
            $skill_to_remove = $clean_gradebook;
136
        }
137
138
        if ($deleteSkillNotInList) {
139
            if (!empty($skill_to_remove)) {
140
                foreach ($skill_to_remove as $remove) {
141
                    $skill_item = $skill_gradebook->getSkillInfo(
142
                        $remove,
143
                        $gradebook_id
144
                    );
145
                    $skill_gradebook->delete($skill_item['id']);
146
                }
147
            }
148
        }
149
150
        return false;
151
    }
152
153
    /**
154
     * Returns a Form validator Obj.
155
     *
156
     * @todo the form should be auto generated
157
     *
158
     * @param   string  url
159
     * @param   string  action add, edit
160
     *
161
     * @return FormValidator
162
     */
163
    public function show_skill_form($gradebook_id, $url, $header = null)
164
    {
165
        $form = new FormValidator('gradebook_add_skill', 'POST', $url);
166
        // Setting the form elements
167
        if (!isset($header)) {
168
            $header = get_lang('Add');
169
        }
170
        $id = isset($_GET['id']) ? intval($_GET['id']) : '';
171
172
        $form->addHeader($header);
173
        $form->addElement('hidden', 'id', $id);
174
175
        $skill = new Skill();
176
        $skills = $skill->get_all();
177
        $clean_skill_list = [];
178
        foreach ($skills as $skill) {
179
            $clean_skill_list[$skill['id']] = $skill['name'];
180
        }
181
        $form->addElement(
182
            'select',
183
            'skill',
184
            get_lang('Skills'),
185
            $clean_skill_list,
186
            [
187
                'multiple' => 'multiple',
188
            ]
189
        );
190
191
        $selected_skills = self::getSkillsByGradebook($gradebook_id);
0 ignored issues
show
Bug Best Practice introduced by
The method Gradebook::getSkillsByGradebook() 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

191
        /** @scrutinizer ignore-call */ 
192
        $selected_skills = self::getSkillsByGradebook($gradebook_id);
Loading history...
192
        $clean_selected_skills = [];
193
        if (!empty($selected_skills)) {
194
            foreach ($selected_skills as $skill) {
195
                $clean_selected_skills[] = $skill['id'];
196
            }
197
        }
198
199
        $form->addButtonCreate(get_lang('Add'), 'submit');
200
        $form->setDefaults(['skill' => $clean_selected_skills]);
201
202
        return $form;
203
    }
204
205
    /**
206
     * @param int $gradebook_id
207
     *
208
     * @return array|resource
209
     */
210
    public function getSkillsByGradebook($gradebook_id)
211
    {
212
        $gradebook_id = intval($gradebook_id);
213
        $sql = "SELECT skill.id, skill.name 
214
                FROM {$this->table_skill} skill
215
                INNER JOIN {$this->table_skill_rel_gradebook} skill_rel_gradebook
216
                ON skill.id = skill_rel_gradebook.skill_id
217
                WHERE skill_rel_gradebook.gradebook_id = $gradebook_id";
218
        $result = Database::query($sql);
219
        $result = Database::store_result($result, 'ASSOC');
220
221
        return $result;
222
    }
223
224
    /**
225
     * Displays the title + grid.
226
     */
227
    public function display()
228
    {
229
        // action links
230
        echo self::returnGrid();
0 ignored issues
show
Bug Best Practice introduced by
The method Gradebook::returnGrid() 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

230
        echo self::/** @scrutinizer ignore-call */ returnGrid();
Loading history...
231
    }
232
233
    /**
234
     * Displays the title + grid.
235
     */
236
    public function returnGrid()
237
    {
238
        // action links
239
        return Display::grid_html('gradebooks');
240
    }
241
}
242