events_email.class.php 11 KB

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