events_email.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Manages the email sending action when a event requires it.
  5. * @package chamilo.libraries.events
  6. */
  7. /**
  8. * EventsMail class: manages the e-mail sending action when a event requires it
  9. */
  10. class EventsMail
  11. {
  12. /**
  13. * Sends email according to an event
  14. *
  15. * @param string $event_name the name of the event that was triggered
  16. * @param array $event_data what to put in the mail
  17. *
  18. * Possible key :
  19. * - $event_data["about_user"] (= $user_id)
  20. * - $event_data["prior_lang"]
  21. *
  22. * Warning :
  23. * - $event_data["send_to"] MUST BE an array
  24. */
  25. public static function send_mail($event_name, $event_data)
  26. {
  27. /**
  28. * Global explanation :
  29. * 1. we get information about the user that fired the event (in $event_data["about_user"])
  30. * 2. we send mail to people that are in the $event_data["send_to"]
  31. * 2b. if a language was specified, we use that one to send the mail, else we get the user's language, if there isn't any, we get the english one
  32. * 3. we do the same with the people associated to the event through the admin panel
  33. */
  34. global $event_config;
  35. // common variable for every mail sent
  36. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  37. $email_admin = api_get_setting('emailAdministrator');
  38. // basic keys
  39. $event_data["sitename"] = api_get_setting('siteName');
  40. $event_data["administrator_name"] = api_get_setting('administratorName');
  41. $event_data["administrator_surname"] = api_get_setting('administratorSurname');
  42. $event_data["administrator_phone"] = api_get_setting('administratorTelephone');
  43. $event_data["administrator_email"] = api_get_setting('emailAdministrator');
  44. $event_data["portal"] = api_get_path(WEB_PATH);
  45. // Fill the array's cells with info regarding the user that fired the event
  46. // (for the keys in the template)
  47. if ( isset($event_data["about_user"]) )
  48. {
  49. $about_user = api_get_user_info($event_data["about_user"]);
  50. $event_data["firstname"] = $about_user["firstname"];
  51. $event_data["lastname"] = $about_user["lastname"];
  52. $event_data["username"] = $about_user["username"];
  53. $event_data["usermail"] = $about_user["mail"];
  54. $event_data["language"] = $about_user["language"];
  55. $event_data["user_id"] = $about_user["user_id"];
  56. }
  57. // First, we send the mail to people we put in the $event_data["send_to"]
  58. if ($event_data["send_to"] != null) // the users we precised need to receive the mail
  59. {
  60. foreach ($event_data["send_to"] as $id) // for every member put in the array
  61. {
  62. // get user's info (to know where to send)
  63. $user_info = api_get_user_info($id);
  64. // get the language the email will be in
  65. if ($event_data["prior_lang"] != null) // if $lang is not null, we use that lang
  66. {
  67. $language = $event_data["prior_lang"];
  68. }
  69. else // else we use the user's language
  70. {
  71. $sql = 'SELECT language FROM ' . Database::get_main_table(TABLE_MAIN_USER) . ' u
  72. WHERE u.user_id = "' . $id . '"
  73. ';
  74. $language = Database::store_result(Database::query($sql), 'ASSOC');
  75. $language = $language[0]["language"];
  76. }
  77. // we get the message in the correct language (or in english if doesn't exist)
  78. $result = self::getMessage($event_name, $language);
  79. $message = "";
  80. $subject = "";
  81. self::getCorrectMessage($message, $subject, $language, $result);
  82. // replace the keycodes used in the message
  83. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  84. // sending email
  85. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  86. // checks if there's a file we need to join to the mail
  87. if (isset($values["certificate_pdf_file"]))
  88. {
  89. $message = str_replace("\n", "<br />", $message);
  90. @api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, array($values['certificate_pdf_file']));
  91. }
  92. else
  93. {
  94. @api_mail($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin);
  95. }
  96. // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table
  97. if ($event_config[$event_name]["sending_mail_once"])
  98. {
  99. $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  100. (user_from, user_to, event_type_name)
  101. VALUES ('.$event_data["user_id"].', '.$id.' ,"'.Database::escape_string($event_name).'");
  102. ';
  103. Database::query($sql);
  104. }
  105. }
  106. }
  107. // Second, we send to people linked to the event
  108. // So, we get everyone
  109. $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname FROM ' . Database::get_main_table(TABLE_EVENT_TYPE_REL_USER) . ' ue
  110. INNER JOIN '.Database::get_main_table(TABLE_MAIN_USER).' u ON u.user_id = ue.user_id
  111. WHERE event_type_name = "' . $event_name . '"';
  112. $result = Database::store_result(Database::query($sql), 'ASSOC');
  113. foreach ($result as $key => $value) // for each of the linked users
  114. {
  115. // we get the language
  116. if ($event_data["prior_lang"] != null) // if $lang is not null, we use that lang
  117. {
  118. $language = $event_data["prior_lang"];
  119. }
  120. else // else we get the user's lang
  121. {
  122. $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).'
  123. where user_id = '.$value["user_id"].' ';
  124. $result = Database::store_result(Database::query($sql), 'ASSOC');
  125. $language = $result[0]["language"];
  126. }
  127. // we get the message in the correct language (or in english if doesn't exist)
  128. $result = self::getMessage($event_name, $language);
  129. $message = "";
  130. $subject = "";
  131. self::getCorrectMessage($message, $subject, $language, $result);
  132. // replace the keycodes used in the message
  133. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  134. // we send the mail
  135. $recipient_name = api_get_person_name($value['firstname'], $value['lastname']);
  136. @api_mail($recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin);
  137. // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table
  138. if ($event_config[$event_name]["sending_mail_once"])
  139. {
  140. $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  141. (user_from, user_to, event_type_name)
  142. VALUES ('.$event_data["user_id"].', '.$value["user_id"].' , "'.Database::escape_string($event_name).'");
  143. ';
  144. Database::query($sql);
  145. }
  146. }
  147. }
  148. /**
  149. * Checks if a message in a language exists, if the event is activated
  150. * and if "manage event" is checked in admin panel.
  151. * If yes to three, we can use this class, else we still use api_mail.
  152. *
  153. * @param string $event_name
  154. * @return boolean
  155. */
  156. public static function check_if_using_class($event_name) {
  157. if (api_get_setting('activate_email_template') === 'false') {
  158. return false;
  159. }
  160. $current_language = api_get_interface_language();
  161. $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em
  162. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l on em.language_id = l.id
  163. WHERE em.event_type_name = "' . $event_name . '" and l.dokeos_folder = "'.$current_language.'" and em.activated = 1';
  164. $exists = Database::store_result(Database::query($sql), 'ASSOC');
  165. if ($exists[0]["total"]) {
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. /**
  172. * Get the record containing the good message and subject
  173. *
  174. * @param string $event_name
  175. * @param string $language
  176. * @return array
  177. */
  178. private static function getMessage($event_name, $language)
  179. {
  180. $sql = 'SELECT message, subject, l.dokeos_folder FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em
  181. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l on em.language_id = l.id
  182. WHERE em.event_type_name = "' . $event_name . '" and (l.dokeos_folder = "' . $language . '" OR l.dokeos_folder = "english") and em.message <> ""
  183. ';
  184. return Database::store_result(Database::query($sql), 'ASSOC');
  185. }
  186. /**
  187. * Get the correct message, meaning in the specified language or in english if previous one doesn't exist
  188. *
  189. * @param string $message
  190. * @param string $subject
  191. * @param string $language
  192. * @param array $result
  193. */
  194. private static function getCorrectMessage(&$message, &$subject, $language, $result)
  195. {
  196. foreach ($result as $msg)
  197. {
  198. if ($msg["dokeos_folder"] == $language)
  199. {
  200. $message = $msg["message"];
  201. $subject = $msg["subject"];
  202. break;
  203. }
  204. else if ($msg["dokeos_folder"] == "english")
  205. {
  206. $message = $msg["message"];
  207. $subject = $msg["subject"];
  208. }
  209. }
  210. }
  211. /**
  212. * Replaces the ((key)) by the good values
  213. *
  214. * @param string $message
  215. * @param string $subject
  216. * @param array $event_config
  217. * @param string $event_name
  218. */
  219. private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data)
  220. {
  221. foreach ($event_config[$event_name]["available_keyvars"] as $key => $word)
  222. {
  223. $message = str_replace('((' . $key . '))', $event_data[$word], $message);
  224. $subject = str_replace('((' . $key . '))', $event_data[$word], $subject);
  225. }
  226. }
  227. }