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

BlockStudent::getContent()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 43
nc 4
nop 0
dl 0
loc 65
rs 7.9875
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of student 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
/**
12
 * This class is used like controller for student block plugin,
13
 * the class name must be registered inside path.info file
14
 * (e.g: controller = "BlockStudent"), so dashboard controller will be instantiate it.
15
 *
16
 * @package chamilo.dashboard
17
 */
18
class BlockStudent extends Block
19
{
20
    private $user_id;
21
    private $students;
22
    private $permission = [DRH];
23
24
    /**
25
     * Constructor.
26
     */
27
    public function __construct($user_id)
28
    {
29
        $this->user_id = $user_id;
30
        $this->path = 'block_student';
31
        if ($this->is_block_visible_for_user($user_id)) {
32
            $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
33
        }
34
    }
35
36
    /**
37
     * This method check if a user is allowed to see the block inside dashboard interface.
38
     *
39
     * @param int        User id
40
     *
41
     * @return bool Is block visible for user
42
     */
43
    public function is_block_visible_for_user($user_id)
44
    {
45
        $user_info = api_get_user_info($user_id);
46
        $user_status = $user_info['status'];
47
        $is_block_visible_for_user = false;
48
        if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
49
            $is_block_visible_for_user = true;
50
        }
51
52
        return $is_block_visible_for_user;
53
    }
54
55
    /**
56
     * This method return content html containing information
57
     * about students and its position for showing it inside dashboard interface
58
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
59
     *
60
     * @return array column and content html
61
     */
62
    public function get_block()
63
    {
64
        $column = 1;
65
        $data = [];
66
        $html = $this->getBlockCard(
67
            get_lang('YourStudents'),
68
            $this->getContent()
69
        );
70
        $data['column'] = $column;
71
        $data['content_html'] = $html;
72
73
        return $data;
74
    }
75
76
    /**
77
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
78
     *
79
     * @return string content html
80
     */
81
    public function getContent()
82
    {
83
        $students = $this->students;
84
        $students_table = null;
85
        if (count($students) > 0) {
86
            $students_table .= '<table class="data_table">';
87
            $students_table .= '<tr>
88
                                    <th width="10%" rowspan="2">'.get_lang('FirstName').'</th>
89
                                    <th width="10%" rowspan="2">'.get_lang('LastName').'</th>
90
                                    <th width="30%" colspan="2">'.get_lang('CourseInformation').'</th>
91
                                </tr>
92
                                <tr>
93
                                    <th width="10%">'.get_lang('Courses').'</th>
94
                                    <th width="10%">'.get_lang('Time').'</th>
95
                                </tr>';
96
97
            $i = 1;
98
            foreach ($students as $student) {
99
                $courses_by_user = CourseManager::get_courses_list_by_user_id($student['user_id'], true);
100
                $count_courses = count($courses_by_user);
101
                $rowspan = $count_courses ? $count_courses + 1 : 2;
102
103
                if ($i % 2 == 0) {
104
                    $style = ' style="background-color:#F2F2F2" ';
105
                } else {
106
                    $style = ' style="background-color:#FFF" ';
107
                }
108
109
                $students_table .= '<tr '.$style.'>
110
                                        <td rowspan="'.$rowspan.'">'.$student['firstname'].'</td>
111
                                        <td rowspan="'.$rowspan.'">'.$student['lastname'].'</td>
112
                                    </tr>';
113
114
                // courses information about the student
115
                if (!empty($courses_by_user)) {
116
                    foreach ($courses_by_user as $course) {
117
                        $course_code = $course['code'];
118
                        $courseInfo = api_get_course_info($course_code);
119
                        $courseId = $courseInfo['real_id'];
120
                        $course_title = $course['title'];
121
                        $time = api_time_to_hms(Tracking::get_time_spent_on_the_course($student['user_id'], $courseId));
122
                        $students_table .= '<tr '.$style.'>
123
                                            <td align="right">'.$course_title.'</td>
124
                                            <td align="right">'.$time.'</td>
125
                                            </tr>';
126
                    }
127
                } else {
128
                    $students_table .= '<tr '.$style.'>
129
                                            <td align="center" colspan="2"><i>'.get_lang('Empty').'</i></td>
130
                                        </tr>';
131
                }
132
                $i++;
133
            }
134
            $students_table .= '</table>';
135
        } else {
136
            $students_table .= get_lang('ThereIsNoInformationAboutYourStudents');
137
        }
138
139
        $content = $students_table;
140
141
        if (count($students) > 0) {
142
            $content .= '<div style="text-align:right;margin-top:10px;"><a href="'.api_get_path(WEB_CODE_PATH).'mySpace/index.php?view=admin&display=useroverview">'.get_lang('SeeMore').'</a></div>';
143
        }
144
145
        return $content;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function get_students_content_html_for_drh()
152
    {
153
        $attendance = new Attendance();
154
        $students = $this->students;
155
        $content = '<h4>'.get_lang('YourStudents').'</h4>';
156
        $students_table = null;
157
        if (count($students) > 0) {
158
            $students_table .= '<table class="data_table">';
159
            $students_table .= '<tr>
160
                                    <th>'.get_lang('User').'</th>
161
                                    <th>'.get_lang('AttendancesFaults').'</th>
162
                                    <th>'.get_lang('Evaluations').'</th>
163
                                </tr>';
164
            $i = 1;
165
            foreach ($students as $student) {
166
                $student_id = $student['user_id'];
167
                $firstname = $student['firstname'];
168
                $lastname = $student['lastname'];
169
                $username = $student['username'];
170
                // get average of faults in attendances by student
171
                $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
172
                if (!empty($results_faults_avg)) {
173
                    $attendances_faults_avg = '<a title="'.get_lang('GoToStudentDetails').'" href="'.api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$student_id.'">'.$results_faults_avg['faults'].'/'.$results_faults_avg['total'].' ('.$results_faults_avg['porcent'].'%)</a>';
174
                } else {
175
                    $attendances_faults_avg = '0%';
176
                }
177
178
                $courses_by_user = CourseManager::get_courses_list_by_user_id($student_id, true);
179
                $evaluations_avg = 0;
180
                $score = $weight = 0;
181
                foreach ($courses_by_user as $course) {
182
                    $course_code = $course['code'];
183
                    $cats = Category::load(
184
                        null,
185
                        null,
186
                        $course_code,
187
                        null,
188
                        null,
189
                        null,
190
                        false
191
                    );
192
                    $scoretotal = [];
193
                    if (isset($cats) && isset($cats[0])) {
194
                        $scoretotal = $cats[0]->calc_score($student_id, null, $course_code);
195
                    }
196
197
                    if (!empty($scoretotal)) {
198
                        $score += $scoretotal[0];
199
                        $weight += $scoretotal[1];
200
                    }
201
                }
202
203
                if (!empty($weight)) {
204
                    $evaluations_avg = '<a title="'.get_lang('GoToStudentDetails').'" href="'.api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$student_id.'">'.round($score, 2).'/'.round($weight, 2).'('.round(($score / $weight) * 100, 2).' %)</a>';
205
                }
206
207
                if ($i % 2 == 0) {
208
                    $class_tr = 'row_odd';
209
                } else {
210
                    $class_tr = 'row_even';
211
                }
212
                $students_table .= '<tr class="'.$class_tr.'">
213
                                        <td>'.api_get_person_name($firstname, $lastname).' ('.$username.')</td>
214
                                        <td>'.$attendances_faults_avg.'</td>
215
                                        <td>'.$evaluations_avg.'</td>
216
                                    </tr>';
217
218
                $i++;
219
            }
220
            $students_table .= '</table>';
221
        } else {
222
            $students_table .= get_lang('ThereIsNoInformationAboutYourStudents');
223
        }
224
225
        $content .= $students_table;
226
227
        if (count($students) > 0) {
228
            $content .= '<div style="text-align:right;margin-top:10px;">
229
                            <a href="'.api_get_path(WEB_CODE_PATH).'mySpace/index.php?view=admin&display=yourstudents">'.get_lang('SeeMore').'</a>
230
                         </div>';
231
        }
232
        //$content .= '</div>';
233
234
        return $content;
235
    }
236
237
    /**
238
     * Get number of students.
239
     *
240
     * @return int
241
     */
242
    public function get_number_of_students()
243
    {
244
        return count($this->students);
245
    }
246
}
247