announcements.inc.php 51 KB

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