Passed
Push — master ( ccb5fd...57fe34 )
by Julito
13:03
created

ForumThreadLink::get_not_created_links()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 4
nop 0
dl 0
loc 32
rs 9.3222
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class ForumThreadLink.
6
 *
7
 * @author Bert Steppé
8
 *
9
 * @package chamilo.gradebook
10
 */
11
class ForumThreadLink extends AbstractLink
12
{
13
    private $forum_thread_table;
14
    private $itemprop_table;
0 ignored issues
show
introduced by
The private property $itemprop_table is not used, and could be removed.
Loading history...
15
16
    /**
17
     * Constructor.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
        $this->set_type(LINK_FORUM_THREAD);
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function get_type_name()
29
    {
30
        return get_lang('ForumThreads');
31
    }
32
33
    /**
34
     * @return bool
35
     */
36
    public function is_allowed_to_change_name()
37
    {
38
        return false;
39
    }
40
41
    /**
42
     * Generate an array of all exercises available.
43
     *
44
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
45
     */
46
    public function get_all_links()
47
    {
48
        if (empty($this->course_code)) {
49
            return [];
50
        }
51
52
        $tbl_grade_links = Database::get_course_table(TABLE_FORUM_THREAD);
53
        $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
54
        $sessionId = $this->get_session_id();
55
56
        if ($sessionId) {
57
            $session_condition = 'tl.session_id='.$sessionId;
58
        } else {
59
            $session_condition = '(tl.session_id = 0 OR tl.session_id IS NULL)';
60
        }
61
62
        $sql = 'SELECT tl.thread_id, tl.thread_title, tl.thread_title_qualify
63
                FROM '.$tbl_grade_links.' tl INNER JOIN '.$tbl_item_property.' ip
64
                ON (tl.thread_id = ip.ref AND tl.c_id = ip.c_id)
65
                WHERE
66
                    tl.c_id = '.$this->course_id.' AND
67
                    ip.c_id = '.$this->course_id.' AND
68
                    ip.tool = "forum_thread" AND
69
                    ip.visibility <> 2 AND
70
                    '.$session_condition.'
71
                ';
72
73
        $result = Database::query($sql);
74
        while ($data = Database::fetch_array($result)) {
75
            if (isset($data['thread_title_qualify']) && $data['thread_title_qualify'] != '') {
76
                $cats[] = [$data['thread_id'], $data['thread_title_qualify']];
77
            } else {
78
                $cats[] = [$data['thread_id'], $data['thread_title']];
79
            }
80
        }
81
        $my_cats = isset($cats) ? $cats : [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cats does not seem to be defined for all execution paths leading up to this point.
Loading history...
82
83
        return $my_cats;
84
    }
85
86
    /**
87
     * Has anyone done this exercise yet ?
88
     *
89
     * @return bool
90
     */
91
    public function has_results()
92
    {
93
        $table = Database::get_course_table(TABLE_FORUM_POST);
94
95
        $sql = "SELECT count(*) AS number FROM $table
96
                WHERE
97
                    c_id = ".$this->course_id." AND
98
                    thread_id = '".$this->get_ref_id()."'                    
99
                ";
100
        $result = Database::query($sql);
101
        $number = Database::fetch_row($result);
102
103
        return $number[0] != 0;
104
    }
105
106
    /**
107
     * @param int    $stud_id
108
     * @param string $type
109
     *
110
     * @return array|null
111
     */
112
    public function calc_score($stud_id = null, $type = null)
113
    {
114
        require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
115
        $threadInfo = get_thread_information('', $this->get_ref_id());
116
        $thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
117
        $sessionId = $this->get_session_id();
118
        $sessionCondition = api_get_session_condition(
119
            $sessionId,
120
            true,
121
            false,
122
            'session_id'
123
        );
124
125
        $sql = 'SELECT thread_qualify_max
126
                FROM '.Database::get_course_table(TABLE_FORUM_THREAD)."
127
                WHERE 
128
                    c_id = ".$this->course_id." AND 
129
                    thread_id = '".$this->get_ref_id()."'
130
                    $sessionCondition
131
                ";
132
        $query = Database::query($sql);
133
        $assignment = Database::fetch_array($query);
134
135
        $sql = "SELECT * FROM $thread_qualify
136
                WHERE 
137
                    c_id = ".$this->course_id." AND 
138
                    thread_id = ".$this->get_ref_id()."
139
                    $sessionCondition
140
                ";
141
        if (isset($stud_id)) {
142
            $sql .= ' AND user_id = '.intval($stud_id);
143
        }
144
145
        // order by id, that way the student's first attempt is accessed first
146
        $sql .= ' ORDER BY qualify_time DESC';
147
        $scores = Database::query($sql);
148
149
        // for 1 student
150
        if (isset($stud_id)) {
151
            if ($threadInfo['thread_peer_qualify'] == 0) {
152
                // Classic way of calculate score
153
                if ($data = Database::fetch_array($scores)) {
154
                    return [
155
                        $data['qualify'],
156
                        $assignment['thread_qualify_max'],
157
                    ];
158
                } else {
159
                    // We sent the 0/thread_qualify_max instead of null for correct calculations
160
                    return [0, $assignment['thread_qualify_max']];
161
                }
162
            } else {
163
                // Take average
164
                $score = 0;
165
                $counter = 0;
166
                if (Database::num_rows($scores)) {
167
                    while ($data = Database::fetch_array($scores, 'ASSOC')) {
168
                        $score += $data['qualify'];
169
                        $counter++;
170
                    }
171
                }
172
                // If no result
173
                if (empty($counter) || $counter <= 2) {
174
                    return [0, $assignment['thread_qualify_max']];
175
                }
176
177
                return [$score / $counter, $assignment['thread_qualify_max']];
178
            }
179
        } else {
180
            // All students -> get average
181
            $students = []; // user list, needed to make sure we only
182
            // take first attempts into account
183
            $counter = 0;
184
            $sum = 0;
185
            $bestResult = 0;
186
            $weight = 0;
187
            $sumResult = 0;
188
189
            while ($data = Database::fetch_array($scores)) {
190
                if (!(array_key_exists($data['user_id'], $students))) {
191
                    if ($assignment['thread_qualify_max'] != 0) {
192
                        $students[$data['user_id']] = $data['qualify'];
193
                        $counter++;
194
                        $sum += $data['qualify'] / $assignment['thread_qualify_max'];
195
                        $sumResult += $data['qualify'];
196
                        if ($data['qualify'] > $bestResult) {
197
                            $bestResult = $data['qualify'];
198
                        }
199
                        $weight = $assignment['thread_qualify_max'];
200
                    }
201
                }
202
            }
203
204
            if ($counter == 0) {
205
                return [null, null];
206
            } else {
207
                switch ($type) {
208
                    case 'best':
209
                        return [$bestResult, $weight];
210
                        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...
211
                    case 'average':
212
                        return [$sumResult / $counter, $weight];
213
                        break;
214
                    case 'ranking':
215
                        return AbstractLink::getCurrentUserRanking($stud_id, $students);
216
                        break;
217
                    default:
218
                        return [$sum, $counter];
219
                        break;
220
                }
221
            }
222
        }
223
    }
224
225
    public function needs_name_and_description()
226
    {
227
        return false;
228
    }
229
230
    public function needs_max()
231
    {
232
        return false;
233
    }
234
235
    public function needs_results()
236
    {
237
        return false;
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function get_name()
244
    {
245
        $this->get_exercise_data();
246
        $thread_title = isset($this->exercise_data['thread_title']) ? $this->exercise_data['thread_title'] : '';
247
        $thread_title_qualify = isset($this->exercise_data['thread_title_qualify']) ? $this->exercise_data['thread_title_qualify'] : '';
248
        if (isset($thread_title_qualify) && $thread_title_qualify != '') {
249
            return $this->exercise_data['thread_title_qualify'];
250
        }
251
252
        return $thread_title;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function get_description()
259
    {
260
        return ''; //$this->exercise_data['description'];
261
    }
262
263
    /**
264
     * Check if this still links to an exercise.
265
     */
266
    public function is_valid_link()
267
    {
268
        $sessionId = $this->get_session_id();
269
        $sql = 'SELECT count(id) from '.$this->get_forum_thread_table().'
270
                WHERE 
271
                    c_id = '.$this->course_id.' AND 
272
                    thread_id = '.$this->get_ref_id().' AND 
273
                    session_id='.$sessionId;
274
        $result = Database::query($sql);
275
        $number = Database::fetch_row($result);
276
277
        return $number[0] != 0;
278
    }
279
280
    public function get_link()
281
    {
282
        $sessionId = $this->get_session_id();
283
        //it was extracts the forum id
284
        $sql = 'SELECT * FROM '.$this->get_forum_thread_table()."
285
                WHERE
286
                    c_id = '.$this->course_id.' AND 
287
                    thread_id = '".$this->get_ref_id()."' AND 
288
                    session_id = $sessionId ";
289
        $result = Database::query($sql);
290
        $row = Database::fetch_array($result, 'ASSOC');
291
        $forum_id = $row['forum_id'];
292
293
        $url = api_get_path(WEB_PATH).'main/forum/viewthread.php?'.api_get_cidreq_params($this->get_course_code(), $sessionId).'&thread='.$this->get_ref_id().'&gradebook=view&forum='.$forum_id;
294
295
        return $url;
296
    }
297
298
    public function get_icon_name()
299
    {
300
        return 'forum';
301
    }
302
303
    public function save_linked_data()
304
    {
305
        $weight = $this->get_weight();
306
        $ref_id = $this->get_ref_id();
307
308
        if (!empty($ref_id)) {
309
            $sql = 'UPDATE '.$this->get_forum_thread_table().' SET 
310
                    thread_weight='.api_float_val($weight).'
311
                    WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
312
            Database::query($sql);
313
        }
314
    }
315
316
    public function delete_linked_data()
317
    {
318
        $ref_id = $this->get_ref_id();
319
        if (!empty($ref_id)) {
320
            // Cleans forum
321
            $sql = 'UPDATE '.$this->get_forum_thread_table().' SET
322
                    thread_qualify_max = 0,
323
                    thread_weight = 0,
324
                    thread_title_qualify = ""
325
                    WHERE c_id = '.$this->course_id.' AND thread_id= '.$ref_id;
326
            Database::query($sql);
327
        }
328
    }
329
330
    /**
331
     * Lazy load function to get the database table of the student publications.
332
     */
333
    private function get_forum_thread_table()
334
    {
335
        return $this->forum_thread_table = Database::get_course_table(TABLE_FORUM_THREAD);
336
    }
337
338
    private function get_exercise_data()
339
    {
340
        $sessionId = $this->get_session_id();
341
        if ($sessionId) {
342
            $session_condition = 'session_id = '.$sessionId;
343
        } else {
344
            $session_condition = '(session_id = 0 OR session_id IS NULL)';
345
        }
346
347
        if (!isset($this->exercise_data)) {
348
            $sql = 'SELECT * FROM '.$this->get_forum_thread_table().'
349
                    WHERE 
350
                        c_id = '.$this->course_id.' AND  
351
                        thread_id = '.$this->get_ref_id().' AND 
352
                        '.$session_condition;
353
            $query = Database::query($sql);
354
            $this->exercise_data = Database::fetch_array($query);
0 ignored issues
show
Bug Best Practice introduced by
The property exercise_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
355
        }
356
357
        return $this->exercise_data;
358
    }
359
}
360