announcement_email.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Announcement Email
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. * @author Julio Montoya <gugli100@gmail.com> Adding session support
  8. */
  9. class AnnouncementEmail
  10. {
  11. /**
  12. *
  13. * @param int|array $course
  14. * @param int|array $annoucement
  15. *
  16. * @return AnnouncementEmail
  17. */
  18. public static function create($course, $announcement) {
  19. return new self($course, $announcement);
  20. }
  21. protected $course = null;
  22. protected $announcement = null;
  23. public $session_id = null;
  24. function __construct($course, $announcement) {
  25. if (empty($course)) {
  26. $course = api_get_course_int_id();
  27. $course = CourseManager::get_course_information_by_id($course);
  28. } else if (is_numeric($course)) {
  29. $course = CourseManager::get_course_information_by_id(intval($course));
  30. }
  31. $this->course = $course;
  32. $this->session_id = api_get_session_id();
  33. if (is_numeric($announcement)) {
  34. $announcement = AnnouncementManager::get_by_id($course['real_id'], $announcement);
  35. }
  36. $this->announcement = $announcement;
  37. }
  38. /**
  39. * Course info
  40. *
  41. * @param string $key
  42. * @return array
  43. */
  44. public function course($key = '') {
  45. $result = $key ? $this->course[$key] : $this->course;
  46. $result = $key == 'id' ? intval($result) : $result;
  47. return $result;
  48. }
  49. /**
  50. * Announcement info
  51. *
  52. * @param string $key
  53. * @return array
  54. */
  55. public function announcement($key = '') {
  56. $result = $key ? $this->announcement[$key] : $this->announcement;
  57. $result = $key == 'id' ? intval($result) : $result;
  58. return $result;
  59. }
  60. /**
  61. * Returns either all course users or all session users depending on whether
  62. * session is turned on or not
  63. *
  64. * @return array
  65. */
  66. public function all_users() {
  67. $course_code = $this->course('code');
  68. if (empty($this->session_id)) {
  69. $group_id = api_get_group_id();
  70. if (empty($group_id)) {
  71. $user_list = CourseManager::get_user_list_from_course_code($course_code);
  72. } else {
  73. $user_list = GroupManager::get_users($group_id);
  74. $new_user_list = array();
  75. foreach ($user_list as $user) {
  76. $new_user_list[] = array('user_id' => $user);
  77. }
  78. $user_list = $new_user_list;
  79. }
  80. } else {
  81. $user_list = CourseManager::get_user_list_from_course_code($course_code, $this->session_id);
  82. }
  83. return $user_list;
  84. }
  85. /**
  86. * Returns users and groups an announcement item has been sent to.
  87. *
  88. * @return array Array of users and groups to whom the element has been sent
  89. */
  90. public function sent_to_info() {
  91. $result = array();
  92. $result['groups'] = array();
  93. $result['users'] = array();
  94. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  95. $tool = TOOL_ANNOUNCEMENT;
  96. $id = $this->announcement('id');
  97. $course_id = $this->course('id');
  98. $sql = "SELECT to_group_id, to_user_id FROM $tbl_item_property WHERE c_id = $course_id AND tool = '$tool' AND ref = $id AND id_session = {$this->session_id} ";
  99. $rs = Database::query($sql);
  100. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  101. // if to_group_id is null then it is sent to a specific user
  102. // if to_group_id = 0 then it is sent to everybody
  103. $group_id = $row['to_group_id'];
  104. if (!empty($group_id)) {
  105. $result['groups'][] = (int) $group_id;
  106. }
  107. // if to_user_id <> 0 then it is sent to a specific user
  108. $user_id = $row['to_user_id'];
  109. if (!empty($user_id)) {
  110. $result['users'][] = (int) $user_id;
  111. }
  112. }
  113. return $result;
  114. }
  115. /**
  116. * Returns the list of user info to which an announcement was sent.
  117. * This function returns a list of actual users even when recipient
  118. * are groups
  119. *
  120. * @return array
  121. */
  122. public function sent_to() {
  123. $sent_to = $this->sent_to_info();
  124. $users = $sent_to['users'];
  125. $users = $users ? $users : array();
  126. $groups = $sent_to['groups'];
  127. if ($users) {
  128. $users = UserManager::get_user_list_by_ids($users, true);
  129. }
  130. if (!empty($groups)) {
  131. $group_users = GroupManager::get_groups_users($groups);
  132. $group_users = UserManager::get_user_list_by_ids($group_users, true);
  133. if (!empty($group_users)) {
  134. $users = array_merge($users, $group_users);
  135. }
  136. }
  137. if (empty($users)) {
  138. $users = self::all_users();
  139. }
  140. //Clean users just in case
  141. $new_list_users = array();
  142. if (!empty($users)) {
  143. foreach ($users as $user) {
  144. $new_list_users[$user['user_id']] = array('user_id' => $user['user_id']);
  145. }
  146. }
  147. return $new_list_users;
  148. }
  149. /**
  150. * Sender info
  151. *
  152. * @param string $key
  153. * @return array
  154. */
  155. public function sender($key = '') {
  156. global $_user;
  157. return $key ? $_user[$key] : $_user;
  158. }
  159. /**
  160. * Email subject
  161. *
  162. * @return string
  163. */
  164. public function subject() {
  165. $result = $this->course('title').' - '.$this->announcement('title');
  166. $result = stripslashes($result);
  167. return $result;
  168. }
  169. /**
  170. * Email message
  171. *
  172. * @return string
  173. */
  174. public function message() {
  175. $title = $this->announcement('title');
  176. $title = stripslashes($title);
  177. $content = $this->announcement('content');
  178. $content = stripslashes($content);
  179. $content = AnnouncementManager::parse_content($content, $this->course('code'));
  180. $user_email = $this->sender('mail');
  181. $course_param = api_get_cidreq();
  182. $course_name = $this->course('title');
  183. $result = "<div>$content</div>";
  184. //Adding attachment
  185. $attachment = $this->attachement();
  186. if (!empty($attachment)) {
  187. $result .= '<br />';
  188. $result .= Display::url($attachment['filename'], api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param).'<br />';
  189. }
  190. $result .= '<hr />';
  191. $sender_name = api_get_person_name($this->sender('firstName'), $this->sender('lastName'), PERSON_NAME_EMAIL_ADDRESS);
  192. $result .= '<a href="mailto:'.$user_email.'">'.$sender_name.'</a><br/>';
  193. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>';
  194. return $result;
  195. }
  196. /**
  197. * Returns the one file that can be attached to an announcement.
  198. *
  199. * @return array
  200. */
  201. public function attachement() {
  202. $result = array();
  203. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  204. $id = $this->announcement('id');
  205. $course_id = $this->course('id');
  206. $sql = "SELECT * FROM $tbl_announcement_attachment WHERE c_id = $course_id AND announcement_id = $id ";
  207. $rs = Database::query($sql);
  208. $course_path = $this->course('directory');
  209. while ($row = Database::fetch_array($rs)) {
  210. $path = api_get_path(SYS_COURSE_PATH).$course_path.'/upload/announcements/'.$row['path'];
  211. $filename = $row['filename'];
  212. $result[] = array('path' => $path, 'filename' => $filename);
  213. }
  214. $result = $result ? reset($result) : array();
  215. return $result;
  216. }
  217. /**
  218. * Send emails to users.
  219. */
  220. public function send() {
  221. $sender = $this->sender();
  222. $subject = $this->subject();
  223. $message = $this->message();
  224. // Send email one by one to avoid antispam
  225. $users = $this->sent_to();
  226. foreach ($users as $user) {
  227. MessageManager::send_message_simple($user['user_id'], $subject, $message, $sender['user_id']);
  228. }
  229. $this->log_mail_sent();
  230. }
  231. /**
  232. * Store that emails where sent
  233. */
  234. public function log_mail_sent() {
  235. $id = $this->announcement('id');
  236. $course_id = $this->course('id');
  237. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  238. $sql = "UPDATE $tbl_announcement SET email_sent=1 WHERE c_id = $course_id AND id=$id AND session_id = {$this->session_id} ";
  239. Database::query($sql);
  240. }
  241. }