Test Setup Failed
Push — master ( c7183e...00c715 )
by Julito
30:43
created

ScheduledAnnouncement::sendPendingMessages()   D

Complexity

Conditions 16
Paths 8

Size

Total Lines 111
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 76
nc 8
nop 1
dl 0
loc 111
rs 4.8736
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class ScheduledAnnouncement
6
 * Requires DB change:
7
 *
8
 * CREATE TABLE scheduled_announcements (id INT AUTO_INCREMENT NOT NULL, subject VARCHAR(255) NOT NULL, message LONGTEXT NOT NULL, date DATETIME DEFAULT NULL, sent TINYINT(1) NOT NULL, session_id INT NOT NULL, c_id INT DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
9
 *
10
 * Config setting:
11
 * $_configuration['allow_scheduled_announcements'] = true;
12
 *
13
 * Setup linux cron file:
14
 * main/cron/scheduled_announcement.php
15
 *
16
 * Requires:
17
 * composer update
18
 *
19
 * @package chamilo.library
20
 */
21
class ScheduledAnnouncement extends Model
22
{
23
    public $table;
24
    public $columns = array('id', 'subject', 'message', 'date', 'sent', 'session_id');
25
26
    /**
27
     * Constructor
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
        $this->table = 'scheduled_announcements';
33
    }
34
35
    /**
36
     * @param array $where_conditions
37
     *
38
     * @return array
39
     */
40
    public function get_all($where_conditions = array())
41
    {
42
        return Database::select(
43
            '*',
44
            $this->table,
45
            array('where' => $where_conditions, 'order' => 'subject ASC')
46
        );
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function get_count()
53
    {
54
        $row = Database::select(
55
            'count(*) as count',
56
            $this->table,
57
            array(),
58
            'first'
59
        );
60
61
        return $row['count'];
62
    }
63
64
    /**
65
     * Displays the title + grid
66
     */
67
    public function getGrid($sessionId)
68
    {
69
        // action links
70
        $action = '<div class="actions" style="margin-bottom:20px">';
71
        $action .= Display::url(
72
            Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
73
            api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$sessionId
74
        );
75
76
        $action .= '<a href="'.api_get_self().'?action=add&session_id='.$sessionId.'">'.
77
            Display::return_icon('add.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
78
        $action .= '<a href="scheduled_announcement.php?action=run&session_id='.$sessionId.'">'.
79
            Display::return_icon('mail_send.png', get_lang('Send'), '', ICON_SIZE_MEDIUM).
80
            '</a>';
81
82
        $action .= '</div>';
83
84
        $html = $action;
85
        $html .= '<div id="session-table" class="table-responsive">';
86
        $html .= Display::grid_html('programmed');
87
        $html .= '</div>';
88
89
        return $html;
90
    }
91
92
    /**
93
     * Returns a Form validator Obj
94
     * @param   string  $url
95
     * @param   string  $action add, edit
96
     *
97
     * @return  FormValidator form validator obj
98
     */
99
    public function returnSimpleForm($url, $action, $sessionInfo = [])
100
    {
101
        $form = new FormValidator(
102
            'announcement',
103
            'post',
104
            $url
105
        );
106
107
        $form->addHidden('session_id', $sessionInfo['id']);
108
        $form->addDateTimePicker('date', get_lang('Date'));
109
        $form->addText('subject', get_lang('Subject'));
110
        $form->addHtmlEditor('message', get_lang('Message'));
111
        $this->setTagsInForm($form);
112
113
        $form->addCheckBox('sent', null, get_lang('MessageSent'));
114
115
        if ($action == 'edit') {
116
            $form->addButtonUpdate(get_lang('Modify'));
117
        }
118
119
        return $form;
120
    }
121
122
    /**
123
     * @param FormValidator $form
124
     */
125
    private function setTagsInForm(& $form)
126
    {
127
        $form->addLabel(
128
            get_lang('Tags'),
129
            Display::return_message(
130
                implode('<br />', $this->getTags()),
131
                'normal',
132
                false
133
            )
134
        );
135
    }
136
137
    /**
138
     * Returns a Form validator Obj
139
     * @todo the form should be auto generated
140
     * @param   string  $url
141
     * @param   string  $action add, edit
142
     * @param array
143
     * @return  FormValidator form validator obj
144
     */
145
    public function returnForm($url, $action, $sessionInfo = [])
146
    {
147
        // Setting the form elements
148
        $header = get_lang('Add');
149
150
        if ($action == 'edit') {
151
            $header = get_lang('Modify');
152
        }
153
154
        $form = new FormValidator(
155
            'announcement',
156
            'post',
157
            $url
158
        );
159
160
        $form->addHeader($header);
161
        if ($action == 'add') {
162
            $form->addHtml(
163
                Display::return_message(
164
                    nl2br(get_lang('ScheduleAnnouncementDescription')),
165
                    'normal',
166
                    false
167
                )
168
            );
169
        }
170
        $form->addHidden('session_id', $sessionInfo['id']);
171
172
        $useBaseDate = false;
173
        $startDate = $sessionInfo['access_start_date'];
174
        $endDate = $sessionInfo['access_end_date'];
175
176
        if (!empty($startDate) || !empty($endDate)) {
177
            $useBaseDate = true;
178
        }
179
180
        $typeOptions = [
181
            'specific_date' => get_lang('SpecificDate')
182
        ];
183
184
        if ($useBaseDate) {
185
            $typeOptions['base_date'] = get_lang('BaseDate');
186
        }
187
188
        $form->addSelect(
189
            'type',
190
            get_lang('Type'),
191
            $typeOptions,
192
            [
193
                'onchange' => "javascript: 
194
                    if (this.options[this.selectedIndex].value == 'base_date') {
195
                        document.getElementById('options').style.display = 'block';
196
                        document.getElementById('specific_date').style.display = 'none';
197
                    } else {
198
                        document.getElementById('options').style.display = 'none';
199
                        document.getElementById('specific_date').style.display = 'block';
200
                    }
201
            "]
202
        );
203
204
        $form->addElement('html', '<div id="specific_date">');
205
        $form->addDateTimePicker('date', get_lang('Date'));
206
        $form->addElement('html', '</div>');
207
        $form->addElement('html', '<div id="options" style="display:none">');
208
209
        $startDate = $sessionInfo['access_start_date'];
210
        $endDate = $sessionInfo['access_end_date'];
211
212
        $form->addText(
213
            'days',
214
            get_lang('Days'),
215
            false
216
        );
217
218
        $form->addSelect(
219
            'moment_type',
220
            get_lang('AfterOrBefore'),
221
            [
222
                'after' => get_lang('After'),
223
                'before' => get_lang('Before'),
224
            ]
225
        );
226
227
        if (!empty($startDate)) {
228
            $options['start_date'] = get_lang('StartDate').' - '.$startDate;
229
        }
230
231
        if (!empty($endDate)) {
232
            $options['end_date'] = get_lang('EndDate').' - '.$endDate;
233
        }
234
        if (!empty($options)) {
235
            $form->addSelect('base_date', get_lang('BaseDate'), $options);
236
        }
237
238
        $form->addElement('html', '</div>');
239
240
        $form->addText('subject', get_lang('Subject'));
241
        $form->addHtmlEditor('message', get_lang('Message'));
242
        $this->setTagsInForm($form);
243
244
        if ($action == 'edit') {
245
            $form->addButtonUpdate(get_lang('Modify'));
246
        } else {
247
            $form->addButtonCreate(get_lang('Add'));
248
        }
249
250
        return $form;
251
    }
252
253
    /**
254
     * @param int $urlId
255
     *
256
     * @return int
257
     */
258
    public function sendPendingMessages($urlId = 0)
259
    {
260
        if (!$this->allowed()) {
261
            return 0;
262
        }
263
264
        $messagesSent = 0;
265
        $now = api_get_utc_datetime();
266
        $result = $this->get_all();
267
268
        foreach ($result as $result) {
269
            if (empty($result['sent'])) {
270
                if (!empty($result['date']) && $result['date'] < $now) {
271
                    $sessionId = $result['session_id'];
272
                    $sessionInfo = api_get_session_info($sessionId);
273
                    if (empty($sessionInfo)) {
274
                        continue;
275
                    }
276
                    $users = SessionManager::get_users_by_session(
277
                        $sessionId,
278
                        '0',
279
                        false,
280
                        $urlId
281
                    );
282
283
                    if (empty($users)) {
284
                        continue;
285
                    }
286
287
                    self::update(['id' => $result['id'], 'sent' => 1]);
288
289
                    $subject = $result['subject'];
290
                    $message = $result['message'];
291
292
                    if ($users) {
293
                        foreach ($users as $user) {
0 ignored issues
show
Bug introduced by
The expression $users of type array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
294
                            $userInfo = api_get_user_info($user['user_id']);
295
                            $courseList = SessionManager::getCoursesInSession($sessionId);
296
                            $courseInfo = [];
297
                            if (!empty($courseList)) {
298
                                $courseId = current($courseList);
299
                                $courseInfo = api_get_course_info_by_id($courseId);
300
                            }
301
302
                            $progress = '';
303
                            if (!empty($sessionInfo) && !empty($courseInfo)) {
304
                                $progress = Tracking::get_avg_student_progress(
305
                                    $user['user_id'],
306
                                    $courseInfo['code'],
307
                                    null,
308
                                    $sessionId
309
                                );
310
                            }
311
312
                            if (is_numeric($progress)) {
313
                                $progress = $progress.'%';
314
                            } else {
315
                                $progress = '0%';
316
                            }
317
318
                            $startTime = api_get_local_time(
319
                                $sessionInfo['access_start_date'],
320
                                null,
321
                                null,
322
                                true
323
                            );
324
                            $endTime = api_get_local_time(
325
                                $sessionInfo['access_end_date'],
326
                                null,
327
                                null,
328
                                true
329
                            );
330
331
                            $generalCoach = '';
332
                            $generalCoachEmail = '';
333
                            if (!empty($sessionInfo['coach_id'])) {
334
                                $coachInfo = api_get_user_info($sessionInfo['coach_id']);
335
                                if (!empty($coachInfo)) {
336
                                    $generalCoach = $coachInfo['complete_name'];
337
                                    $generalCoachEmail = $coachInfo['email'];
338
                                }
339
                            }
340
                            $tags = [
341
                                '((session_name))' => $sessionInfo['name'],
342
                                '((session_start_date))' => $startTime,
343
                                '((general_coach))' => $generalCoach,
344
                                '((general_coach_email))' => $generalCoachEmail,
345
                                '((session_end_date))' => $endTime,
346
                                '((user_complete_name))' => $userInfo['complete_name'],
347
                                '((user_first_name))' => $userInfo['firstname'],
348
                                '((user_last_name))' => $userInfo['lastname'],
349
                                '((lp_progress))' => $progress,
350
                            ];
351
352
                            $message = str_replace(array_keys($tags), $tags, $message);
353
354
                            MessageManager::send_message(
355
                                $userInfo['user_id'],
356
                                $subject,
357
                                $message
358
                            );
359
                        }
360
                    }
361
362
                    $messagesSent++;
363
                }
364
            }
365
        }
366
367
        return $messagesSent;
368
    }
369
370
    /**
371
     * @return array
372
     */
373 View Code Duplication
    public function getTags()
374
    {
375
        $tags = [
376
            '((session_name))',
377
            '((session_start_date))',
378
            '((session_end_date))',
379
            '((general_coach))',
380
            '((general_coach_email))',
381
            '((user_complete_name))',
382
            '((user_first_name))',
383
            '((user_last_name))',
384
            '((lp_progress))'
385
        ];
386
387
        return $tags;
388
    }
389
390
    /**
391
     * @return bool
392
     */
393
    public function allowed()
394
    {
395
        return api_get_configuration_value('allow_scheduled_announcements');
396
    }
397
}
398