announcements.inc.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. * Announcements handler class
  10. * @author jmontoya
  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. /**
  246. * Get last announcement order in the list (by max display_order)
  247. * @return int 0 or the integer display_order of the last announcement
  248. * @assert () === 0
  249. */
  250. public static function get_last_announcement_order()
  251. {
  252. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  253. $course_id = api_get_course_int_id();
  254. $sql_max = "SELECT MAX(display_order) FROM $tbl_announcement WHERE c_id = $course_id ";
  255. $res_max = Database::query($sql_max);
  256. $order = 0;
  257. if (Database::num_rows($res_max)) {
  258. $row_max = Database::fetch_array($res_max);
  259. $order = intval($row_max[0]) + 1;
  260. }
  261. return $order;
  262. }
  263. /**
  264. * Get last announcement in a course by id (not iid)
  265. * @param int Course ID (will get from context if none provided)
  266. * @return int 0 or the integer id last announcement inserted
  267. * @assert () === 0
  268. */
  269. public static function get_last_announcement_id($course_id=null)
  270. {
  271. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  272. if (empty($course_id)) {
  273. $course_id = api_get_course_int_id();
  274. } else {
  275. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  276. }
  277. $sql_max = "SELECT MAX(id) FROM $tbl_announcement WHERE c_id = $course_id";
  278. $res_max = Database::query($sql_max);
  279. $id = 0;
  280. if (Database::num_rows($res_max)) {
  281. $row_max = Database::fetch_array($res_max);
  282. $id = $row_max[0];
  283. }
  284. return $id;
  285. }
  286. /**
  287. * Get last announcement attachment in a course by id (not iid)
  288. * @param int Course ID (will get from context if none provided)
  289. * @return int 0 or the integer id last announcement attachment inserted
  290. * @assert () === 0
  291. */
  292. public static function get_last_announcement_attachment_id($course_id=null)
  293. {
  294. $tbl_announcement_att = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  295. if (empty($course_id)) {
  296. $course_id = api_get_course_int_id();
  297. } else {
  298. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  299. }
  300. $sql_max = "SELECT MAX(id) FROM $tbl_announcement_att WHERE c_id = $course_id";
  301. $res_max = Database::query($sql_max);
  302. $id = 0;
  303. if (Database::num_rows($res_max)) {
  304. $row_max = Database::fetch_array($res_max);
  305. $id = $row_max[0];
  306. }
  307. return $id;
  308. }
  309. /**
  310. * Store an announcement in the database (including its attached file if any)
  311. * @param string Announcement title (pure text)
  312. * @param string Content of the announcement (can be HTML)
  313. * @param int Display order in the list of announcements
  314. * @param array Array of users and groups to send the announcement to
  315. * @param array uploaded file $_FILES
  316. * @param string Comment describing the attachment
  317. * @return int false on failure, ID of the announcement on success
  318. */
  319. public static function add_announcement(
  320. $emailTitle,
  321. $newContent,
  322. $sent_to,
  323. $file = array(),
  324. $file_comment = null,
  325. $end_date = null
  326. ) {
  327. $_course = api_get_course_info();
  328. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  329. // filter data
  330. $emailTitle = Database::escape_string($emailTitle);
  331. $newContent = Database::escape_string($newContent);
  332. if (empty($end_date)) {
  333. $end_date = api_get_utc_datetime();
  334. } else {
  335. $end_date = Database::escape_string($end_date);
  336. }
  337. $course_id = api_get_course_int_id();
  338. $order = self::get_last_announcement_order();
  339. $aid = self::get_last_announcement_id($course_id)+1;
  340. // store in the table announcement
  341. $sql = "INSERT INTO $tbl_announcement SET
  342. c_id = '$course_id',
  343. id = $aid,
  344. content = '$newContent',
  345. title = '$emailTitle',
  346. end_date = '$end_date',
  347. display_order = '$order',
  348. session_id = ".api_get_session_id();
  349. $result = Database::query($sql);
  350. if ($result === false) {
  351. return false;
  352. } else {
  353. //Store the attach file
  354. $last_id = $aid;
  355. if (!empty($file)) {
  356. self::add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  357. }
  358. // store in item_property (first the groups, then the users
  359. if (!is_null($sent_to)) {
  360. // !is_null($sent_to): when no user is selected we send it to everyone
  361. $send_to = self::separate_users_groups($sent_to);
  362. // storing the selected groups
  363. if (is_array($send_to['groups'])) {
  364. foreach ($send_to['groups'] as $group) {
  365. api_item_property_update(
  366. $_course,
  367. TOOL_ANNOUNCEMENT,
  368. $last_id,
  369. "AnnouncementAdded",
  370. api_get_user_id(),
  371. $group
  372. );
  373. }
  374. }
  375. // storing the selected users
  376. if (is_array($send_to['users'])) {
  377. foreach ($send_to['users'] as $user) {
  378. api_item_property_update(
  379. $_course,
  380. TOOL_ANNOUNCEMENT,
  381. $last_id,
  382. "AnnouncementAdded",
  383. api_get_user_id(),
  384. '',
  385. $user
  386. );
  387. }
  388. }
  389. } else {
  390. // the message is sent to everyone, so we set the group to 0
  391. api_item_property_update(
  392. $_course,
  393. TOOL_ANNOUNCEMENT,
  394. $last_id,
  395. "AnnouncementAdded",
  396. api_get_user_id(),
  397. '0'
  398. );
  399. }
  400. return $last_id;
  401. }
  402. }
  403. /*
  404. * STORE ANNOUNCEMENT GROUP ITEM
  405. */
  406. public static function add_group_announcement(
  407. $emailTitle,
  408. $newContent,
  409. $to,
  410. $to_users,
  411. $file = array(),
  412. $file_comment = ''
  413. ) {
  414. $_course = api_get_course_info();
  415. // database definitions
  416. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  417. $emailTitle = Database::escape_string($emailTitle);
  418. $newContent = Database::escape_string($newContent);
  419. $order = self::get_last_announcement_order();
  420. $now = api_get_utc_datetime();
  421. $course_id = api_get_course_int_id();
  422. $aid = self::get_last_announcement_id($course_id)+1;
  423. // store in the table announcement
  424. $sql = "INSERT INTO $tbl_announcement SET
  425. c_id = '$course_id',
  426. id = $aid,
  427. content = '$newContent',
  428. title = '$emailTitle',
  429. end_date = '$now',
  430. display_order = '$order',
  431. session_id = ".api_get_session_id();
  432. $result = Database::query($sql);
  433. if ($result === false) {
  434. return false;
  435. }
  436. //store the attach file
  437. $last_id = $aid;
  438. if (!empty($file)) {
  439. self::add_announcement_attachment_file($last_id, $file_comment, $file);
  440. }
  441. // store in item_property (first the groups, then the users
  442. if (!isset($to_users)) {
  443. $send_to = self::separate_users_groups($to);
  444. // storing the selected groups
  445. if (is_array($send_to['groups'])) {
  446. foreach ($send_to['groups'] as $group) {
  447. api_item_property_update(
  448. $_course,
  449. TOOL_ANNOUNCEMENT,
  450. $last_id,
  451. "AnnouncementAdded",
  452. api_get_user_id(),
  453. $group
  454. );
  455. }
  456. }
  457. } else {
  458. // the message is sent to everyone, so we set the group to 0
  459. // storing the selected users
  460. if (is_array($to_users)) {
  461. foreach ($to_users as $user) {
  462. api_item_property_update(
  463. $_course,
  464. TOOL_ANNOUNCEMENT,
  465. $last_id,
  466. "AnnouncementAdded",
  467. api_get_user_id(),
  468. api_get_group_id(),
  469. $user
  470. );
  471. }
  472. }
  473. }
  474. return $last_id;
  475. }
  476. /*
  477. EDIT ANNOUNCEMENT
  478. */
  479. /**
  480. * This function stores the announcement item in the announcement table
  481. * and updates the item_property table
  482. *
  483. * @param int id of the announcement
  484. * @param string email
  485. * @param string content
  486. * @param array users that will receive the announcement
  487. * @param mixed attachment
  488. * @param string file comment
  489. *
  490. */
  491. public static function edit_announcement($id, $emailTitle, $newContent, $to, $file = array(), $file_comment = '')
  492. {
  493. $_course = api_get_course_info();
  494. $course_id = api_get_course_int_id();
  495. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  496. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  497. $emailTitle = Database::escape_string($emailTitle);
  498. $newContent = Database::escape_string($newContent);
  499. $id = intval($id);
  500. // store the modifications in the table announcement
  501. $sql = "UPDATE $tbl_announcement SET content = '$newContent', title = '$emailTitle' WHERE c_id = $course_id AND id='$id'";
  502. $result = Database::query($sql);
  503. // save attachment file
  504. $row_attach = self::get_attachment($id);
  505. $id_attach = intval($row_attach['id']);
  506. if (!empty($file)) {
  507. if (empty($id_attach)) {
  508. self::add_announcement_attachment_file($id, $file_comment, $file);
  509. } else {
  510. self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
  511. }
  512. }
  513. // we remove everything from item_property for this
  514. $sql_delete = "DELETE FROM $tbl_item_property WHERE c_id = $course_id AND ref='$id' AND tool='announcement'";
  515. $result = Database::query($sql_delete);
  516. // store in item_property (first the groups, then the users
  517. if (!is_null($to)) {
  518. // !is_null($to): when no user is selected we send it to everyone
  519. $send_to = self::separate_users_groups($to);
  520. // storing the selected groups
  521. if (is_array($send_to['groups'])) {
  522. foreach ($send_to['groups'] as $group) {
  523. api_item_property_update(
  524. $_course,
  525. TOOL_ANNOUNCEMENT,
  526. $id,
  527. "AnnouncementUpdated",
  528. api_get_user_id(),
  529. $group
  530. );
  531. }
  532. }
  533. // storing the selected users
  534. if (is_array($send_to['users'])) {
  535. foreach ($send_to['users'] as $user) {
  536. api_item_property_update(
  537. $_course,
  538. TOOL_ANNOUNCEMENT,
  539. $id,
  540. "AnnouncementUpdated",
  541. api_get_user_id(),
  542. 0,
  543. $user
  544. );
  545. }
  546. }
  547. } else {
  548. // the message is sent to everyone, so we set the group to 0
  549. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", api_get_user_id(), '0');
  550. }
  551. }
  552. /*
  553. MAIL FUNCTIONS
  554. */
  555. public static function update_mail_sent($insert_id)
  556. {
  557. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  558. if ($insert_id != strval(intval($insert_id))) {
  559. return false;
  560. }
  561. $insert_id = Database::escape_string($insert_id);
  562. $course_id = api_get_course_int_id();
  563. // store the modifications in the table tbl_annoucement
  564. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE c_id = $course_id AND id='$insert_id'";
  565. Database::query($sql);
  566. }
  567. /**
  568. * Gets all announcements from a user by course
  569. * @param string course db
  570. * @param int user id
  571. * @return array html with the content and count of announcements or false otherwise
  572. */
  573. public static function get_all_annoucement_by_user_course($course_code, $user_id)
  574. {
  575. $course_info = api_get_course_info($course_code);
  576. $course_id = $course_info['real_id'];
  577. if (empty($user_id)) {
  578. return false;
  579. }
  580. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  581. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  582. if (!empty($user_id) && is_numeric($user_id)) {
  583. $user_id = intval($user_id);
  584. $sql = "SELECT DISTINCT announcement.title, announcement.content
  585. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  586. WHERE
  587. announcement.c_id = $course_id AND
  588. toolitemproperties.c_id = $course_id AND
  589. announcement.id = toolitemproperties.ref AND
  590. toolitemproperties.tool='announcement' AND
  591. (toolitemproperties.insert_user_id='$user_id' AND (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null))
  592. AND toolitemproperties.visibility='1'
  593. AND announcement.session_id = 0
  594. ORDER BY display_order DESC";
  595. $rs = Database::query($sql);
  596. $num_rows = Database::num_rows($rs);
  597. $content = '';
  598. $i = 0;
  599. $result = array();
  600. if ($num_rows > 0) {
  601. while ($myrow = Database::fetch_array($rs)) {
  602. $content .= '<strong>'.$myrow['title'].'</strong><br /><br />';
  603. $content .= $myrow['content'];
  604. $i++;
  605. }
  606. $result['content'] = $content;
  607. $result['count'] = $i;
  608. return $result;
  609. }
  610. return false;
  611. }
  612. return false;
  613. }
  614. /*
  615. SHOW_TO_FORM
  616. */
  617. /**
  618. * this function shows the form for sending a message to a specific group or user.
  619. */
  620. public static function show_to_form($to_already_selected)
  621. {
  622. $user_list = self::get_course_users();
  623. $group_list = self::get_course_groups();
  624. if ($to_already_selected == '' || $to_already_selected == 'everyone') {
  625. $to_already_selected = array();
  626. }
  627. echo "<table id=\"recipient_list\" style=\"display: none;\">";
  628. echo '<tr>';
  629. // the form containing all the groups and all the users of the course
  630. echo '<td>';
  631. echo "<strong>".get_lang('Users')."</strong><br />";
  632. self::construct_not_selected_select_form($group_list, $user_list, $to_already_selected);
  633. echo "</td>";
  634. // the buttons for adding or removing groups/users
  635. echo '<td valign="middle">';
  636. 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>';
  637. echo '<br /> <br />';
  638. 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>';
  639. echo "</td>";
  640. echo "<td>";
  641. // the form containing the selected groups and users
  642. echo "<strong>".get_lang('DestinationUsers')."</strong><br />";
  643. self::construct_selected_select_form($group_list, $user_list, $to_already_selected);
  644. echo "</td>";
  645. echo "</tr>";
  646. echo "</table>";
  647. }
  648. /**
  649. * this function shows the form for sending a message to a specific group or user.
  650. */
  651. public static function show_to_form_group($group_id, $to = array())
  652. {
  653. $group_users = GroupManager::get_subscribed_users($group_id);
  654. echo "<table id=\"recipient_list\" style=\"display: none;\">";
  655. echo "<tr>";
  656. echo "<td>";
  657. echo "<select name=\"not_selected_form[]\" size=5 class=\"span4\" multiple>";
  658. foreach ($group_users as $user) {
  659. echo '<option value="'.$user['user_id'].'" title="'.sprintf(
  660. get_lang('LoginX'),
  661. $user['username']
  662. ).'" >'.api_get_person_name($user['firstname'], $user['lastname']).'</option>';
  663. }
  664. echo '</select>';
  665. echo "</td>";
  666. // the buttons for adding or removing groups/users
  667. echo "<td valign=\"middle\">";
  668. 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>';
  669. echo '<br /> <br />';
  670. 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>';
  671. echo "</td>";
  672. echo "<td>";
  673. echo "<select id=\"selectedform\" name=\"selectedform[]\" size=5 class=\"span4\" multiple>";
  674. if (!empty($to)) {
  675. foreach ($to as $user) {
  676. $user = explode(':', $user);
  677. if ($user[0] == 'USER') {
  678. $user = api_get_user_info($user[1]);
  679. echo '<option value="'.$user['user_id'].'" title="'.sprintf(
  680. get_lang('LoginX'),
  681. $user['username']
  682. ).'" >'.api_get_person_name($user['firstname'], $user['lastname']).'</option>';
  683. }
  684. }
  685. }
  686. echo '</select>';
  687. echo "</td>";
  688. echo "</tr>";
  689. echo "</table>";
  690. }
  691. /*
  692. CONSTRUCT_NOT_SELECT_SELECT_FORM
  693. */
  694. /**
  695. * this function shows the form for sending a message to a specific group or user.
  696. */
  697. public static function construct_not_selected_select_form(
  698. $group_list = null,
  699. $user_list = null,
  700. $to_already_selected
  701. ) {
  702. echo '<select name="not_selected_form[]" size="7" class="span4" multiple>';
  703. // adding the groups to the select form
  704. if ($group_list) {
  705. foreach ($group_list as $this_group) {
  706. if (is_array($to_already_selected)) {
  707. if (!in_array(
  708. "GROUP:".$this_group['id'],
  709. $to_already_selected
  710. )
  711. ) { // $to_already_selected is the array containing the groups (and users) that are already selected
  712. $user_label = ($this_group['userNb'] > 0) ? get_lang('Users') : get_lang('LowerCaseUser');
  713. $user_disabled = ($this_group['userNb'] > 0) ? "" : "disabled=disabled";
  714. echo "<option $user_disabled value=\"GROUP:".$this_group['id']."\">",
  715. "G: ", $this_group['name'], " - ".$this_group['userNb']." ".$user_label.
  716. "</option>";
  717. }
  718. }
  719. }
  720. // a divider
  721. echo "<option value=\"\">---------------------------------------------------------</option>";
  722. }
  723. // adding the individual users to the select form
  724. if ($user_list) {
  725. foreach ($user_list as $this_user) {
  726. if (is_array($to_already_selected)) {
  727. if (!in_array(
  728. "USER:".$this_user['user_id'],
  729. $to_already_selected
  730. )
  731. ) { // $to_already_selected is the array containing the users (and groups) that are already selected
  732. echo "<option value=\"USER:".$this_user['user_id']."\" title='".sprintf(
  733. get_lang('LoginX'),
  734. $this_user['username']
  735. )."'>",
  736. "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
  737. "</option>";
  738. }
  739. }
  740. }
  741. }
  742. echo "</select>";
  743. }
  744. /*
  745. CONSTRUCT_SELECTED_SELECT_FORM
  746. */
  747. /**
  748. * this function shows the form for sending a message to a specific group or user.
  749. */
  750. public static function construct_selected_select_form($group_list = null, $user_list = null, $to_already_selected)
  751. {
  752. // we separate the $to_already_selected array (containing groups AND users into
  753. // two separate arrays
  754. $groupuser = array();
  755. if (is_array($to_already_selected)) {
  756. $groupuser = self::separate_users_groups($to_already_selected);
  757. }
  758. $groups_to_already_selected = $groupuser['groups'];
  759. $users_to_already_selected = $groupuser['users'];
  760. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  761. $ref_array_groups = self::get_course_groups();
  762. $ref_array_users = self::get_course_users();
  763. // we construct the form of the already selected groups / users
  764. echo '<select id="selectedform" name="selectedform[]" size="7" multiple class="span4">';
  765. if (is_array($to_already_selected)) {
  766. foreach ($to_already_selected as $groupuser) {
  767. list($type, $id) = explode(":", $groupuser);
  768. if ($type == "GROUP") {
  769. echo "<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  770. } else {
  771. foreach ($ref_array_users as $key => $value) {
  772. if ($value['user_id'] == $id) {
  773. echo "<option value=\"".$groupuser."\" title='".sprintf(
  774. get_lang('LoginX'),
  775. $value['username']
  776. )."'>".api_get_person_name($value['firstname'], $value['lastname'])."</option>";
  777. break;
  778. }
  779. }
  780. }
  781. }
  782. } else {
  783. if ($to_already_selected == 'everyone') {
  784. // adding the groups to the select form
  785. if (is_array($ref_array_groups)) {
  786. foreach ($ref_array_groups as $this_group) {
  787. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  788. if (!is_array($to_already_selected) || !in_array(
  789. "GROUP:".$this_group['id'],
  790. $to_already_selected
  791. )
  792. ) { // $to_already_selected is the array containing the groups (and users) that are already selected
  793. echo "<option value=\"GROUP:".$this_group['id']."\">",
  794. "G: ", $this_group['name'], " &ndash; ".$this_group['userNb']." ".get_lang('Users').
  795. "</option>";
  796. }
  797. }
  798. }
  799. // adding the individual users to the select form
  800. foreach ($ref_array_users as $this_user) {
  801. if (!is_array($to_already_selected) || !in_array(
  802. "USER:".$this_user['user_id'],
  803. $to_already_selected
  804. )
  805. ) { // $to_already_selected is the array containing the users (and groups) that are already selected
  806. echo "<option value=\"USER:", $this_user['user_id'], "\" title='".sprintf(
  807. get_lang('LoginX'),
  808. $user['username']
  809. )."'>",
  810. "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
  811. "</option>";
  812. }
  813. }
  814. }
  815. }
  816. echo "</select>";
  817. }
  818. /*
  819. DATA FUNCTIONS
  820. */
  821. /**
  822. * Returns announcement info from its id
  823. *
  824. * @param type $course_id
  825. * @param type $annoucement_id
  826. * @return array
  827. */
  828. public static function get_by_id($course_id, $annoucement_id)
  829. {
  830. $annoucement_id = intval($annoucement_id);
  831. $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
  832. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  833. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  834. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  835. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property toolitemproperties
  836. ON announcement.id = toolitemproperties.ref AND
  837. announcement.c_id = $course_id AND
  838. toolitemproperties.c_id = $course_id
  839. WHERE toolitemproperties.tool='announcement' AND
  840. announcement.id = $annoucement_id";
  841. $result = Database::query($sql);
  842. if (Database::num_rows($result)) {
  843. return Database::fetch_array($result);
  844. }
  845. return array();
  846. }
  847. /**
  848. * this function gets all the users of the course,
  849. * including users from linked courses
  850. * @todo deprecate this function, use CourseManager class
  851. */
  852. public static function get_course_users()
  853. {
  854. //this would return only the users from real courses:
  855. $session_id = api_get_session_id();
  856. if ($session_id != 0) {
  857. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $session_id);
  858. } else {
  859. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), false, 0);
  860. }
  861. return $user_list;
  862. }
  863. /**
  864. * this function gets all the groups of the course,
  865. * not including linked courses
  866. */
  867. public static function get_course_groups()
  868. {
  869. $session_id = api_get_session_id();
  870. if ($session_id != 0) {
  871. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), $session_id, 1);
  872. } else {
  873. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0, 1);
  874. }
  875. return $new_group_list;
  876. }
  877. /*
  878. *
  879. LOAD_EDIT_USERS
  880. */
  881. /**
  882. * This tools loads all the users and all the groups who have received
  883. * a specific item (in this case an announcement item)
  884. */
  885. public static function load_edit_users($tool, $id)
  886. {
  887. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  888. $tool = Database::escape_string($tool);
  889. $id = Database::escape_string($id);
  890. $course_id = api_get_course_int_id();
  891. $sql = "SELECT to_group_id, to_user_id FROM $tbl_item_property WHERE c_id = $course_id AND tool='$tool' AND ref='$id'";
  892. $result = Database::query($sql);
  893. while ($row = Database::fetch_array($result)) {
  894. $to_group = $row['to_group_id'];
  895. $to_user = $row['to_user_id'];
  896. if (empty($to_user) && empty($to_group)) {
  897. return "everyone";
  898. }
  899. if (!empty($to_user) && !empty($to_group)) {
  900. $to[] = "USER:".$to_user;
  901. }
  902. if (empty($to_user) && !empty($to_group)) {
  903. $to[] = "GROUP:".$to_group;
  904. }
  905. if (!empty($to_user) && empty($to_group)) {
  906. $to[] = "USER:".$to_user;
  907. }
  908. }
  909. return $to;
  910. }
  911. /*
  912. USER_GROUP_FILTER_JAVASCRIPT
  913. */
  914. /**
  915. * returns the javascript for setting a filter
  916. * this goes into the $htmlHeadXtra[] array
  917. */
  918. public static function user_group_filter_javascript()
  919. {
  920. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  921. <!--
  922. function jumpMenu(targ,selObj,restore)
  923. {
  924. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  925. if (restore) selObj.selectedIndex=0;
  926. }
  927. //-->
  928. </script>";
  929. }
  930. /*
  931. TO_JAVASCRIPT
  932. */
  933. /**
  934. * returns all the javascript that is required for easily
  935. * setting the target people/groups
  936. * this goes into the $htmlHeadXtra[] array
  937. */
  938. public static function to_javascript()
  939. {
  940. $www = api_get_path(WEB_PATH);
  941. $src = $www.'main/announcements/resources/js/main.js';
  942. $result = Javascript::tag($src);
  943. $root = Chamilo::url();
  944. $code = "var www = '$root';\n";
  945. $code .= Javascript::get_lang('FieldRequired', 'Send2All', 'AddAnAttachment', 'Everybody');
  946. $result .= Javascript::tag_code($code);
  947. return $result;
  948. }
  949. /*
  950. SENT_TO_FORM
  951. */
  952. /**
  953. * constructs the form to display all the groups and users the message has been sent to
  954. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  955. * the first level is a distinction between groups and users:
  956. * $sent_to_array['groups'] * and $sent_to_array['users']
  957. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  958. * containing all the id's of the groups (resp. users) who have received this message.
  959. * @author Patrick Cool <patrick.cool@>
  960. */
  961. public static function sent_to_form($sent_to_array)
  962. {
  963. // we find all the names of the groups
  964. $group_names = self::get_course_groups();
  965. // we count the number of users and the number of groups
  966. if (isset($sent_to_array['users'])) {
  967. $number_users = count($sent_to_array['users']);
  968. } else {
  969. $number_users = 0;
  970. }
  971. if (isset($sent_to_array['groups'])) {
  972. $number_groups = count($sent_to_array['groups']);
  973. } else {
  974. $number_groups = 0;
  975. }
  976. $total_numbers = $number_users + $number_groups;
  977. // starting the form if there is more than one user/group
  978. $output = array();
  979. if ($total_numbers > 1) {
  980. // outputting the name of the groups
  981. if (is_array($sent_to_array['groups'])) {
  982. foreach ($sent_to_array['groups'] as $group_id) {
  983. $output[] = $group_names[$group_id]['name'];
  984. }
  985. }
  986. if (isset($sent_to_array['users'])) {
  987. if (is_array($sent_to_array['users'])) {
  988. foreach ($sent_to_array['users'] as $user_id) {
  989. $user_info = api_get_user_info($user_id);
  990. $output[] = $user_info['complete_name_with_username'];
  991. }
  992. }
  993. }
  994. } else {
  995. // there is only one user/group
  996. if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
  997. $user_info = api_get_user_info($sent_to_array['users'][0]);
  998. $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  999. }
  1000. if (isset($sent_to_array['groups']) and is_array(
  1001. $sent_to_array['groups']
  1002. ) and isset($sent_to_array['groups'][0]) and $sent_to_array['groups'][0] !== 0
  1003. ) {
  1004. $group_id = $sent_to_array['groups'][0];
  1005. $output[] = "&nbsp;".$group_names[$group_id]['name'];
  1006. }
  1007. if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
  1008. $output[] = "&nbsp;".get_lang('Everybody');
  1009. }
  1010. }
  1011. if (!empty($output)) {
  1012. $output = array_filter($output);
  1013. if (count($output) > 0) {
  1014. $output = implode(', ', $output);
  1015. }
  1016. return $output;
  1017. }
  1018. }
  1019. /*
  1020. SEPARATE_USERS_GROUPS
  1021. */
  1022. /**
  1023. * This function separates the users from the groups
  1024. * users have a value USER:XXX (with XXX the dokeos id
  1025. * groups have a value GROUP:YYY (with YYY the group id)
  1026. * @param array Array of strings that define the type and id of each destination
  1027. * @return array Array of groups and users (each an array of IDs)
  1028. */
  1029. public static function separate_users_groups($to)
  1030. {
  1031. $grouplist = array();
  1032. $userlist = array();
  1033. foreach ($to as $to_item) {
  1034. list($type, $id) = explode(':', $to_item);
  1035. switch ($type) {
  1036. case 'GROUP':
  1037. $grouplist[] = intval($id);
  1038. break;
  1039. case 'USER':
  1040. $userlist[] = intval($id);
  1041. break;
  1042. }
  1043. }
  1044. $send_to['groups'] = $grouplist;
  1045. $send_to['users'] = $userlist;
  1046. return $send_to;
  1047. }
  1048. /*
  1049. SENT_TO()
  1050. */
  1051. /**
  1052. * Returns all the users and all the groups a specific announcement item
  1053. * has been sent to
  1054. * @param string The tool (announcement, agenda, ...)
  1055. * @param int ID of the element of the corresponding type
  1056. * @return array Array of users and groups to whom the element has been sent
  1057. */
  1058. public static function sent_to($tool, $id)
  1059. {
  1060. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1061. $tool = Database::escape_string($tool);
  1062. $id = intval($id);
  1063. $sent_to_group = array();
  1064. $sent_to = array();
  1065. $course_id = api_get_course_int_id();
  1066. $sql = "SELECT to_group_id, to_user_id FROM $tbl_item_property WHERE c_id = $course_id AND tool = '$tool' AND ref=".$id;
  1067. $result = Database::query($sql);
  1068. while ($row = Database::fetch_array($result)) {
  1069. // if to_group_id is null then it is sent to a specific user
  1070. // if to_group_id = 0 then it is sent to everybody
  1071. if (empty($row['to_user_id'])) {
  1072. if ($row['to_group_id'] != 0) {
  1073. $sent_to_group[] = $row['to_group_id'];
  1074. }
  1075. }
  1076. // if to_user_id <> 0 then it is sent to a specific user
  1077. if ($row['to_user_id'] <> 0) {
  1078. $sent_to_user[] = $row['to_user_id'];
  1079. }
  1080. }
  1081. if (isset($sent_to_group)) {
  1082. $sent_to['groups'] = $sent_to_group;
  1083. }
  1084. if (isset($sent_to_user)) {
  1085. $sent_to['users'] = $sent_to_user;
  1086. }
  1087. return $sent_to;
  1088. }
  1089. /* ATTACHMENT FUNCTIONS */
  1090. /**
  1091. * Show a list with all the attachments according to the post's id
  1092. * @param int announcement id
  1093. * @return array with the post info
  1094. * @author Arthur Portugal
  1095. * @version November 2009, dokeos 1.8.6.2
  1096. */
  1097. public static function get_attachment($announcement_id)
  1098. {
  1099. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1100. $announcement_id = intval($announcement_id);
  1101. $course_id = api_get_course_int_id();
  1102. $row = array();
  1103. $sql = 'SELECT id, path, filename, comment FROM '.$tbl_announcement_attachment.'
  1104. WHERE c_id = '.$course_id.' AND announcement_id = '.$announcement_id.'';
  1105. $result = Database::query($sql);
  1106. if (Database::num_rows($result) != 0) {
  1107. $row = Database::fetch_array($result, 'ASSOC');
  1108. }
  1109. return $row;
  1110. }
  1111. /**
  1112. * This function add a attachment file into announcement
  1113. * @param int announcement id
  1114. * @param string file comment
  1115. * @param array uploaded file $_FILES
  1116. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  1117. */
  1118. public static function add_announcement_attachment_file($announcement_id, $file_comment, $file)
  1119. {
  1120. $_course = api_get_course_info();
  1121. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1122. $return = 0;
  1123. $announcement_id = intval($announcement_id);
  1124. $course_id = api_get_course_int_id();
  1125. if (is_array($file) && $file['error'] == 0) {
  1126. $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1127. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1128. $updir = $sys_course_path.$courseDir;
  1129. // Try to add an extension to the file if it hasn't one
  1130. $new_file_name = FileManager::add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1131. // user's file name
  1132. $file_name = $file['name'];
  1133. if (!FileManager::filter_extension($new_file_name)) {
  1134. $return = -1;
  1135. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1136. } else {
  1137. $new_file_name = uniqid('');
  1138. $new_path = $updir.'/'.$new_file_name;
  1139. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1140. $safe_file_comment = Database::escape_string($file_comment);
  1141. $safe_file_name = Database::escape_string($file_name);
  1142. $safe_new_file_name = Database::escape_string($new_file_name);
  1143. $aid = self::get_last_announcement_attachment_id($course_id)+1;
  1144. // Storing the attachments if any
  1145. $sql = "INSERT INTO $tbl_announcement_attachment (c_id, id, filename, comment, path, announcement_id, size) VALUES
  1146. ($course_id, $aid, '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '".intval(
  1147. $file['size']
  1148. )."' )";
  1149. Database::query($sql);
  1150. $return = 1;
  1151. }
  1152. }
  1153. return $return;
  1154. }
  1155. /**
  1156. * This function edit a attachment file into announcement
  1157. * @param int attach id
  1158. * @param array uploaded file $_FILES
  1159. * @param string file comment
  1160. * @return int
  1161. */
  1162. public static function edit_announcement_attachment_file($id_attach, $file, $file_comment)
  1163. {
  1164. $_course = api_get_course_info();
  1165. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1166. $return = 0;
  1167. $course_id = api_get_course_int_id();
  1168. if (is_array($file) && $file['error'] == 0) {
  1169. $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1170. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1171. $updir = $sys_course_path.$courseDir;
  1172. // Try to add an extension to the file if it hasn't one
  1173. $new_file_name = FileManager::add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1174. // user's file name
  1175. $file_name = $file ['name'];
  1176. if (!FileManager::filter_extension($new_file_name)) {
  1177. $return - 1;
  1178. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1179. } else {
  1180. $new_file_name = uniqid('');
  1181. $new_path = $updir.'/'.$new_file_name;
  1182. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1183. $safe_file_comment = Database::escape_string($file_comment);
  1184. $safe_file_name = Database::escape_string($file_name);
  1185. $safe_new_file_name = Database::escape_string($new_file_name);
  1186. $id_attach = intval($id_attach);
  1187. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='".intval(
  1188. $file['size']
  1189. )."'
  1190. WHERE c_id = $course_id AND id = '$id_attach'";
  1191. $result = Database::query($sql);
  1192. if ($result === false) {
  1193. $return = -1;
  1194. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1195. } else {
  1196. $return = 1;
  1197. }
  1198. }
  1199. }
  1200. return $return;
  1201. }
  1202. /**
  1203. * This function delete a attachment file by id
  1204. * @param integer attachment file Id
  1205. *
  1206. */
  1207. public static function delete_announcement_attachment_file($id)
  1208. {
  1209. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1210. $id = intval($id);
  1211. $course_id = api_get_course_int_id();
  1212. $sql = "DELETE FROM $tbl_announcement_attachment WHERE c_id = $course_id AND id = $id";
  1213. Database::query($sql);
  1214. // update item_property
  1215. //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id());
  1216. }
  1217. public static function send_email($annoucement_id)
  1218. {
  1219. $email = AnnouncementEmail::create(null, $annoucement_id);
  1220. $email->send();
  1221. }
  1222. public static function user_can_edit_announcement()
  1223. {
  1224. $group_id = api_get_group_id();
  1225. return api_is_allowed_to_edit(false, true) OR
  1226. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) OR
  1227. (!empty($group_id) AND GroupManager::user_has_access(
  1228. api_get_user_id(),
  1229. $group_id,
  1230. GroupManager::GROUP_TOOL_ANNOUNCEMENT
  1231. ) AND GroupManager::is_tutor_of_group(api_get_user_id(), $group_id));
  1232. }
  1233. }
  1234. //end class