clockworksms.lib.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /* For licensing terms, see /vendor/license.txt */
  3. /**
  4. * Class Clockworksms
  5. * This script handles incoming SMS information, process it and sends an SMS if everything is right.
  6. *
  7. * @package chamilo.plugin.clockworksms.lib
  8. *
  9. * @author Imanol Losada <imanol.losada@beeznest.com>
  10. *
  11. * Clockworksms-Chamilo connector class
  12. */
  13. class Clockworksms implements SmsPluginLibraryInterface
  14. {
  15. public $apiKey;
  16. public $api;
  17. public $plugin_enabled = false;
  18. /**
  19. * Constructor (generates a connection to the API).
  20. *
  21. * @param string Clockworksms API key required to use the plugin
  22. */
  23. public function __construct($apiKey = null)
  24. {
  25. $plugin = ClockworksmsPlugin::create();
  26. $clockWorkSMSPlugin = $plugin->get('tool_enable');
  27. if (empty($apiKey)) {
  28. $clockWorkSMSApiKey = $plugin->get('api_key');
  29. } else {
  30. $clockWorkSMSApiKey = $apiKey;
  31. }
  32. $this->table = Database::get_main_table('user_field_values');
  33. if ($clockWorkSMSPlugin == true) {
  34. $this->apiKey = $clockWorkSMSApiKey;
  35. // Setting Clockworksms api
  36. if (!defined('CONFIG_SECURITY_API_KEY')) {
  37. define('CONFIG_SECURITY_API_KEY', $this->apiKey);
  38. }
  39. $trimmedApiKey = trim(CONFIG_SECURITY_API_KEY);
  40. if (!empty($trimmedApiKey)) {
  41. $this->api = new Clockwork(CONFIG_SECURITY_API_KEY);
  42. } else {
  43. $this->api = new Clockwork(' ');
  44. $recipient_name = api_get_person_name(
  45. api_get_setting('administratorName'),
  46. api_get_setting('administratorSurname'),
  47. null,
  48. PERSON_NAME_EMAIL_ADDRESS
  49. );
  50. $email_form = api_get_setting('emailAdministrator');
  51. $emailsubject = 'Clockworksms error';
  52. $emailbody = 'Key cannot be blank';
  53. $sender_name = $recipient_name;
  54. $email_admin = $email_form;
  55. api_mail_html(
  56. $recipient_name,
  57. $email_form,
  58. $emailsubject,
  59. $emailbody,
  60. $sender_name,
  61. $email_admin
  62. );
  63. }
  64. $this->plugin_enabled = true;
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getMobilePhoneNumberById($userId)
  71. {
  72. $mobilePhoneNumberExtraField = new ExtraField('user');
  73. $mobilePhoneNumberExtraField = $mobilePhoneNumberExtraField->get_handler_field_info_by_field_variable(
  74. 'mobile_phone_number'
  75. );
  76. $mobilePhoneNumberExtraFieldValue = new ExtraFieldValue('user');
  77. $mobilePhoneNumberExtraFieldValue = $mobilePhoneNumberExtraFieldValue->get_values_by_handler_and_field_id(
  78. $userId,
  79. $mobilePhoneNumberExtraField['id']
  80. );
  81. return $mobilePhoneNumberExtraFieldValue['value'];
  82. }
  83. /**
  84. * send (sends an SMS to the user).
  85. *
  86. * @param array Data needed to send the SMS. It is mandatory to include the
  87. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  88. * More data may be neccesary depending on the message type
  89. * Example: $additional_parameters = array(
  90. * 'smsType' => EXAMPLE_SMS_TYPE,
  91. * 'userId' => $userId,
  92. * 'moreData' => $moreData
  93. * );
  94. */
  95. public function send($additionalParameters)
  96. {
  97. $trimmedKey = trim(CONFIG_SECURITY_API_KEY);
  98. if (!empty($trimmedKey)) {
  99. $phoneExists = array_key_exists('mobilePhoneNumber', $additionalParameters);
  100. $to = $phoneExists ? $additionalParameters['mobilePhoneNumber'] : $this->getMobilePhoneNumberById($additionalParameters['userId']);
  101. $message = [
  102. 'to' => $to,
  103. 'message' => $this->getSms($additionalParameters),
  104. ];
  105. if (!empty($message['message'])) {
  106. $this->api->send($message);
  107. }
  108. }
  109. }
  110. /**
  111. * buildSms (builds an SMS from a template and data).
  112. *
  113. * @param ClockworksmsPlugin $plugin
  114. * @param Template $tpl
  115. * @param string Template file name
  116. * @param string $messageKey Text key from lang file
  117. * @param array $parameters Data to fill message variables (if any)
  118. *
  119. * @return string
  120. */
  121. public function buildSms(
  122. $plugin,
  123. $tpl,
  124. $templateName,
  125. $messageKey,
  126. $parameters = []
  127. ) {
  128. // Send direct message with out using plugin get_lang
  129. if (isset($parameters['direct_message'])) {
  130. return $parameters['direct_message'];
  131. }
  132. $message = $plugin->get_lang($messageKey);
  133. if ($parameters !== null) {
  134. $message = vsprintf($message, $parameters);
  135. }
  136. return $message;
  137. }
  138. /**
  139. * getSms (returns an SMS message depending of its type).
  140. *
  141. * @param array Data needed to send the SMS. It is mandatory to include the
  142. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  143. * More data may be neccesary depending on the message type
  144. * Example: $additional_parameters = array(
  145. * 'smsType' => EXAMPLE_SMS_TYPE,
  146. * 'userId' => $userId,
  147. * 'moreData' => $moreData
  148. * );
  149. *
  150. * @return string A ready to be sent SMS
  151. */
  152. public function getSms($additionalParameters)
  153. {
  154. $plugin = ClockworksmsPlugin::create();
  155. $tool_name = $plugin->get_lang('plugin_title');
  156. $tpl = new Template($tool_name);
  157. switch ($additionalParameters['smsType']) {
  158. case SmsPlugin::WELCOME_LOGIN_PASSWORD:
  159. $userInfo = api_get_user_info($additionalParameters['userId']);
  160. return $this->buildSms(
  161. $plugin,
  162. $tpl,
  163. 'welcome_login_password.tpl',
  164. 'WelcomeXLoginXPasswordX',
  165. [
  166. api_get_setting('siteName'),
  167. $userInfo['username'],
  168. $additionalParameters['password'],
  169. ]
  170. );
  171. break;
  172. case SmsPlugin::NEW_FILE_SHARED_COURSE_BY:
  173. return $this->buildSms(
  174. $plugin,
  175. $tpl,
  176. 'new_file_shared_course_by.tpl',
  177. 'XNewFileSharedCourseXByX',
  178. [
  179. api_get_setting('siteName'),
  180. $additionalParameters['courseTitle'],
  181. $additionalParameters['userUsername'],
  182. ]
  183. );
  184. break;
  185. case SmsPlugin::ACCOUNT_APPROVED_CONNECT:
  186. return $this->buildSms(
  187. $plugin,
  188. $tpl,
  189. 'account_approved_connect.tpl',
  190. 'XAccountApprovedConnectX',
  191. [
  192. api_get_setting('siteName'),
  193. $tpl->params['_p']['web'],
  194. ]
  195. );
  196. break;
  197. case SmsPlugin::NEW_COURSE_BEEN_CREATED:
  198. return $this->buildSms(
  199. $plugin,
  200. $tpl,
  201. 'new_course_been_created.tpl',
  202. 'XNewCourseXBeenCreatedX',
  203. [
  204. api_get_setting('siteName'),
  205. $additionalParameters['courseName'],
  206. $additionalParameters['creatorUsername'],
  207. ]
  208. );
  209. break;
  210. case SmsPlugin::NEW_USER_SUBSCRIBED_COURSE:
  211. return $this->buildSms(
  212. $plugin,
  213. $tpl,
  214. 'new_user_subscribed_course.tpl',
  215. 'XNewUserXSubscribedCourseX',
  216. [
  217. api_get_setting('siteName'),
  218. $additionalParameters['userUsername'],
  219. $additionalParameters['courseCode'],
  220. ]
  221. );
  222. break;
  223. case SmsPlugin::NEW_COURSE_SUGGESTED_TEACHER:
  224. return $this->buildSms(
  225. $plugin,
  226. $tpl,
  227. 'new_course_suggested_teacher.tpl',
  228. 'XNewCourseSuggestedTeacherX',
  229. [
  230. api_get_setting('siteName'),
  231. $additionalParameters['userUsername'],
  232. ]
  233. );
  234. break;
  235. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REGISTERED:
  236. return $this->buildSms(
  237. $plugin,
  238. $tpl,
  239. 'course_opening_request_code_registered.tpl',
  240. 'XCourseOpeningRequestCodeXRegistered',
  241. [
  242. api_get_setting('siteName'),
  243. $additionalParameters['courseCode'],
  244. ]
  245. );
  246. break;
  247. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED:
  248. return $this->buildSms(
  249. $plugin,
  250. $tpl,
  251. 'course_opening_request_course_code_approved.tpl',
  252. 'XCourseOpeningRequestCourseCodeXApproved',
  253. [
  254. api_get_setting('siteName'),
  255. $additionalParameters['courseCode'],
  256. ]
  257. );
  258. break;
  259. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED:
  260. return $this->buildSms(
  261. $plugin,
  262. $tpl,
  263. 'request_open_course_code_rejected.tpl',
  264. 'XRequestOpenCourseCodeXReject',
  265. [
  266. api_get_setting('siteName'),
  267. $additionalParameters['courseCode'],
  268. ]
  269. );
  270. break;
  271. case SmsPlugin::COURSE_OPENING_REQUEST_CODE:
  272. return $this->buildSms(
  273. $plugin,
  274. $tpl,
  275. 'course_opening_request_course_code.tpl',
  276. 'XCourseOpeningRequestCourseCodeX',
  277. [
  278. api_get_setting('siteName'),
  279. $additionalParameters['courseCode'],
  280. ]
  281. );
  282. break;
  283. case SmsPlugin::BEEN_SUBSCRIBED_COURSE:
  284. return $this->buildSms(
  285. $plugin,
  286. $tpl,
  287. 'been_subscribed_course.tpl',
  288. 'XBeenSubscribedCourseX',
  289. [
  290. api_get_setting('siteName'),
  291. $additionalParameters['courseTitle'],
  292. ]
  293. );
  294. break;
  295. case SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE:
  296. return $this->buildSms(
  297. $plugin,
  298. $tpl,
  299. 'assignment_been_created_course.tpl',
  300. 'XAssignmentBeenCreatedCourseX',
  301. [
  302. api_get_setting('siteName'),
  303. $additionalParameters['courseTitle'],
  304. ]
  305. );
  306. break;
  307. case SmsPlugin::CERTIFICATE_NOTIFICATION:
  308. return $this->buildSms(
  309. $plugin,
  310. $tpl,
  311. 'certificate_notification.tpl',
  312. '',
  313. $additionalParameters
  314. );
  315. break;
  316. // Message types to be implemented. Fill the array parameter with arguments.
  317. /*case SmsPlugin::ACCOUNT_CREATED_UPDATED_LOGIN_PASSWORD:
  318. return $this->buildSms(
  319. $plugin,
  320. $tpl,
  321. 'account_created_updated_login_password.tpl',
  322. 'XAccountCreatedUpdatedLoginXPasswordX',
  323. array(
  324. api_get_setting('siteName')
  325. )
  326. );
  327. break;*/
  328. /*case SmsPlugin::PASSWORD_UPDATED_LOGIN_PASSWORD:
  329. return $this->buildSms(
  330. $plugin,
  331. $tpl,
  332. 'password_updated_login_password.tpl',
  333. 'XPasswordUpdatedLoginXPasswordX',
  334. array(
  335. api_get_setting('siteName')
  336. )
  337. );
  338. break;*/
  339. /*case SmsPlugin::REQUESTED_PASSWORD_CHANGE:
  340. return $this->buildSms(
  341. $plugin,
  342. $tpl,
  343. 'requested_password_change.tpl',
  344. 'XPasswordUpdatedLoginXPasswordX',
  345. array(
  346. api_get_setting('siteName')
  347. )
  348. );
  349. break;*/
  350. /*case SmsPlugin::RECEIVED_NEW_PERSONAL_MESSAGES:
  351. return $this->buildSms(
  352. $plugin,
  353. $tpl,
  354. 'received_new_personal_messages.tpl',
  355. 'XReceivedNewPersonalMessages',
  356. array(
  357. api_get_setting('siteName')
  358. )
  359. );
  360. break;*/
  361. /*case SmsPlugin::NEW_USER_PENDING_APPROVAL:
  362. return $this->buildSms(
  363. $plugin,
  364. $tpl,
  365. 'new_user_pending_approval.tpl',
  366. 'XNewUserXPendingApproval',
  367. array(
  368. api_get_setting('siteName')
  369. )
  370. );
  371. break;*/
  372. /*case SmsPlugin::POSTED_FORUM_COURSE:
  373. return $this->buildSms(
  374. $plugin,
  375. $tpl,
  376. 'posted_forum_course.tpl',
  377. 'XXPostedForumXCourseX',
  378. array(
  379. api_get_setting('siteName')
  380. )
  381. );
  382. break;*/
  383. /*case SmsPlugin::CHECK_EMAIL_CONNECT_MORE_INFO:
  384. return $this->buildSms(
  385. $plugin,
  386. $tpl,
  387. 'check_email_connect_more_info.tpl',
  388. 'XXXCheckEmailConnectMoreInfo',
  389. array(
  390. api_get_setting('siteName')
  391. )
  392. );
  393. break;*/
  394. /*case SmsPlugin::STUDENT_ANSWERED_TEST:
  395. return $this->buildSms(
  396. $plugin,
  397. $tpl,
  398. 'student_answered_test.tpl',
  399. 'XXStudentXAnsweredTestX',
  400. array(
  401. api_get_setting('siteName')
  402. )
  403. );
  404. break;*/
  405. /*case SmsPlugin::STUDENT_ANSWERED_TEST_OPEN_QUESTION:
  406. return $this->buildSms(
  407. $plugin,
  408. $tpl,
  409. 'student_answered_test_open_question.tpl',
  410. 'XXStudentXAnsweredTestXOpenQuestion',
  411. array(
  412. api_get_setting('siteName')
  413. )
  414. );
  415. break;*/
  416. /*case SmsPlugin::STUDENT_ANSWERED_TEST_VOICE_QUESTION:
  417. return $this->buildSms(
  418. $plugin,
  419. $tpl,
  420. 'student_answered_test_voice_question.tpl',
  421. 'XXStudentXAnsweredTestXVoiceQuestion',
  422. array(
  423. api_get_setting('siteName')
  424. )
  425. );
  426. break;*/
  427. /*case SmsPlugin::ANSWER_OPEN_QUESTION_TEST_REVIEWED:
  428. return $this->buildSms(
  429. $plugin,
  430. $tpl,
  431. 'answer_open_question_test_reviewed.tpl',
  432. 'XXAnswerOpenQuestionTestXReviewed',
  433. array(
  434. api_get_setting('siteName')
  435. )
  436. );
  437. break;*/
  438. /*case SmsPlugin::NEW_THREAD_STARTED_FORUM:
  439. return $this->buildSms(
  440. $plugin,
  441. $tpl,
  442. 'new_thread_started_forum.tpl',
  443. 'XXNewThreadXStartedForumX',
  444. array(
  445. api_get_setting('siteName')
  446. )
  447. );
  448. break;*/
  449. /*case SmsPlugin::NEW_ANSWER_POSTED_FORUM:
  450. return $this->buildSms(
  451. $plugin,
  452. $tpl,
  453. 'new_answer_posted_forum.tpl',
  454. 'XXNewAnswerPostedXForumX',
  455. array(
  456. api_get_setting('siteName')
  457. )
  458. );
  459. break;*/
  460. /*case SmsPlugin::NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  461. return $this->buildSms(
  462. $plugin,
  463. $tpl,
  464. 'new_system_announcement_added.tpl',
  465. 'XXNewSystemAnnouncementAdded',
  466. array(
  467. api_get_setting('siteName')
  468. )
  469. );
  470. break;*/
  471. /*case SmsPlugin::TEST_NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  472. return $this->buildSms(
  473. $plugin,
  474. $tpl,
  475. 'test_new_system_announcement_added.tpl',
  476. 'XTestXNewSystemAnnouncementAdded',
  477. array(
  478. api_get_setting('siteName')
  479. )
  480. );
  481. break;*/
  482. /*case SmsPlugin::SYSTEM_ANNOUNCEMENT_UPDATE:
  483. return $this->buildSms(
  484. $plugin,
  485. $tpl,
  486. 'system_announcement_update.tpl',
  487. 'XXSystemAnnouncementUpdate',
  488. array(
  489. api_get_setting('siteName')
  490. )
  491. );
  492. break;*/
  493. /*case SmsPlugin::TEST_SYSTEM_ANNOUNCEMENT_UPDATE:
  494. return $this->buildSms(
  495. $plugin,
  496. $tpl,
  497. 'test_system_announcement_update.tpl',
  498. 'XXSystemAnnouncementUpdate',
  499. array(
  500. api_get_setting('siteName')
  501. )
  502. );
  503. break;*/
  504. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE_STUDENT_SUBMITS_PAPER:
  505. return $this->buildSms(
  506. $plugin,
  507. $tpl,
  508. 'user_uploaded_assignment_course_student_submits_paper.tpl',
  509. 'XUserXUploadedAssignmentXCourseXStudentSubmitsPaper',
  510. array(
  511. api_get_setting('siteName')
  512. )
  513. );
  514. break;*/
  515. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK_STUDENT_SUBMITS_PAPER:
  516. return $this->buildSms(
  517. $plugin,
  518. $tpl,
  519. 'user_uploaded_assignment_check_student_submits_paper.tpl',
  520. 'XUserXUploadedAssignmentXCheckXStudentSubmitsPaper',
  521. array(
  522. api_get_setting('siteName')
  523. )
  524. );
  525. break;*/
  526. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE:
  527. return $this->buildSms(
  528. $plugin,
  529. $tpl,
  530. 'user_uploaded_assignment_course.tpl',
  531. 'XUserXUploadedAssignmentXCourseX',
  532. array(
  533. api_get_setting('siteName')
  534. )
  535. );
  536. break;*/
  537. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK:
  538. return $this->buildSms(
  539. $plugin,
  540. $tpl,
  541. 'user_uploaded_assignment_check.tpl',
  542. 'XUserXUploadedAssignmentXCheckX',
  543. array(
  544. api_get_setting('siteName')
  545. )
  546. );
  547. break;*/
  548. /*case SmsPlugin::SUBSCRIBED_SESSION:
  549. return $this->buildSms(
  550. $plugin,
  551. $tpl,
  552. 'subscribed_session.tpl',
  553. 'XSubscribedSessionX',
  554. array(
  555. api_get_setting('siteName')
  556. )
  557. );
  558. break;*/
  559. /*case SmsPlugin::SUBSCRIBED_SESSION_CSV:
  560. return $this->buildSms(
  561. $plugin,
  562. $tpl,
  563. 'subscribed_session_csv.tpl',
  564. 'XSubscribedSessionXCSV',
  565. array(
  566. api_get_setting('siteName')
  567. )
  568. );
  569. break;*/
  570. /*case SmsPlugin::USER_SUGGESTED_BE_FRIENDS:
  571. return $this->buildSms(
  572. $plugin,
  573. $tpl,
  574. 'user_suggested_be_friends.tpl',
  575. 'XUserXSuggestedBeFriends',
  576. array(
  577. api_get_setting('siteName')
  578. )
  579. );
  580. break;*/
  581. /*case SmsPlugin::USER_ANSWERED_INBOX_MESSAGE:
  582. return $this->buildSms(
  583. $plugin,
  584. $tpl,
  585. 'user_answered_inbox_message.tpl',
  586. 'XUserXAnsweredInboxMessage',
  587. array(
  588. api_get_setting('siteName')
  589. )
  590. );
  591. break;*/
  592. /*case SmsPlugin::BEEN_INVITED_JOIN_GROUP:
  593. return $this->buildSms(
  594. $plugin,
  595. $tpl,
  596. 'been_invited_join_group.tpl',
  597. 'XBeenInvitedJoinGroupX',
  598. array(
  599. api_get_setting('siteName')
  600. )
  601. );
  602. break;*/
  603. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_EDITED:
  604. return $this->buildSms(
  605. $plugin,
  606. $tpl,
  607. 'messages_sent_edited_group_edited.tpl',
  608. 'XMessagesSentEditedGroupXEdited',
  609. array(
  610. api_get_setting('siteName')
  611. )
  612. );
  613. break;*/
  614. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_ADDED:
  615. return $this->buildSms(
  616. $plugin,
  617. $tpl,
  618. 'messages_sent_edited_group_added.tpl',
  619. 'XMessagesSentEditedGroupXAdded',
  620. array(
  621. api_get_setting('siteName')
  622. )
  623. );
  624. break;*/
  625. /*case SmsPlugin::BEEN_INVITED_COMPLETE_SURVEY_COURSE:
  626. return $this->buildSms(
  627. $plugin,
  628. $tpl,
  629. 'been_invited_complete_survey_course.tpl',
  630. 'XBeenInvitedCompleteSurveyXCourseX',
  631. array(
  632. api_get_setting('siteName')
  633. )
  634. );
  635. break;*/
  636. /*case SmsPlugin::REMINDER_ASSIGNMENT_COURSE_DUE:
  637. return $this->buildSms(
  638. $plugin,
  639. $tpl,
  640. 'reminder_assignment_course_due.tpl',
  641. 'XReminderAssignmentXCourseXDue',
  642. array(
  643. api_get_setting('siteName')
  644. )
  645. );
  646. break;*/
  647. /*case SmsPlugin::USER_DETAILS_MODIFIED:
  648. return $this->buildSms(
  649. $plugin,
  650. $tpl,
  651. 'user_details_modified.tpl',
  652. 'XUserDetailsModified',
  653. array(
  654. api_get_setting('siteName')
  655. )
  656. );
  657. break;*/
  658. default:
  659. return '';
  660. }
  661. }
  662. }