Test Setup Failed
Push — master ( f71949...6c6bd7 )
by Julito
55:21
created

LearnpathLink::get_all_links()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 5
Ratio 20.83 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 0
dl 5
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Defines a gradebook LearnpathLink object.
6
 * @author Yannick Warnier <[email protected]>
7
 * @author Bert Steppé
8
 * @package chamilo.gradebook
9
 */
10
class LearnpathLink extends AbstractLink
11
{
12
    private $course_info = null;
0 ignored issues
show
Unused Code introduced by
The property $course_info is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
    private $learnpath_table = null;
14
    private $learnpath_data = null;
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 learnpaths that a teacher hasn't created a link for.
27
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
28
     */
29
    public function get_not_created_links()
30
    {
31
        return false;
32
        if (empty($this->course_code)) {
0 ignored issues
show
Unused Code introduced by
if (empty($this->course_...ourse code not set'); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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