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 = array();
  84. foreach ($userList as $user) {
  85. $new_user_list[] = array('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 = array();
  105. $result['groups'] = array();
  106. $result['users'] = array();
  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 : array();
  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. *
  177. * @return array
  178. */
  179. public function sender($key = '', $userId = '')
  180. {
  181. $_user = api_get_user_info($userId);
  182. return $key ? $_user[$key] : $_user;
  183. }
  184. /**
  185. * Email subject
  186. *
  187. * @return string
  188. */
  189. public function subject()
  190. {
  191. $result = $this->course('title').' - '.$this->announcement('title');
  192. $result = stripslashes($result);
  193. return $result;
  194. }
  195. /**
  196. * Email message
  197. * @param int $receiverUserId
  198. *
  199. * @return string
  200. */
  201. public function message($receiverUserId)
  202. {
  203. $content = $this->announcement('content');
  204. $session_id = $this->session_id;
  205. $courseCode = $this->course('code');
  206. $content = AnnouncementManager::parse_content(
  207. $receiverUserId,
  208. $content,
  209. $courseCode,
  210. $session_id
  211. );
  212. $user_email = $this->sender('mail');
  213. // Build the link by hand because api_get_cidreq() doesn't accept course params
  214. $course_param = 'cidReq='.$courseCode.'&id_session='.$session_id.'&gidReq='.api_get_group_id();
  215. $course_name = $this->course('title');
  216. $result = "<div>$content</div>";
  217. // Adding attachment
  218. $attachment = $this->attachment();
  219. if (!empty($attachment)) {
  220. $result .= '<br />';
  221. $result .= Display::url(
  222. $attachment['filename'],
  223. api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param
  224. );
  225. $result .= '<br />';
  226. }
  227. $result .= '<hr />';
  228. $sender_name = api_get_person_name(
  229. $this->sender('firstName'),
  230. $this->sender('lastName'),
  231. PERSON_NAME_EMAIL_ADDRESS
  232. );
  233. $result .= '<a href="mailto:'.$user_email.'">'.$sender_name.'</a><br/>';
  234. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>';
  235. return $result;
  236. }
  237. /**
  238. * Returns the one file that can be attached to an announcement.
  239. *
  240. * @return array
  241. */
  242. public function attachment()
  243. {
  244. $result = array();
  245. $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  246. $id = $this->announcement('id');
  247. $course_id = $this->course('id');
  248. $sql = "SELECT * FROM $table
  249. WHERE c_id = $course_id AND announcement_id = $id ";
  250. $rs = Database::query($sql);
  251. $course_path = $this->course('directory');
  252. while ($row = Database::fetch_array($rs)) {
  253. $path = api_get_path(SYS_COURSE_PATH).$course_path.'/upload/announcements/'.$row['path'];
  254. $filename = $row['filename'];
  255. $result[] = array('path' => $path, 'filename' => $filename);
  256. }
  257. $result = $result ? reset($result) : array();
  258. return $result;
  259. }
  260. /**
  261. * Send emails to users.
  262. * @param bool $sendToUsersInSession
  263. * @param bool $sendToDrhUsers send a copy of the message to the DRH users
  264. * related to the main user
  265. */
  266. public function send($sendToUsersInSession = false, $sendToDrhUsers = false)
  267. {
  268. $sender = $this->sender();
  269. $subject = $this->subject();
  270. // Send email one by one to avoid antispam
  271. $users = $this->sent_to();
  272. $batchSize = 20;
  273. $counter = 1;
  274. $em = Database::getManager();
  275. foreach ($users as $user) {
  276. $message = $this->message($user['user_id']);
  277. MessageManager::send_message_simple(
  278. $user['user_id'],
  279. $subject,
  280. $message,
  281. $sender['user_id'],
  282. $sendToDrhUsers,
  283. true
  284. );
  285. if (($counter % $batchSize) === 0) {
  286. $em->flush();
  287. $em->clear();
  288. }
  289. $counter++;
  290. }
  291. if ($sendToUsersInSession) {
  292. $sessionList = SessionManager::get_session_by_course($this->course['real_id']);
  293. if (!empty($sessionList)) {
  294. foreach ($sessionList as $sessionInfo) {
  295. $sessionId = $sessionInfo['id'];
  296. $message = $this->message(null);
  297. $userList = CourseManager::get_user_list_from_course_code(
  298. $this->course['code'],
  299. $sessionId
  300. );
  301. if (!empty($userList)) {
  302. foreach ($userList as $user) {
  303. MessageManager::send_message_simple(
  304. $user['user_id'],
  305. $subject,
  306. $message,
  307. $sender['user_id'],
  308. false,
  309. true
  310. );
  311. }
  312. }
  313. }
  314. }
  315. }
  316. $this->log_mail_sent();
  317. }
  318. /**
  319. * Store that emails where sent
  320. */
  321. public function log_mail_sent()
  322. {
  323. $id = $this->announcement('id');
  324. $course_id = $this->course('id');
  325. $table = Database::get_course_table(TABLE_ANNOUNCEMENT);
  326. $sql = "UPDATE $table SET
  327. email_sent = 1
  328. WHERE
  329. c_id = $course_id AND
  330. id = $id AND
  331. session_id = {$this->session_id}
  332. ";
  333. Database::query($sql);
  334. }
  335. }