AnnouncementEmail.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 array $courseInfo
  17. * @param int $sessionId
  18. * @param int $announcementId
  19. *
  20. * @return AnnouncementEmail
  21. */
  22. public static function create($courseInfo, $sessionId, $announcementId)
  23. {
  24. return new self($courseInfo, $sessionId, $announcementId);
  25. }
  26. /**
  27. * @param array $courseInfo
  28. * @param int $sessionId
  29. * @param int $announcementId
  30. */
  31. public function __construct($courseInfo, $sessionId, $announcementId)
  32. {
  33. if (empty($courseInfo)) {
  34. $courseInfo = api_get_course_info();
  35. }
  36. $this->course = $courseInfo;
  37. $this->session_id = !empty($sessionId) ? (int) $sessionId : api_get_session_id();
  38. if (is_numeric($announcementId)) {
  39. $announcementId = AnnouncementManager::get_by_id($courseInfo['real_id'], $announcementId);
  40. }
  41. $this->announcement = $announcementId;
  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. $courseCode = $this->course('code');
  77. if (empty($this->session_id)) {
  78. $group_id = api_get_group_id();
  79. if (empty($group_id)) {
  80. $userList = CourseManager::get_user_list_from_course_code($courseCode);
  81. } else {
  82. $userList = GroupManager::get_users($group_id);
  83. $new_user_list = [];
  84. foreach ($userList as $user) {
  85. $new_user_list[] = ['user_id' => $user];
  86. }
  87. $userList = $new_user_list;
  88. }
  89. } else {
  90. $userList = CourseManager::get_user_list_from_course_code(
  91. $courseCode,
  92. $this->session_id
  93. );
  94. }
  95. return $userList;
  96. }
  97. /**
  98. * Returns users and groups an announcement item has been sent to.
  99. *
  100. * @return array Array of users and groups to whom the element has been sent
  101. */
  102. public function sent_to_info()
  103. {
  104. $result = [];
  105. $result['groups'] = [];
  106. $result['users'] = [];
  107. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  108. $tool = TOOL_ANNOUNCEMENT;
  109. $id = $this->announcement('id');
  110. $course_id = $this->course('real_id');
  111. $sessionCondition = api_get_session_condition($this->session_id);
  112. $sql = "SELECT to_group_id, to_user_id
  113. FROM $table
  114. WHERE
  115. c_id = $course_id AND
  116. tool = '$tool' AND
  117. ref = $id
  118. $sessionCondition";
  119. $rs = Database::query($sql);
  120. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  121. // if to_user_id <> 0 then it is sent to a specific user
  122. $user_id = $row['to_user_id'];
  123. if (!empty($user_id)) {
  124. $result['users'][] = (int) $user_id;
  125. // If user is set then skip the group
  126. continue;
  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. $result['groups'][] = (int) $group_id;
  133. }
  134. }
  135. return $result;
  136. }
  137. /**
  138. * Returns the list of user info to which an announcement was sent.
  139. * This function returns a list of actual users even when recipient
  140. * are groups
  141. *
  142. * @return array
  143. */
  144. public function sent_to()
  145. {
  146. $sent_to = $this->sent_to_info();
  147. $users = $sent_to['users'];
  148. $users = $users ? $users : [];
  149. $groups = $sent_to['groups'];
  150. if ($users) {
  151. $users = UserManager::get_user_list_by_ids($users, true);
  152. }
  153. if (!empty($groups)) {
  154. $groupUsers = GroupManager::get_groups_users($groups);
  155. $groupUsers = UserManager::get_user_list_by_ids($groupUsers, true);
  156. if (!empty($groupUsers)) {
  157. $users = array_merge($users, $groupUsers);
  158. }
  159. }
  160. if (empty($users)) {
  161. $users = self::all_users();
  162. }
  163. // Clean users just in case
  164. $newListUsers = [];
  165. if (!empty($users)) {
  166. foreach ($users as $user) {
  167. $newListUsers[$user['user_id']] = ['user_id' => $user['user_id']];
  168. }
  169. }
  170. return $newListUsers;
  171. }
  172. /**
  173. * Sender info
  174. *
  175. * @param string $key
  176. * @param int $userId
  177. *
  178. * @return array
  179. */
  180. public function sender($key = '', $userId = 0)
  181. {
  182. $_user = api_get_user_info($userId);
  183. return $key ? $_user[$key] : $_user;
  184. }
  185. /**
  186. * Email subject
  187. *
  188. * @return string
  189. */
  190. public function subject()
  191. {
  192. $result = $this->course('title').' - '.$this->announcement('title');
  193. $result = stripslashes($result);
  194. return $result;
  195. }
  196. /**
  197. * Email message
  198. * @param int $receiverUserId
  199. *
  200. * @return string
  201. */
  202. public function message($receiverUserId)
  203. {
  204. $content = $this->announcement('content');
  205. $session_id = $this->session_id;
  206. $courseCode = $this->course('code');
  207. $content = AnnouncementManager::parse_content(
  208. $receiverUserId,
  209. $content,
  210. $courseCode,
  211. $session_id
  212. );
  213. $user_email = $this->sender('mail');
  214. // Build the link by hand because api_get_cidreq() doesn't accept course params
  215. $course_param = 'cidReq='.$courseCode.'&id_session='.$session_id.'&gidReq='.api_get_group_id();
  216. $course_name = $this->course('title');
  217. $result = "<div>$content</div>";
  218. // Adding attachment
  219. $attachment = $this->attachment();
  220. if (!empty($attachment)) {
  221. $result .= '<br />';
  222. $result .= Display::url(
  223. $attachment['filename'],
  224. api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param
  225. );
  226. $result .= '<br />';
  227. }
  228. $result .= '<hr />';
  229. $sender_name = api_get_person_name(
  230. $this->sender('firstName'),
  231. $this->sender('lastName'),
  232. PERSON_NAME_EMAIL_ADDRESS
  233. );
  234. $result .= '<a href="mailto:'.$user_email.'">'.$sender_name.'</a><br/>';
  235. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>';
  236. return $result;
  237. }
  238. /**
  239. * Returns the one file that can be attached to an announcement.
  240. *
  241. * @return array
  242. */
  243. public function attachment()
  244. {
  245. $result = [];
  246. $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  247. $id = $this->announcement('id');
  248. $course_id = $this->course('real_id');
  249. $sql = "SELECT * FROM $table
  250. WHERE c_id = $course_id AND announcement_id = $id ";
  251. $rs = Database::query($sql);
  252. $course_path = $this->course('directory');
  253. while ($row = Database::fetch_array($rs)) {
  254. $path = api_get_path(SYS_COURSE_PATH).$course_path.'/upload/announcements/'.$row['path'];
  255. $filename = $row['filename'];
  256. $result[] = ['path' => $path, 'filename' => $filename];
  257. }
  258. $result = $result ? reset($result) : [];
  259. return $result;
  260. }
  261. /**
  262. * Send emails to users.
  263. * @param bool $sendToUsersInSession
  264. * @param bool $sendToDrhUsers send a copy of the message to the DRH users
  265. * related to the main user
  266. */
  267. public function send($sendToUsersInSession = false, $sendToDrhUsers = false)
  268. {
  269. $sender = $this->sender();
  270. $subject = $this->subject();
  271. // Send email one by one to avoid antispam
  272. $users = $this->sent_to();
  273. $batchSize = 20;
  274. $counter = 1;
  275. $em = Database::getManager();
  276. foreach ($users as $user) {
  277. $message = $this->message($user['user_id']);
  278. MessageManager::send_message_simple(
  279. $user['user_id'],
  280. $subject,
  281. $message,
  282. $sender['user_id'],
  283. $sendToDrhUsers,
  284. true
  285. );
  286. if (($counter % $batchSize) === 0) {
  287. $em->flush();
  288. $em->clear();
  289. }
  290. $counter++;
  291. }
  292. if ($sendToUsersInSession) {
  293. $sessionList = SessionManager::get_session_by_course($this->course['real_id']);
  294. if (!empty($sessionList)) {
  295. foreach ($sessionList as $sessionInfo) {
  296. $sessionId = $sessionInfo['id'];
  297. $message = $this->message(null);
  298. $userList = CourseManager::get_user_list_from_course_code(
  299. $this->course['code'],
  300. $sessionId
  301. );
  302. if (!empty($userList)) {
  303. foreach ($userList as $user) {
  304. MessageManager::send_message_simple(
  305. $user['user_id'],
  306. $subject,
  307. $message,
  308. $sender['user_id'],
  309. false,
  310. true
  311. );
  312. }
  313. }
  314. }
  315. }
  316. }
  317. $this->logMailSent();
  318. }
  319. /**
  320. * Store that emails where sent
  321. */
  322. public function logMailSent()
  323. {
  324. $id = $this->announcement('id');
  325. $course_id = $this->course('real_id');
  326. $table = Database::get_course_table(TABLE_ANNOUNCEMENT);
  327. $sql = "UPDATE $table SET
  328. email_sent = 1
  329. WHERE
  330. c_id = $course_id AND
  331. id = $id AND
  332. session_id = {$this->session_id}
  333. ";
  334. Database::query($sql);
  335. }
  336. }