announcements.inc.php 44 KB

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