AnnouncementManager.php 81 KB

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