announcements.inc.php 43 KB

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