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

BlockCourse::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 Christian Fasanando
9
 */
10
/**
11
 * This class is used like controller for this course block plugin,
12
 * the class name must be registered inside path.info file
13
 * (e.g: controller = "BlockCourse"), so dashboard controller will be instantiate it.
14
 *
15
 * @package chamilo.dashboard
16
 */
17
class BlockCourse extends Block
18
{
19
    private $user_id;
20
    private $courses;
21
    private $permission = [DRH];
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct($user_id)
27
    {
28
        $this->user_id = $user_id;
29
        $this->path = 'block_course';
30
        if ($this->is_block_visible_for_user($user_id)) {
31
            $this->courses = CourseManager::get_courses_followed_by_drh($user_id);
32
        }
33
    }
34
35
    /**
36
     * This method check if a user is allowed to see the block inside dashboard interface.
37
     *
38
     * @param    int        User id
39
     *
40
     * @return bool Is block visible for user
41
     */
42
    public function is_block_visible_for_user($user_id)
43
    {
44
        $user_info = api_get_user_info($user_id);
45
        $user_status = $user_info['status'];
46
        $is_block_visible_for_user = false;
47
        if (UserManager::is_admin($user_id) || in_array(
48
                $user_status,
49
                $this->permission
50
            )
51
        ) {
52
            $is_block_visible_for_user = true;
53
        }
54
55
        return $is_block_visible_for_user;
56
    }
57
58
    /**
59
     * This method return content html containing information
60
     * about courses and its position for showing it inside dashboard interface
61
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
62
     *
63
     * @return array column and content html
64
     */
65
    public function get_block()
66
    {
67
        $column = 2;
68
        $data = [];
69
        $html = $this->getBlockCard(
70
            get_lang('YourCourseList'),
71
            $this->getContent()
72
        );
73
        $data['column'] = $column;
74
        $data['content_html'] = $html;
75
76
        return $data;
77
    }
78
79
    /**
80
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
81
     *
82
     * @return string content html
83
     */
84
    public function getContent()
85
    {
86
        $course_data = $this->get_course_information_data();
87
        $content = '';
88
        $data_table = null;
89
        if (!empty($course_data)) {
90
            $data_table .= '<table class="data_table" width:"95%">';
91
            $data_table .= '<tr>
92
	    						<th>'.get_lang('CourseTitle').'</th>
93
	    						<th width="20%">'.get_lang('NbStudents').'</th>
94
	    						<th width="20%">'.get_lang('AvgTimeSpentInTheCourse').'</th>
95
	    						<th width="20%">'.get_lang('ThematicAdvance').'</th>
96
	    					</tr>';
97
            $i = 1;
98
            foreach ($course_data as $course) {
99
                if ($i % 2 == 0) {
100
                    $class_tr = 'row_odd';
101
                } else {
102
                    $class_tr = 'row_even';
103
                }
104
                $data_table .= '<tr class="'.$class_tr.'">';
105
                if (!isset($course[2])) {
106
                    $course[2] = '0:00:00';
107
                }
108
                foreach ($course as $cell) {
109
                    $data_table .= '<td align="right">'.$cell.'</td>';
110
                }
111
                $data_table .= '</tr>';
112
                $i++;
113
            }
114
            $data_table .= '</table>';
115
        } else {
116
            $data_table .= get_lang('ThereIsNoInformationAboutYourCourses');
117
        }
118
        $content .= $data_table;
119
        if (!empty($course_data)) {
120
            $content .= '<div style="text-align:right;margin-top:10px;">
121
            <a href="'.api_get_path(WEB_CODE_PATH).'mySpace/course.php?follow">'.get_lang('SeeMore').'</a></div>';
122
        }
123
124
        return $content;
125
    }
126
127
    /**
128
     * Get number of courses.
129
     *
130
     * @return int
131
     */
132
    public function get_number_of_courses()
133
    {
134
        return count($this->courses);
135
    }
136
137
    /**
138
     * Get course information data.
139
     *
140
     * @return array
141
     */
142
    public function get_course_information_data()
143
    {
144
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
145
        $course_data = [];
146
        $courses = $this->courses;
147
        $thematic = new Thematic();
148
149
        foreach ($courses as $row_course) {
150
            $course_code = $row_course['code'];
151
            $courseInfo = api_get_course_info($course_code);
152
            $courseId = $courseInfo['real_id'];
153
            $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
154
155
            // students directly subscribed to the course
156
            $sql = "SELECT user_id FROM $tbl_course_user as course_rel_user
157
                    WHERE course_rel_user.status=".STUDENT." AND course_rel_user.c_id='$courseId'";
158
            $rs = Database::query($sql);
159
            $users = [];
160
            while ($row = Database::fetch_array($rs)) {
161
                $users[] = $row['user_id'];
162
            }
163
            if (count($users) > 0) {
164
                $nb_students_in_course = count($users);
165
                $avg_time_spent_in_course = api_time_to_hms(
166
                    Tracking::get_time_spent_on_the_course($users, $courseId) / $nb_students_in_course
167
                );
168
            } else {
169
                $avg_time_spent_in_course = null;
170
            }
171
            $tematic_advance = $thematic->get_total_average_of_thematic_advances(
172
                $course_code,
173
                0
174
            );
175
176
            if (!empty($tematic_advance)) {
177
                $tematic_advance_progress = '<a title="'.get_lang('GoToThematicAdvance').'" href="'.api_get_path(WEB_CODE_PATH).'course_progress/index.php?cidReq='.$course_code.'&action=thematic_details">'.$tematic_advance.'%</a>';
178
            } else {
179
                $tematic_advance_progress = '0%';
180
            }
181
182
            $table_row = [];
183
            $table_row[] = $row_course['title'];
184
            $table_row[] = $nb_students_in_course;
185
            $table_row[] = $avg_time_spent_in_course;
186
            $table_row[] = $tematic_advance_progress;
187
            $course_data[] = $table_row;
188
        }
189
190
        return $course_data;
191
    }
192
}
193