events_email.class.php 10 KB

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