Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

plugin/clockworksms/lib/clockworksms.lib.php (1 issue)

1
<?php
2
/* For licensing terms, see /vendor/license.txt */
3
4
/**
5
 * Class Clockworksms
6
 * This script handles incoming SMS information, process it and sends an SMS if everything is right.
7
 *
8
 * @package chamilo.plugin.clockworksms.lib
9
 *
10
 * @author  Imanol Losada <[email protected]>
11
 *
12
 * Clockworksms-Chamilo connector class
13
 */
14
class Clockworksms implements SmsPluginLibraryInterface
15
{
16
    public $apiKey;
17
    public $api;
18
    public $plugin_enabled = false;
19
20
    /**
21
     * Constructor (generates a connection to the API).
22
     *
23
     * @param   string  Clockworksms API key required to use the plugin
24
     */
25
    public function __construct($apiKey = null)
26
    {
27
        $plugin = ClockworksmsPlugin::create();
28
        $clockWorkSMSPlugin = $plugin->get('tool_enable');
29
        if (empty($apiKey)) {
30
            $clockWorkSMSApiKey = $plugin->get('api_key');
31
        } else {
32
            $clockWorkSMSApiKey = $apiKey;
33
        }
34
        $this->table = Database::get_main_table('user_field_values');
35
        if ($clockWorkSMSPlugin == true) {
36
            $this->apiKey = $clockWorkSMSApiKey;
37
            // Setting Clockworksms api
38
            if (!defined('CONFIG_SECURITY_API_KEY')) {
39
                define('CONFIG_SECURITY_API_KEY', $this->apiKey);
40
            }
41
            $trimmedApiKey = trim(CONFIG_SECURITY_API_KEY);
42
            if (!empty($trimmedApiKey)) {
43
                $this->api = new Clockwork(CONFIG_SECURITY_API_KEY);
44
            } else {
45
                $this->api = new Clockwork(' ');
46
                $recipient_name = api_get_person_name(
47
                    api_get_setting('administratorName'),
48
                    api_get_setting('administratorSurname'),
49
                    null,
50
                    PERSON_NAME_EMAIL_ADDRESS
51
                );
52
                $email_form = api_get_setting('emailAdministrator');
53
                $emailsubject = 'Clockworksms error';
54
                $emailbody = 'Key cannot be blank';
55
                $sender_name = $recipient_name;
56
                $email_admin = $email_form;
57
                api_mail_html(
58
                    $recipient_name,
59
                    $email_form,
60
                    $emailsubject,
61
                    $emailbody,
62
                    $sender_name,
63
                    $email_admin
64
                );
65
            }
66
            $this->plugin_enabled = true;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getMobilePhoneNumberById($userId)
74
    {
75
        $mobilePhoneNumberExtraField = new ExtraField('user');
76
        $mobilePhoneNumberExtraField = $mobilePhoneNumberExtraField->get_handler_field_info_by_field_variable(
77
            'mobile_phone_number'
78
        );
79
80
        $mobilePhoneNumberExtraFieldValue = new ExtraFieldValue('user');
81
        $mobilePhoneNumberExtraFieldValue = $mobilePhoneNumberExtraFieldValue->get_values_by_handler_and_field_id(
82
            $userId,
83
            $mobilePhoneNumberExtraField['id']
84
        );
85
86
        return $mobilePhoneNumberExtraFieldValue['value'];
87
    }
88
89
    /**
90
     * send (sends an SMS to the user).
91
     *
92
     * @param   array   Data needed to send the SMS. It is mandatory to include the
93
     *                  'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
94
     *                  More data may be neccesary depending on the message type
95
     * Example: $additional_parameters = array(
96
     *              'smsType' => EXAMPLE_SMS_TYPE,
97
     *              'userId' => $userId,
98
     *              'moreData' => $moreData
99
     *          );
100
     */
101
    public function send($additionalParameters)
102
    {
103
        $trimmedKey = trim(CONFIG_SECURITY_API_KEY);
104
        if (!empty($trimmedKey)) {
105
            $phoneExists = array_key_exists('mobilePhoneNumber', $additionalParameters);
106
            $to = $phoneExists ? $additionalParameters['mobilePhoneNumber'] : $this->getMobilePhoneNumberById($additionalParameters['userId']);
107
108
            $message = [
109
                'to' => $to,
110
                'message' => $this->getSms($additionalParameters),
111
            ];
112
113
            if (!empty($message['message'])) {
114
                $this->api->send($message);
115
            }
116
        }
117
    }
118
119
    /**
120
     * buildSms (builds an SMS from a template and data).
121
     *
122
     * @param ClockworksmsPlugin $plugin
123
     * @param Template           $tpl
124
     * @param string  Template file name
125
     * @param string $messageKey Text key from lang file
126
     * @param array  $parameters Data to fill message variables (if any)
127
     *
128
     * @return string
129
     */
130
    public function buildSms(
131
        $plugin,
132
        $tpl,
133
        $templateName,
134
        $messageKey,
135
        $parameters = []
136
    ) {
137
        // Send direct message with out using plugin get_lang
138
        if (isset($parameters['direct_message'])) {
139
            return $parameters['direct_message'];
140
        }
141
142
        $message = $plugin->get_lang($messageKey);
143
        if ($parameters !== null) {
144
            $message = vsprintf($message, $parameters);
145
        }
146
147
        return $message;
148
    }
149
150
    /**
151
     * getSms (returns an SMS message depending of its type).
152
     *
153
     * @param   array   Data needed to send the SMS. It is mandatory to include the
154
     *                  'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
155
     *                  More data may be neccesary depending on the message type
156
     * Example: $additional_parameters = array(
157
     *              'smsType' => EXAMPLE_SMS_TYPE,
158
     *              'userId' => $userId,
159
     *              'moreData' => $moreData
160
     *          );
161
     *
162
     * @return string A ready to be sent SMS
163
     */
164
    public function getSms($additionalParameters)
165
    {
166
        $plugin = ClockworksmsPlugin::create();
167
        $tool_name = $plugin->get_lang('plugin_title');
168
        $tpl = new Template($tool_name);
169
170
        switch ($additionalParameters['smsType']) {
171
            case SmsPlugin::WELCOME_LOGIN_PASSWORD:
172
                $userInfo = api_get_user_info($additionalParameters['userId']);
173
174
                return $this->buildSms(
175
                    $plugin,
176
                    $tpl,
177
                    'welcome_login_password.tpl',
178
                    'WelcomeXLoginXPasswordX',
179
                    [
180
                        api_get_setting('siteName'),
181
                        $userInfo['username'],
182
                        $additionalParameters['password'],
183
                    ]
184
                );
185
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
186
            case SmsPlugin::NEW_FILE_SHARED_COURSE_BY:
187
                return $this->buildSms(
188
                    $plugin,
189
                    $tpl,
190
                    'new_file_shared_course_by.tpl',
191
                    'XNewFileSharedCourseXByX',
192
                    [
193
                        api_get_setting('siteName'),
194
                        $additionalParameters['courseTitle'],
195
                        $additionalParameters['userUsername'],
196
                    ]
197
                );
198
                break;
199
            case SmsPlugin::ACCOUNT_APPROVED_CONNECT:
200
                return $this->buildSms(
201
                    $plugin,
202
                    $tpl,
203
                    'account_approved_connect.tpl',
204
                    'XAccountApprovedConnectX',
205
                    [
206
                        api_get_setting('siteName'),
207
                        $tpl->params['_p']['web'],
208
                    ]
209
                );
210
                break;
211
            case SmsPlugin::NEW_COURSE_BEEN_CREATED:
212
                return $this->buildSms(
213
                    $plugin,
214
                    $tpl,
215
                    'new_course_been_created.tpl',
216
                    'XNewCourseXBeenCreatedX',
217
                    [
218
                        api_get_setting('siteName'),
219
                        $additionalParameters['courseName'],
220
                        $additionalParameters['creatorUsername'],
221
                    ]
222
                );
223
                break;
224
            case SmsPlugin::NEW_USER_SUBSCRIBED_COURSE:
225
                return $this->buildSms(
226
                    $plugin,
227
                    $tpl,
228
                    'new_user_subscribed_course.tpl',
229
                    'XNewUserXSubscribedCourseX',
230
                    [
231
                        api_get_setting('siteName'),
232
                        $additionalParameters['userUsername'],
233
                        $additionalParameters['courseCode'],
234
                    ]
235
                );
236
                break;
237
            case SmsPlugin::NEW_COURSE_SUGGESTED_TEACHER:
238
                return $this->buildSms(
239
                    $plugin,
240
                    $tpl,
241
                    'new_course_suggested_teacher.tpl',
242
                    'XNewCourseSuggestedTeacherX',
243
                    [
244
                        api_get_setting('siteName'),
245
                        $additionalParameters['userUsername'],
246
                    ]
247
                );
248
                break;
249
            case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REGISTERED:
250
                return $this->buildSms(
251
                    $plugin,
252
                    $tpl,
253
                    'course_opening_request_code_registered.tpl',
254
                    'XCourseOpeningRequestCodeXRegistered',
255
                    [
256
                        api_get_setting('siteName'),
257
                        $additionalParameters['courseCode'],
258
                    ]
259
                );
260
                break;
261
            case SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED:
262
                return $this->buildSms(
263
                    $plugin,
264
                    $tpl,
265
                    'course_opening_request_course_code_approved.tpl',
266
                    'XCourseOpeningRequestCourseCodeXApproved',
267
                    [
268
                        api_get_setting('siteName'),
269
                        $additionalParameters['courseCode'],
270
                    ]
271
                );
272
                break;
273
            case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED:
274
                return $this->buildSms(
275
                    $plugin,
276
                    $tpl,
277
                    'request_open_course_code_rejected.tpl',
278
                    'XRequestOpenCourseCodeXReject',
279
                    [
280
                        api_get_setting('siteName'),
281
                        $additionalParameters['courseCode'],
282
                    ]
283
                );
284
                break;
285
            case SmsPlugin::COURSE_OPENING_REQUEST_CODE:
286
                return $this->buildSms(
287
                    $plugin,
288
                    $tpl,
289
                    'course_opening_request_course_code.tpl',
290
                    'XCourseOpeningRequestCourseCodeX',
291
                    [
292
                        api_get_setting('siteName'),
293
                        $additionalParameters['courseCode'],
294
                    ]
295
                );
296
                break;
297
            case SmsPlugin::BEEN_SUBSCRIBED_COURSE:
298
                return $this->buildSms(
299
                    $plugin,
300
                    $tpl,
301
                    'been_subscribed_course.tpl',
302
                    'XBeenSubscribedCourseX',
303
                    [
304
                        api_get_setting('siteName'),
305
                        $additionalParameters['courseTitle'],
306
                    ]
307
                );
308
                break;
309
            case SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE:
310
                return $this->buildSms(
311
                    $plugin,
312
                    $tpl,
313
                    'assignment_been_created_course.tpl',
314
                    'XAssignmentBeenCreatedCourseX',
315
                    [
316
                        api_get_setting('siteName'),
317
                        $additionalParameters['courseTitle'],
318
                    ]
319
                );
320
                break;
321
            case SmsPlugin::CERTIFICATE_NOTIFICATION:
322
                return $this->buildSms(
323
                    $plugin,
324
                    $tpl,
325
                    'certificate_notification.tpl',
326
                    '',
327
                    $additionalParameters
328
                );
329
                break;
330
            // Message types to be implemented. Fill the array parameter with arguments.
331
            /*case SmsPlugin::ACCOUNT_CREATED_UPDATED_LOGIN_PASSWORD:
332
                return $this->buildSms(
333
                    $plugin,
334
                    $tpl,
335
                    'account_created_updated_login_password.tpl',
336
                    'XAccountCreatedUpdatedLoginXPasswordX',
337
                    array(
338
                        api_get_setting('siteName')
339
                    )
340
                );
341
                break;*/
342
            /*case SmsPlugin::PASSWORD_UPDATED_LOGIN_PASSWORD:
343
                return $this->buildSms(
344
                    $plugin,
345
                    $tpl,
346
                    'password_updated_login_password.tpl',
347
                    'XPasswordUpdatedLoginXPasswordX',
348
                    array(
349
                        api_get_setting('siteName')
350
                    )
351
                );
352
                break;*/
353
            /*case SmsPlugin::REQUESTED_PASSWORD_CHANGE:
354
                return $this->buildSms(
355
                    $plugin,
356
                    $tpl,
357
                    'requested_password_change.tpl',
358
                    'XPasswordUpdatedLoginXPasswordX',
359
                    array(
360
                        api_get_setting('siteName')
361
                    )
362
                );
363
                break;*/
364
            /*case SmsPlugin::RECEIVED_NEW_PERSONAL_MESSAGES:
365
                return $this->buildSms(
366
                    $plugin,
367
                    $tpl,
368
                    'received_new_personal_messages.tpl',
369
                    'XReceivedNewPersonalMessages',
370
                    array(
371
                        api_get_setting('siteName')
372
                    )
373
                );
374
                break;*/
375
            /*case SmsPlugin::NEW_USER_PENDING_APPROVAL:
376
                return $this->buildSms(
377
                    $plugin,
378
                    $tpl,
379
                    'new_user_pending_approval.tpl',
380
                    'XNewUserXPendingApproval',
381
                    array(
382
                        api_get_setting('siteName')
383
                    )
384
                );
385
                break;*/
386
            /*case SmsPlugin::POSTED_FORUM_COURSE:
387
                return $this->buildSms(
388
                    $plugin,
389
                    $tpl,
390
                    'posted_forum_course.tpl',
391
                    'XXPostedForumXCourseX',
392
                    array(
393
                        api_get_setting('siteName')
394
                    )
395
                );
396
                break;*/
397
            /*case SmsPlugin::CHECK_EMAIL_CONNECT_MORE_INFO:
398
                return $this->buildSms(
399
                    $plugin,
400
                    $tpl,
401
                    'check_email_connect_more_info.tpl',
402
                    'XXXCheckEmailConnectMoreInfo',
403
                    array(
404
                        api_get_setting('siteName')
405
                    )
406
                );
407
                break;*/
408
            /*case SmsPlugin::STUDENT_ANSWERED_TEST:
409
                return $this->buildSms(
410
                    $plugin,
411
                    $tpl,
412
                    'student_answered_test.tpl',
413
                    'XXStudentXAnsweredTestX',
414
                    array(
415
                        api_get_setting('siteName')
416
                    )
417
                );
418
                break;*/
419
            /*case SmsPlugin::STUDENT_ANSWERED_TEST_OPEN_QUESTION:
420
                return $this->buildSms(
421
                    $plugin,
422
                    $tpl,
423
                    'student_answered_test_open_question.tpl',
424
                    'XXStudentXAnsweredTestXOpenQuestion',
425
                    array(
426
                        api_get_setting('siteName')
427
                    )
428
                );
429
                break;*/
430
            /*case SmsPlugin::STUDENT_ANSWERED_TEST_VOICE_QUESTION:
431
                return $this->buildSms(
432
                    $plugin,
433
                    $tpl,
434
                    'student_answered_test_voice_question.tpl',
435
                    'XXStudentXAnsweredTestXVoiceQuestion',
436
                    array(
437
                        api_get_setting('siteName')
438
                    )
439
                );
440
                break;*/
441
            /*case SmsPlugin::ANSWER_OPEN_QUESTION_TEST_REVIEWED:
442
                return $this->buildSms(
443
                    $plugin,
444
                    $tpl,
445
                    'answer_open_question_test_reviewed.tpl',
446
                    'XXAnswerOpenQuestionTestXReviewed',
447
                    array(
448
                        api_get_setting('siteName')
449
                    )
450
                );
451
                break;*/
452
            /*case SmsPlugin::NEW_THREAD_STARTED_FORUM:
453
                return $this->buildSms(
454
                    $plugin,
455
                    $tpl,
456
                    'new_thread_started_forum.tpl',
457
                    'XXNewThreadXStartedForumX',
458
                    array(
459
                        api_get_setting('siteName')
460
                    )
461
                );
462
                break;*/
463
            /*case SmsPlugin::NEW_ANSWER_POSTED_FORUM:
464
                return $this->buildSms(
465
                    $plugin,
466
                    $tpl,
467
                    'new_answer_posted_forum.tpl',
468
                    'XXNewAnswerPostedXForumX',
469
                    array(
470
                        api_get_setting('siteName')
471
                    )
472
                );
473
                break;*/
474
            /*case SmsPlugin::NEW_SYSTEM_ANNOUNCEMENT_ADDED:
475
                return $this->buildSms(
476
                    $plugin,
477
                    $tpl,
478
                    'new_system_announcement_added.tpl',
479
                    'XXNewSystemAnnouncementAdded',
480
                    array(
481
                        api_get_setting('siteName')
482
                    )
483
                );
484
                break;*/
485
            /*case SmsPlugin::TEST_NEW_SYSTEM_ANNOUNCEMENT_ADDED:
486
                return $this->buildSms(
487
                    $plugin,
488
                    $tpl,
489
                    'test_new_system_announcement_added.tpl',
490
                    'XTestXNewSystemAnnouncementAdded',
491
                    array(
492
                        api_get_setting('siteName')
493
                    )
494
                );
495
                break;*/
496
            /*case SmsPlugin::SYSTEM_ANNOUNCEMENT_UPDATE:
497
                return $this->buildSms(
498
                    $plugin,
499
                    $tpl,
500
                    'system_announcement_update.tpl',
501
                    'XXSystemAnnouncementUpdate',
502
                    array(
503
                        api_get_setting('siteName')
504
                    )
505
                );
506
                break;*/
507
            /*case SmsPlugin::TEST_SYSTEM_ANNOUNCEMENT_UPDATE:
508
                return $this->buildSms(
509
                    $plugin,
510
                    $tpl,
511
                    'test_system_announcement_update.tpl',
512
                    'XXSystemAnnouncementUpdate',
513
                    array(
514
                        api_get_setting('siteName')
515
                    )
516
                );
517
                break;*/
518
            /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE_STUDENT_SUBMITS_PAPER:
519
                return $this->buildSms(
520
                    $plugin,
521
                    $tpl,
522
                    'user_uploaded_assignment_course_student_submits_paper.tpl',
523
                    'XUserXUploadedAssignmentXCourseXStudentSubmitsPaper',
524
                    array(
525
                        api_get_setting('siteName')
526
                    )
527
                );
528
                break;*/
529
            /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK_STUDENT_SUBMITS_PAPER:
530
                return $this->buildSms(
531
                    $plugin,
532
                    $tpl,
533
                    'user_uploaded_assignment_check_student_submits_paper.tpl',
534
                    'XUserXUploadedAssignmentXCheckXStudentSubmitsPaper',
535
                    array(
536
                        api_get_setting('siteName')
537
                    )
538
                );
539
                break;*/
540
            /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE:
541
                return $this->buildSms(
542
                    $plugin,
543
                    $tpl,
544
                    'user_uploaded_assignment_course.tpl',
545
                    'XUserXUploadedAssignmentXCourseX',
546
                    array(
547
                        api_get_setting('siteName')
548
                    )
549
                );
550
                break;*/
551
            /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK:
552
                return $this->buildSms(
553
                    $plugin,
554
                    $tpl,
555
                    'user_uploaded_assignment_check.tpl',
556
                    'XUserXUploadedAssignmentXCheckX',
557
                    array(
558
                        api_get_setting('siteName')
559
                    )
560
                );
561
                break;*/
562
            /*case SmsPlugin::SUBSCRIBED_SESSION:
563
                return $this->buildSms(
564
                    $plugin,
565
                    $tpl,
566
                    'subscribed_session.tpl',
567
                    'XSubscribedSessionX',
568
                    array(
569
                        api_get_setting('siteName')
570
                    )
571
                );
572
                break;*/
573
            /*case SmsPlugin::SUBSCRIBED_SESSION_CSV:
574
                return $this->buildSms(
575
                    $plugin,
576
                    $tpl,
577
                    'subscribed_session_csv.tpl',
578
                    'XSubscribedSessionXCSV',
579
                    array(
580
                        api_get_setting('siteName')
581
                    )
582
                );
583
                break;*/
584
            /*case SmsPlugin::USER_SUGGESTED_BE_FRIENDS:
585
                return $this->buildSms(
586
                    $plugin,
587
                    $tpl,
588
                    'user_suggested_be_friends.tpl',
589
                    'XUserXSuggestedBeFriends',
590
                    array(
591
                        api_get_setting('siteName')
592
                    )
593
                );
594
                break;*/
595
            /*case SmsPlugin::USER_ANSWERED_INBOX_MESSAGE:
596
                return $this->buildSms(
597
                    $plugin,
598
                    $tpl,
599
                    'user_answered_inbox_message.tpl',
600
                    'XUserXAnsweredInboxMessage',
601
                    array(
602
                        api_get_setting('siteName')
603
                    )
604
                );
605
                break;*/
606
            /*case SmsPlugin::BEEN_INVITED_JOIN_GROUP:
607
                return $this->buildSms(
608
                    $plugin,
609
                    $tpl,
610
                    'been_invited_join_group.tpl',
611
                    'XBeenInvitedJoinGroupX',
612
                    array(
613
                        api_get_setting('siteName')
614
                    )
615
                );
616
                break;*/
617
            /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_EDITED:
618
                return $this->buildSms(
619
                    $plugin,
620
                    $tpl,
621
                    'messages_sent_edited_group_edited.tpl',
622
                    'XMessagesSentEditedGroupXEdited',
623
                    array(
624
                        api_get_setting('siteName')
625
                    )
626
                );
627
                break;*/
628
            /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_ADDED:
629
                return $this->buildSms(
630
                    $plugin,
631
                    $tpl,
632
                    'messages_sent_edited_group_added.tpl',
633
                    'XMessagesSentEditedGroupXAdded',
634
                    array(
635
                        api_get_setting('siteName')
636
                    )
637
                );
638
                break;*/
639
            /*case SmsPlugin::BEEN_INVITED_COMPLETE_SURVEY_COURSE:
640
                return $this->buildSms(
641
                    $plugin,
642
                    $tpl,
643
                    'been_invited_complete_survey_course.tpl',
644
                    'XBeenInvitedCompleteSurveyXCourseX',
645
                    array(
646
                        api_get_setting('siteName')
647
                    )
648
                );
649
                break;*/
650
            /*case SmsPlugin::REMINDER_ASSIGNMENT_COURSE_DUE:
651
                return $this->buildSms(
652
                    $plugin,
653
                    $tpl,
654
                    'reminder_assignment_course_due.tpl',
655
                    'XReminderAssignmentXCourseXDue',
656
                    array(
657
                        api_get_setting('siteName')
658
                    )
659
                );
660
                break;*/
661
            /*case SmsPlugin::USER_DETAILS_MODIFIED:
662
                return $this->buildSms(
663
                    $plugin,
664
                    $tpl,
665
                    'user_details_modified.tpl',
666
                    'XUserDetailsModified',
667
                    array(
668
                        api_get_setting('siteName')
669
                    )
670
                );
671
                break;*/
672
            default:
673
                return '';
674
        }
675
    }
676
}
677