AnnouncementManager.php 71 KB

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