announcements.inc.php 47 KB

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