Passed
Push — master ( 34bdad...b152c5 )
by Julito
20:20
created

BlockDaily::getContent()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 4
nop 0
dl 0
loc 41
rs 8.4906
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of course block plugin for dashboard,
4
 * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
5
 *
6
 * @package chamilo.dashboard
7
 *
8
 * @author Marco Sousa original code
9
 * @author Julio Montoya class named was changed of name, and some minor changes
10
 */
11
12
/**
13
 * required files for getting data.
14
 */
15
16
/**
17
 * This class is used like controller for this course block plugin,
18
 * the class name must be registered inside path.info file
19
 * (e.g: controller = "BlockDiario"), so dashboard controller will be instantiate it.
20
 *
21
 * @package chamilo.dashboard
22
 */
23
class BlockDaily extends Block
24
{
25
    private $user_id;
26
    private $courses;
27
    private $permission = [DRH];
28
29
    /**
30
     * Constructor.
31
     */
32
    public function __construct($user_id)
33
    {
34
        $this->user_id = $user_id;
35
        $this->path = 'block_daily';
36
        if ($this->is_block_visible_for_user($user_id)) {
37
            $this->courses = CourseManager::get_courses_followed_by_drh(
38
                $user_id
39
            );
40
        }
41
    }
42
43
    /**
44
     * This method check if a user is allowed to see the block inside dashboard interface.
45
     *
46
     * @param    int        User id
47
     *
48
     * @return bool Is block visible for user
49
     */
50
    public function is_block_visible_for_user($user_id)
51
    {
52
        $user_info = api_get_user_info($user_id);
53
        $user_status = $user_info['status'];
54
        $is_block_visible_for_user = false;
55
        if (UserManager::is_admin($user_id) || in_array(
56
                $user_status,
57
                $this->permission
58
            )
59
        ) {
60
            $is_block_visible_for_user = true;
61
        }
62
63
        return $is_block_visible_for_user;
64
    }
65
66
    /**
67
     * This method return content html containing information about courses and its position for showing it inside dashboard interface
68
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
69
     *
70
     * @return array column and content html
71
     */
72
    public function get_block()
73
    {
74
        $column = 2;
75
        $data = [];
76
77
        $html = $this->getBlockCard(
78
            get_lang('GradebookAndAttendances'),
79
            $this->getContent()
80
        );
81
82
        $data['column'] = $column;
83
        $data['content_html'] = $html;
84
85
        return $data;
86
    }
87
88
    /**
89
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
90
     *
91
     * @return string content html
92
     */
93
    public function getContent()
94
    {
95
        $course_data = $this->get_course_information_data();
96
        $content = '<h4>'.get_lang('YourCourseList').'</h4>';
97
        $data_table = null;
98
        if (!empty($course_data)) {
99
            $data_table .= '<table class="data_table" width:"95%">';
100
            $data_table .= '<tr>
101
	    						<th>'.get_lang('CourseTitle').'</th>
102
	    						<th width="20%">'.get_lang('NbStudents').'</th>
103
	    						<th width="20%">'.get_lang('Evaluation').'</th>
104
	    						<th width="20%">'.get_lang('ToolAttendance').'</th>
105
	    					</tr>';
106
            $i = 1;
107
            foreach ($course_data as $course) {
108
                if ($i % 2 == 0) {
109
                    $class_tr = 'row_odd';
110
                } else {
111
                    $class_tr = 'row_even';
112
                }
113
                $data_table .= '<tr class="'.$class_tr.'">';
114
                if (!isset($course[3])) {
115
                    $course[3] = get_lang('NotAvailable');
116
                }
117
                foreach ($course as $cell) {
118
                    $data_table .= '<td align="right">'.$cell.'</td>';
119
                }
120
                $data_table .= '</tr>';
121
                $i++;
122
            }
123
            $data_table .= '</table>';
124
        } else {
125
            $data_table .= get_lang('ThereIsNoInformationAboutYourCourses');
126
        }
127
        $content .= $data_table;
128
        if (!empty($course_data)) {
129
            $content .= '<div style="text-align:right;margin-top:10px;">
130
            <a href="'.api_get_path(WEB_CODE_PATH).'mySpace/course.php">'.get_lang('SeeMore').'</a></div>';
131
        }
132
        //$content .= '</div>';
133
        return $content;
134
    }
135
136
    /**
137
     * Get number of courses.
138
     *
139
     * @return int
140
     */
141
    public function get_number_of_courses()
142
    {
143
        return count($this->courses);
144
    }
145
146
    /**
147
     * Get course information data.
148
     *
149
     * @return array
150
     */
151
    public function get_course_information_data()
152
    {
153
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
154
        $course_data = [];
155
        $courses = $this->courses;
156
157
        foreach ($courses as $row_course) {
158
            $score = null;
159
            $courseId = $row_course['c_id'];
160
            $course_info = api_get_course_info_by_id($courseId);
161
            $course_code = $course_info['code'];
162
            if (empty($course_info)) {
163
                continue;
164
            }
165
166
            // Attendance table
167
            $table_course = Database::get_course_table(TABLE_ATTENDANCE);
168
169
            $sql = "SELECT id, name, attendance_qualify_max FROM $table_course
170
                    WHERE c_id = ".$course_info['real_id']." AND active = 1 AND session_id = 0";
171
            $rs = Database::query($sql);
172
            $attendance = [];
173
            $attendances = [];
174
175
            while ($row = Database::fetch_array($rs, 'ASSOC')) {
176
                $attendance['done'] = $row['attendance_qualify_max'];
177
                $attendance['id'] = $row['id'];
178
                //$attendance['name'] = $row['name'];
179
                $attendance['course_code'] = $course_info['code'];
180
181
                if ($attendance['done'] != '0') {
182
                    $attendances[] = '<a href="'.api_get_path(WEB_PATH).'main/attendance/index.php?cidReq='.$attendance['course_code'].'&action=attendance_sheet_print&attendance_id='.$attendance['id'].'">'.Display::return_icon('printmgr.gif', get_lang('Print')).'</a>';
183
                } else {
184
                    $attendances[] = get_lang("NotAvailable");
185
                }
186
            }
187
            if (count($attendances) == 0) {
188
                $attendances[] = get_lang("NotAvailable");
189
            }
190
191
            // Number of students
192
193
            $sql = "SELECT user_id FROM $tbl_course_user as course_rel_user
194
                    WHERE course_rel_user.status=".STUDENT." AND course_rel_user.c_id=$courseId";
195
            $rs = Database::query($sql);
196
            $users = [];
197
            while ($row = Database::fetch_array($rs)) {
198
                $users[] = $row['user_id'];
199
            }
200
            if (count($users) > 0) {
201
                $nb_students_in_course = count($users);
202
            }
203
204
            if (!empty($tematic_advance)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tematic_advance does not exist. Did you maybe mean $tematic_advance_progress?
Loading history...
205
                $tematic_advance_progress = '<a title="'.get_lang(
206
                        'GoToThematicAdvance'
207
                    ).'" href="'.api_get_path(
208
                        WEB_CODE_PATH
209
                    ).'attendance/index.php?cidReq='.$course_code.'&action=attendance_sheet_print&attendance_id=">'.$tematic_advance.'%</a>';
210
            } else {
211
                $tematic_advance_progress = '0%';
212
            }
213
214
            // Score
215
            $tbl_grade_categories = Database::get_main_table(
216
                TABLE_MAIN_GRADEBOOK_CATEGORY
217
            );
218
            $sql = "SELECT id from ".$tbl_grade_categories."
219
                    WHERE course_code ='".$course_code."'";
220
            $rs = Database::query($sql);
221
            $category = null;
222
            while ($row = Database::fetch_array($rs)) {
223
                $category = $row['id'];
224
            }
225
226
            if (!empty($category)) {
227
                $cat = Category::load($category);
228
                $eval = $cat[0]->get_evaluations();
229
                if (count($eval) > 0) {
230
                    $i = 0;
231
                    foreach ($eval as $item) {
232
                        $score .= '<a href="'.api_get_path(WEB_PATH).'main/gradebook/gradebook_view_result.php?export=pdf&cat_code='.$cat[0]->get_id().'&official_code='.$cat[0]->get_course_code().'&selecteval='.$item->get_id().'">'.$item->get_name().'</a>';
233
                        if (count($eval) - 1 != $i) {
234
                            $score .= ', ';
235
                        }
236
                        $i++;
237
                    }
238
                } else {
239
                    $score = get_lang("NotAvailable");
240
                }
241
            } else {
242
                $score = get_lang("NotAvailable");
243
            }
244
245
            $table_row = [];
246
            $table_row[] = $row_course['title'];
247
            $table_row[] = $nb_students_in_course;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nb_students_in_course does not seem to be defined for all execution paths leading up to this point.
Loading history...
248
            $table_row[] = $score;
249
            $table_row[] = $attendances[0];
250
            $course_data[] = $table_row;
251
        }
252
253
        return $course_data;
254
    }
255
}
256