AnnouncementEmail.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 integer $announcement
  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 string|null
  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. $table = 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 $table
  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_user_id <> 0 then it is sent to a specific user
  119. $user_id = $row['to_user_id'];
  120. if (!empty($user_id)) {
  121. $result['users'][] = (int) $user_id;
  122. // If user is set then skip the group
  123. continue;
  124. }
  125. // if to_group_id is null then it is sent to a specific user
  126. // if to_group_id = 0 then it is sent to everybody
  127. $group_id = $row['to_group_id'];
  128. if (!empty($group_id)) {
  129. $result['groups'][] = (int) $group_id;
  130. }
  131. }
  132. return $result;
  133. }
  134. /**
  135. * Returns the list of user info to which an announcement was sent.
  136. * This function returns a list of actual users even when recipient
  137. * are groups
  138. *
  139. * @return array
  140. */
  141. public function sent_to()
  142. {
  143. $sent_to = $this->sent_to_info();
  144. $users = $sent_to['users'];
  145. $users = $users ? $users : array();
  146. $groups = $sent_to['groups'];
  147. if ($users) {
  148. $users = UserManager::get_user_list_by_ids($users, true);
  149. }
  150. if (!empty($groups)) {
  151. $group_users = GroupManager::get_groups_users($groups);
  152. $group_users = UserManager::get_user_list_by_ids($group_users, true);
  153. if (!empty($group_users)) {
  154. $users = array_merge($users, $group_users);
  155. }
  156. }
  157. if (empty($users)) {
  158. $users = self::all_users();
  159. }
  160. //Clean users just in case
  161. $new_list_users = array();
  162. if (!empty($users)) {
  163. foreach ($users as $user) {
  164. $new_list_users[$user['user_id']] = array('user_id' => $user['user_id']);
  165. }
  166. }
  167. return $new_list_users;
  168. }
  169. /**
  170. * Sender info
  171. *
  172. * @param string $key
  173. *
  174. * @return array
  175. */
  176. public function sender($key = '', $userId = '')
  177. {
  178. $_user = api_get_user_info($userId);
  179. return $key ? $_user[$key] : $_user;
  180. }
  181. /**
  182. * Email subject
  183. *
  184. * @return string
  185. */
  186. public function subject()
  187. {
  188. $result = $this->course('title').' - '.$this->announcement('title');
  189. $result = stripslashes($result);
  190. return $result;
  191. }
  192. /**
  193. * Email message
  194. * @param int $receiverUserId
  195. *
  196. * @return string
  197. */
  198. public function message($receiverUserId)
  199. {
  200. $content = $this->announcement('content');
  201. $session_id = $this->session_id;
  202. $content = AnnouncementManager::parse_content(
  203. $receiverUserId,
  204. $content,
  205. $this->course('code'),
  206. $session_id
  207. );
  208. $user_email = $this->sender('mail');
  209. // Build the link by hand because api_get_cidreq() doesn't accept course params
  210. $course_param = 'cidReq='.api_get_course_id().'&id_session='.$session_id.'&gidReq='.api_get_group_id();
  211. $course_name = $this->course('title');
  212. $result = "<div>$content</div>";
  213. // Adding attachment
  214. $attachment = $this->attachment();
  215. if (!empty($attachment)) {
  216. $result .= '<br />';
  217. $result .= Display::url(
  218. $attachment['filename'],
  219. api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param
  220. );
  221. $result .= '<br />';
  222. }
  223. $result .= '<hr />';
  224. $sender_name = api_get_person_name(
  225. $this->sender('firstName'),
  226. $this->sender('lastName'),
  227. PERSON_NAME_EMAIL_ADDRESS
  228. );
  229. $result .= '<a href="mailto:'.$user_email.'">'.$sender_name.'</a><br/>';
  230. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>';
  231. return $result;
  232. }
  233. /**
  234. * Returns the one file that can be attached to an announcement.
  235. *
  236. * @return array
  237. */
  238. public function attachment()
  239. {
  240. $result = array();
  241. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  242. $id = $this->announcement('id');
  243. $course_id = $this->course('id');
  244. $sql = "SELECT * FROM $tbl_announcement_attachment
  245. WHERE c_id = $course_id AND announcement_id = $id ";
  246. $rs = Database::query($sql);
  247. $course_path = $this->course('directory');
  248. while ($row = Database::fetch_array($rs)) {
  249. $path = api_get_path(SYS_COURSE_PATH).$course_path.'/upload/announcements/'.$row['path'];
  250. $filename = $row['filename'];
  251. $result[] = array('path' => $path, 'filename' => $filename);
  252. }
  253. $result = $result ? reset($result) : array();
  254. return $result;
  255. }
  256. /**
  257. * Send emails to users.
  258. * @param bool $sendToUsersInSession
  259. * @param bool $sendToDrhUsers send a copy of the message to the DRH users
  260. * related to the main user
  261. */
  262. public function send($sendToUsersInSession = false, $sendToDrhUsers = false)
  263. {
  264. $sender = $this->sender();
  265. $subject = $this->subject();
  266. // Send email one by one to avoid antispam
  267. $users = $this->sent_to();
  268. $batchSize = 20;
  269. $counter = 1;
  270. $em = Database::getManager();
  271. foreach ($users as $user) {
  272. $message = $this->message($user['user_id']);
  273. MessageManager::send_message_simple(
  274. $user['user_id'],
  275. $subject,
  276. $message,
  277. $sender['user_id'],
  278. $sendToDrhUsers,
  279. true
  280. );
  281. if (($counter % $batchSize) === 0) {
  282. $em->flush();
  283. $em->clear();
  284. }
  285. $counter++;
  286. }
  287. if ($sendToUsersInSession) {
  288. $sessionList = SessionManager::get_session_by_course($this->course['real_id']);
  289. if (!empty($sessionList)) {
  290. foreach ($sessionList as $sessionInfo) {
  291. $sessionId = $sessionInfo['id'];
  292. $message = $this->message(null, $sessionId);
  293. $userList = CourseManager::get_user_list_from_course_code(
  294. $this->course['code'],
  295. $sessionId
  296. );
  297. if (!empty($userList)) {
  298. foreach ($userList as $user) {
  299. MessageManager::send_message_simple(
  300. $user['user_id'],
  301. $subject,
  302. $message,
  303. $sender['user_id'],
  304. false,
  305. true
  306. );
  307. }
  308. }
  309. }
  310. }
  311. }
  312. $this->log_mail_sent();
  313. }
  314. /**
  315. * Store that emails where sent
  316. */
  317. public function log_mail_sent()
  318. {
  319. $id = $this->announcement('id');
  320. $course_id = $this->course('id');
  321. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  322. $sql = "UPDATE $tbl_announcement SET email_sent=1
  323. WHERE c_id = $course_id AND id=$id AND session_id = {$this->session_id} ";
  324. Database::query($sql);
  325. }
  326. }