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

BlockSession::getContent()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 41
nc 4
nop 0
dl 0
loc 59
rs 8.6417
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 session 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 this session block plugin,
13
 * the class name must be registered inside path.info file
14
 * (e.g: controller = "BlockSession"), so dashboard controller will be instantiate it.
15
 *
16
 * @package chamilo.dashboard
17
 */
18
class BlockSession extends Block
19
{
20
    private $user_id;
21
    private $sessions;
22
    private $permission = [DRH, SESSIONADMIN];
23
24
    /**
25
     * Constructor.
26
     */
27
    public function __construct($user_id)
28
    {
29
        $this->user_id = $user_id;
30
        $this->path = 'block_session';
31
        if ($this->is_block_visible_for_user($user_id)) {
32
            $this->sessions = SessionManager::get_sessions_followed_by_drh($user_id);
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
57
     * information about sessions 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 = 2;
65
        $data = [];
66
        $html = $this->getBlockCard(
67
            get_lang('YourSessionsList'),
68
            $this->getContent()
69
        );
70
71
        $data['column'] = $column;
72
        $data['content_html'] = $html;
73
74
        return $data;
75
    }
76
77
    /**
78
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
79
     *
80
     * @return string content html
81
     */
82
    public function getContent()
83
    {
84
        $content = '';
85
        $sessions = $this->sessions;
86
        if (count($sessions) > 0) {
87
            $sessions_table = '<table class="data_table" width:"95%">';
88
            $sessions_table .= '<tr>
89
                                    <th >'.get_lang('Title').'</th>
90
                                    <th >'.get_lang('Date').'</th>
91
                                    <th width="100px">'.get_lang('NbCoursesPerSession').'</th>
92
                                </tr>';
93
            $i = 1;
94
            foreach ($sessions as $session) {
95
                $session_id = intval($session['id']);
96
                $title = $session['name'];
97
98
                if (!empty($session['access_start_date'])) {
99
                    $dateFrom = api_convert_and_format_date(
100
                        $session['access_start_date'],
101
                        DATE_FORMAT_SHORT,
102
                        date_default_timezone_get()
103
                    );
104
                    $dateUntil = api_convert_and_format_date(
105
                        $session['access_end_date'],
106
                        DATE_FORMAT_SHORT,
107
                        date_default_timezone_get()
108
                    );
109
110
                    $date = vsprintf(get_lang('FromDateXToDateY'), [$dateFrom, $dateUntil]);
111
                } else {
112
                    $date = ' - ';
113
                }
114
115
                $count_courses_in_session = count(Tracking::get_courses_list_from_session($session_id));
116
117
                if ($i % 2 == 0) {
118
                    $class_tr = 'row_odd';
119
                } else {
120
                    $class_tr = 'row_even';
121
                }
122
123
                $sessions_table .= '<tr class="'.$class_tr.'">
124
                                        <td>'.$title.'</td>
125
                                        <td align="center">'.$date.'</td>
126
                                        <td align="center">'.$count_courses_in_session.'</td>
127
                                   </tr>';
128
                $i++;
129
            }
130
            $sessions_table .= '</table>';
131
            $content .= $sessions_table;
132
        } else {
133
            $content .= get_lang('ThereIsNoInformationAboutYourSessions');
134
        }
135
136
        if (count($sessions) > 0) {
137
            $content .= '<div style="text-align:right;margin-top:10px;"><a href="'.api_get_path(WEB_CODE_PATH).'mySpace/session.php">'.get_lang('SeeMore').'</a></div>';
138
        }
139
140
        return $content;
141
    }
142
143
    /**
144
     * Get number of sessions.
145
     *
146
     * @return int
147
     */
148
    public function get_number_of_sessions()
149
    {
150
        return count($this->sessions);
151
    }
152
}
153