Issues (2037)

gradebook/lib/be/studentpublicationlink.class.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Gradebook link to student publication item.
6
 *
7
 * @author Bert Steppé
8
 */
9
class StudentPublicationLink extends AbstractLink
10
{
11
    private $studpub_table;
12
    private $itemprop_table;
13
14
    /**
15
     * Constructor.
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->set_type(LINK_STUDENTPUBLICATION);
21
    }
22
23
    /**
24
     * Returns the URL of a document
25
     * This function is loaded when using a gradebook as a tab (gradebook = -1)
26
     * see issue #2705.
27
     */
28
    public function get_view_url($stud_id)
29
    {
30
        return null;
31
        // find a file uploaded by the given student,
32
        // with the same title as the evaluation name
33
34
        $eval = $this->get_evaluation();
35
        $stud_id = (int) $stud_id;
36
        $itemProperty = $this->get_itemprop_table();
37
        $workTable = $this->get_studpub_table();
38
        $courseId = $this->course_id;
39
40
        $sql = "SELECT pub.url
41
                FROM $itemProperty prop 
42
                INNER JOIN $workTable pub
43
                ON (prop.c_id = pub.c_id AND prop.ref = pub.id)
44
                WHERE
45
                    prop.c_id = $courseId AND
46
                    pub.c_id = $courseId AND
47
                    prop.tool = 'work' AND 
48
                    prop.insert_user_id = $stud_id AND                     
49
                    pub.title = '".Database::escape_string($eval->get_name())."' AND 
50
                    pub.session_id=".api_get_session_id();
51
52
        $result = Database::query($sql);
53
        if ($fileurl = Database::fetch_row($result)) {
54
            return null;
55
        } else {
56
            return null;
57
        }
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function get_type_name()
64
    {
65
        return get_lang('Works');
66
    }
67
68
    public function is_allowed_to_change_name()
69
    {
70
        return false;
71
    }
72
73
    /**
74
     * Generate an array of all exercises available.
75
     *
76
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
77
     */
78
    public function get_all_links()
79
    {
80
        if (empty($this->course_code)) {
81
            return [];
82
        }
83
        $em = Database::getManager();
84
        $sessionId = $this->get_session_id();
85
        $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
86
        /*
87
        if (empty($session_id)) {
88
            $session_condition = api_get_session_condition(0, true);
89
        } else {
90
            $session_condition = api_get_session_condition($session_id, true, true);
91
        }
92
        $sql = "SELECT id, url, title FROM $tbl_grade_links
93
                WHERE c_id = {$this->course_id}  AND filetype='folder' AND active = 1 $session_condition ";*/
94
95
        //Only show works from the session
96
        //AND has_properties != ''
97
        $links = $em
98
            ->getRepository('ChamiloCourseBundle:CStudentPublication')
99
            ->findBy([
100
                'cId' => $this->course_id,
101
                'active' => true,
102
                'filetype' => 'folder',
103
                'session' => $session,
104
            ]);
105
106
        foreach ($links as $data) {
107
            $work_name = $data->getTitle();
108
            if (empty($work_name)) {
109
                $work_name = basename($data->getUrl());
110
            }
111
            $cats[] = [$data->getId(), $work_name];
112
        }
113
        $cats = isset($cats) ? $cats : [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cats seems to be defined by a foreach iteration on line 106. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
114
115
        return $cats;
116
    }
117
118
    /**
119
     * Has anyone done this exercise yet ?
120
     */
121
    public function has_results()
122
    {
123
        $data = $this->get_exercise_data();
124
125
        if (empty($data)) {
126
            return '';
127
        }
128
        $id = $data['id'];
129
130
        $em = Database::getManager();
131
        $session = $em->find('ChamiloCoreBundle:Session', $this->get_session_id());
132
        $results = $em
133
            ->getRepository('ChamiloCourseBundle:CStudentPublication')
134
            ->findBy([
135
                'cId' => $this->course_id,
136
                'parentId' => $id,
137
                'session' => $session,
138
            ]);
139
140
        return 0 != count($results);
141
    }
142
143
    /**
144
     * @param null $stud_id
145
     *
146
     * @return array
147
     */
148
    public function calc_score($stud_id = null, $type = null)
149
    {
150
        $stud_id = (int) $stud_id;
151
        $em = Database::getManager();
152
        $data = $this->get_exercise_data();
153
154
        if (empty($data)) {
155
            return [];
156
        }
157
        $id = $data['id'];
158
        $session = api_get_session_entity($this->get_session_id());
159
160
        $assignment = $em
161
            ->getRepository('ChamiloCourseBundle:CStudentPublication')
162
            ->findOneBy([
163
                'cId' => $this->course_id,
164
                'id' => $id,
165
                'session' => $session,
166
            ])
167
        ;
168
169
        $parentId = !$assignment ? 0 : $assignment->getId();
170
171
        if (empty($session)) {
172
            $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
173
                   WHERE
174
                        a.cId = :course AND
175
                        a.active = :active AND
176
                        a.parentId = :parent AND
177
                        a.session is null AND
178
                        a.qualificatorId <> 0
179
                    ';
180
181
            $params = [
182
                'course' => $this->course_id,
183
                'parent' => $parentId,
184
                'active' => true,
185
            ];
186
        } else {
187
            $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a
188
                    WHERE
189
                        a.cId = :course AND
190
                        a.active = :active AND
191
                        a.parentId = :parent AND
192
                        a.session = :session AND
193
                        a.qualificatorId <> 0
194
                    ';
195
196
            $params = [
197
                'course' => $this->course_id,
198
                'parent' => $parentId,
199
                'session' => $session,
200
                'active' => true,
201
            ];
202
        }
203
204
        if (!empty($stud_id)) {
205
            $dql .= ' AND a.userId = :student ';
206
            $params['student'] = $stud_id;
207
        }
208
209
        $order = api_get_setting('student_publication_to_take_in_gradebook');
210
211
        switch ($order) {
212
            case 'last':
213
                // latest attempt
214
                $dql .= ' ORDER BY a.sentDate DESC';
215
                break;
216
            case 'first':
217
            default:
218
                // first attempt
219
                $dql .= ' ORDER BY a.id';
220
                break;
221
        }
222
223
        $scores = $em->createQuery($dql)->execute($params);
224
225
        // for 1 student
226
        if (!empty($stud_id)) {
227
            if (!count($scores)) {
228
                return [null, null];
229
            }
230
231
            $data = $scores[0];
232
233
            return [
234
                $data->getQualification(),
235
                $assignment->getQualification(),
236
                api_get_local_time($assignment->getDateOfQualification()),
237
                1,
238
            ];
239
        }
240
241
        $students = []; // user list, needed to make sure we only
242
        // take first attempts into account
243
        $rescount = 0;
244
        $sum = 0;
245
        $bestResult = 0;
246
        $weight = 0;
247
        $sumResult = 0;
248
249
        foreach ($scores as $data) {
250
            if (!(array_key_exists($data->getUserId(), $students))) {
251
                if (0 != $assignment->getQualification()) {
252
                    $students[$data->getUserId()] = $data->getQualification();
253
                    $rescount++;
254
                    $sum += $data->getQualification() / $assignment->getQualification();
255
                    $sumResult += $data->getQualification();
256
257
                    if ($data->getQualification() > $bestResult) {
258
                        $bestResult = $data->getQualification();
259
                    }
260
                    $weight = $assignment->getQualification();
261
                }
262
            }
263
        }
264
265
        if (0 == $rescount) {
266
            return [null, null];
267
        }
268
269
        switch ($type) {
270
            case 'best':
271
                return [$bestResult, $weight];
272
                break;
273
            case 'average':
274
                return [$sumResult / $rescount, $weight];
275
                break;
276
            case 'ranking':
277
                return AbstractLink::getCurrentUserRanking($stud_id, $students);
278
                break;
279
            default:
280
                return [$sum, $rescount];
281
                break;
282
        }
283
    }
284
285
    public function needs_name_and_description()
286
    {
287
        return false;
288
    }
289
290
    public function get_name()
291
    {
292
        $this->get_exercise_data();
293
        $name = isset($this->exercise_data['title']) && !empty($this->exercise_data['title']) ? $this->exercise_data['title'] : get_lang('Untitled');
294
295
        return $name;
296
    }
297
298
    public function get_description()
299
    {
300
        $this->get_exercise_data();
301
302
        return isset($this->exercise_data['description']) ? $this->exercise_data['description'] : null;
303
    }
304
305
    public function get_link()
306
    {
307
        $sessionId = $this->get_session_id();
308
        $url = api_get_path(WEB_PATH).'main/work/work.php?'.api_get_cidreq_params($this->get_course_code(), $sessionId).'&id='.$this->exercise_data['id'].'&gradebook=view';
309
310
        return $url;
311
    }
312
313
    public function needs_max()
314
    {
315
        return false;
316
    }
317
318
    public function needs_results()
319
    {
320
        return false;
321
    }
322
323
    public function is_valid_link()
324
    {
325
        $data = $this->get_exercise_data();
326
327
        if (empty($data)) {
328
            return '';
329
        }
330
        $id = $data['id'];
331
        $sql = 'SELECT count(id) FROM '.$this->get_studpub_table().'
332
                WHERE 
333
                    c_id = "'.$this->course_id.'" AND 
334
                    id = '.$id;
335
        $result = Database::query($sql);
336
        $number = Database::fetch_row($result);
337
338
        return 0 != $number[0];
339
    }
340
341
    public function get_icon_name()
342
    {
343
        return 'studentpublication';
344
    }
345
346
    public function save_linked_data()
347
    {
348
        $data = $this->get_exercise_data();
349
350
        if (empty($data)) {
351
            return '';
352
        }
353
        $id = $data['id'];
354
355
        $weight = api_float_val($this->get_weight());
356
        if (!empty($id)) {
357
            //Cleans works
358
            $sql = 'UPDATE '.$this->get_studpub_table().' 
359
                    SET weight= '.$weight.'
360
                    WHERE c_id = '.$this->course_id.' AND id ='.$id;
361
            Database::query($sql);
362
        }
363
    }
364
365
    /**
366
     * @return string
367
     */
368
    public function delete_linked_data()
369
    {
370
        $data = $this->get_exercise_data();
371
        if (empty($data)) {
372
            return '';
373
        }
374
375
        if (!empty($id)) {
376
            //Cleans works
377
            $sql = 'UPDATE '.$this->get_studpub_table().' 
378
                    SET weight = 0
379
                    WHERE c_id = '.$this->course_id.' AND id ='.$id;
380
            Database::query($sql);
381
        }
382
    }
383
384
    /**
385
     * Lazy load function to get the database table of the student publications.
386
     */
387
    private function get_studpub_table()
388
    {
389
        return $this->studpub_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
390
    }
391
392
    /**
393
     * Lazy load function to get the database table of the item properties.
394
     */
395
    private function get_itemprop_table()
396
    {
397
        return $this->itemprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
398
    }
399
400
    /**
401
     * @return array
402
     */
403
    private function get_exercise_data()
404
    {
405
        $course_info = api_get_course_info($this->get_course_code());
406
        if (!isset($this->exercise_data)) {
407
            $sql = 'SELECT * FROM '.$this->get_studpub_table()."
408
                    WHERE
409
                        c_id ='".$course_info['real_id']."' AND
410
                        id = '".$this->get_ref_id()."' ";
411
            $query = Database::query($sql);
412
            $this->exercise_data = Database::fetch_array($query);
413
414
            // Try with iid
415
            if (empty($this->exercise_data)) {
416
                $sql = 'SELECT * FROM '.$this->get_studpub_table()."
417
                        WHERE
418
                            c_id ='".$course_info['real_id']."' AND
419
                            iid = '".$this->get_ref_id()."' ";
420
                $query = Database::query($sql);
421
                $this->exercise_data = Database::fetch_array($query);
422
            }
423
        }
424
425
        return $this->exercise_data;
426
    }
427
}
428