Issues (2037)

main/gradebook/lib/be/learnpathlink.class.php (2 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Defines a gradebook LearnpathLink object.
6
 *
7
 * @author Yannick Warnier <[email protected]>
8
 * @author Bert Steppé
9
 */
10
class LearnpathLink extends AbstractLink
11
{
12
    private $course_info;
0 ignored issues
show
The private property $course_info is not used, and could be removed.
Loading history...
13
    private $learnpath_table;
14
    private $learnpath_data;
15
16
    /**
17
     * Constructor.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
        $this->set_type(LINK_LEARNPATH);
23
    }
24
25
    /**
26
     * Generate an array of all learnpaths available.
27
     *
28
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
29
     */
30
    public function get_all_links()
31
    {
32
        if (empty($this->course_code)) {
33
            return [];
34
        }
35
36
        $session_id = $this->get_session_id();
37
        if (empty($session_id)) {
38
            $session_condition = api_get_session_condition(0, true);
39
        } else {
40
            $session_condition = api_get_session_condition($session_id, true, true);
41
        }
42
43
        $sql = 'SELECT id, name FROM '.$this->get_learnpath_table().'
44
                WHERE c_id = '.$this->course_id.' '.$session_condition.' ';
45
        $result = Database::query($sql);
46
47
        $cats = [];
48
        while ($data = Database::fetch_array($result)) {
49
            $cats[] = [$data['id'], $data['name']];
50
        }
51
52
        return $cats;
53
    }
54
55
    /**
56
     * Has anyone used this learnpath yet ?
57
     */
58
    public function has_results()
59
    {
60
        $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
61
        $sql = "SELECT count(id) AS number FROM $tbl_stats
62
				WHERE c_id = ".$this->course_id." AND lp_id = ".$this->get_ref_id();
63
        $result = Database::query($sql);
64
        $number = Database::fetch_array($result, 'NUM');
65
66
        return 0 != $number[0];
67
    }
68
69
    /**
70
     * Get the progress of this learnpath. Only the last attempt are taken into account.
71
     *
72
     * @param $stud_id student id (default: all students who have results - then the average is returned)
73
     * @param $type The type of score we want to get: best|average|ranking
74
     *
75
     * @return array (score, max) if student is given
76
     *               array (sum of scores, number of scores) otherwise
77
     *               or null if no scores available
78
     */
79
    public function calc_score($stud_id = null, $type = null)
80
    {
81
        $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
82
        $session_id = $this->get_session_id();
83
        if (empty($session_id)) {
84
            $session_id = api_get_session_id();
85
        }
86
87
        $sql = "SELECT * FROM $tbl_stats
88
                WHERE
89
                	c_id = ".$this->course_id." AND
90
                    lp_id = ".$this->get_ref_id()." AND
91
                    session_id = $session_id ";
92
93
        if (isset($stud_id)) {
94
            $sql .= ' AND user_id = '.intval($stud_id);
95
        }
96
97
        // order by id, that way the student's first attempt is accessed first
98
        $sql .= ' ORDER BY view_count DESC';
99
100
        $scores = Database::query($sql);
101
        // for 1 student
102
        if (isset($stud_id)) {
103
            if ($data = Database::fetch_assoc($scores)) {
104
                return [$data['progress'], 100];
105
            } else {
106
                return null;
107
            }
108
        } else {
109
            // all students -> get average
110
            $students = []; // user list, needed to make sure we only
111
            // take first attempts into account
112
            $rescount = 0;
113
            $sum = 0;
114
            $bestResult = 0;
115
            $sumResult = 0;
116
            while ($data = Database::fetch_array($scores)) {
117
                if (!(array_key_exists($data['user_id'], $students))) {
118
                    $students[$data['user_id']] = $data['progress'];
119
                    $rescount++;
120
                    $sum += $data['progress'] / 100;
121
                    $sumResult += $data['progress'];
122
123
                    if ($data['progress'] > $bestResult) {
124
                        $bestResult = $data['progress'];
125
                    }
126
                }
127
            }
128
129
            if (0 == $rescount) {
130
                return [null, null];
131
            } else {
132
                switch ($type) {
133
                    case 'best':
134
                        return [$bestResult, 100];
135
                        break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
136
                    case 'average':
137
                        return [$sumResult / $rescount, 100];
138
                        break;
139
                    case 'ranking':
140
                        return AbstractLink::getCurrentUserRanking($stud_id, $students);
141
                        break;
142
                    default:
143
                        return [$sum, $rescount];
144
                        break;
145
                }
146
            }
147
        }
148
    }
149
150
    /**
151
     * Get URL where to go to if the user clicks on the link.
152
     */
153
    public function get_link()
154
    {
155
        $session_id = $this->get_session_id();
156
        $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq_params(
157
            $this->get_course_code(),
158
                $session_id
159
        ).'&gradebook=view';
160
161
        if (!api_is_allowed_to_edit() || null == $this->calc_score(api_get_user_id())) {
162
            $url .= '&action=view&lp_id='.$this->get_ref_id();
163
        } else {
164
            $url .= '&action=build&lp_id='.$this->get_ref_id();
165
        }
166
167
        return $url;
168
    }
169
170
    /**
171
     * Get name to display: same as learnpath title.
172
     */
173
    public function get_name()
174
    {
175
        $data = $this->get_learnpath_data();
176
177
        return $data['name'];
178
    }
179
180
    /**
181
     * Get description to display: same as learnpath description.
182
     */
183
    public function get_description()
184
    {
185
        $data = $this->get_learnpath_data();
186
187
        return $data['description'];
188
    }
189
190
    /**
191
     * Check if this still links to a learnpath.
192
     */
193
    public function is_valid_link()
194
    {
195
        $sql = 'SELECT count(id) FROM '.$this->get_learnpath_table().'
196
                WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
197
        $result = Database::query($sql);
198
        $number = Database::fetch_row($result, 'NUM');
199
200
        return 0 != $number[0];
201
    }
202
203
    public function get_type_name()
204
    {
205
        return get_lang('LearningPaths');
206
    }
207
208
    public function needs_name_and_description()
209
    {
210
        return false;
211
    }
212
213
    public function needs_max()
214
    {
215
        return false;
216
    }
217
218
    public function needs_results()
219
    {
220
        return false;
221
    }
222
223
    public function is_allowed_to_change_name()
224
    {
225
        return false;
226
    }
227
228
    public function get_icon_name()
229
    {
230
        return 'learnpath';
231
    }
232
233
    /**
234
     * Lazy load function to get the database table of the learnpath.
235
     */
236
    private function get_learnpath_table()
237
    {
238
        $this->learnpath_table = Database::get_course_table(TABLE_LP_MAIN);
239
240
        return $this->learnpath_table;
241
    }
242
243
    /**
244
     * Lazy load function to get the database contents of this learnpath.
245
     */
246
    private function get_learnpath_data()
247
    {
248
        if (!isset($this->learnpath_data)) {
249
            $sql = 'SELECT * FROM '.$this->get_learnpath_table().'
250
                    WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
251
            $result = Database::query($sql);
252
            $this->learnpath_data = Database::fetch_array($result);
253
        }
254
255
        return $this->learnpath_data;
256
    }
257
}
258