announcements.inc.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Include file with functions for the announcements module.
  5. * @author jmontoya
  6. * @package chamilo.announcements
  7. * @todo use OOP
  8. */
  9. class AnnouncementManager
  10. {
  11. public function __construct()
  12. {
  13. }
  14. /**
  15. * @return array
  16. */
  17. public static function get_tags()
  18. {
  19. return array(
  20. '((user_name))',
  21. '((user_firstname))',
  22. '((user_lastname))',
  23. '((teacher_name))',
  24. '((teacher_email))',
  25. '((course_title))',
  26. '((course_link))',
  27. '((official_code))',
  28. );
  29. }
  30. /**
  31. * @param int $userId
  32. * @param string $content
  33. * @param string $course_code
  34. * @param int $session_id
  35. *
  36. * @return mixed
  37. */
  38. public static function parse_content($userId, $content, $course_code, $session_id = 0)
  39. {
  40. $readerInfo = api_get_user_info($userId);
  41. $courseInfo = api_get_course_info($course_code);
  42. $teacher_list = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
  43. $teacher_name = '';
  44. if (!empty($teacher_list)) {
  45. foreach ($teacher_list as $teacher_data) {
  46. $teacher_name = api_get_person_name($teacher_data['firstname'], $teacher_data['lastname']);
  47. $teacher_email = $teacher_data['email'];
  48. break;
  49. }
  50. }
  51. $courseLink = api_get_course_url($course_code, $session_id);
  52. $data['user_name'] = $readerInfo['username'];
  53. $data['user_firstname'] = $readerInfo['firstname'];
  54. $data['user_lastname'] = $readerInfo['lastname'];
  55. $data['teacher_name'] = $teacher_name;
  56. $data['teacher_email'] = $teacher_email;
  57. $data['course_title'] = $courseInfo['name'];
  58. $data['course_link'] = Display::url($courseLink, $courseLink);
  59. $data['official_code'] = $readerInfo['official_code'];
  60. $content = str_replace(self::get_tags(), $data, $content);
  61. return $content;
  62. }
  63. /**
  64. * Gets all announcements from a course
  65. * @param string course db
  66. * @param int session id
  67. * @return array html with the content and count of announcements or false otherwise
  68. */
  69. public static function get_all_annoucement_by_course($course_info, $session_id = 0)
  70. {
  71. $session_id = intval($session_id);
  72. $course_id = $course_info['real_id'];
  73. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  74. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  75. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  76. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  77. WHERE announcement.id = toolitemproperties.ref AND
  78. toolitemproperties.tool='announcement' AND
  79. announcement.session_id = '$session_id' AND
  80. announcement.c_id = $course_id AND
  81. toolitemproperties.c_id = $course_id
  82. ORDER BY display_order DESC";
  83. $rs = Database::query($sql);
  84. $num_rows = Database::num_rows($rs);
  85. if ($num_rows > 0) {
  86. $list = array();
  87. while ($row = Database::fetch_array($rs)) {
  88. $list[] = $row;
  89. }
  90. return $list;
  91. }
  92. return false;
  93. }
  94. /**
  95. * This functions swithes the visibility a course resource
  96. * using the visibility field in 'item_property'
  97. * @param array the course array
  98. * @param int ID of the element of the corresponding type
  99. * @return bool False on failure, True on success
  100. */
  101. public static function change_visibility_announcement($_course, $id)
  102. {
  103. $session_id = api_get_session_id();
  104. $item_visibility = api_get_item_visibility($_course, TOOL_ANNOUNCEMENT, $id, $session_id);
  105. if ($item_visibility == '1') {
  106. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, 'invisible', api_get_user_id());
  107. } else {
  108. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, 'visible', api_get_user_id());
  109. }
  110. return true;
  111. }
  112. /**
  113. * Deletes an announcement
  114. * @param array $_course the course array
  115. * @param int $id the announcement id
  116. */
  117. public static function delete_announcement($_course, $id)
  118. {
  119. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, 'delete', api_get_user_id());
  120. }
  121. /**
  122. * Deletes all announcements by course
  123. * @param array $_course the course array
  124. */
  125. public static function delete_all_announcements($_course)
  126. {
  127. $announcements = self::get_all_annoucement_by_course($_course, api_get_session_id());
  128. if (!empty($announcements)) {
  129. foreach ($announcements as $annon) {
  130. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $annon['id'], 'delete', api_get_user_id());
  131. }
  132. }
  133. }
  134. /**
  135. * Displays one specific announcement
  136. * @param int $announcement_id, the id of the announcement you want to display
  137. */
  138. public static function display_announcement($announcement_id)
  139. {
  140. if ($announcement_id != strval(intval($announcement_id))) {
  141. return false;
  142. }
  143. global $charset;
  144. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  145. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  146. $course_id = api_get_course_int_id();
  147. if (api_is_allowed_to_edit(false, true) || (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  148. $sql_query = " SELECT announcement.*, toolitemproperties.*
  149. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  150. WHERE announcement.id = toolitemproperties.ref
  151. AND announcement.id = '$announcement_id'
  152. AND toolitemproperties.tool='announcement' AND
  153. announcement.c_id = $course_id AND
  154. toolitemproperties.c_id = $course_id
  155. ORDER BY display_order DESC";
  156. } else {
  157. $group_list = GroupManager::get_group_ids($course_id, api_get_user_id());
  158. if (empty($group_list)) {
  159. $group_list[] = 0;
  160. }
  161. if (api_get_user_id() != 0) {
  162. $sql_query = " SELECT announcement.*, toolitemproperties.*
  163. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  164. WHERE announcement.id = toolitemproperties.ref
  165. AND announcement.id = '$announcement_id'
  166. AND toolitemproperties.tool='announcement'
  167. AND (toolitemproperties.to_user_id='" . api_get_user_id() . "' OR toolitemproperties.to_group_id IN ('0', '" . implode("', '", $group_list) . "'))
  168. AND toolitemproperties.visibility='1' AND
  169. announcement.c_id = $course_id AND
  170. toolitemproperties.c_id = $course_id
  171. ORDER BY display_order DESC";
  172. } else {
  173. $sql_query = " SELECT announcement.*, toolitemproperties.*
  174. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  175. WHERE announcement.id = toolitemproperties.ref
  176. AND announcement.id = '$announcement_id'
  177. AND toolitemproperties.tool='announcement'
  178. AND toolitemproperties.to_group_id='0'
  179. AND toolitemproperties.visibility='1' AND
  180. announcement.c_id = $course_id AND
  181. toolitemproperties.c_id = $course_id
  182. ";
  183. }
  184. }
  185. $sql_result = Database::query($sql_query);
  186. if (Database::num_rows($sql_result) > 0) {
  187. $result = Database::fetch_array($sql_result, 'ASSOC');
  188. $title = $result['title'];
  189. $content = $result['content'];
  190. echo "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">";
  191. echo "<tr><td><h2>" . $title . "</h2></td></tr>";
  192. if (api_is_allowed_to_edit(false, true) || (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  193. $modify_icons = "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&action=modify&id=" . $announcement_id . "\">" . Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL) . "</a>";
  194. if ($result['visibility'] == 1) {
  195. $image_visibility = "visible";
  196. $alt_visibility = get_lang('Hide');
  197. } else {
  198. $image_visibility = "invisible";
  199. $alt_visibility = get_lang('Visible');
  200. }
  201. global $stok;
  202. $modify_icons .= "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&origin=" . (!empty($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : '') . "&action=showhide&id=" . $announcement_id . "&sec_token=" . $stok . "\">" .
  203. Display::return_icon($image_visibility . '.png', $alt_visibility, '', ICON_SIZE_SMALL) . "</a>";
  204. if (api_is_allowed_to_edit(false, true)) {
  205. $modify_icons .= "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&action=delete&id=" . $announcement_id . "&sec_token=" . $stok . "\" onclick=\"javascript:if(!confirm('" . addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)) . "')) return false;\">" .
  206. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) .
  207. "</a>";
  208. }
  209. echo "<tr><th style='text-align:right'>$modify_icons</th></tr>";
  210. }
  211. $content = self::parse_content($result['to_user_id'], $content, api_get_course_id(), api_get_session_id());
  212. echo "<tr><td>$content</td></tr>";
  213. echo "<tr><td class=\"announcements_datum\">" . get_lang('LastUpdateDate') . " : " . api_convert_and_format_date($result['insert_date'], DATE_TIME_FORMAT_LONG) . "</td></tr>";
  214. // User or group icon
  215. $sent_to_icon = '';
  216. if ($result['to_group_id'] !== '0' and $result['to_group_id'] !== 'NULL') {
  217. $sent_to_icon = Display::return_icon('group.gif', get_lang('AnnounceSentToUserSelection'));
  218. }
  219. $sent_to = self::sent_to('announcement', $announcement_id);
  220. $sent_to_form = self::sent_to_form($sent_to);
  221. echo Display::tag('td', get_lang('SentTo') . ' : ' . $sent_to_form, array('class' => 'announcements_datum'));
  222. $attachment_list = self::get_attachment($announcement_id);
  223. if (count($attachment_list) > 0) {
  224. echo "<tr><td>";
  225. $realname = $attachment_list['path'];
  226. $user_filename = $attachment_list['filename'];
  227. $full_file_name = 'download.php?'.api_get_cidreq().'&file=' . $realname;
  228. echo '<br/>';
  229. echo Display::return_icon('attachment.gif', get_lang('Attachment'));
  230. echo '<a href="' . $full_file_name . ' "> ' . $user_filename . ' </a>';
  231. echo ' - <span class="forum_attach_comment" >' . $attachment_list['comment'] . '</span>';
  232. if (api_is_allowed_to_edit(false, true)) {
  233. echo Display::url(Display::return_icon('delete.png', get_lang('Delete'), '', 16), api_get_self() . "?" . api_get_cidreq() . "&action=delete_attachment&id_attach=" . $attachment_list['id'] . "&sec_token=" . $stok);
  234. }
  235. echo '</td></tr>';
  236. }
  237. echo "</table>";
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * @return int
  244. */
  245. public static function get_last_announcement_order()
  246. {
  247. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  248. $course_id = api_get_course_int_id();
  249. $sql_max = "SELECT MAX(display_order) FROM $tbl_announcement WHERE c_id = $course_id ";
  250. $res_max = Database::query($sql_max);
  251. $order = 0;
  252. if (Database::num_rows($res_max)) {
  253. $row_max = Database::fetch_array($res_max);
  254. $order = intval($row_max[0])+1;
  255. }
  256. return $order;
  257. }
  258. /**
  259. * Store an announcement in the database (including its attached file if any)
  260. * @param string Announcement title (pure text)
  261. * @param string Content of the announcement (can be HTML)
  262. * @param int Display order in the list of announcements
  263. * @param array Array of users and groups to send the announcement to
  264. * @param array uploaded file $_FILES
  265. * @param string Comment describing the attachment
  266. * @return int false on failure, ID of the announcement on success
  267. */
  268. public static function add_announcement(
  269. $emailTitle,
  270. $newContent,
  271. $sentTo,
  272. $file = array(),
  273. $file_comment = null,
  274. $end_date = null,
  275. $sendToUsersInSession = false
  276. ) {
  277. $_course = api_get_course_info();
  278. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  279. // filter data
  280. $emailTitle = Database::escape_string($emailTitle);
  281. $newContent = Database::escape_string($newContent);
  282. if (empty($end_date)) {
  283. $end_date = api_get_utc_datetime();
  284. } else {
  285. $end_date = Database::escape_string($end_date);
  286. }
  287. $course_id = api_get_course_int_id();
  288. $order = self::get_last_announcement_order();
  289. // store in the table announcement
  290. $sql = "INSERT INTO $tbl_announcement SET
  291. c_id = '$course_id',
  292. content = '$newContent',
  293. title = '$emailTitle',
  294. end_date = '$end_date',
  295. display_order = '$order',
  296. session_id = ".api_get_session_id();
  297. $result = Database::query($sql);
  298. if ($result === false) {
  299. return false;
  300. } else {
  301. // Store the attach file.
  302. $last_id = Database::insert_id();
  303. if (!empty($file)) {
  304. self::add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  305. }
  306. // store in item_property (first the groups, then the users
  307. if (empty($sentTo) || !empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone') {
  308. // The message is sent to EVERYONE, so we set the group to 0
  309. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '0');
  310. } else {
  311. $send_to = self::separate_users_groups($sentTo);
  312. // Storing the selected groups
  313. if (is_array($send_to['groups'])) {
  314. foreach ($send_to['groups'] as $group) {
  315. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), $group);
  316. }
  317. }
  318. // Storing the selected users
  319. if (is_array($send_to['users'])) {
  320. foreach ($send_to['users'] as $user) {
  321. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '', $user);
  322. }
  323. }
  324. }
  325. if ($sendToUsersInSession) {
  326. self::addAnnouncementToAllUsersInSessions($last_id);
  327. }
  328. return $last_id;
  329. }
  330. }
  331. /**
  332. * @param $emailTitle
  333. * @param $newContent
  334. * @param $to
  335. * @param $to_users
  336. * @param array $file
  337. * @param string $file_comment
  338. * @return bool|int
  339. */
  340. public static function add_group_announcement($emailTitle, $newContent, $to, $to_users, $file = array(), $file_comment = '', $sendToUsersInSession = false)
  341. {
  342. $_course = api_get_course_info();
  343. // Database definitions
  344. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  345. $emailTitle = Database::escape_string($emailTitle);
  346. $newContent = Database::escape_string($newContent);
  347. $order = self::get_last_announcement_order();
  348. $now = api_get_utc_datetime();
  349. $course_id = api_get_course_int_id();
  350. // store in the table announcement
  351. $sql = "INSERT INTO $tbl_announcement SET
  352. c_id = '$course_id',
  353. content = '$newContent',
  354. title = '$emailTitle',
  355. end_date = '$now',
  356. display_order = '$order',
  357. session_id = " . api_get_session_id();
  358. $result = Database::query($sql);
  359. if ($result === false) {
  360. return false;
  361. }
  362. //store the attach file
  363. $last_id = Database::insert_id();
  364. if (!empty($file)) {
  365. self::add_announcement_attachment_file($last_id, $file_comment, $file);
  366. }
  367. // store in item_property (first the groups, then the users
  368. if (!isset($to_users)) { // !isset($to): when no user is selected we send it to everyone
  369. $send_to = self::separate_users_groups($to);
  370. // storing the selected groups
  371. if (is_array($send_to['groups'])) {
  372. foreach ($send_to['groups'] as $group) {
  373. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), $group);
  374. }
  375. }
  376. } else {
  377. // the message is sent to everyone, so we set the group to 0
  378. // storing the selected users
  379. if (is_array($to_users)) {
  380. foreach ($to_users as $user) {
  381. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", api_get_user_id(), '', $user);
  382. }
  383. }
  384. }
  385. if ($sendToUsersInSession) {
  386. self::addAnnouncementToAllUsersInSessions($last_id);
  387. }
  388. return $last_id;
  389. }
  390. /**
  391. * This function stores the announcement item in the announcement table
  392. * and updates the item_property table
  393. *
  394. * @param int id of the announcement
  395. * @param string email
  396. * @param string content
  397. * @param array users that will receive the announcement
  398. * @param mixed attachment
  399. * @param string file comment
  400. */
  401. public static function edit_announcement($id, $emailTitle, $newContent, $to, $file = array(), $file_comment = '', $sendToUsersInSession = false)
  402. {
  403. global $_course;
  404. $course_id = api_get_course_int_id();
  405. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  406. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  407. $emailTitle = Database::escape_string($emailTitle);
  408. $newContent = Database::escape_string($newContent);
  409. $id = intval($id);
  410. // store the modifications in the table announcement
  411. $sql = "UPDATE $tbl_announcement SET content = '$newContent', title = '$emailTitle' WHERE c_id = $course_id AND id='$id'";
  412. Database::query($sql);
  413. // save attachment file
  414. $row_attach = self::get_attachment($id);
  415. $id_attach = intval($row_attach['id']);
  416. if (!empty($file)) {
  417. if (empty($id_attach)) {
  418. self::add_announcement_attachment_file($id, $file_comment, $file);
  419. } else {
  420. self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
  421. }
  422. }
  423. // we remove everything from item_property for this
  424. $sql_delete = "DELETE FROM $tbl_item_property WHERE c_id = $course_id AND ref='$id' AND tool='announcement'";
  425. Database::query($sql_delete);
  426. if ($sendToUsersInSession) {
  427. self::addAnnouncementToAllUsersInSessions($id);
  428. }
  429. if ($sendToUsersInSession) {
  430. self::addAnnouncementToAllUsersInSessions($id);
  431. }
  432. // store in item_property (first the groups, then the users
  433. if (!is_null($to)) {
  434. // !is_null($to): when no user is selected we send it to everyone
  435. $send_to = self::separate_users_groups($to);
  436. // storing the selected groups
  437. if (is_array($send_to['groups'])) {
  438. foreach ($send_to['groups'] as $group) {
  439. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), $group);
  440. }
  441. }
  442. // storing the selected users
  443. if (is_array($send_to['users'])) {
  444. foreach ($send_to['users'] as $user) {
  445. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), 0, $user);
  446. }
  447. }
  448. } else {
  449. // the message is sent to everyone, so we set the group to 0
  450. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), '0');
  451. }
  452. }
  453. /**
  454. * @param int $announcementId
  455. */
  456. public static function addAnnouncementToAllUsersInSessions($announcementId)
  457. {
  458. $courseCode = api_get_course_id();
  459. $_course = api_get_course_info();
  460. $sessionList = SessionManager::get_session_by_course(api_get_course_id());
  461. if (!empty($sessionList)) {
  462. foreach ($sessionList as $sessionInfo) {
  463. $sessionId = $sessionInfo['id'];
  464. $userList = CourseManager::get_user_list_from_course_code($courseCode, $sessionId);
  465. if (!empty($userList)) {
  466. foreach ($userList as $user) {
  467. api_item_property_update(
  468. $_course,
  469. TOOL_ANNOUNCEMENT,
  470. $announcementId,
  471. "AnnouncementUpdated",
  472. api_get_user_id(),
  473. 0,
  474. $user['user_id'],
  475. 0,
  476. 0,
  477. $sessionId
  478. );
  479. }
  480. }
  481. }
  482. }
  483. }
  484. /**
  485. * @param int $insert_id
  486. * @return bool
  487. */
  488. public static function update_mail_sent($insert_id)
  489. {
  490. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  491. if ($insert_id != strval(intval($insert_id))) {
  492. return false;
  493. }
  494. $insert_id = Database::escape_string($insert_id);
  495. $course_id = api_get_course_int_id();
  496. // store the modifications in the table tbl_annoucement
  497. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE c_id = $course_id AND id='$insert_id'";
  498. Database::query($sql);
  499. }
  500. /**
  501. * Gets all announcements from a user by course
  502. * @param string course db
  503. * @param int user id
  504. * @return array html with the content and count of announcements or false otherwise
  505. */
  506. public static function get_all_annoucement_by_user_course($course_code, $user_id)
  507. {
  508. $course_info = api_get_course_info($course_code);
  509. $course_id = $course_info['real_id'];
  510. if (empty($user_id)) {
  511. return false;
  512. }
  513. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  514. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  515. if (!empty($user_id) && is_numeric($user_id)) {
  516. $user_id = intval($user_id);
  517. $sql = "SELECT DISTINCT announcement.title, announcement.content
  518. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  519. WHERE
  520. announcement.c_id = $course_id AND
  521. toolitemproperties.c_id = $course_id AND
  522. announcement.id = toolitemproperties.ref AND
  523. toolitemproperties.tool='announcement' AND
  524. (
  525. toolitemproperties.insert_user_id='$user_id' AND
  526. (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null)
  527. )
  528. AND toolitemproperties.visibility='1'
  529. AND announcement.session_id = 0
  530. ORDER BY display_order DESC";
  531. $rs = Database::query($sql);
  532. $num_rows = Database::num_rows($rs);
  533. $content = '';
  534. $i = 0;
  535. $result = array();
  536. if ($num_rows > 0) {
  537. while ($myrow = Database::fetch_array($rs)) {
  538. $content.= '<strong>' . $myrow['title'] . '</strong><br /><br />';
  539. $content.= $myrow['content'];
  540. $i++;
  541. }
  542. $result['content'] = $content;
  543. $result['count'] = $i;
  544. return $result;
  545. }
  546. return false;
  547. }
  548. return false;
  549. }
  550. /**
  551. * This function shows the form for sending a message to a specific group or user.
  552. * @param $to_already_selected
  553. */
  554. public static function show_to_form($to_already_selected)
  555. {
  556. $user_list = self::get_course_users();
  557. $group_list = self::get_course_groups();
  558. if ($to_already_selected == '' || $to_already_selected == 'everyone') {
  559. $to_already_selected = array();
  560. }
  561. echo "<table id=\"recipient_list\" >";
  562. echo '<tr>';
  563. echo '<td>';
  564. echo '<label><input type="checkbox" id="send_to_all_users">'.get_lang('SendToAllUsers') . "</label>";
  565. echo "</td>";
  566. echo '</tr>';
  567. echo '<tr>';
  568. // the form containing all the groups and all the users of the course
  569. echo '<td>';
  570. echo "<strong>" . get_lang('Users') . "</strong><br />";
  571. self::construct_not_selected_select_form($group_list, $user_list, $to_already_selected);
  572. echo "</td>";
  573. // the buttons for adding or removing groups/users
  574. echo '<td valign="middle">';
  575. echo '<button class="arrowr" type="button" onClick="javascript: move(this.form.elements[1], this.form.elements[4])" onClick="javascript: move(this.form.elements[1], this.form.elements[4])"></button>';
  576. echo '<br /> <br />';
  577. echo '<button class="arrowl" type="button" onClick="javascript: move(this.form.elements[4], this.form.elements[1])" onClick="javascript: move(this.form.elements[4], this.form.elements[1])"></button>';
  578. echo "</td>";
  579. echo "<td>";
  580. // the form containing the selected groups and users
  581. echo "<strong>" . get_lang('DestinationUsers') . "</strong><br />";
  582. self::construct_selected_select_form($group_list, $user_list, $to_already_selected);
  583. echo "</td>";
  584. echo "</tr>";
  585. echo "</table>";
  586. }
  587. /**
  588. * this function shows the form for sending a message to a specific group or user.
  589. */
  590. public static function show_to_form_group($group_id)
  591. {
  592. echo "<table id=\"recipient_list\" >";
  593. echo "<tr>";
  594. echo "<td>";
  595. echo "<select id=\"not_selected_form\" name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>";
  596. $group_users = GroupManager::getStudentsAndTutors($group_id);
  597. foreach ($group_users as $user) {
  598. echo '<option value="' . $user['user_id'] . '" title="' . sprintf(get_lang('LoginX'), $user['username']) . '" >' .
  599. api_get_person_name($user['firstname'], $user['lastname']) .
  600. '</option>';
  601. }
  602. echo '</select>';
  603. echo "</td>";
  604. // the buttons for adding or removing groups/users
  605. echo "<td valign=\"middle\">";
  606. echo '<button class="arrowr" type="button" onClick="javascript: move(this.form.elements[1], this.form.elements[4])" onClick="javascript: move(this.form.elements[1], this.form.elements[4])"></button>';
  607. echo '<br /> <br />';
  608. echo '<button class="arrowl" type="button" onClick="javascript: move(this.form.elements[4], this.form.elements[1])" onClick="javascript: move(this.form.elements[4], this.form.elements[1])"></button>';
  609. echo "</td>";
  610. echo "<td>";
  611. echo "<select id=\"selectedform\" name=\"selectedform[]\" size=5 style=\"width:200px\" multiple>";
  612. echo '</select>';
  613. echo "</td>";
  614. echo "</tr>";
  615. echo "</table>";
  616. }
  617. /**
  618. * this function shows the form for sending a message to a specific group or user.
  619. */
  620. public static function construct_not_selected_select_form($group_list = null, $user_list = null, $to_already_selected)
  621. {
  622. echo '<select id="not_selected_form" name="not_selected_form[]" size="7" class="span4" multiple>';
  623. // adding the groups to the select form
  624. if ($group_list) {
  625. foreach ($group_list as $this_group) {
  626. if (is_array($to_already_selected)) {
  627. if (!in_array("GROUP:" . $this_group['id'], $to_already_selected)) { // $to_already_selected is the array containing the groups (and users) that are already selected
  628. $user_label = ($this_group['userNb'] > 0) ? get_lang('Users') : get_lang('LowerCaseUser') ;
  629. $user_disabled = ($this_group['userNb'] > 0) ? "" : "disabled=disabled" ;
  630. echo "<option $user_disabled value=\"GROUP:" . $this_group['id'] . "\">",
  631. "G: ", $this_group['name'], " - " . $this_group['userNb'] . " " . $user_label .
  632. "</option>";
  633. }
  634. }
  635. }
  636. // a divider
  637. echo "<option value=\"\">---------------------------------------------------------</option>";
  638. }
  639. // adding the individual users to the select form
  640. if ($user_list) {
  641. foreach ($user_list as $this_user) {
  642. if (is_array($to_already_selected)) {
  643. if (!in_array("USER:" . $this_user['user_id'], $to_already_selected)) {
  644. // $to_already_selected is the array containing the users (and groups) that are already selected
  645. echo "<option value=\"USER:" . $this_user['user_id'] . "\" title='" . sprintf(get_lang('LoginX'), $this_user['username']) . "'>",
  646. "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
  647. "</option>";
  648. }
  649. }
  650. }
  651. }
  652. echo "</select>";
  653. }
  654. /**
  655. * this function shows the form for sending a message to a specific group or user.
  656. */
  657. public static function construct_selected_select_form($group_list = null, $user_list = null, $to_already_selected)
  658. {
  659. // we separate the $to_already_selected array (containing groups AND users into
  660. // two separate arrays
  661. $groupuser = array();
  662. if (is_array($to_already_selected)) {
  663. $groupuser = self::separate_users_groups($to_already_selected);
  664. }
  665. $groups_to_already_selected = $groupuser['groups'];
  666. $users_to_already_selected = $groupuser['users'];
  667. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  668. $ref_array_groups = self::get_course_groups();
  669. $ref_array_users = self::get_course_users();
  670. // we construct the form of the already selected groups / users
  671. echo '<select id="selectedform" name="selectedform[]" size="7" multiple class="span4">';
  672. if (is_array($to_already_selected)) {
  673. foreach ($to_already_selected as $groupuser) {
  674. list($type, $id) = explode(":", $groupuser);
  675. if ($type == "GROUP") {
  676. echo "<option value=\"" . $groupuser . "\">G: " . $ref_array_groups[$id]['name'] . "</option>";
  677. } else {
  678. foreach ($ref_array_users as $key => $value) {
  679. if ($value['user_id'] == $id) {
  680. echo "<option value=\"" . $groupuser . "\" title='" . sprintf(get_lang('LoginX'), $value['username']) . "'>" . api_get_person_name($value['firstname'], $value['lastname']) . "</option>";
  681. break;
  682. }
  683. }
  684. }
  685. }
  686. } else {
  687. if ($to_already_selected == 'everyone') {
  688. // adding the groups to the select form
  689. if (is_array($ref_array_groups)) {
  690. foreach ($ref_array_groups as $this_group) {
  691. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  692. if (!is_array($to_already_selected) || !in_array("GROUP:" . $this_group['id'], $to_already_selected)) { // $to_already_selected is the array containing the groups (and users) that are already selected
  693. echo "<option value=\"GROUP:" . $this_group['id'] . "\">",
  694. "G: ", $this_group['name'], " &ndash; " . $this_group['userNb'] . " " . get_lang('Users') .
  695. "</option>";
  696. }
  697. }
  698. }
  699. // adding the individual users to the select form
  700. foreach ($ref_array_users as $this_user) {
  701. if (!is_array($to_already_selected) || !in_array("USER:" . $this_user['user_id'], $to_already_selected)) { // $to_already_selected is the array containing the users (and groups) that are already selected
  702. echo "<option value=\"USER:", $this_user['user_id'], "\" title='" . sprintf(get_lang('LoginX'), $user['username']) . "'>",
  703. "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
  704. "</option>";
  705. }
  706. }
  707. }
  708. }
  709. echo "</select>";
  710. }
  711. /**
  712. * Returns announcement info from its id
  713. *
  714. * @param type $course_id
  715. * @param type $annoucement_id
  716. * @return array
  717. */
  718. public static function get_by_id($course_id, $annoucement_id)
  719. {
  720. $annoucement_id = intval($annoucement_id);
  721. $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
  722. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  723. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  724. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  725. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property toolitemproperties
  726. ON announcement.id = toolitemproperties.ref AND
  727. announcement.c_id = $course_id AND
  728. toolitemproperties.c_id = $course_id
  729. WHERE toolitemproperties.tool='announcement' AND
  730. announcement.id = $annoucement_id";
  731. $result = Database::query($sql);
  732. if (Database::num_rows($result)) {
  733. return Database::fetch_array($result);
  734. }
  735. return array();
  736. }
  737. /**
  738. * this function gets all the users of the course,
  739. * including users from linked courses
  740. * @deprecate use CourseManager class
  741. */
  742. public static function get_course_users()
  743. {
  744. //this would return only the users from real courses:
  745. $session_id = api_get_session_id();
  746. if ($session_id != 0) {
  747. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $session_id);
  748. } else {
  749. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), false, 0);
  750. }
  751. return $user_list;
  752. }
  753. /**
  754. * this function gets all the groups of the course,
  755. * not including linked courses
  756. */
  757. public static function get_course_groups()
  758. {
  759. $session_id = api_get_session_id();
  760. if ($session_id != 0) {
  761. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), $session_id, 1);
  762. } else {
  763. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0, 1);
  764. }
  765. return $new_group_list;
  766. }
  767. /**
  768. * This tools loads all the users and all the groups who have received
  769. * a specific item (in this case an announcement item)
  770. */
  771. public static function load_edit_users($tool, $id)
  772. {
  773. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  774. $tool = Database::escape_string($tool);
  775. $id = Database::escape_string($id);
  776. $course_id = api_get_course_int_id();
  777. $sql = "SELECT * FROM $tbl_item_property WHERE c_id = $course_id AND tool='$tool' AND ref='$id'";
  778. $result = Database::query($sql);
  779. while ($row = Database::fetch_array($result)) {
  780. $to_group = $row['to_group_id'];
  781. switch ($to_group) {
  782. // it was send to one specific user
  783. case null:
  784. $to[] = "USER:" . $row['to_user_id'];
  785. break;
  786. // it was sent to everyone
  787. case 0:
  788. return "everyone";
  789. exit;
  790. break;
  791. default:
  792. $to[] = "GROUP:" . $row['to_group_id'];
  793. }
  794. }
  795. return $to;
  796. }
  797. /**
  798. * returns the javascript for setting a filter
  799. * this goes into the $htmlHeadXtra[] array
  800. */
  801. public static function user_group_filter_javascript()
  802. {
  803. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  804. <!--
  805. function jumpMenu(targ,selObj,restore)
  806. {
  807. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  808. if (restore) selObj.selectedIndex=0;
  809. }
  810. //-->
  811. </script>";
  812. }
  813. /**
  814. * returns all the javascript that is required for easily
  815. * setting the target people/groups
  816. * this goes into the $htmlHeadXtra[] array
  817. */
  818. public static function to_javascript()
  819. {
  820. $www = api_get_path(WEB_PATH);
  821. /*
  822. * Do not allow this kind of behaviour. js minifieds should be manage by the template class or assetic or whatever see #4757 for more info
  823. if (api_get_setting('server_type') == 'test') {
  824. $src = $www . 'main/announcements/resources/js/main.js';
  825. } else {
  826. $src = $www . 'main/announcements/resources/js/main.min.js';
  827. }*/
  828. $src = $www . 'main/announcements/resources/js/main.js';
  829. $result = Javascript::tag($src);
  830. $root = Chamilo::url();
  831. $code = "var www = '$root';\n";
  832. $code .= Javascript::get_lang('FieldRequired', 'Send2All', 'AddAnAttachment', 'Everybody');
  833. $result .= Javascript::tag_code($code);
  834. return $result;
  835. }
  836. /**
  837. * constructs the form to display all the groups and users the message has been sent to
  838. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  839. * the first level is a distinction between groups and users:
  840. * $sent_to_array['groups'] * and $sent_to_array['users']
  841. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  842. * containing all the id's of the groups (resp. users) who have received this message.
  843. * @author Patrick Cool <patrick.cool@>
  844. */
  845. public static function sent_to_form($sent_to_array)
  846. {
  847. // we find all the names of the groups
  848. $group_names = self::get_course_groups();
  849. // we count the number of users and the number of groups
  850. if (isset($sent_to_array['users'])) {
  851. $number_users = count($sent_to_array['users']);
  852. } else {
  853. $number_users = 0;
  854. }
  855. if (isset($sent_to_array['groups'])) {
  856. $number_groups = count($sent_to_array['groups']);
  857. } else {
  858. $number_groups = 0;
  859. }
  860. $total_numbers = $number_users + $number_groups;
  861. // starting the form if there is more than one user/group
  862. $output = array();
  863. if ($total_numbers > 1) {
  864. //$output.="<option>".get_lang("SentTo")."</option>";
  865. // outputting the name of the groups
  866. if (is_array($sent_to_array['groups'])) {
  867. foreach ($sent_to_array['groups'] as $group_id) {
  868. $output[] = $group_names[$group_id]['name'];
  869. }
  870. }
  871. if (isset($sent_to_array['users'])) {
  872. if (is_array($sent_to_array['users'])) {
  873. foreach ($sent_to_array['users'] as $user_id) {
  874. $user_info = api_get_user_info($user_id);
  875. $output[] = $user_info['complete_name_with_username'];
  876. }
  877. }
  878. }
  879. } else {
  880. // there is only one user/group
  881. if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
  882. $user_info = api_get_user_info($sent_to_array['users'][0]);
  883. $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  884. }
  885. if (isset($sent_to_array['groups']) and is_array($sent_to_array['groups']) and isset($sent_to_array['groups'][0]) and $sent_to_array['groups'][0] !== 0) {
  886. $group_id = $sent_to_array['groups'][0];
  887. $output[] = "&nbsp;" . $group_names[$group_id]['name'];
  888. }
  889. if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
  890. $output[] = "&nbsp;" . get_lang('Everybody');
  891. }
  892. }
  893. if (!empty($output)) {
  894. $output = array_filter($output);
  895. if (count($output) > 0) {
  896. $output = implode(', ', $output);
  897. }
  898. return $output;
  899. }
  900. }
  901. /**
  902. * This function separates the users from the groups
  903. * users have a value USER:XXX (with XXX the groups id have a value
  904. * GROUP:YYY (with YYY the group id)
  905. * @param array Array of strings that define the type and id of each destination
  906. * @return array Array of groups and users (each an array of IDs)
  907. */
  908. public static function separate_users_groups($to)
  909. {
  910. $grouplist = array();
  911. $userlist = array();
  912. foreach ($to as $to_item) {
  913. list($type, $id) = explode(':', $to_item);
  914. switch ($type) {
  915. case 'GROUP':
  916. $grouplist[] = intval($id);
  917. break;
  918. case 'USER':
  919. $userlist[] = intval($id);
  920. break;
  921. }
  922. }
  923. $send_to['groups'] = $grouplist;
  924. $send_to['users'] = $userlist;
  925. return $send_to;
  926. }
  927. /**
  928. * Returns all the users and all the groups a specific announcement item
  929. * has been sent to
  930. * @param string The tool (announcement, agenda, ...)
  931. * @param int ID of the element of the corresponding type
  932. * @return array Array of users and groups to whom the element has been sent
  933. */
  934. public static function sent_to($tool, $id)
  935. {
  936. global $tbl_item_property;
  937. $tool = Database::escape_string($tool);
  938. $id = intval($id);
  939. $sent_to_group = array();
  940. $sent_to = array();
  941. $course_id = api_get_course_int_id();
  942. $sql = "SELECT to_group_id, to_user_id
  943. FROM $tbl_item_property
  944. WHERE c_id = $course_id AND tool = '$tool' AND ref=" . $id;
  945. $result = Database::query($sql);
  946. while ($row = Database::fetch_array($result)) {
  947. // if to_group_id is null then it is sent to a specific user
  948. // if to_group_id = 0 then it is sent to everybody
  949. if ($row['to_group_id'] != 0) {
  950. $sent_to_group[] = $row['to_group_id'];
  951. }
  952. // if to_user_id <> 0 then it is sent to a specific user
  953. if ($row['to_user_id'] <> 0) {
  954. $sent_to_user[] = $row['to_user_id'];
  955. }
  956. }
  957. if (isset($sent_to_group)) {
  958. $sent_to['groups'] = $sent_to_group;
  959. }
  960. if (isset($sent_to_user)) {
  961. $sent_to['users'] = $sent_to_user;
  962. }
  963. return $sent_to;
  964. }
  965. /**
  966. * Show a list with all the attachments according to the post's id
  967. * @param int announcement id
  968. * @return array with the post info
  969. * @author Arthur Portugal
  970. * @version November 2009, dokeos 1.8.6.2
  971. */
  972. public static function get_attachment($announcement_id)
  973. {
  974. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  975. $announcement_id = intval($announcement_id);
  976. $course_id = api_get_course_int_id();
  977. $row = array();
  978. $sql = 'SELECT id, path, filename, comment FROM ' . $tbl_announcement_attachment . '
  979. WHERE c_id = ' . $course_id . ' AND announcement_id = ' . $announcement_id . '';
  980. $result = Database::query($sql);
  981. if (Database::num_rows($result) != 0) {
  982. $row = Database::fetch_array($result, 'ASSOC');
  983. }
  984. return $row;
  985. }
  986. /**
  987. * This function add a attachment file into announcement
  988. * @param int announcement id
  989. * @param string file comment
  990. * @param array uploaded file $_FILES
  991. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  992. */
  993. public static function add_announcement_attachment_file($announcement_id, $file_comment, $file)
  994. {
  995. $_course = api_get_course_info();
  996. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  997. $return = 0;
  998. $announcement_id = intval($announcement_id);
  999. $course_id = api_get_course_int_id();
  1000. if (is_array($file) && $file['error'] == 0) {
  1001. $courseDir = $_course['path'] . '/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1002. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1003. $updir = $sys_course_path . $courseDir;
  1004. // Try to add an extension to the file if it hasn't one
  1005. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1006. // user's file name
  1007. $file_name = $file['name'];
  1008. if (!filter_extension($new_file_name)) {
  1009. $return = -1;
  1010. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1011. } else {
  1012. $new_file_name = uniqid('');
  1013. $new_path = $updir . '/' . $new_file_name;
  1014. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1015. $safe_file_comment = Database::escape_string($file_comment);
  1016. $safe_file_name = Database::escape_string($file_name);
  1017. $safe_new_file_name = Database::escape_string($new_file_name);
  1018. // Storing the attachments if any
  1019. $sql = 'INSERT INTO ' . $tbl_announcement_attachment . ' (c_id, filename, comment, path, announcement_id, size) ' .
  1020. "VALUES ($course_id, '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '" . intval($file['size']) . "' )";
  1021. $result = Database::query($sql);
  1022. $return = 1;
  1023. }
  1024. }
  1025. return $return;
  1026. }
  1027. /**
  1028. * This function edit a attachment file into announcement
  1029. * @param int attach id
  1030. * @param array uploaded file $_FILES
  1031. * @param string file comment
  1032. * @return int
  1033. */
  1034. public static function edit_announcement_attachment_file($id_attach, $file, $file_comment)
  1035. {
  1036. global $_course;
  1037. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1038. $return = 0;
  1039. $course_id = api_get_course_int_id();
  1040. if (is_array($file) && $file['error'] == 0) {
  1041. $courseDir = $_course['path'] . '/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1042. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1043. $updir = $sys_course_path . $courseDir;
  1044. // Try to add an extension to the file if it hasn't one
  1045. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1046. // user's file name
  1047. $file_name = $file ['name'];
  1048. if (!filter_extension($new_file_name)) {
  1049. $return - 1;
  1050. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1051. } else {
  1052. $new_file_name = uniqid('');
  1053. $new_path = $updir . '/' . $new_file_name;
  1054. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1055. $safe_file_comment = Database::escape_string($file_comment);
  1056. $safe_file_name = Database::escape_string($file_name);
  1057. $safe_new_file_name = Database::escape_string($new_file_name);
  1058. $id_attach = intval($id_attach);
  1059. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='" . intval($file['size']) . "'
  1060. WHERE c_id = $course_id AND id = '$id_attach'";
  1061. $result = Database::query($sql);
  1062. if ($result === false) {
  1063. $return = -1;
  1064. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1065. } else {
  1066. $return = 1;
  1067. }
  1068. }
  1069. }
  1070. return $return;
  1071. }
  1072. /**
  1073. * This function delete a attachment file by id
  1074. * @param integer $id attachment file Id
  1075. *
  1076. */
  1077. public static function delete_announcement_attachment_file($id)
  1078. {
  1079. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1080. $id = intval($id);
  1081. $course_id = api_get_course_int_id();
  1082. $sql = "DELETE FROM $tbl_announcement_attachment WHERE c_id = $course_id AND id = $id";
  1083. Database::query($sql);
  1084. }
  1085. /**
  1086. * @param int $id
  1087. * @param bool $sendToUsersInSession
  1088. */
  1089. public static function send_email($id, $sendToUsersInSession = false)
  1090. {
  1091. $email = AnnouncementEmail::create(null, $id);
  1092. $email->send($sendToUsersInSession);
  1093. }
  1094. }