notification.lib.php 7.8 KB

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