AnnouncementManager.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  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. }