Completed
Push — master ( ae5621...ef667c )
by Julito
13:23
created

LearnpathLink::get_link()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
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
 * @package chamilo.gradebook
11
 */
12
class LearnpathLink extends AbstractLink
13
{
14
    private $course_info = null;
0 ignored issues
show
introduced by
The private property $course_info is not used, and could be removed.
Loading history...
15
    private $learnpath_table = null;
16
    private $learnpath_data = null;
17
18
    /**
19
     * Constructor.
20
     */
21
    public function __construct()
22
    {
23
        parent::__construct();
24
        $this->set_type(LINK_LEARNPATH);
25
    }
26
27
    /**
28
     * Generate an array of all learnpaths available.
29
     *
30
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
31
     */
32
    public function get_all_links()
33
    {
34
        if (empty($this->course_code)) {
35
            return [];
36
        }
37
38
        $session_id = api_get_session_id();
39
        if (empty($session_id)) {
40
            $session_condition = api_get_session_condition(0, true);
41
        } else {
42
            $session_condition = api_get_session_condition($session_id, true, true);
43
        }
44
45
        $sql = 'SELECT id, name FROM '.$this->get_learnpath_table().'
46
                WHERE c_id = '.$this->course_id.' '.$session_condition.' ';
47
        $result = Database::query($sql);
48
49
        $cats = [];
50
        while ($data = Database::fetch_array($result)) {
51
            $cats[] = [$data['id'], $data['name']];
52
        }
53
54
        return $cats;
55
    }
56
57
    /**
58
     * Has anyone used this learnpath yet ?
59
     */
60
    public function has_results()
61
    {
62
        $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
63
        $sql = "SELECT count(id) AS number FROM $tbl_stats
64
				WHERE c_id = ".$this->course_id." AND lp_id = ".$this->get_ref_id();
65
        $result = Database::query($sql);
66
        $number = Database::fetch_array($result, 'NUM');
67
68
        return $number[0] != 0;
69
    }
70
71
    /**
72
     * Get the progress of this learnpath. Only the last attempt are taken into account.
73
     *
74
     * @param $stud_id student id (default: all students who have results - then the average is returned)
75
     * @param $type The type of score we want to get: best|average|ranking
76
     *
77
     * @return array (score, max) if student is given
78
     *               array (sum of scores, number of scores) otherwise
79
     *               or null if no scores available
80
     */
81
    public function calc_score($stud_id = null, $type = null)
82
    {
83
        $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
84
        $session_id = $this->get_session_id();
85
        if (empty($session_id)) {
86
            $session_id = api_get_session_id();
87
        }
88
89
        $sql = "SELECT * FROM $tbl_stats
90
                WHERE
91
                	c_id = ".$this->course_id." AND
92
                    lp_id = ".$this->get_ref_id()." AND
93
                    session_id = $session_id ";
94
95
        if (isset($stud_id)) {
96
            $sql .= ' AND user_id = '.intval($stud_id);
97
        }
98
99
        // order by id, that way the student's first attempt is accessed first
100
        $sql .= ' ORDER BY view_count DESC';
101
102
        $scores = Database::query($sql);
103
        // for 1 student
104
        if (isset($stud_id)) {
105
            if ($data = Database::fetch_assoc($scores)) {
106
                return [$data['progress'], 100];
107
            } else {
108
                return null;
109
            }
110
        } else {
111
            // all students -> get average
112
            $students = []; // user list, needed to make sure we only
113
            // take first attempts into account
114
            $rescount = 0;
115
            $sum = 0;
116
            $bestResult = 0;
117
            $sumResult = 0;
118
            while ($data = Database::fetch_array($scores)) {
119
                if (!(array_key_exists($data['user_id'], $students))) {
120
                    $students[$data['user_id']] = $data['progress'];
121
                    $rescount++;
122
                    $sum += $data['progress'] / 100;
123
                    $sumResult += $data['progress'];
124
125
                    if ($data['progress'] > $bestResult) {
126
                        $bestResult = $data['progress'];
127
                    }
128
                }
129
            }
130
131
            if ($rescount == 0) {
132
                return null;
133
            } else {
134
                switch ($type) {
135
                    case 'best':
136
                        return [$bestResult, 100];
137
                        break;
0 ignored issues
show
Unused Code introduced by
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...
138
                    case 'average':
139
                        return [$sumResult / $rescount, 100];
140
                        break;
141
                    case 'ranking':
142
                        return AbstractLink::getCurrentUserRanking($stud_id, $students);
143
                        break;
144
                    default:
145
                        return [$sum, $rescount];
146
                        break;
147
                }
148
            }
149
        }
150
    }
151
152
    /**
153
     * Get URL where to go to if the user clicks on the link.
154
     */
155
    public function get_link()
156
    {
157
        $session_id = api_get_session_id();
158
        $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq_params(
159
            $this->get_course_code(),
160
                $session_id
161
        ).'&gradebook=view';
162
163
        if (!api_is_allowed_to_edit() || $this->calc_score(api_get_user_id()) == null) {
164
            $url .= '&action=view&lp_id='.$this->get_ref_id();
165
        } else {
166
            $url .= '&action=build&lp_id='.$this->get_ref_id();
167
        }
168
169
        return $url;
170
    }
171
172
    /**
173
     * Get name to display: same as learnpath title.
174
     */
175
    public function get_name()
176
    {
177
        $data = $this->get_learnpath_data();
178
179
        return $data['name'];
180
    }
181
182
    /**
183
     * Get description to display: same as learnpath description.
184
     */
185
    public function get_description()
186
    {
187
        $data = $this->get_learnpath_data();
188
189
        return $data['description'];
190
    }
191
192
    /**
193
     * Check if this still links to a learnpath.
194
     */
195
    public function is_valid_link()
196
    {
197
        $sql = 'SELECT count(id) FROM '.$this->get_learnpath_table().'
198
                WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
199
        $result = Database::query($sql);
200
        $number = Database::fetch_row($result, 'NUM');
201
202
        return $number[0] != 0;
203
    }
204
205
    public function get_type_name()
206
    {
207
        return get_lang('LearningPaths');
208
    }
209
210
    public function needs_name_and_description()
211
    {
212
        return false;
213
    }
214
215
    public function needs_max()
216
    {
217
        return false;
218
    }
219
220
    public function needs_results()
221
    {
222
        return false;
223
    }
224
225
    public function is_allowed_to_change_name()
226
    {
227
        return false;
228
    }
229
230
    public function get_icon_name()
231
    {
232
        return 'learnpath';
233
    }
234
235
    /**
236
     * Lazy load function to get the database table of the learnpath.
237
     */
238
    private function get_learnpath_table()
239
    {
240
        $this->learnpath_table = Database::get_course_table(TABLE_LP_MAIN);
241
242
        return $this->learnpath_table;
243
    }
244
245
    /**
246
     * Lazy load function to get the database contents of this learnpath.
247
     */
248
    private function get_learnpath_data()
249
    {
250
        if (!isset($this->learnpath_data)) {
251
            $sql = 'SELECT * FROM '.$this->get_learnpath_table().'
252
                    WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
253
            $result = Database::query($sql);
254
            $this->learnpath_data = Database::fetch_array($result);
255
        }
256
257
        return $this->learnpath_data;
258
    }
259
}
260