clockworksms.lib.php 24 KB

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