announcements.inc.php 41 KB

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