PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/inc/lib/notification.lib.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 233 lines | 164 code | 23 blank | 46 comment | 22 complexity | c7316cac7a5520d4bde717a3d418feb9 MD5 | raw file
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the Notification management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Code
  10. */
  11. /**
  12. * Notification class
  13. * @package chamilo.library
  14. */
  15. class Notification extends Model
  16. {
  17. public $table;
  18. public $columns = array('id', 'dest_user_id', 'dest_mail', 'title', 'content', 'send_freq', 'created_at', 'sent_at');
  19. public $max_content_length = 254; //Max lenght of the notification.content field
  20. public $debug = false;
  21. /* message, invitation, group messages */
  22. public $type;
  23. public $admin_name;
  24. public $admin_email;
  25. //mail_notify_message ("At once", "Daily", "No")
  26. const NOTIFY_MESSAGE_AT_ONCE = 1;
  27. const NOTIFY_MESSAGE_DAILY = 8;
  28. const NOTIFY_MESSAGE_WEEKLY = 12;
  29. const NOTIFY_MESSAGE_NO = 0;
  30. //mail_notify_invitation ("At once", "Daily", "No")
  31. const NOTIFY_INVITATION_AT_ONCE = 1;
  32. const NOTIFY_INVITATION_DAILY = 8;
  33. const NOTIFY_INVITATION_WEEKLY = 12;
  34. const NOTIFY_INVITATION_NO = 0;
  35. // mail_notify_group_message ("At once", "Daily", "No")
  36. const NOTIFY_GROUP_AT_ONCE = 1;
  37. const NOTIFY_GROUP_DAILY = 8;
  38. const NOTIFY_GROUP_WEEKLY = 12;
  39. const NOTIFY_GROUP_NO = 0;
  40. const NOTIFICATION_TYPE_MESSAGE = 1;
  41. const NOTIFICATION_TYPE_INVITATION = 2;
  42. const NOTIFICATION_TYPE_GROUP = 3;
  43. public function __construct()
  44. {
  45. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  46. $this->admin_email = api_get_setting('noreply_email_address');
  47. $this->admin_name = api_get_setting('siteName');
  48. // If no-reply email doesn't exist use the admin email
  49. if (empty($this->admin_email)) {
  50. $this->admin_email = api_get_setting('emailAdministrator');
  51. $this->admin_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  52. }
  53. }
  54. /**
  55. * Send the notifications
  56. * @param int notification frecuency
  57. */
  58. public function send($frec = 8)
  59. {
  60. $notifications = $this->find('all', array('where' => array('sent_at IS NULL AND send_freq = ?' => $frec)));
  61. if (!empty($notifications)) {
  62. foreach ($notifications as $item_to_send) {
  63. //Sending email
  64. api_mail_html($item_to_send['dest_mail'], $item_to_send['dest_mail'], Security::filter_terms($item_to_send['title']), Security::filter_terms($item_to_send['content']), $this->admin_name, $this->admin_email);
  65. if ($this->debug) {
  66. error_log('Sending message to: '.$item_to_send['dest_mail']);
  67. }
  68. //Updating
  69. $item_to_send['sent_at'] = api_get_utc_datetime();
  70. $this->update($item_to_send);
  71. if ($this->debug) {
  72. error_log('Updating record : '.print_r($item_to_send, 1));
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * Save message notification
  79. * @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP
  80. * @param array recipients: user list of ids
  81. * @param string title
  82. * @param string content of the message
  83. * @param array result of api_get_user_info() or GroupPortalManager:get_group_data()
  84. */
  85. public function save_notification($type, $user_list, $title, $content, $sender_info = array())
  86. {
  87. $this->type = intval($type);
  88. $content = $this->format_content($content, $sender_info);
  89. $setting_to_check = '';
  90. $avoid_my_self = false;
  91. switch ($this->type) {
  92. case self::NOTIFICATION_TYPE_MESSAGE;
  93. $setting_to_check = 'mail_notify_message';
  94. $default_status = self::NOTIFY_MESSAGE_AT_ONCE;
  95. break;
  96. case self::NOTIFICATION_TYPE_INVITATION;
  97. $setting_to_check = 'mail_notify_invitation';
  98. $default_status = self::NOTIFY_INVITATION_AT_ONCE;
  99. break;
  100. case self::NOTIFICATION_TYPE_GROUP;
  101. $setting_to_check = 'mail_notify_group_message';
  102. $default_status = self::NOTIFY_GROUP_AT_ONCE;
  103. $avoid_my_self = true;
  104. break;
  105. }
  106. $setting_info = UserManager::get_extra_field_information_by_name($setting_to_check);
  107. if (!empty($user_list)) {
  108. foreach ($user_list as $user_id) {
  109. if ($avoid_my_self) {
  110. if ($user_id == api_get_user_id()) {
  111. continue;
  112. }
  113. }
  114. $user_info = api_get_user_info($user_id);
  115. //Extra field was deleted or removed? Use the default status
  116. if (empty($setting_info)) {
  117. $user_setting = $default_status;
  118. } else {
  119. $extra_data = UserManager::get_extra_user_data($user_id);
  120. $user_setting = $extra_data[$setting_to_check];
  121. }
  122. $params = array();
  123. switch ($user_setting) {
  124. //No notifications
  125. case self::NOTIFY_MESSAGE_NO:
  126. case self::NOTIFY_INVITATION_NO:
  127. case self::NOTIFY_GROUP_NO:
  128. break;
  129. //Send notification right now!
  130. case self::NOTIFY_MESSAGE_AT_ONCE:
  131. case self::NOTIFY_INVITATION_AT_ONCE:
  132. case self::NOTIFY_GROUP_AT_ONCE:
  133. if (!empty($user_info['mail'])) {
  134. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  135. if (!empty($sender_info['complete_name']) && !empty($sender_info['email'])) {
  136. $extra_headers = array();
  137. $extra_headers['reply_to']['mail'] = $sender_info['email'];
  138. $extra_headers['reply_to']['name'] = $sender_info['complete_name'];
  139. api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $sender_info['complete_name'], $sender_info['email'], $extra_headers);
  140. } else {
  141. api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $sender_info['complete_name'], $sender_info['email']);
  142. }
  143. }
  144. $params['sent_at'] = api_get_utc_datetime();
  145. //Saving the notification to be sent some day
  146. default:
  147. $params['dest_user_id'] = $user_id;
  148. $params['dest_mail'] = $user_info['mail'];
  149. $params['title'] = $title;
  150. $params['content'] = cut($content, $this->max_content_length);
  151. $params['send_freq'] = $user_setting;
  152. $this->save($params);
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Formats the content in order to add the welcome message, the notification preference, etc
  160. * @param string the content
  161. * @param array result of api_get_user_info() or GroupPortalManager:get_group_data()
  162. * */
  163. public function format_content($content, $sender_info)
  164. {
  165. $new_message_text = $link_to_new_message = '';
  166. switch ($this->type) {
  167. case self::NOTIFICATION_TYPE_MESSAGE:
  168. if (!empty($sender_info)) {
  169. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  170. //$sender_mail = $sender_info['email'] ;
  171. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name);
  172. }
  173. $link_to_new_message = Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'messages/inbox.php');
  174. break;
  175. case self::NOTIFICATION_TYPE_INVITATION:
  176. if (!empty($sender_info)) {
  177. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  178. //$sender_mail = $sender_info['email'] ;
  179. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name);
  180. }
  181. $link_to_new_message = Display::url(get_lang('SeeInvitation'), api_get_path(WEB_CODE_PATH).'social/invitations.php');
  182. break;
  183. case self::NOTIFICATION_TYPE_GROUP:
  184. $topic_page = intval($_REQUEST['topics_page_nr']);
  185. if (!empty($sender_info)) {
  186. $sender_name = $sender_info['group_info']['name'];
  187. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name);
  188. $sender_name = api_get_person_name($sender_info['user_info']['firstname'], $sender_info['user_info']['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  189. $sender_name = Display::url($sender_name, api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']);
  190. $new_message_text .= '<br />'.get_lang('User').': '.$sender_name;
  191. }
  192. $group_url = api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$sender_info['group_info']['id'].'&topic_id='.$sender_info['group_info']['topic_id'].'&msg_id='.$sender_info['group_info']['msg_id'].'&topics_page_nr='.$topic_page;
  193. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  194. break;
  195. }
  196. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  197. // You have received a new message text
  198. if (!empty($new_message_text)) {
  199. $content = $new_message_text.'<br /><hr><br />'.$content;
  200. }
  201. // See message with link text
  202. if (!empty($link_to_new_message)) {
  203. $content = $content.'<br /><br />'.$link_to_new_message;
  204. }
  205. // You have received this message because you are subscribed text
  206. $content = $content.'<br /><hr><i>'.
  207. sprintf(get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'), Display::url($preference_url, $preference_url)).'</i>';
  208. return $content;
  209. }
  210. }