AnnouncementEmail.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Announcement Email
  5. *
  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. protected $course = null;
  12. protected $announcement = null;
  13. public $session_id = null;
  14. /**
  15. *
  16. * @param int|array $course
  17. * @param int|array $annoucement
  18. *
  19. * @return AnnouncementEmail
  20. */
  21. public static function create($course, $announcement)
  22. {
  23. return new self($course, $announcement);
  24. }
  25. /**
  26. * @param int $courseId
  27. * @param int $announcement
  28. */
  29. public function __construct($courseId, $announcement)
  30. {
  31. if (!empty($courseId)) {
  32. $course = api_get_course_info_by_id($courseId);
  33. } else {
  34. $course = api_get_course_info();
  35. }
  36. $this->course = $course;
  37. $this->session_id = api_get_session_id();
  38. if (is_numeric($announcement)) {
  39. $announcement = AnnouncementManager::get_by_id($course['real_id'], $announcement);
  40. }
  41. $this->announcement = $announcement;
  42. }
  43. /**
  44. * Course info
  45. *
  46. * @param string $key
  47. *
  48. * @return array
  49. */
  50. public function course($key = '')
  51. {
  52. $result = $key ? $this->course[$key] : $this->course;
  53. $result = $key == 'id' ? intval($result) : $result;
  54. return $result;
  55. }
  56. /**
  57. * Announcement info
  58. *
  59. * @param string $key
  60. * @return array
  61. */
  62. public function announcement($key = '')
  63. {
  64. $result = $key ? $this->announcement[$key] : $this->announcement;
  65. $result = $key == 'id' ? intval($result) : $result;
  66. return $result;
  67. }
  68. /**
  69. * Returns either all course users or all session users depending on whether
  70. * session is turned on or not
  71. *
  72. * @return array
  73. */
  74. public function all_users()
  75. {
  76. $course_code = $this->course('code');
  77. if (empty($this->session_id)) {
  78. $group_id = api_get_group_id();
  79. if (empty($group_id)) {
  80. $user_list = CourseManager::get_user_list_from_course_code($course_code);
  81. } else {
  82. $user_list = GroupManager::get_users($group_id);
  83. $new_user_list = array();
  84. foreach ($user_list as $user) {
  85. $new_user_list[] = array('user_id' => $user);
  86. }
  87. $user_list = $new_user_list;
  88. }
  89. } else {
  90. $user_list = CourseManager::get_user_list_from_course_code($course_code, $this->session_id);
  91. }
  92. return $user_list;
  93. }
  94. /**
  95. * Returns users and groups an announcement item has been sent to.
  96. *
  97. * @return array Array of users and groups to whom the element has been sent
  98. */
  99. public function sent_to_info()
  100. {
  101. $result = array();
  102. $result['groups'] = array();
  103. $result['users'] = array();
  104. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  105. $tool = TOOL_ANNOUNCEMENT;
  106. $id = $this->announcement('id');
  107. $course_id = $this->course('real_id');
  108. $sessionCondition = api_get_session_condition($this->session_id);
  109. $sql = "SELECT to_group_id, to_user_id
  110. FROM $tbl_item_property
  111. WHERE
  112. c_id = $course_id AND
  113. tool = '$tool' AND
  114. ref = $id
  115. $sessionCondition";
  116. $rs = Database::query($sql);
  117. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  118. // if to_group_id is null then it is sent to a specific user
  119. // if to_group_id = 0 then it is sent to everybody
  120. $group_id = $row['to_group_id'];
  121. if (!empty($group_id)) {
  122. $result['groups'][] = (int) $group_id;
  123. }
  124. // if to_user_id <> 0 then it is sent to a specific user
  125. $user_id = $row['to_user_id'];
  126. if (!empty($user_id)) {
  127. $result['users'][] = (int) $user_id;
  128. }
  129. }
  130. return $result;
  131. }
  132. /**
  133. * Returns the list of user info to which an announcement was sent.
  134. * This function returns a list of actual users even when recipient
  135. * are groups
  136. *
  137. * @return array
  138. */
  139. public function sent_to()
  140. {
  141. $sent_to = $this->sent_to_info();
  142. $users = $sent_to['users'];
  143. $users = $users ? $users : array();
  144. $groups = $sent_to['groups'];
  145. if ($users) {
  146. $users = UserManager::get_user_list_by_ids($users, true);
  147. }
  148. if (!empty($groups)) {
  149. $group_users = GroupManager::get_groups_users($groups);
  150. $group_users = UserManager::get_user_list_by_ids($group_users, true);
  151. if (!empty($group_users)) {
  152. $users = array_merge($users, $group_users);
  153. }
  154. }
  155. if (empty($users)) {
  156. $users = self::all_users();
  157. }
  158. //Clean users just in case
  159. $new_list_users = array();
  160. if (!empty($users)) {
  161. foreach ($users as $user) {
  162. $new_list_users[$user['user_id']] = array('user_id' => $user['user_id']);
  163. }
  164. }
  165. return $new_list_users;
  166. }
  167. /**
  168. * Sender info
  169. *
  170. * @param string $key
  171. *
  172. * @return array
  173. */
  174. public function sender($key = '', $userId = '')
  175. {
  176. $_user = api_get_user_info($userId);
  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. * @param int $receiverUserId
  193. *
  194. * @return string
  195. */
  196. public function message($receiverUserId)
  197. {
  198. $content = $this->announcement('content');
  199. $session_id = $this->session_id;
  200. $content = AnnouncementManager::parse_content(
  201. $receiverUserId,
  202. $content,
  203. $this->course('code'),
  204. $session_id
  205. );
  206. $user_email = $this->sender('mail');
  207. //$course_param = api_get_cidreq();
  208. // Build the link by hand because api_get_cidreq() doesn't accept course params
  209. $course_param = 'cidReq='.api_get_course_id().'&amp;id_session='.$session_id.'&amp;gidReq='.api_get_group_id();
  210. $course_name = $this->course('title');
  211. $result = "<div>$content</div>";
  212. // Adding attachment
  213. $attachment = $this->attachment();
  214. if (!empty($attachment)) {
  215. $result .= '<br />';
  216. $result .= Display::url(
  217. $attachment['filename'],
  218. api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param
  219. ).'<br />';
  220. }
  221. $result .= '<hr />';
  222. $sender_name = api_get_person_name(
  223. $this->sender('firstName'),
  224. $this->sender('lastName'),
  225. PERSON_NAME_EMAIL_ADDRESS
  226. );
  227. $result .= '<a href="mailto:'.$user_email.'">'.$sender_name.'</a><br/>';
  228. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>';
  229. return $result;
  230. }
  231. /**
  232. * Returns the one file that can be attached to an announcement.
  233. *
  234. * @return array
  235. */
  236. public function attachment()
  237. {
  238. $result = array();
  239. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  240. $id = $this->announcement('id');
  241. $course_id = $this->course('id');
  242. $sql = "SELECT * FROM $tbl_announcement_attachment WHERE c_id = $course_id AND announcement_id = $id ";
  243. $rs = Database::query($sql);
  244. $course_path = $this->course('directory');
  245. while ($row = Database::fetch_array($rs)) {
  246. $path = api_get_path(SYS_COURSE_PATH).$course_path.'/upload/announcements/'.$row['path'];
  247. $filename = $row['filename'];
  248. $result[] = array('path' => $path, 'filename' => $filename);
  249. }
  250. $result = $result ? reset($result) : array();
  251. return $result;
  252. }
  253. /**
  254. * Send emails to users.
  255. * @param bool $sendToUsersInSession
  256. * @param bool $sendToDrhUsers send a copy of the message to the DRH users
  257. * related to the main user
  258. */
  259. public function send($sendToUsersInSession = false, $sendToDrhUsers = false)
  260. {
  261. $sender = $this->sender();
  262. $subject = $this->subject();
  263. // Send email one by one to avoid antispam
  264. $users = $this->sent_to();
  265. foreach ($users as $user) {
  266. $message = $this->message($user['user_id']);
  267. MessageManager::send_message_simple(
  268. $user['user_id'],
  269. $subject,
  270. $message,
  271. $sender['user_id'],
  272. $sendToDrhUsers,
  273. true
  274. );
  275. }
  276. if ($sendToUsersInSession) {
  277. $sessionList = SessionManager::get_session_by_course($this->course['real_id']);
  278. if (!empty($sessionList)) {
  279. foreach ($sessionList as $sessionInfo) {
  280. $sessionId = $sessionInfo['id'];
  281. $message = $this->message(null, $sessionId);
  282. $userList = CourseManager::get_user_list_from_course_code(
  283. $this->course['code'],
  284. $sessionId
  285. );
  286. if (!empty($userList)) {
  287. foreach ($userList as $user) {
  288. MessageManager::send_message_simple(
  289. $user['user_id'],
  290. $subject,
  291. $message,
  292. $sender['user_id'],
  293. false,
  294. true
  295. );
  296. }
  297. }
  298. }
  299. }
  300. }
  301. $this->log_mail_sent();
  302. }
  303. /**
  304. * Store that emails where sent
  305. */
  306. public function log_mail_sent()
  307. {
  308. $id = $this->announcement('id');
  309. $course_id = $this->course('id');
  310. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  311. $sql = "UPDATE $tbl_announcement SET email_sent=1
  312. WHERE c_id = $course_id AND id=$id AND session_id = {$this->session_id} ";
  313. Database::query($sql);
  314. }
  315. }