AnnouncementManager.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CAnnouncement;
  4. use Chamilo\CourseBundle\Entity\CItemProperty;
  5. /**
  6. * Include file with functions for the announcements module.
  7. * @author jmontoya
  8. * @package chamilo.announcements
  9. * @todo use OOP
  10. */
  11. class AnnouncementManager
  12. {
  13. /**
  14. * Constructor
  15. */
  16. public function __construct()
  17. {
  18. }
  19. /**
  20. * @return array
  21. */
  22. public static function get_tags()
  23. {
  24. return array(
  25. '((user_name))',
  26. '((user_firstname))',
  27. '((user_lastname))',
  28. '((teacher_name))',
  29. '((teacher_email))',
  30. '((course_title))',
  31. '((course_link))',
  32. '((official_code))'
  33. );
  34. }
  35. /**
  36. * @param int $userId
  37. * @param string $content
  38. * @param string $course_code
  39. * @param int $session_id
  40. *
  41. * @return mixed
  42. */
  43. public static function parse_content($userId, $content, $course_code, $session_id = 0)
  44. {
  45. $readerInfo = api_get_user_info($userId);
  46. $courseInfo = api_get_course_info($course_code);
  47. $teacherList = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
  48. $teacher_email = '';
  49. $teacher_name = '';
  50. if (!empty($teacherList)) {
  51. foreach ($teacherList as $teacher_data) {
  52. $teacher_name = api_get_person_name($teacher_data['firstname'], $teacher_data['lastname']);
  53. $teacher_email = $teacher_data['email'];
  54. break;
  55. }
  56. }
  57. $courseLink = api_get_course_url($course_code, $session_id);
  58. if (!empty($readerInfo)) {
  59. $data['user_name'] = $readerInfo['username'];
  60. $data['user_firstname'] = $readerInfo['firstname'];
  61. $data['user_lastname'] = $readerInfo['lastname'];
  62. $data['official_code'] = $readerInfo['official_code'];
  63. }
  64. $data['teacher_name'] = $teacher_name;
  65. $data['teacher_email'] = $teacher_email;
  66. $data['course_title'] = $courseInfo['name'];
  67. $data['course_link'] = Display::url($courseLink, $courseLink);
  68. $content = str_replace(self::get_tags(), $data, $content);
  69. return $content;
  70. }
  71. /**
  72. * Gets all announcements from a course
  73. * @param array $course_info
  74. * @param int $session_id
  75. * @return array html with the content and count of announcements or false otherwise
  76. */
  77. public static function get_all_annoucement_by_course($course_info, $session_id = 0)
  78. {
  79. $session_id = intval($session_id);
  80. $course_id = $course_info['real_id'];
  81. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  82. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  83. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  84. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  85. WHERE
  86. announcement.id = toolitemproperties.ref AND
  87. toolitemproperties.tool='announcement' AND
  88. announcement.session_id = '$session_id' AND
  89. announcement.c_id = $course_id AND
  90. toolitemproperties.c_id = $course_id
  91. ORDER BY display_order DESC";
  92. $rs = Database::query($sql);
  93. $num_rows = Database::num_rows($rs);
  94. if ($num_rows > 0) {
  95. $list = array();
  96. while ($row = Database::fetch_array($rs)) {
  97. $list[] = $row;
  98. }
  99. return $list;
  100. }
  101. return false;
  102. }
  103. /**
  104. * This functions switches the visibility a course resource
  105. * using the visibility field in 'item_property'
  106. * @param array $_course
  107. * @param int $id ID of the element of the corresponding type
  108. * @return bool False on failure, True on success
  109. */
  110. public static function change_visibility_announcement($_course, $id)
  111. {
  112. $session_id = api_get_session_id();
  113. $item_visibility = api_get_item_visibility(
  114. $_course,
  115. TOOL_ANNOUNCEMENT,
  116. $id,
  117. $session_id
  118. );
  119. if ($item_visibility == '1') {
  120. api_item_property_update(
  121. $_course,
  122. TOOL_ANNOUNCEMENT,
  123. $id,
  124. 'invisible',
  125. api_get_user_id()
  126. );
  127. } else {
  128. api_item_property_update(
  129. $_course,
  130. TOOL_ANNOUNCEMENT,
  131. $id,
  132. 'visible',
  133. api_get_user_id()
  134. );
  135. }
  136. return true;
  137. }
  138. /**
  139. * Deletes an announcement
  140. * @param array $_course the course array
  141. * @param int $id the announcement id
  142. */
  143. public static function delete_announcement($_course, $id)
  144. {
  145. api_item_property_update(
  146. $_course,
  147. TOOL_ANNOUNCEMENT,
  148. $id,
  149. 'delete',
  150. api_get_user_id()
  151. );
  152. }
  153. /**
  154. * Deletes all announcements by course
  155. * @param array $_course the course array
  156. */
  157. public static function delete_all_announcements($_course)
  158. {
  159. $announcements = self::get_all_annoucement_by_course($_course, api_get_session_id());
  160. if (!empty($announcements)) {
  161. foreach ($announcements as $annon) {
  162. api_item_property_update(
  163. $_course,
  164. TOOL_ANNOUNCEMENT,
  165. $annon['id'],
  166. 'delete',
  167. api_get_user_id()
  168. );
  169. }
  170. }
  171. }
  172. /**
  173. * @param int $announcementId
  174. * @param int $courseId
  175. * @param int $userId
  176. *
  177. * @return array
  178. */
  179. public static function getAnnouncementInfoById($announcementId, $courseId, $userId)
  180. {
  181. if (api_is_allowed_to_edit(false, true) ||
  182. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  183. ) {
  184. $dql = "SELECT CA, IP
  185. FROM ChamiloCourseBundle:CAnnouncement CA, ChamiloCourseBundle:CItemProperty IP
  186. WHERE
  187. CA.id = IP.ref AND
  188. CA.id = :announcement AND
  189. IP.tool = 'announcement' AND
  190. CA.cId = IP.course AND
  191. CA.cId = :course
  192. ORDER BY CA.displayOrder DESC";
  193. } else {
  194. $group_list = GroupManager::get_group_ids($courseId, api_get_user_id());
  195. if (empty($group_list)) {
  196. $group_list[] = 0;
  197. }
  198. if (api_get_user_id() != 0) {
  199. $dql = "SELECT CA, IP
  200. FROM ChamiloCourseBundle:CAnnouncement CA, ChamiloCourseBundle:CItemProperty IP
  201. WHERE
  202. CA.id = IP.ref AND
  203. CA.id = :announcement AND
  204. IP.tool='announcement' AND
  205. (
  206. IP.toUser = $userId OR
  207. IP.group IN ('0', '" . implode("', '", $group_list) . "') OR
  208. IP.group IS NULL
  209. ) AND
  210. IP.visibility = '1' AND
  211. CA.cId = IP.course AND
  212. IP.course = :course
  213. ORDER BY CA.displayOrder DESC";
  214. } else {
  215. $dql = "SELECT CA, IP
  216. FROM ChamiloCourseBundle:CAnnouncement CA, ChamiloCourseBundle:CItemProperty IP
  217. WHERE
  218. CA.id = IP.ref AND
  219. CA.id = :announcement AND
  220. IP.tool = 'announcement' AND
  221. (IP.group = '0' OR IP.group IS NULL) AND
  222. IP.visibility = '1' AND
  223. CA.cId = IP.course AND
  224. IP.course = :course";
  225. }
  226. }
  227. $result = Database::getManager()
  228. ->createQuery($dql)
  229. ->execute([
  230. 'announcement' => $announcementId,
  231. 'course' => $courseId
  232. ]);
  233. return [
  234. 'announcement' => $result[0],
  235. 'item_property' => $result[1]
  236. ];
  237. }
  238. /**
  239. * Displays one specific announcement
  240. * @param int $announcement_id, the id of the announcement you want to display
  241. */
  242. public static function display_announcement($announcement_id)
  243. {
  244. if ($announcement_id != strval(intval($announcement_id))) {
  245. return null;
  246. }
  247. global $charset;
  248. $html = '';
  249. $result = self::getAnnouncementInfoById(
  250. $announcement_id,
  251. api_get_course_int_id(),
  252. api_get_user_id()
  253. );
  254. /** @var CAnnouncement $announcement */
  255. $announcement = $result['announcement'];
  256. /** @var CItemProperty $itemProperty */
  257. $itemProperty = $result['item_property'];
  258. if (empty($announcement) || empty($itemProperty)) {
  259. return '';
  260. }
  261. $title = $announcement->getTitle();
  262. $content = $announcement->getContent();
  263. $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">";
  264. $html .= "<tr><td><h2>" . $title . "</h2></td></tr>";
  265. if (api_is_allowed_to_edit(false, true) ||
  266. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  267. ) {
  268. $modify_icons = "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&action=modify&id=" . $announcement_id . "\">" .
  269. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL) . "</a>";
  270. if ($itemProperty->getVisibility() === 1) {
  271. $image_visibility = "visible";
  272. $alt_visibility = get_lang('Hide');
  273. } else {
  274. $image_visibility = "invisible";
  275. $alt_visibility = get_lang('Visible');
  276. }
  277. global $stok;
  278. $modify_icons .= "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&action=showhide&id=" . $announcement_id . "&sec_token=" . $stok . "\">" .
  279. Display::return_icon($image_visibility . '.png', $alt_visibility, '', ICON_SIZE_SMALL) . "</a>";
  280. if (api_is_allowed_to_edit(false, true)) {
  281. $modify_icons .= "<a href=\"" . api_get_self() . "?" . api_get_cidreq() . "&action=delete&id=" . $announcement_id . "&sec_token=" . $stok . "\" onclick=\"javascript:if(!confirm('" . addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)) . "')) return false;\">" .
  282. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) .
  283. "</a>";
  284. }
  285. $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>";
  286. }
  287. $toUser = $itemProperty->getToUser();
  288. $toUserId = !empty($toUser) ? $toUser->getId() : 0;
  289. $content = self::parse_content(
  290. $toUserId,
  291. $content,
  292. api_get_course_id(),
  293. api_get_session_id()
  294. );
  295. $lastEdit = $itemProperty->getLasteditDate();
  296. $html .= "<tr><td>$content</td></tr>";
  297. $html .= "<tr><td class=\"announcements_datum\">" . get_lang('LastUpdateDate') . " : " .
  298. Display::dateToStringAgoAndLongDate(
  299. !empty($lastEdit) ? $lastEdit->format('Y-m-d h:i:s') : ''
  300. ) . "</td></tr>";
  301. if ($itemProperty->getGroup() !== null) {
  302. $sent_to_icon = Display::return_icon('group.gif', get_lang('AnnounceSentToUserSelection'));
  303. }
  304. if (api_is_allowed_to_edit(false, true)) {
  305. $sent_to = self::sent_to('announcement', $announcement_id);
  306. $sent_to_form = self::sent_to_form($sent_to);
  307. $html .= Display::tag(
  308. 'td',
  309. get_lang('SentTo').': ' . $sent_to_form,
  310. array('class' => 'announcements_datum')
  311. );
  312. }
  313. $attachment_list = self::get_attachment($announcement_id);
  314. if (count($attachment_list) > 0) {
  315. $html .= "<tr><td>";
  316. $realname = $attachment_list['path'];
  317. $user_filename = $attachment_list['filename'];
  318. $full_file_name = 'download.php?'.api_get_cidreq().'&file=' . $realname;
  319. $html .= '<br/>';
  320. $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
  321. $html .= '<a href="' . $full_file_name . ' "> ' . $user_filename . ' </a>';
  322. $html .= ' - <span class="forum_attach_comment" >' . $attachment_list['comment'] . '</span>';
  323. if (api_is_allowed_to_edit(false, true)) {
  324. $html .= Display::url(
  325. Display::return_icon('delete.png', get_lang('Delete'), '', 16),
  326. api_get_self() . "?" . api_get_cidreq() . "&action=delete_attachment&id_attach=" . $attachment_list['id'] . "&sec_token=" . $stok
  327. );
  328. }
  329. $html .= '</td></tr>';
  330. }
  331. $html .= "</table>";
  332. return $html;
  333. }
  334. /**
  335. * @return int
  336. */
  337. public static function get_last_announcement_order()
  338. {
  339. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  340. $course_id = api_get_course_int_id();
  341. $sql = "SELECT MAX(display_order)
  342. FROM $tbl_announcement
  343. WHERE c_id = $course_id ";
  344. $res_max = Database::query($sql);
  345. $order = 0;
  346. if (Database::num_rows($res_max)) {
  347. $row_max = Database::fetch_array($res_max);
  348. $order = intval($row_max[0])+1;
  349. }
  350. return $order;
  351. }
  352. /**
  353. * Store an announcement in the database (including its attached file if any)
  354. * @param string $emailTitle Announcement title (pure text)
  355. * @param string $newContent Content of the announcement (can be HTML)
  356. * @param array $sentTo Array of users and groups to send the announcement to
  357. * @param array $file uploaded file $_FILES
  358. * @param string $file_comment Comment describing the attachment
  359. * @param string $end_date
  360. * @param bool $sendToUsersInSession
  361. * @return int false on failure, ID of the announcement on success
  362. */
  363. public static function add_announcement(
  364. $emailTitle,
  365. $newContent,
  366. $sentTo,
  367. $file = array(),
  368. $file_comment = null,
  369. $end_date = null,
  370. $sendToUsersInSession = false
  371. ) {
  372. $_course = api_get_course_info();
  373. $course_id = api_get_course_int_id();
  374. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  375. if (empty($end_date)) {
  376. $end_date = api_get_utc_datetime();
  377. }
  378. $order = self::get_last_announcement_order();
  379. // store in the table announcement
  380. $params = array(
  381. 'c_id' => $course_id,
  382. 'content' => $newContent,
  383. 'title' => $emailTitle,
  384. 'end_date' => $end_date,
  385. 'display_order' => $order,
  386. 'session_id' => api_get_session_id()
  387. );
  388. $last_id = Database::insert($tbl_announcement, $params);
  389. if (empty($last_id)) {
  390. return false;
  391. } else {
  392. $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
  393. Database::query($sql);
  394. if (!empty($file)) {
  395. self::add_announcement_attachment_file(
  396. $last_id,
  397. $file_comment,
  398. $_FILES['user_upload']
  399. );
  400. }
  401. // store in item_property (first the groups, then the users
  402. if (empty($sentTo) || !empty($sentTo) &&
  403. isset($sentTo[0]) && $sentTo[0] == 'everyone'
  404. ) {
  405. // The message is sent to EVERYONE, so we set the group to 0
  406. api_item_property_update(
  407. $_course,
  408. TOOL_ANNOUNCEMENT,
  409. $last_id,
  410. 'AnnouncementAdded',
  411. api_get_user_id(),
  412. '0'
  413. );
  414. } else {
  415. $send_to = CourseManager::separateUsersGroups($sentTo);
  416. $batchSize = 20;
  417. $em = Database::getManager();
  418. // Storing the selected groups
  419. if (is_array($send_to['groups']) && !empty($send_to['groups'])) {
  420. $counter = 1;
  421. foreach ($send_to['groups'] as $group) {
  422. api_item_property_update(
  423. $_course,
  424. TOOL_ANNOUNCEMENT,
  425. $last_id,
  426. "AnnouncementAdded",
  427. api_get_user_id(),
  428. $group
  429. );
  430. if (($counter % $batchSize) === 0) {
  431. $em->flush();
  432. $em->clear();
  433. }
  434. $counter++;
  435. }
  436. }
  437. // Storing the selected users
  438. if (is_array($send_to['users'])) {
  439. $counter = 1;
  440. foreach ($send_to['users'] as $user) {
  441. api_item_property_update(
  442. $_course,
  443. TOOL_ANNOUNCEMENT,
  444. $last_id,
  445. "AnnouncementAdded",
  446. api_get_user_id(),
  447. '',
  448. $user
  449. );
  450. if (($counter % $batchSize) === 0) {
  451. $em->flush();
  452. $em->clear();
  453. }
  454. $counter++;
  455. }
  456. }
  457. }
  458. if ($sendToUsersInSession) {
  459. self::addAnnouncementToAllUsersInSessions($last_id);
  460. }
  461. return $last_id;
  462. }
  463. }
  464. /**
  465. * @param $emailTitle
  466. * @param $newContent
  467. * @param $to
  468. * @param $to_users
  469. * @param array $file
  470. * @param string $file_comment
  471. * @param bool $sendToUsersInSession
  472. *
  473. * @return bool|int
  474. */
  475. public static function add_group_announcement(
  476. $emailTitle,
  477. $newContent,
  478. $to,
  479. $to_users,
  480. $file = array(),
  481. $file_comment = '',
  482. $sendToUsersInSession = false
  483. ) {
  484. $_course = api_get_course_info();
  485. // Database definitions
  486. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  487. $order = self::get_last_announcement_order();
  488. $now = api_get_utc_datetime();
  489. $course_id = api_get_course_int_id();
  490. // store in the table announcement
  491. $params = [
  492. 'c_id' => $course_id,
  493. 'content' => $newContent,
  494. 'title' => $emailTitle,
  495. 'end_date' => $now,
  496. 'display_order' => $order,
  497. 'session_id' => api_get_session_id()
  498. ];
  499. $last_id = Database::insert($tbl_announcement, $params);
  500. // Store the attach file
  501. if ($last_id) {
  502. $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
  503. Database::query($sql);
  504. if (!empty($file)) {
  505. self::add_announcement_attachment_file(
  506. $last_id,
  507. $file_comment,
  508. $file
  509. );
  510. }
  511. // Store in item_property (first the groups, then the users
  512. if (!isset($to_users)) {
  513. // when no user is selected we send it to everyone
  514. $send_to = CourseManager::separateUsersGroups($to);
  515. // storing the selected groups
  516. if (is_array($send_to['groups'])) {
  517. foreach ($send_to['groups'] as $group) {
  518. api_item_property_update(
  519. $_course,
  520. TOOL_ANNOUNCEMENT,
  521. $last_id,
  522. "AnnouncementAdded",
  523. api_get_user_id(),
  524. $group
  525. );
  526. }
  527. }
  528. } else {
  529. $send_to_groups = CourseManager::separateUsersGroups($to);
  530. $send_to_users = CourseManager::separateUsersGroups($to_users);
  531. $to_groups = $send_to_groups['groups'];
  532. $to_users = $send_to_users['users'];
  533. // storing the selected users
  534. if (is_array($to_users) && is_array($to_groups)) {
  535. foreach ($to_groups as $group) {
  536. foreach ($to_users as $user) {
  537. api_item_property_update(
  538. $_course,
  539. TOOL_ANNOUNCEMENT,
  540. $last_id,
  541. 'AnnouncementAdded',
  542. api_get_user_id(),
  543. $group,
  544. $user
  545. );
  546. }
  547. }
  548. }
  549. }
  550. if ($sendToUsersInSession) {
  551. self::addAnnouncementToAllUsersInSessions($last_id);
  552. }
  553. }
  554. return $last_id;
  555. }
  556. /**
  557. * This function stores the announcement item in the announcement table
  558. * and updates the item_property table
  559. *
  560. * @param int id of the announcement
  561. * @param string email
  562. * @param string content
  563. * @param array users that will receive the announcement
  564. * @param mixed attachment
  565. * @param string file comment
  566. */
  567. public static function edit_announcement(
  568. $id,
  569. $emailTitle,
  570. $newContent,
  571. $to,
  572. $file = array(),
  573. $file_comment = '',
  574. $sendToUsersInSession = false
  575. ) {
  576. $_course = api_get_course_info();
  577. $course_id = api_get_course_int_id();
  578. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  579. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  580. $id = intval($id);
  581. $params = [
  582. 'title' => $emailTitle,
  583. 'content' => $newContent
  584. ];
  585. Database::update(
  586. $tbl_announcement,
  587. $params,
  588. ['c_id = ? AND id = ?' => [$course_id, $id]]
  589. );
  590. // save attachment file
  591. $row_attach = self::get_attachment($id);
  592. $id_attach = 0;
  593. if ($row_attach) {
  594. $id_attach = intval($row_attach['id']);
  595. }
  596. if (!empty($file)) {
  597. if (empty($id_attach)) {
  598. self::add_announcement_attachment_file($id, $file_comment, $file);
  599. } else {
  600. self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
  601. }
  602. }
  603. // we remove everything from item_property for this
  604. $sql = "DELETE FROM $tbl_item_property
  605. WHERE c_id = $course_id AND ref='$id' AND tool='announcement'";
  606. Database::query($sql);
  607. if ($sendToUsersInSession) {
  608. self::addAnnouncementToAllUsersInSessions($id);
  609. }
  610. // store in item_property (first the groups, then the users
  611. if (!is_null($to)) {
  612. // !is_null($to): when no user is selected we send it to everyone
  613. $send_to = CourseManager::separateUsersGroups($to);
  614. // storing the selected groups
  615. if (is_array($send_to['groups'])) {
  616. foreach ($send_to['groups'] as $group) {
  617. api_item_property_update(
  618. $_course,
  619. TOOL_ANNOUNCEMENT,
  620. $id,
  621. "AnnouncementUpdated",
  622. api_get_user_id(),
  623. $group
  624. );
  625. }
  626. }
  627. // storing the selected users
  628. if (is_array($send_to['users'])) {
  629. foreach ($send_to['users'] as $user) {
  630. api_item_property_update(
  631. $_course,
  632. TOOL_ANNOUNCEMENT,
  633. $id,
  634. "AnnouncementUpdated",
  635. api_get_user_id(),
  636. 0,
  637. $user
  638. );
  639. }
  640. }
  641. } else {
  642. // the message is sent to everyone, so we set the group to 0
  643. api_item_property_update(
  644. $_course,
  645. TOOL_ANNOUNCEMENT,
  646. $id,
  647. "AnnouncementUpdated",
  648. api_get_user_id(),
  649. '0'
  650. );
  651. }
  652. }
  653. /**
  654. * @param int $announcementId
  655. */
  656. public static function addAnnouncementToAllUsersInSessions($announcementId)
  657. {
  658. $courseCode = api_get_course_id();
  659. $_course = api_get_course_info();
  660. $sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
  661. if (!empty($sessionList)) {
  662. foreach ($sessionList as $sessionInfo) {
  663. $sessionId = $sessionInfo['id'];
  664. $userList = CourseManager::get_user_list_from_course_code(
  665. $courseCode,
  666. $sessionId
  667. );
  668. if (!empty($userList)) {
  669. foreach ($userList as $user) {
  670. api_item_property_update(
  671. $_course,
  672. TOOL_ANNOUNCEMENT,
  673. $announcementId,
  674. "AnnouncementUpdated",
  675. api_get_user_id(),
  676. 0,
  677. $user['user_id'],
  678. 0,
  679. 0,
  680. $sessionId
  681. );
  682. }
  683. }
  684. }
  685. }
  686. }
  687. /**
  688. * @param int $insert_id
  689. * @return bool
  690. */
  691. public static function update_mail_sent($insert_id)
  692. {
  693. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  694. if ($insert_id != strval(intval($insert_id))) {
  695. return false;
  696. }
  697. $insert_id = intval($insert_id);
  698. $course_id = api_get_course_int_id();
  699. // store the modifications in the table tbl_annoucement
  700. $sql = "UPDATE $tbl_announcement SET email_sent='1'
  701. WHERE c_id = $course_id AND id = $insert_id";
  702. Database::query($sql);
  703. }
  704. /**
  705. * Gets all announcements from a user by course
  706. * @param string course db
  707. * @param int user id
  708. * @return array html with the content and count of announcements or false otherwise
  709. */
  710. public static function get_all_annoucement_by_user_course($course_code, $user_id)
  711. {
  712. $course_info = api_get_course_info($course_code);
  713. $course_id = $course_info['real_id'];
  714. if (empty($user_id)) {
  715. return false;
  716. }
  717. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  718. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  719. if (!empty($user_id) && is_numeric($user_id)) {
  720. $user_id = (int) $user_id;
  721. $sql = "SELECT DISTINCT announcement.title, announcement.content, display_order
  722. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  723. WHERE
  724. announcement.c_id = $course_id AND
  725. toolitemproperties.c_id = $course_id AND
  726. announcement.id = toolitemproperties.ref AND
  727. toolitemproperties.tool='announcement' AND
  728. (
  729. toolitemproperties.insert_user_id='$user_id' AND
  730. (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id IS NULL)
  731. )
  732. AND toolitemproperties.visibility='1'
  733. AND announcement.session_id = 0
  734. ORDER BY display_order DESC";
  735. $rs = Database::query($sql);
  736. $num_rows = Database::num_rows($rs);
  737. $content = '';
  738. $i = 0;
  739. $result = array();
  740. if ($num_rows > 0) {
  741. while ($myrow = Database::fetch_array($rs)) {
  742. $content.= '<strong>' . $myrow['title'] . '</strong><br /><br />';
  743. $content.= $myrow['content'];
  744. $i++;
  745. }
  746. $result['content'] = $content;
  747. $result['count'] = $i;
  748. return $result;
  749. }
  750. return false;
  751. }
  752. return false;
  753. }
  754. /**
  755. * Returns announcement info from its id
  756. *
  757. * @param int $course_id
  758. * @param int $annoucement_id
  759. * @return array
  760. */
  761. public static function get_by_id($course_id, $annoucement_id)
  762. {
  763. $annoucement_id = intval($annoucement_id);
  764. $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
  765. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  766. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  767. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  768. FROM $tbl_announcement announcement
  769. INNER JOIN $tbl_item_property toolitemproperties
  770. ON
  771. announcement.id = toolitemproperties.ref AND
  772. announcement.c_id = $course_id AND
  773. toolitemproperties.c_id = $course_id
  774. WHERE
  775. toolitemproperties.tool='announcement' AND
  776. announcement.id = $annoucement_id";
  777. $result = Database::query($sql);
  778. if (Database::num_rows($result)) {
  779. return Database::fetch_array($result);
  780. }
  781. return array();
  782. }
  783. /**
  784. * this function gets all the users of the course,
  785. * including users from linked courses
  786. * @deprecate use CourseManager class
  787. */
  788. public static function get_course_users()
  789. {
  790. //this would return only the users from real courses:
  791. $session_id = api_get_session_id();
  792. if ($session_id != 0) {
  793. $userList = CourseManager::get_real_and_linked_user_list(
  794. api_get_course_id(),
  795. true,
  796. $session_id,
  797. true
  798. );
  799. } else {
  800. $userList = CourseManager::get_real_and_linked_user_list(
  801. api_get_course_id(),
  802. false,
  803. 0,
  804. true
  805. );
  806. }
  807. return $userList;
  808. }
  809. /**
  810. * this function gets all the groups of the course,
  811. * not including linked courses
  812. */
  813. public static function get_course_groups()
  814. {
  815. $session_id = api_get_session_id();
  816. if ($session_id != 0) {
  817. $new_group_list = CourseManager::get_group_list_of_course(
  818. api_get_course_id(),
  819. $session_id,
  820. 1
  821. );
  822. } else {
  823. $new_group_list = CourseManager::get_group_list_of_course(
  824. api_get_course_id(),
  825. 0,
  826. 1
  827. );
  828. }
  829. return $new_group_list;
  830. }
  831. /**
  832. * This tools loads all the users and all the groups who have received
  833. * a specific item (in this case an announcement item)
  834. */
  835. public static function load_edit_users($tool, $id)
  836. {
  837. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  838. $tool = Database::escape_string($tool);
  839. $id = intval($id);
  840. $course_id = api_get_course_int_id();
  841. $sql = "SELECT * FROM $tbl_item_property
  842. WHERE c_id = $course_id AND tool='$tool' AND ref = $id";
  843. $result = Database::query($sql);
  844. $to = array();
  845. while ($row = Database::fetch_array($result)) {
  846. $to_group = $row['to_group_id'];
  847. switch ($to_group) {
  848. // it was send to one specific user
  849. case null:
  850. $to[] = "USER:" . $row['to_user_id'];
  851. break;
  852. // it was sent to everyone
  853. case 0:
  854. return "everyone";
  855. break;
  856. default:
  857. $to[] = "GROUP:" . $row['to_group_id'];
  858. }
  859. }
  860. return $to;
  861. }
  862. /**
  863. * constructs the form to display all the groups and users the message has been sent to
  864. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  865. * the first level is a distinction between groups and users:
  866. * $sent_to_array['groups'] * and $sent_to_array['users']
  867. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  868. * containing all the id's of the groups (resp. users) who have received this message.
  869. * @author Patrick Cool <patrick.cool@>
  870. */
  871. public static function sent_to_form($sent_to_array)
  872. {
  873. // we find all the names of the groups
  874. $group_names = self::get_course_groups();
  875. // we count the number of users and the number of groups
  876. if (isset($sent_to_array['users'])) {
  877. $number_users = count($sent_to_array['users']);
  878. } else {
  879. $number_users = 0;
  880. }
  881. if (isset($sent_to_array['groups'])) {
  882. $number_groups = count($sent_to_array['groups']);
  883. } else {
  884. $number_groups = 0;
  885. }
  886. $total_numbers = $number_users + $number_groups;
  887. // starting the form if there is more than one user/group
  888. $output = array();
  889. if ($total_numbers > 1) {
  890. // outputting the name of the groups
  891. if (is_array($sent_to_array['groups'])) {
  892. foreach ($sent_to_array['groups'] as $group_id) {
  893. $output[] = $group_names[$group_id]['name'];
  894. }
  895. }
  896. if (isset($sent_to_array['users'])) {
  897. if (is_array($sent_to_array['users'])) {
  898. foreach ($sent_to_array['users'] as $user_id) {
  899. $user_info = api_get_user_info($user_id);
  900. $output[] = $user_info['complete_name_with_username'];
  901. }
  902. }
  903. }
  904. } else {
  905. // there is only one user/group
  906. if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
  907. $user_info = api_get_user_info($sent_to_array['users'][0]);
  908. $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  909. }
  910. if (isset($sent_to_array['groups']) and
  911. is_array($sent_to_array['groups']) and
  912. isset($sent_to_array['groups'][0]) and
  913. $sent_to_array['groups'][0] !== 0
  914. ) {
  915. $group_id = $sent_to_array['groups'][0];
  916. $output[] = "&nbsp;" . $group_names[$group_id]['name'];
  917. }
  918. if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
  919. $output[] = "&nbsp;" . get_lang('Everybody');
  920. }
  921. }
  922. if (!empty($output)) {
  923. $output = array_filter($output);
  924. if (count($output) > 0) {
  925. $output = implode(', ', $output);
  926. }
  927. return $output;
  928. }
  929. }
  930. /**
  931. * Returns all the users and all the groups a specific announcement item
  932. * has been sent to
  933. * @param string The tool (announcement, agenda, ...)
  934. * @param int ID of the element of the corresponding type
  935. * @return array Array of users and groups to whom the element has been sent
  936. */
  937. public static function sent_to($tool, $id)
  938. {
  939. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  940. $tool = Database::escape_string($tool);
  941. $id = (int) $id;
  942. $sent_to_group = array();
  943. $sent_to = array();
  944. $course_id = api_get_course_int_id();
  945. $sql = "SELECT to_group_id, to_user_id
  946. FROM $tbl_item_property
  947. WHERE c_id = $course_id AND tool = '$tool' AND ref=" . $id;
  948. $result = Database::query($sql);
  949. while ($row = Database::fetch_array($result)) {
  950. // if to_user_id <> 0 then it is sent to a specific user
  951. if ($row['to_user_id'] <> 0) {
  952. $sent_to_user[] = $row['to_user_id'];
  953. continue;
  954. }
  955. // if to_group_id is null then it is sent to a specific user
  956. // if to_group_id = 0 then it is sent to everybody
  957. if ($row['to_group_id'] != 0) {
  958. $sent_to_group[] = $row['to_group_id'];
  959. }
  960. }
  961. if (isset($sent_to_group)) {
  962. $sent_to['groups'] = $sent_to_group;
  963. }
  964. if (isset($sent_to_user)) {
  965. $sent_to['users'] = $sent_to_user;
  966. }
  967. return $sent_to;
  968. }
  969. /**
  970. * Show a list with all the attachments according to the post's id
  971. * @param int $announcementId
  972. * @return array with the post info
  973. * @author Arthur Portugal
  974. * @version November 2009, dokeos 1.8.6.2
  975. */
  976. public static function get_attachment($announcementId)
  977. {
  978. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  979. $announcementId = intval($announcementId);
  980. $course_id = api_get_course_int_id();
  981. $row = array();
  982. $sql = 'SELECT id, path, filename, comment FROM ' . $tbl_announcement_attachment . '
  983. WHERE c_id = ' . $course_id . ' AND announcement_id = ' . $announcementId;
  984. $result = Database::query($sql);
  985. if (Database::num_rows($result) != 0) {
  986. $row = Database::fetch_array($result, 'ASSOC');
  987. }
  988. return $row;
  989. }
  990. /**
  991. * This function add a attachment file into announcement
  992. * @param int announcement id
  993. * @param string file comment
  994. * @param array uploaded file $_FILES
  995. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  996. */
  997. public static function add_announcement_attachment_file($announcement_id, $file_comment, $file)
  998. {
  999. $_course = api_get_course_info();
  1000. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1001. $return = 0;
  1002. $announcement_id = intval($announcement_id);
  1003. $course_id = api_get_course_int_id();
  1004. if (is_array($file) && $file['error'] == 0) {
  1005. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1006. $courseDir = $_course['path'] . '/upload/announcements';
  1007. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1008. $updir = $sys_course_path . $courseDir;
  1009. // Try to add an extension to the file if it hasn't one
  1010. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1011. // user's file name
  1012. $file_name = $file['name'];
  1013. if (!filter_extension($new_file_name)) {
  1014. $return = -1;
  1015. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1016. } else {
  1017. $new_file_name = uniqid('');
  1018. $new_path = $updir . '/' . $new_file_name;
  1019. // This file is copy here but its cleaned in api_mail_html in api.lib.php
  1020. copy($file['tmp_name'], $new_path);
  1021. $params = [
  1022. 'c_id' => $course_id,
  1023. 'filename' => $file_name,
  1024. 'comment' => $file_comment,
  1025. 'path' => $new_file_name,
  1026. 'announcement_id' => $announcement_id,
  1027. 'size' => intval($file['size']),
  1028. ];
  1029. $insertId = Database::insert($tbl_announcement_attachment, $params);
  1030. if ($insertId) {
  1031. $sql = "UPDATE $tbl_announcement_attachment SET id = iid WHERE iid = $insertId";
  1032. Database::query($sql);
  1033. }
  1034. $return = 1;
  1035. }
  1036. }
  1037. return $return;
  1038. }
  1039. /**
  1040. * This function edit a attachment file into announcement
  1041. * @param int attach id
  1042. * @param array uploaded file $_FILES
  1043. * @param string file comment
  1044. * @return int
  1045. */
  1046. public static function edit_announcement_attachment_file($id_attach, $file, $file_comment)
  1047. {
  1048. $_course = api_get_course_info();
  1049. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1050. $return = 0;
  1051. $course_id = api_get_course_int_id();
  1052. if (is_array($file) && $file['error'] == 0) {
  1053. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1054. $courseDir = $_course['path'] . '/upload/announcements';
  1055. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1056. $updir = $sys_course_path . $courseDir;
  1057. // Try to add an extension to the file if it hasn't one
  1058. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1059. // user's file name
  1060. $file_name = $file ['name'];
  1061. if (!filter_extension($new_file_name)) {
  1062. $return = -1;
  1063. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1064. } else {
  1065. $new_file_name = uniqid('');
  1066. $new_path = $updir . '/' . $new_file_name;
  1067. copy($file['tmp_name'], $new_path);
  1068. $safe_file_comment = Database::escape_string($file_comment);
  1069. $safe_file_name = Database::escape_string($file_name);
  1070. $safe_new_file_name = Database::escape_string($new_file_name);
  1071. $id_attach = intval($id_attach);
  1072. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='" . intval($file['size']) . "'
  1073. WHERE c_id = $course_id AND id = '$id_attach'";
  1074. $result = Database::query($sql);
  1075. if ($result === false) {
  1076. $return = -1;
  1077. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1078. } else {
  1079. $return = 1;
  1080. }
  1081. }
  1082. }
  1083. return $return;
  1084. }
  1085. /**
  1086. * This function delete a attachment file by id
  1087. * @param integer $id attachment file Id
  1088. *
  1089. */
  1090. public static function delete_announcement_attachment_file($id)
  1091. {
  1092. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1093. $id = intval($id);
  1094. $course_id = api_get_course_int_id();
  1095. $sql = "DELETE FROM $tbl_announcement_attachment
  1096. WHERE c_id = $course_id AND id = $id";
  1097. Database::query($sql);
  1098. }
  1099. /**
  1100. * @param int $id
  1101. * @param bool $sendToUsersInSession
  1102. * @param bool $sendToDrhUsers
  1103. */
  1104. public static function send_email(
  1105. $id,
  1106. $sendToUsersInSession = false,
  1107. $sendToDrhUsers = false
  1108. ) {
  1109. $email = AnnouncementEmail::create(null, $id);
  1110. $email->send($sendToUsersInSession, $sendToDrhUsers);
  1111. }
  1112. /**
  1113. * @param $stok
  1114. * @param $announcement_number
  1115. * @param bool $getCount
  1116. * @param null $start
  1117. * @param null $limit
  1118. * @param string $sidx
  1119. * @param string $sord
  1120. * @param string $titleToSearch
  1121. * @param int $userIdToSearch
  1122. * @param int $userId
  1123. * @param int $courseId
  1124. * @param int $sessionId
  1125. * @return array
  1126. */
  1127. public static function getAnnouncements(
  1128. $stok,
  1129. $announcement_number,
  1130. $getCount = false,
  1131. $start = null,
  1132. $limit = null,
  1133. $sidx = '',
  1134. $sord = '',
  1135. $titleToSearch = '',
  1136. $userIdToSearch = 0,
  1137. $userId = 0,
  1138. $courseId = 0,
  1139. $sessionId = 0
  1140. ) {
  1141. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1142. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1143. $user_id = $userId ?: api_get_user_id();
  1144. $group_id = api_get_group_id();
  1145. $session_id = $sessionId ?: api_get_session_id();
  1146. $condition_session = api_get_session_condition($session_id, true, true, 'announcement.session_id');
  1147. $course_id = $courseId ?: api_get_course_int_id();
  1148. $_course = api_get_course_info();
  1149. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  1150. $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement');
  1151. $select = ' DISTINCT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date';
  1152. if ($getCount) {
  1153. $select = ' COUNT(announcement.iid) count';
  1154. }
  1155. $searchCondition = '';
  1156. if (!empty($titleToSearch)) {
  1157. $titleToSearch = Database::escape_string($titleToSearch);
  1158. $searchCondition .= " AND (title LIKE '%$titleToSearch%')";
  1159. }
  1160. if (!empty($userIdToSearch)) {
  1161. $userIdToSearch = intval($userIdToSearch);
  1162. $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)";
  1163. }
  1164. if (api_is_allowed_to_edit(false, true) ||
  1165. ($allowUserEditSetting && !api_is_anonymous())
  1166. ) {
  1167. // A.1. you are a course admin with a USER filter
  1168. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  1169. //if (!empty($user_id)) {
  1170. if (0) {
  1171. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1172. $sql = "SELECT $select
  1173. FROM $tbl_announcement announcement, $tbl_item_property ip
  1174. WHERE
  1175. announcement.c_id = $course_id AND
  1176. ip.c_id = $course_id AND
  1177. announcement.id = ip.ref AND
  1178. ip.tool = 'announcement' AND
  1179. (
  1180. ip.to_user_id = $user_id OR
  1181. ip.to_group_id IS NULL OR
  1182. ip.to_group_id IN (0, ".implode(", ", $group_memberships).")
  1183. ) AND
  1184. ip.visibility IN ('1', '0')
  1185. $condition_session
  1186. $searchCondition
  1187. ORDER BY display_order DESC";
  1188. } else {
  1189. $sql = "SELECT $select
  1190. FROM $tbl_announcement announcement, $tbl_item_property ip
  1191. WHERE
  1192. announcement.c_id = $course_id AND
  1193. ip.c_id = $course_id AND
  1194. announcement.id = ip.ref AND
  1195. ip.tool ='announcement' AND
  1196. (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND
  1197. ip.visibility IN ('1', '0')
  1198. $condition_session
  1199. $searchCondition
  1200. ORDER BY display_order DESC";
  1201. }
  1202. } elseif ($group_id != 0) {
  1203. // A.2. you are a course admin with a GROUP filter
  1204. // => see only the messages of this specific group
  1205. $sql = "SELECT $select
  1206. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property ip
  1207. ON (announcement.id = ip.ref AND ip.tool='announcement')
  1208. WHERE
  1209. announcement.c_id = $course_id AND
  1210. ip.c_id = $course_id AND
  1211. ip.visibility<>'2' AND
  1212. (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1213. $condition_session
  1214. $searchCondition
  1215. ORDER BY display_order DESC";
  1216. //GROUP BY ip.ref
  1217. } else {
  1218. // A.3 you are a course admin without any group or user filter
  1219. // A.3.a you are a course admin without user or group filter but WITH studentview
  1220. // => see all the messages of all the users and groups without editing possibilities
  1221. if (isset($isStudentView) && $isStudentView == "true") {
  1222. $sql = "SELECT $select
  1223. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property ip
  1224. ON (announcement.id = ip.ref AND ip.tool='announcement')
  1225. WHERE
  1226. announcement.c_id = $course_id AND
  1227. ip.c_id = $course_id AND
  1228. ip.tool='announcement' AND
  1229. ip.visibility='1'
  1230. $condition_session
  1231. $searchCondition
  1232. ORDER BY display_order DESC";
  1233. //GROUP BY ip.ref
  1234. } else {
  1235. // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
  1236. // => see all the messages of all the users and groups with editing possibilities
  1237. $sql = "SELECT $select
  1238. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property ip
  1239. ON (announcement.id = ip.ref AND ip.tool='announcement')
  1240. WHERE
  1241. announcement.c_id = $course_id AND
  1242. ip.c_id = $course_id AND
  1243. (ip.visibility='0' or ip.visibility='1')
  1244. $condition_session
  1245. $searchCondition
  1246. ORDER BY display_order DESC";
  1247. //GROUP BY ip.ref
  1248. }
  1249. }
  1250. } else {
  1251. // STUDENT
  1252. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1253. if ($allowUserEditSetting && !api_is_anonymous()) {
  1254. if ($group_id == 0) {
  1255. // No group
  1256. $cond_user_id = " AND (
  1257. ip.lastedit_user_id = '".$user_id."' OR (
  1258. ip.to_user_id='".$user_id."' OR
  1259. (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1260. )
  1261. ) ";
  1262. } else {
  1263. $cond_user_id = " AND (
  1264. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")
  1265. )";
  1266. }
  1267. } else {
  1268. if ($group_id == 0) {
  1269. $cond_user_id = " AND (
  1270. ip.to_user_id = $user_id AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1271. ) ";
  1272. } else {
  1273. $cond_user_id = " AND (
  1274. ip.to_user_id = $user_id AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id."))
  1275. )";
  1276. }
  1277. }
  1278. $sql = "SELECT $select
  1279. FROM $tbl_announcement announcement,
  1280. $tbl_item_property ip
  1281. WHERE
  1282. announcement.c_id = $course_id AND
  1283. ip.c_id = $course_id AND
  1284. announcement.id = ip.ref
  1285. AND ip.tool='announcement'
  1286. $cond_user_id
  1287. $condition_session
  1288. $searchCondition
  1289. AND ip.visibility='1'
  1290. ORDER BY display_order DESC";
  1291. } else {
  1292. if ($user_id) {
  1293. if ($allowUserEditSetting && !api_is_anonymous()) {
  1294. $cond_user_id = " AND (
  1295. ip.lastedit_user_id = '".api_get_user_id()."' OR
  1296. (ip.to_user_id='".$user_id."' AND (ip.to_group_id='0' OR ip.to_group_id IS NULL))
  1297. ) ";
  1298. } else {
  1299. $cond_user_id = " AND (ip.to_user_id='".$user_id."' AND (ip.to_group_id='0' OR ip.to_group_id IS NULL) ) ";
  1300. }
  1301. $sql = "SELECT $select
  1302. FROM $tbl_announcement announcement, $tbl_item_property ip
  1303. WHERE
  1304. announcement.c_id = $course_id AND
  1305. ip.c_id = $course_id AND
  1306. announcement.id = ip.ref AND
  1307. ip.tool='announcement'
  1308. $cond_user_id
  1309. $condition_session
  1310. $searchCondition
  1311. AND ip.visibility='1'
  1312. AND announcement.session_id IN(0, ".$session_id.")
  1313. ORDER BY display_order DESC";
  1314. } else {
  1315. if (($allowUserEditSetting && !api_is_anonymous())) {
  1316. $cond_user_id = " AND (
  1317. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1318. )";
  1319. } else {
  1320. $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL ";
  1321. }
  1322. $sql = "SELECT $select
  1323. FROM $tbl_announcement announcement, $tbl_item_property ip
  1324. WHERE
  1325. announcement.c_id = $course_id AND
  1326. ip.c_id = $course_id AND
  1327. announcement.id = ip.ref AND
  1328. ip.tool='announcement'
  1329. $cond_user_id
  1330. $condition_session
  1331. $searchCondition
  1332. AND
  1333. ip.visibility='1' AND
  1334. announcement.session_id IN ( 0,".api_get_session_id().")";
  1335. }
  1336. }
  1337. }
  1338. if (!is_null($start) && !is_null($limit)) {
  1339. $start = intval($start);
  1340. $limit = intval($limit);
  1341. $sql .= " LIMIT $start, $limit";
  1342. }
  1343. $result = Database::query($sql);
  1344. if ($getCount) {
  1345. $result = Database::fetch_array($result, 'ASSOC');
  1346. return $result['count'];
  1347. }
  1348. $iterator = 1;
  1349. $bottomAnnouncement = $announcement_number;
  1350. $displayed = [];
  1351. $results = [];
  1352. $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq();
  1353. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  1354. if (!in_array($myrow['id'], $displayed)) {
  1355. $sent_to_icon = '';
  1356. // the email icon
  1357. if ($myrow['email_sent'] == '1') {
  1358. $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail'));
  1359. }
  1360. $groupReference = ($myrow['to_group_id'] > 0) ? ' <span class="label label-info">' . get_lang('Group') . '</span> ' : '' ;
  1361. $title = $myrow['title'] . $groupReference . $sent_to_icon;
  1362. $item_visibility = api_get_item_visibility($_course, TOOL_ANNOUNCEMENT, $myrow['id'], $session_id);
  1363. $myrow['visibility'] = $item_visibility;
  1364. // show attachment list
  1365. $attachment_list = AnnouncementManager::get_attachment($myrow['id']);
  1366. $attachment_icon = '';
  1367. if (count($attachment_list)>0) {
  1368. $attachment_icon = ' '.Display::return_icon('attachment.gif',get_lang('Attachment'));
  1369. }
  1370. /* TITLE */
  1371. $user_info = api_get_user_info($myrow['insert_user_id']);
  1372. $username = sprintf(get_lang("LoginX"), $user_info['username']);
  1373. $username_span = Display::tag('span', api_get_person_name($user_info['firstName'], $user_info['lastName']), array('title'=>$username));
  1374. $title = Display::url($title.$attachment_icon, $actionUrl.'&action=view&id='.$myrow['id']);
  1375. //$html .= Display::tag('td', $username_span, array('class' => 'announcements-list-line-by-user'));
  1376. //$html .= Display::tag('td', api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG), array('class' => 'announcements-list-line-datetime'));
  1377. $modify_icons = '';
  1378. // we can edit if : we are the teacher OR the element belongs to
  1379. // the session we are coaching OR the option to allow users to edit is on
  1380. if (api_is_allowed_to_edit(false, true) ||
  1381. (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  1382. || (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  1383. ) {
  1384. $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$myrow['id']."\">".
  1385. Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL)."</a>";
  1386. if ($myrow['visibility']==1) {
  1387. $image_visibility="visible";
  1388. $alt_visibility=get_lang('Hide');
  1389. } else {
  1390. $image_visibility="invisible";
  1391. $alt_visibility=get_lang('Visible');
  1392. }
  1393. $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">".
  1394. Display::return_icon($image_visibility.'.png', $alt_visibility,'',ICON_SIZE_SMALL)."</a>";
  1395. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  1396. if ($iterator != 1) {
  1397. $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$myrow["id"]."&sec_token=".$stok."\">".
  1398. Display::return_icon('up.gif', get_lang('Up'))."</a>";
  1399. } else {
  1400. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  1401. }
  1402. if ($iterator < $bottomAnnouncement) {
  1403. $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$myrow["id"]."&sec_token=".$stok."\">".
  1404. Display::return_icon('down.gif', get_lang('Down'))."</a>";
  1405. } else {
  1406. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  1407. }
  1408. if (api_is_allowed_to_edit(false,true)) {
  1409. $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$myrow['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,api_get_system_encoding()))."')) return false;\">".
  1410. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).
  1411. "</a>";
  1412. }
  1413. $iterator ++;
  1414. } else {
  1415. $modify_icons = Display::url(
  1416. Display::return_icon('default.png'),
  1417. $actionUrl.'&action=view&id='.$myrow['id']
  1418. );
  1419. }
  1420. $announcement = [
  1421. 'id' => $myrow["id"],
  1422. 'title' => $title,
  1423. 'username' => $username_span,
  1424. 'insert_date' => api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG),
  1425. 'actions' => $modify_icons
  1426. ];
  1427. $results[] = $announcement;
  1428. }
  1429. $displayed[] = $myrow['id'];
  1430. }
  1431. return $results;
  1432. }
  1433. /**
  1434. * @return int
  1435. */
  1436. public static function getNumberAnnouncements()
  1437. {
  1438. // Maximum title messages to display
  1439. $maximum = '12';
  1440. // Database Table Definitions
  1441. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1442. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1443. $session_id = api_get_session_id();
  1444. $_course = api_get_course_info();
  1445. $course_id = $_course['real_id'];
  1446. $userId = api_get_user_id();
  1447. $condition_session = api_get_session_condition($session_id, true, true, 'announcement.session_id');
  1448. if (api_is_allowed_to_edit(false,true)) {
  1449. // check teacher status
  1450. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1451. if (api_get_group_id() == 0) {
  1452. $group_condition = '';
  1453. } else {
  1454. $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0 OR ip.to_group_id IS NULL)";
  1455. }
  1456. $sql = "SELECT
  1457. announcement.*,
  1458. ip.visibility,
  1459. ip.to_group_id,
  1460. ip.insert_user_id
  1461. FROM $tbl_announcement announcement INNER JOIN $tbl_item_property ip
  1462. ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
  1463. WHERE
  1464. announcement.c_id = $course_id AND
  1465. ip.c_id = $course_id AND
  1466. ip.tool = 'announcement' AND
  1467. ip.visibility <> '2'
  1468. $group_condition
  1469. $condition_session
  1470. GROUP BY ip.ref
  1471. ORDER BY display_order DESC
  1472. LIMIT 0, $maximum";
  1473. }
  1474. } else {
  1475. // students only get to see the visible announcements
  1476. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1477. $group_memberships = GroupManager::get_group_ids($_course['real_id'], $userId);
  1478. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1479. if (api_get_group_id() == 0) {
  1480. $cond_user_id = " AND (
  1481. ip.lastedit_user_id = '".$userId."' OR (
  1482. ip.to_user_id='".$userId."' OR
  1483. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1484. ip.to_group_id IS NULL
  1485. )
  1486. )
  1487. ";
  1488. } else {
  1489. $cond_user_id = " AND (
  1490. ip.lastedit_user_id = '".$userId."'OR
  1491. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1492. ip.to_group_id IS NULL
  1493. )";
  1494. }
  1495. } else {
  1496. if (api_get_group_id() == 0) {
  1497. $cond_user_id = " AND (
  1498. ip.to_user_id='".$userId."' OR
  1499. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1500. ip.to_group_id IS NULL
  1501. ) ";
  1502. } else {
  1503. $cond_user_id = " AND (
  1504. ip.to_user_id='".$userId."' OR
  1505. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1506. ip.to_group_id IS NULL
  1507. ) ";
  1508. }
  1509. }
  1510. // the user is member of several groups => display personal announcements AND
  1511. // his group announcements AND the general announcements
  1512. if (is_array($group_memberships) && count($group_memberships)>0) {
  1513. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1514. FROM $tbl_announcement announcement
  1515. INNER JOIN $tbl_item_property ip
  1516. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1517. WHERE
  1518. announcement.c_id = $course_id AND
  1519. ip.c_id = $course_id AND
  1520. ip.tool='announcement' AND
  1521. ip.visibility='1'
  1522. $cond_user_id
  1523. $condition_session
  1524. GROUP BY ip.ref
  1525. ORDER BY display_order DESC
  1526. LIMIT 0, $maximum";
  1527. } else {
  1528. // the user is not member of any group
  1529. // this is an identified user => show the general announcements AND his personal announcements
  1530. if ($userId) {
  1531. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1532. $cond_user_id = " AND (
  1533. ip.lastedit_user_id = '".$userId."' OR
  1534. ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1535. ) ";
  1536. } else {
  1537. $cond_user_id = " AND ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
  1538. }
  1539. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1540. FROM $tbl_announcement announcement
  1541. INNER JOIN $tbl_item_property ip
  1542. ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
  1543. WHERE
  1544. announcement.c_id = $course_id AND
  1545. ip.c_id = $course_id AND
  1546. ip.tool='announcement' AND
  1547. ip.visibility='1'
  1548. $cond_user_id
  1549. $condition_session
  1550. GROUP BY ip.ref
  1551. ORDER BY display_order DESC
  1552. LIMIT 0, $maximum";
  1553. } else {
  1554. if (api_get_course_setting('allow_user_edit_announcement')) {
  1555. $cond_user_id = " AND (
  1556. ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1557. ) ";
  1558. } else {
  1559. $cond_user_id = " AND ip.to_group_id='0' ";
  1560. }
  1561. // the user is not identiefied => show only the general announcements
  1562. $sql = "SELECT
  1563. announcement.*,
  1564. ip.visibility,
  1565. ip.to_group_id,
  1566. ip.insert_user_id
  1567. FROM $tbl_announcement announcement
  1568. INNER JOIN $tbl_item_property ip
  1569. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1570. WHERE
  1571. announcement.c_id = $course_id AND
  1572. ip.c_id = $course_id AND
  1573. ip.tool='announcement' AND
  1574. ip.visibility='1' AND
  1575. ip.to_group_id='0'
  1576. $condition_session
  1577. GROUP BY ip.ref
  1578. ORDER BY display_order DESC
  1579. LIMIT 0, $maximum";
  1580. }
  1581. }
  1582. }
  1583. }
  1584. $result = Database::query($sql);
  1585. return Database::num_rows($result);
  1586. }
  1587. }