announcement_email.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. */
  8. class AnnouncementEmail
  9. {
  10. /**
  11. *
  12. * @param int|array $course
  13. * @param int|array $annoucement
  14. *
  15. * @return AnnouncementEmail
  16. */
  17. public static function create($course, $announcement)
  18. {
  19. return new self($course, $announcement);
  20. }
  21. protected $course = null;
  22. protected $announcement = null;
  23. function __construct($course, $announcement)
  24. {
  25. if (empty($course))
  26. {
  27. $course = api_get_course_int_id();
  28. $course = CourseManager::get_course_information_by_id($course);
  29. }
  30. else if (is_numeric($course))
  31. {
  32. $course = CourseManager::get_course_information_by_id(intval($course));
  33. }
  34. $this->course = $course;
  35. if (is_numeric($announcement))
  36. {
  37. $announcement = AnnouncementManager::get_by_id($course['id'], intval($announcement));
  38. }
  39. $this->announcement = $announcement;
  40. }
  41. /**
  42. * Course info
  43. *
  44. * @param string $key
  45. * @return array
  46. */
  47. public function course($key = '')
  48. {
  49. $result = $key ? $this->course[$key] : $this->course;
  50. $result = $key == 'id' ? intval($result) : $result;
  51. return $result;
  52. }
  53. /**
  54. * Announcement info
  55. *
  56. * @param string $key
  57. * @return array
  58. */
  59. public function announcement($key = '')
  60. {
  61. $result = $key ? $this->announcement[$key] : $this->announcement;
  62. $result = $key == 'id' ? intval($result) : $result;
  63. return $result;
  64. }
  65. /**
  66. * Returns either all course users or all session users depending on whether
  67. * session is turned on or not
  68. *
  69. * @return array
  70. */
  71. public function all_users()
  72. {
  73. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  74. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  75. $course_code = $this->course('code');
  76. $course_code = Database::escape_string($course_code);
  77. if (empty($_SESSION['id_session']) || api_get_setting('use_session_mode') == 'false')
  78. {
  79. $rel_rh = COURSE_RELATION_TYPE_RRHH;
  80. $sql = "SELECT user.user_id, user.email, user.lastname, user.firstname
  81. FROM $tbl_course_user, $tbl_user
  82. WHERE active = 1 AND
  83. course_code='$course_code' AND
  84. course_rel_user.user_id = user.user_id AND
  85. relation_type <> $rel_rh";
  86. }
  87. else
  88. {
  89. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  90. $session_id = api_get_session_id();
  91. $sql = "SELECT user.user_id, user.email, user.lastname, user.firstname
  92. FROM $tbl_user
  93. INNER JOIN $tbl_session_course_user
  94. ON $tbl_user.user_id = $tbl_session_course_user.id_user AND
  95. $tbl_session_course_user.course_code = $course_code AND
  96. $tbl_session_course_user.id_session = $session_id
  97. WHERE
  98. active = 1";
  99. }
  100. $rs = Database::query($sql);
  101. $result = array();
  102. while ($data = Database::fetch_array($rs))
  103. {
  104. $result[] = $data;
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Returns users and groups an announcement item has been sent to.
  110. *
  111. * @return array Array of users and groups to whom the element has been sent
  112. */
  113. public function sent_to_info()
  114. {
  115. $result = array();
  116. $result['groups'] = array();
  117. $result['users'] = array();
  118. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  119. $tool = TOOL_ANNOUNCEMENT;
  120. $id = $this->announcement('id');
  121. $course_id = $this->course('id');
  122. $sql = "SELECT to_group_id, to_user_id FROM $tbl_item_property WHERE c_id = $course_id AND tool = '$tool' AND ref=$id";
  123. $rs = Database::query($sql);
  124. $sent_to_group = array();
  125. $sent_to_user = array();
  126. while ($row = Database::fetch_array($rs))
  127. {
  128. // if to_group_id is null then it is sent to a specific user
  129. // if to_group_id = 0 then it is sent to everybody
  130. $group_id = $row['to_group_id'];
  131. if (!empty($group_id))
  132. {
  133. $result['groups'][] = (int)$group_id;
  134. }
  135. // if to_user_id <> 0 then it is sent to a specific user
  136. $user_id = $row['to_user_id'];
  137. if (!empty($user_id))
  138. {
  139. $result['users'][] = (int)$user_id;
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * Returns the list of user info to which an announcement was sent.
  146. * This function returns a list of actual users even when recipient
  147. * are groups
  148. *
  149. * @return array
  150. */
  151. public function sent_to()
  152. {
  153. $sent_to = $this->sent_to_info();
  154. $users = $sent_to['users'];
  155. $groups = $sent_to['groups'];
  156. if (!empty($groups))
  157. {
  158. $group_users = GroupManager::get_groups_users($groups);
  159. $group_users = UserManager::get_user_list_by_ids($group_users, true);
  160. $users = array_merge($users, $group_users);
  161. }
  162. if (empty($users))
  163. {
  164. $users = self::all_users();
  165. }
  166. return $users;
  167. }
  168. /**
  169. * Sender info
  170. *
  171. * @param string $key
  172. * @return array
  173. */
  174. public function sender($key = '')
  175. {
  176. global $_user;
  177. return $key ? $_user[$key] : $_user;
  178. }
  179. /**
  180. * Email subject
  181. *
  182. * @return string
  183. */
  184. public function subject()
  185. {
  186. $result = $this->course('title') . ' - ' . $this->announcement('title');
  187. $result = stripslashes($result);
  188. return $result;
  189. }
  190. /**
  191. * Email message
  192. *
  193. * @return string
  194. */
  195. public function message()
  196. {
  197. $title = $this->announcement('title');
  198. $title = stripslashes($title);
  199. $content = $this->announcement('content');
  200. $content = stripslashes($content);
  201. $user_firstname = $this->sender('firstName');
  202. $user_lastname = $this->sender('lastName');
  203. $user_email = $this->sender('mail');
  204. $www = api_get_path(WEB_CODE_PATH);
  205. $course_param = api_get_cidreq();
  206. $course_name = $this->course('title');
  207. $result = '';
  208. $result .= "<div>$content</div>";
  209. $result .= '--<br/>';
  210. $result .= "<a href=\"mailto:$user_email\">$user_firstname $user_lastname</a><br/>";
  211. $result .= "<a href=\"$www/announcements/announcements.php?$course_param\">$course_name</a><br/>";
  212. return $result;
  213. }
  214. /**
  215. * Returns the one file that can be attached to an announcement.
  216. *
  217. * @return array
  218. */
  219. public function attachement()
  220. {
  221. $result = array();
  222. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  223. $id = $this->announcement('id');
  224. $course_id = $this->course('id');
  225. $sql = "SELECT * FROM $tbl_announcement_attachment WHERE c_id = $course_id AND announcement_id = $id ";
  226. $rs = Database::query($sql);
  227. $course_path = $this->course('directory');
  228. while ($row = Database::fetch_array($rs))
  229. {
  230. $path = api_get_path(SYS_COURSE_PATH) . $course_path . '/upload/announcements/' . $row['path'];
  231. $filename = $row['filename'];
  232. $result[] = array('path' => $path, 'filename' => $filename);
  233. }
  234. $result = $result ? reset($result) : array();
  235. return $result;
  236. }
  237. /**
  238. * Send emails to users.
  239. */
  240. public function send()
  241. {
  242. $sender = $this->sender();
  243. $sender_name = api_get_person_name($sender['firstName'], $sender['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
  244. $sender_email = $sender['mail'];
  245. $subject = $this->subject();
  246. $message = $this->message();
  247. $attachement = $this->attachement();
  248. // Send email one by one to avoid antispam
  249. $users = $this->sent_to();
  250. foreach ($users as $user)
  251. {
  252. $recipient_name = api_get_person_name($user['firstname'], $user['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  253. $recipient_email = $user['email'];
  254. @api_mail_html($recipient_name, $recipient_email, $subject, $message, $sender_name, $sender_email, null, $attachement, true);
  255. }
  256. $this->log_mail_sent();
  257. }
  258. /**
  259. * Store that emails where sent
  260. */
  261. public function log_mail_sent()
  262. {
  263. $id = $this->announcement('id');
  264. $course_id = $this->course('id');
  265. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  266. $sql = "UPDATE $tbl_announcement SET email_sent=1 WHERE c_id = $course_id AND id=$id";
  267. Database::query($sql);
  268. }
  269. }