Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/gradebook/lib/be/attendancelink.class.php (1 issue)

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
class AttendanceLink extends AbstractLink
10
{
11
    private $attendance_table = null;
12
    private $itemprop_table = null;
13
14
    /**
15
     * Constructor.
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->set_type(LINK_ATTENDANCE);
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function get_type_name()
27
    {
28
        return get_lang('Attendance');
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    public function is_allowed_to_change_name()
35
    {
36
        return false;
37
    }
38
39
    /**
40
     * Generate an array of all attendances available.
41
     *
42
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
43
     */
44
    public function get_all_links()
45
    {
46
        if (empty($this->course_code)) {
47
            return [];
48
        }
49
        $tbl_attendance = $this->get_attendance_table();
50
        $sessionId = $this->get_session_id();
51
52
        $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
53
                FROM '.$tbl_attendance.' att
54
                WHERE
55
                    att.c_id = '.$this->course_id.' AND
56
                    att.active = 1 AND
57
                    att.session_id = '.$sessionId;
58
59
        $result = Database::query($sql);
60
61
        while ($data = Database::fetch_array($result)) {
62
            if (isset($data['attendance_qualify_title']) && '' != $data['attendance_qualify_title']) {
63
                $cats[] = [$data['id'], $data['attendance_qualify_title']];
64
            } else {
65
                $cats[] = [$data['id'], $data['name']];
66
            }
67
        }
68
        $my_cats = isset($cats) ? $cats : [];
69
70
        return $my_cats;
71
    }
72
73
    /**
74
     * Has anyone done this exercise yet ?
75
     */
76
    public function has_results()
77
    {
78
        $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
79
        $sessionId = $this->get_session_id();
80
81
        $sql = 'SELECT count(*) AS number FROM '.$tbl_attendance_result."
82
                WHERE
83
                    session_id = $sessionId AND
84
                    c_id = '.$this->course_id.' AND
85
                    attendance_id = '".$this->get_ref_id()."'";
86
        $result = Database::query($sql);
87
        $number = Database::fetch_row($result);
88
89
        return 0 != $number[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
        $sessionId = $this->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 = '.$this->get_ref_id().' AND
108
                    att.session_id = '.$sessionId;
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 = '.$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 (0 != $attendance['attendance_qualify_max']) {
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 (0 == $rescount) {
156
                return [null, 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;
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
        $sql = 'SELECT count(att.id) FROM '.$this->get_attendance_table().' att
220
                 WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
221
        $result = Database::query($sql);
222
        $number = Database::fetch_row($result);
223
224
        return 0 != $number[0];
225
    }
226
227
    public function get_link()
228
    {
229
        // it was extracts the attendance id
230
        $sessionId = $this->get_session_id();
231
        $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
232
                WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
233
        $result = Database::query($sql);
234
        $row = Database::fetch_array($result, 'ASSOC');
235
        $attendance_id = $row['id'];
236
        $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(), $sessionId);
237
238
        return $url;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function get_icon_name()
245
    {
246
        return 'attendance';
247
    }
248
249
    /**
250
     * Lazy load function to get the database table of the student publications.
251
     */
252
    private function get_attendance_table()
253
    {
254
        $this->attendance_table = Database::get_course_table(TABLE_ATTENDANCE);
255
256
        return $this->attendance_table;
257
    }
258
259
    /**
260
     * @return array|bool
261
     */
262
    private function get_attendance_data()
263
    {
264
        $tbl_name = $this->get_attendance_table();
265
        if ('' == $tbl_name) {
266
            return false;
267
        } elseif (!isset($this->attendance_data)) {
268
            $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
269
                    WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
270
            $query = Database::query($sql);
271
            $this->attendance_data = Database::fetch_array($query);
272
        }
273
274
        return $this->attendance_data;
275
    }
276
}
277