AnnouncementManager.php 77 KB

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