announcements.inc.php 43 KB

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