notification.lib.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. require_once 'model.lib.php';
  9. require_once 'usermanager.lib.php';
  10. //default values
  11. //mail_notify_message ("At once", "Daily", "No")
  12. define('NOTIFY_MESSAGE_AT_ONCE', '1');
  13. define('NOTIFY_MESSAGE_DAILY', '8');
  14. define('NOTIFY_MESSAGE_WEEKLY', '12');
  15. define('NOTIFY_MESSAGE_NO', '0');
  16. //mail_notify_invitation ("At once", "Daily", "No")
  17. define('NOTIFY_INVITATION_AT_ONCE', '1');
  18. define('NOTIFY_INVITATION_DAILY', '8');
  19. define('NOTIFY_INVITATION_WEEKLY', '12');
  20. define('NOTIFY_INVITATION_NO', '0');
  21. // mail_notify_group_message ("At once", "Daily", "No")
  22. define('NOTIFY_GROUP_AT_ONCE', '1');
  23. define('NOTIFY_GROUP_DAILY', '8');
  24. define('NOTIFY_GROUP_WEEKLY', '12');
  25. define('NOTIFY_GROUP_NO', '0');
  26. class Notification extends Model {
  27. var $table;
  28. var $columns = array('id','dest_user_id','dest_mail','title','content','send_freq','created_at','sent_at');
  29. var $max_content_length = 254; //Max lenght of the notification.content field
  30. var $debug = true;
  31. public function __construct() {
  32. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  33. }
  34. public function send($frec = NOTIFY_MESSAGE_DAILY) {
  35. $notifications = $this->find('all',array('where'=>array('sent_at IS NULL AND send_freq = ?'=>$frec)));
  36. if (!empty($notifications)) {
  37. foreach($notifications as $item_to_send) {
  38. //Sending email
  39. //$name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  40. api_mail_html($item_to_send['dest_mail'], $item_to_send['dest_mail'], $item_to_send['title'], $item_to_send['content']);
  41. if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); }
  42. //Updating
  43. $item_to_send['sent_at'] = api_get_utc_datetime();
  44. $this->update($item_to_send);
  45. //if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); }
  46. }
  47. }
  48. }
  49. /**
  50. * Save message notification
  51. * @param array user list of ids
  52. * @param string title
  53. * @param string content of the message
  54. *
  55. */
  56. public function save_message_notifications($user_list, $title, $content) {
  57. if (!empty($user_list)) {
  58. foreach($user_list as $user_id) {
  59. $extra_data = UserManager::get_extra_user_data($user_id);
  60. $params = array();
  61. switch ($extra_data['mail_notify_message']) {
  62. case NOTIFY_MESSAGE_NO:
  63. break;
  64. case NOTIFY_MESSAGE_AT_ONCE:
  65. $user_info = api_get_user_info($user_id);
  66. if (!empty($user_info['mail'])) {
  67. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  68. api_mail_html($name, $user_info['mail'], $title, $content);
  69. }
  70. $params['sent_at'] = api_get_utc_datetime();
  71. default:
  72. $extra_data = UserManager::get_extra_user_data($user_id);
  73. $user_info = api_get_user_info($user_id);
  74. $params['dest_user_id'] = $user_id;
  75. $params['dest_mail'] = $user_info['mail'];
  76. $params['title'] = $title;
  77. $params['content'] = cut($content, $this->max_content_length);
  78. $params['send_freq'] = $extra_data['mail_notify_message'];
  79. $this->save($params);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * Save invitation notification
  87. * @param array user list of ids
  88. * @param string title
  89. * @param string content of the message
  90. *
  91. */
  92. public function save_invitation_notifications($user_list, $title, $content) {
  93. if (!empty($user_list)) {
  94. foreach($user_list as $user_id) {
  95. $extra_data = UserManager::get_extra_user_data($user_id);
  96. $params = array();
  97. switch ($extra_data['mail_notify_invitation']) {
  98. case NOTIFY_INVITATION_NO:
  99. break;
  100. case NOTIFY_INVITATION_AT_ONCE:
  101. $user_info = api_get_user_info($user_id);
  102. if (!empty($user_info['mail'])) {
  103. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  104. api_mail_html($name, $user_info['mail'], $title, $content);
  105. }
  106. $params['sent_at'] = api_get_utc_datetime();
  107. default:
  108. $extra_data = UserManager::get_extra_user_data($user_id);
  109. $user_info = api_get_user_info($user_id);
  110. $params['dest_user_id'] = $user_id;
  111. $params['dest_mail'] = $user_info['mail'];
  112. $params['title'] = $title;
  113. $params['content'] = cut($content, $this->max_content_length);
  114. $params['send_freq'] = $extra_data['mail_notify_invitation'];
  115. $this->save($params);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Save group notifications
  123. * @param array user list of ids
  124. * @param string title
  125. * @param string content of the message
  126. *
  127. */
  128. public function save_group_notifications($user_list, $title, $content) {
  129. if (!empty($user_list)) {
  130. foreach($user_list as $user_id) {
  131. //Avoiding sending a message to myself
  132. if ($user_id == api_get_user_id()) {
  133. continue;
  134. }
  135. $extra_data = UserManager::get_extra_user_data($user_id);
  136. $params = array();
  137. switch ($extra_data['mail_notify_group_message']) {
  138. case NOTIFY_GROUP_NO:
  139. break;
  140. case NOTIFY_GROUP_AT_ONCE:
  141. $user_info = api_get_user_info($user_id);
  142. if (!empty($user_info['mail'])) {
  143. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  144. api_mail_html($name, $user_info['mail'], $title, $content);
  145. }
  146. $params['sent_at'] = api_get_utc_datetime();
  147. default:
  148. $extra_data = UserManager::get_extra_user_data($user_id);
  149. $user_info = api_get_user_info($user_id);
  150. $params['dest_user_id'] = $user_id;
  151. $params['dest_mail'] = $user_info['mail'];
  152. $params['title'] = $subject;
  153. $params['content'] = cut($content,$this->max_content_length);
  154. $params['send_freq'] = $extra_data['mail_notify_group_message'];
  155. $this->save($params);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }