announcement_email.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 $course
  27. * @param int $announcement
  28. */
  29. public function __construct($course, $announcement)
  30. {
  31. if (empty($course)) {
  32. $course = api_get_course_int_id();
  33. $course = CourseManager::get_course_information_by_id($course);
  34. } else if (is_numeric($course)) {
  35. $course = CourseManager::get_course_information_by_id($course);
  36. }
  37. $this->course = $course;
  38. $this->session_id = api_get_session_id();
  39. if (is_numeric($announcement)) {
  40. $announcement = AnnouncementManager::get_by_id($course['real_id'], $announcement);
  41. }
  42. $this->announcement = $announcement;
  43. }
  44. /**
  45. * Course info
  46. *
  47. * @param string $key
  48. *
  49. * @return array
  50. */
  51. public function course($key = '')
  52. {
  53. $result = $key ? $this->course[$key] : $this->course;
  54. $result = $key == 'id' ? intval($result) : $result;
  55. return $result;
  56. }
  57. /**
  58. * Announcement info
  59. *
  60. * @param string $key
  61. * @return array
  62. */
  63. public function announcement($key = '')
  64. {
  65. $result = $key ? $this->announcement[$key] : $this->announcement;
  66. $result = $key == 'id' ? intval($result) : $result;
  67. return $result;
  68. }
  69. /**
  70. * Returns either all course users or all session users depending on whether
  71. * session is turned on or not
  72. *
  73. * @return array
  74. */
  75. public function all_users()
  76. {
  77. $course_code = $this->course('code');
  78. if (empty($this->session_id)) {
  79. $group_id = api_get_group_id();
  80. if (empty($group_id)) {
  81. $user_list = CourseManager::get_user_list_from_course_code($course_code);
  82. } else {
  83. $user_list = GroupManager::get_users($group_id);
  84. $new_user_list = array();
  85. foreach ($user_list as $user) {
  86. $new_user_list[] = array('user_id' => $user);
  87. }
  88. $user_list = $new_user_list;
  89. }
  90. } else {
  91. $user_list = CourseManager::get_user_list_from_course_code($course_code, $this->session_id);
  92. }
  93. return $user_list;
  94. }
  95. /**
  96. * Returns users and groups an announcement item has been sent to.
  97. *
  98. * @return array Array of users and groups to whom the element has been sent
  99. */
  100. public function sent_to_info()
  101. {
  102. $result = array();
  103. $result['groups'] = array();
  104. $result['users'] = array();
  105. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  106. $tool = TOOL_ANNOUNCEMENT;
  107. $id = $this->announcement('id');
  108. $course_id = $this->course('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 AND
  115. id_session = {$this->session_id} ";
  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 = '')
  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. * @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. */
  257. public function send($sendToUsersInSession = false, $sendToDrhUsers = false)
  258. {
  259. $sender = $this->sender();
  260. $subject = $this->subject();
  261. // Send email one by one to avoid antispam
  262. $users = $this->sent_to();
  263. foreach ($users as $user) {
  264. $message = $this->message($user['user_id']);
  265. MessageManager::send_message_simple(
  266. $user['user_id'],
  267. $subject,
  268. $message,
  269. $sender['user_id'],
  270. $sendToDrhUsers,
  271. true
  272. );
  273. }
  274. if ($sendToUsersInSession) {
  275. $sessionList = SessionManager::get_session_by_course($this->course['code']);
  276. if (!empty($sessionList)) {
  277. foreach ($sessionList as $sessionInfo) {
  278. $sessionId = $sessionInfo['id'];
  279. $message = $this->message(null, $sessionId);
  280. $userList = CourseManager::get_user_list_from_course_code($this->course['code'], $sessionId);
  281. if (!empty($userList)) {
  282. foreach ($userList as $user) {
  283. MessageManager::send_message_simple(
  284. $user['user_id'],
  285. $subject,
  286. $message,
  287. $sender['user_id'],
  288. false,
  289. true
  290. );
  291. }
  292. }
  293. }
  294. }
  295. }
  296. $this->log_mail_sent();
  297. }
  298. /**
  299. * Store that emails where sent
  300. */
  301. public function log_mail_sent()
  302. {
  303. $id = $this->announcement('id');
  304. $course_id = $this->course('id');
  305. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  306. $sql = "UPDATE $tbl_announcement SET email_sent=1
  307. WHERE c_id = $course_id AND id=$id AND session_id = {$this->session_id} ";
  308. Database::query($sql);
  309. }
  310. }