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

BlockTeacher::getContent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
nc 4
nop 0
dl 0
loc 50
rs 9.0648
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This file is part of teacher block plugin for dashboard,
6
 * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
7
 *
8
 * @package chamilo.dashboard
9
 *
10
 * @author Christian Fasanando
11
 */
12
13
/**
14
 * This class is used like controller for teacher block plugin,
15
 * the class name must be registered inside path.info file
16
 * (e.g: controller = "BlockTeacher"), so dashboard controller will be instantiate it.
17
 *
18
 * @package chamilo.dashboard
19
 */
20
class BlockTeacher extends Block
21
{
22
    private $user_id;
23
    private $teachers;
24
    private $permission = [DRH];
25
26
    /**
27
     * Controller.
28
     */
29
    public function __construct($user_id)
30
    {
31
        $this->user_id = $user_id;
32
        $this->path = 'block_teacher';
33
        if ($this->is_block_visible_for_user($user_id)) {
34
            $this->teachers = UserManager::get_users_followed_by_drh($user_id, COURSEMANAGER);
35
        }
36
    }
37
38
    /**
39
     * This method check if a user is allowed to see the block inside dashboard interface.
40
     *
41
     * @param int        User id
42
     *
43
     * @return bool Is block visible for user
44
     */
45
    public function is_block_visible_for_user($user_id)
46
    {
47
        $user_info = api_get_user_info($user_id);
48
        $user_status = $user_info['status'];
49
        $is_block_visible_for_user = false;
50
        if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
51
            $is_block_visible_for_user = true;
52
        }
53
54
        return $is_block_visible_for_user;
55
    }
56
57
    /**
58
     * This method return content html containing information about
59
     * teachers and its position for showing it inside dashboard interface
60
     * it's important to use the name 'get_block' for beeing used from
61
     * dashboard controller.
62
     *
63
     * @return array column and content html
64
     */
65
    public function get_block()
66
    {
67
        $column = 1;
68
        $data = [];
69
        $html = $this->getBlockCard(
70
            get_lang('TeachersInformationsList'),
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
81
     * for showing it inside dashboard interface.
82
     *
83
     * @return string content html
84
     */
85
    public function getContent()
86
    {
87
        $teachers = $this->teachers;
88
        $teachers_table = null;
89
        if (count($teachers) > 0) {
90
            $teachers_table .= '<table class="data_table" width:"95%">';
91
            $teachers_table .= '
92
                                <tr>
93
                                    <th>'.get_lang('User').'</th>
94
                                    <th>'.get_lang('TimeSpentOnThePlatform').'</th>
95
                                    <th>'.get_lang('LastConnexion').'</th>
96
                                </tr>
97
                            ';
98
            $i = 1;
99
            foreach ($teachers as $teacher) {
100
                $teacher_id = $teacher['user_id'];
101
                $firstname = $teacher['firstname'];
102
                $lastname = $teacher['lastname'];
103
                $username = $teacher['username'];
104
105
                $time_on_platform = api_time_to_hms(Tracking::get_time_spent_on_the_platform($teacher_id));
106
                $last_connection = Tracking::get_last_connection_date($teacher_id);
107
108
                if ($i % 2 == 0) {
109
                    $class_tr = 'row_odd';
110
                } else {
111
                    $class_tr = 'row_even';
112
                }
113
                $teachers_table .= '
114
                                    <tr class="'.$class_tr.'">
115
                                        <td>'.api_get_person_name($firstname, $lastname).' ('.$username.')</td>
116
                                        <td align="right">'.$time_on_platform.'</td>
117
                                        <td align="right">'.$last_connection.'</td>
0 ignored issues
show
Bug introduced by
Are you sure $last_connection of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
                                        <td align="right">'./** @scrutinizer ignore-type */ $last_connection.'</td>
Loading history...
118
                                    </tr>
119
                                    ';
120
                $i++;
121
            }
122
            $teachers_table .= '</table>';
123
        } else {
124
            $teachers_table .= get_lang('ThereIsNoInformationAboutYourTeachers');
125
        }
126
127
        $content = $teachers_table;
128
129
        if (count($teachers) > 0) {
130
            $content .= '<div style="text-align:right;margin-top:10px;">
131
            <a href="'.api_get_path(WEB_CODE_PATH).'mySpace/index.php?view=admin">'.get_lang('SeeMore').'</a></div>';
132
        }
133
134
        return $content;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function get_teachers_content_html_for_drh()
141
    {
142
        $teachers = $this->teachers;
143
        $content = '<h4>'.get_lang('YourTeachers').'</h4>';
144
        $teachers_table = null;
145
        if (count($teachers) > 0) {
146
            $a_last_week = get_last_week();
147
            $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
148
149
            $teachers_table .= '<table class="data_table" width:"95%">';
150
            $teachers_table .= '
151
                                <tr>
152
                                    <th>'.get_lang('User').'</th>
153
                                    <th>'.get_lang('TimeSpentLastWeek').'<br />'.$last_week.'</th>
154
                                </tr>
155
                            ';
156
157
            $i = 1;
158
            foreach ($teachers as $teacher) {
159
                $teacher_id = $teacher['user_id'];
160
                $firstname = $teacher['firstname'];
161
                $lastname = $teacher['lastname'];
162
                $username = $teacher['username'];
163
                $time_on_platform = api_time_to_hms(
164
                    Tracking::get_time_spent_on_the_platform($teacher_id, true)
165
                );
166
167
                if ($i % 2 == 0) {
168
                    $class_tr = 'row_odd';
169
                } else {
170
                    $class_tr = 'row_even';
171
                }
172
                $teachers_table .= '<tr class="'.$class_tr.'">
173
                                        <td>'.api_get_person_name($firstname, $lastname).' ('.$username.')</td>
174
                                        <td align="right">'.$time_on_platform.'</td>
175
                                    </tr>';
176
177
                $i++;
178
            }
179
            $teachers_table .= '</table>';
180
        } else {
181
            $teachers_table .= get_lang('ThereIsNoInformationAboutYourTeachers');
182
        }
183
        $content .= $teachers_table;
184
        if (count($teachers) > 0) {
185
            $content .= '<div style="text-align:right;margin-top:10px;"><a href="'.api_get_path(WEB_CODE_PATH).'mySpace/teachers.php">'.get_lang('SeeMore').'</a></div>';
186
        }
187
188
        return $content;
189
    }
190
191
    /**
192
     * Get number of teachers.
193
     *
194
     * @return int
195
     */
196
    public function get_number_of_teachers()
197
    {
198
        return count($this->teachers);
199
    }
200
}
201