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

AttendanceLink::get_name()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 8
nop 0
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Gradebook link to attendance item.
6
 *
7
 * @author Christian Fasanando ([email protected])
8
 *
9
 * @package chamilo.gradebook
10
 */
11
class AttendanceLink extends AbstractLink
12
{
13
    private $attendance_table = null;
14
    private $itemprop_table = null;
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_ATTENDANCE);
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function get_type_name()
29
    {
30
        return get_lang('Attendance');
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 attendances 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
        $tbl_attendance = $this->get_attendance_table();
52
        $session_id = api_get_session_id();
53
        $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
54
                FROM '.$tbl_attendance.' att
55
                WHERE
56
                    att.c_id = '.$this->course_id.' AND
57
                    att.active = 1 AND
58
                    att.session_id = '.$session_id;
59
60
        $result = Database::query($sql);
61
62
        while ($data = Database::fetch_array($result)) {
63
            if (isset($data['attendance_qualify_title']) && $data['attendance_qualify_title'] != '') {
64
                $cats[] = [$data['id'], $data['attendance_qualify_title']];
65
            } else {
66
                $cats[] = [$data['id'], $data['name']];
67
            }
68
        }
69
        $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...
70
71
        return $my_cats;
72
    }
73
74
    /**
75
     * Has anyone done this exercise yet ?
76
     */
77
    public function has_results()
78
    {
79
        $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
80
        $session_id = api_get_session_id();
81
        $sql = 'SELECT count(*) AS number FROM '.$tbl_attendance_result."
82
                WHERE
83
                    session_id = $session_id AND
84
                    c_id = '.$this->course_id.' AND
85
                    attendance_id = '".intval($this->get_ref_id())."'";
86
        $result = Database::query($sql);
87
        $number = Database::fetch_row($result);
88
89
        return $number[0] != 0;
90
    }
91
92
    /**
93
     * @param int $stud_id
94
     *
95
     * @return array|null
96
     */
97
    public function calc_score($stud_id = null, $type = null)
98
    {
99
        $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
100
        $session_id = api_get_session_id();
101
102
        // get attendance qualify max
103
        $sql = 'SELECT att.attendance_qualify_max
104
                FROM '.$this->get_attendance_table().' att
105
                WHERE
106
                    att.c_id = '.$this->course_id.' AND
107
                    att.id = '.intval($this->get_ref_id()).' AND
108
                    att.session_id='.intval($session_id).'';
109
        $query = Database::query($sql);
110
        $attendance = Database::fetch_array($query, 'ASSOC');
111
112
        // Get results
113
        $sql = 'SELECT *
114
                FROM '.$tbl_attendance_result.'
115
                WHERE c_id = '.$this->course_id.' AND attendance_id = '.intval($this->get_ref_id());
116
        if (isset($stud_id)) {
117
            $sql .= ' AND user_id = '.intval($stud_id);
118
        }
119
        $scores = Database::query($sql);
120
        // for 1 student
121
        if (isset($stud_id)) {
122
            if ($data = Database::fetch_array($scores, 'ASSOC')) {
123
                return [
124
                    $data['score'],
125
                    $attendance['attendance_qualify_max'],
126
                ];
127
            } else {
128
                //We sent the 0/attendance_qualify_max instead of null for correct calculations
129
                return [0, $attendance['attendance_qualify_max']];
130
            }
131
        } else {
132
            // all students -> get average
133
            $students = []; // user list, needed to make sure we only
134
            // take first attempts into account
135
            $rescount = 0;
136
            $sum = 0;
137
            $sumResult = 0;
138
            $bestResult = 0;
139
140
            while ($data = Database::fetch_array($scores)) {
141
                if (!(array_key_exists($data['user_id'], $students))) {
142
                    if ($attendance['attendance_qualify_max'] != 0) {
143
                        $students[$data['user_id']] = $data['score'];
144
                        $rescount++;
145
                        $sum += $data['score'] / $attendance['attendance_qualify_max'];
146
                        $sumResult += $data['score'];
147
                        if ($data['score'] > $bestResult) {
148
                            $bestResult = $data['score'];
149
                        }
150
                        $weight = $attendance['attendance_qualify_max'];
151
                    }
152
                }
153
            }
154
155
            if ($rescount == 0) {
156
                return null;
157
            } else {
158
                switch ($type) {
159
                    case 'best':
160
                        return [$bestResult, $weight];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $weight does not seem to be defined for all execution paths leading up to this point.
Loading history...
161
                        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...
162
                    case 'average':
163
                        return [$sumResult / $rescount, $weight];
164
                        break;
165
                    case 'ranking':
166
                        return AbstractLink::getCurrentUserRanking($stud_id, $students);
167
                        break;
168
                    default:
169
                        return [$sum, $rescount];
170
                        break;
171
                }
172
            }
173
        }
174
    }
175
176
    public function needs_name_and_description()
177
    {
178
        return false;
179
    }
180
181
    public function needs_max()
182
    {
183
        return false;
184
    }
185
186
    public function needs_results()
187
    {
188
        return false;
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function get_name()
195
    {
196
        $this->get_attendance_data();
197
        $attendance_title = isset($this->attendance_data['name']) ? $this->attendance_data['name'] : '';
198
        $attendance_qualify_title = isset($this->attendance_data['attendance_qualify_title']) ? $this->attendance_data['attendance_qualify_title'] : '';
199
        if (isset($attendance_qualify_title) && $attendance_qualify_title != '') {
200
            return $this->attendance_data['attendance_qualify_title'];
201
        } else {
202
            return $attendance_title;
203
        }
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function get_description()
210
    {
211
        return '';
212
    }
213
214
    /**
215
     * Check if this still links to an exercise.
216
     */
217
    public function is_valid_link()
218
    {
219
        $session_id = api_get_session_id();
220
        $sql = 'SELECT count(att.id) FROM '.$this->get_attendance_table().' att
221
                 WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
222
        $result = Database::query($sql);
223
        $number = Database::fetch_row($result);
224
225
        return $number[0] != 0;
226
    }
227
228
    public function get_link()
229
    {
230
        //it was extracts the attendance id
231
        $session_id = api_get_session_id();
232
        $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
233
                WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
234
        $result = Database::query($sql);
235
        $row = Database::fetch_array($result, 'ASSOC');
236
        $attendance_id = $row['id'];
237
        $url = api_get_path(WEB_PATH).'main/attendance/index.php?action=attendance_sheet_list&gradebook=view&attendance_id='.$attendance_id.'&'.api_get_cidreq_params($this->get_course_code(), $session_id);
238
239
        return $url;
240
    }
241
242
    /**
243
     * @return string
244
     */
245
    public function get_icon_name()
246
    {
247
        return 'attendance';
248
    }
249
250
    /**
251
     * Lazy load function to get the database table of the student publications.
252
     */
253
    private function get_attendance_table()
254
    {
255
        $this->attendance_table = Database::get_course_table(TABLE_ATTENDANCE);
256
257
        return $this->attendance_table;
258
    }
259
260
    /**
261
     * @return array|bool
262
     */
263
    private function get_attendance_data()
264
    {
265
        $tbl_name = $this->get_attendance_table();
266
        $session_id = api_get_session_id();
267
        if ($tbl_name == '') {
268
            return false;
269
        } elseif (!isset($this->attendance_data)) {
270
            $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
271
                    WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
272
            $query = Database::query($sql);
273
            $this->attendance_data = Database::fetch_array($query);
0 ignored issues
show
Bug Best Practice introduced by
The property attendance_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
274
        }
275
276
        return $this->attendance_data;
277
    }
278
}
279