123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CourseBundle\Entity\CAnnouncement;
- use Chamilo\CourseBundle\Entity\CItemProperty;
- /**
- * Include file with functions for the announcements module.
- * @author jmontoya
- * @package chamilo.announcements
- * @todo use OOP
- */
- class AnnouncementManager
- {
- /**
- * Constructor
- */
- public function __construct()
- {
- }
- /**
- * @return array
- */
- public static function getTags()
- {
- $tags = [
- '((user_name))',
- '((user_firstname))',
- '((user_lastname))',
- '((user_official_code))',
- '((course_title))',
- '((course_link))'
- ];
- $tags[] = '((teachers))';
- if (!empty(api_get_session_id())) {
- $tags[] = '((coaches))';
- $tags[] = '((general_coach))';
- $tags[] = '((general_coach_email))';
- }
- return $tags;
- }
- /**
- * @param int $userId
- * @param string $content
- * @param string $courseCode
- * @param int $sessionId
- *
- * @return string
- */
- public static function parse_content(
- $userId,
- $content,
- $courseCode,
- $sessionId = 0
- ) {
- $readerInfo = api_get_user_info($userId);
- $courseInfo = api_get_course_info($courseCode);
- $teacherList = CourseManager::getTeacherListFromCourseCodeToString(
- $courseInfo['code']
- );
- $generalCoachName = '';
- $generalCoachEmail = '';
- $coaches = '';
- if (!empty($sessionId)) {
- $sessionInfo = api_get_session_info($sessionId);
- $coaches = CourseManager::get_coachs_from_course_to_string(
- $sessionId,
- $courseInfo['real_id']
- );
- $generalCoach = api_get_user_info($sessionInfo['id_coach']);
- $generalCoachName = $generalCoach['complete_name'];
- $generalCoachEmail = $generalCoach['email'];
- }
- $data = [];
- $data['user_name'] = '';
- $data['user_firstname'] = '';
- $data['user_lastname'] = '';
- $data['user_official_code'] = '';
- if (!empty($readerInfo)) {
- $data['user_name'] = $readerInfo['username'];
- $data['user_firstname'] = $readerInfo['firstname'];
- $data['user_lastname'] = $readerInfo['lastname'];
- $data['user_official_code'] = $readerInfo['official_code'];
- }
- $data['course_title'] = $courseInfo['name'];
- $courseLink = api_get_course_url($courseCode, $sessionId);
- $data['course_link'] = Display::url($courseLink, $courseLink);
- $data['teachers'] = $teacherList;
- if (!empty(api_get_session_id())) {
- $data['coaches'] = $coaches;
- $data['general_coach'] = $generalCoachName;
- $data['general_coach_email'] = $generalCoachEmail;
- }
- $content = str_replace(self::getTags(), $data, $content);
- return $content;
- }
- /**
- * Gets all announcements from a course
- * @param array $course_info
- * @param int $session_id
- * @return array html with the content and count of announcements or false otherwise
- */
- public static function get_all_annoucement_by_course($course_info, $session_id = 0)
- {
- $session_id = intval($session_id);
- $courseId = $course_info['real_id'];
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $sql = "SELECT DISTINCT
- announcement.id,
- announcement.title,
- announcement.content
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property i
- ON (announcement.id = i.ref AND announcement.c_id = i.c_id)
- WHERE
- i.tool='announcement' AND
- announcement.session_id = '$session_id' AND
- announcement.c_id = $courseId AND
- i.c_id = $courseId
- ORDER BY display_order DESC";
- $rs = Database::query($sql);
- $num_rows = Database::num_rows($rs);
- if ($num_rows > 0) {
- $list = [];
- while ($row = Database::fetch_array($rs)) {
- $list[] = $row;
- }
- return $list;
- }
- return false;
- }
- /**
- * This functions switches the visibility a course resource
- * using the visibility field in 'item_property'
- * @param array $courseInfo
- * @param int $id ID of the element of the corresponding type
- * @return bool False on failure, True on success
- */
- public static function change_visibility_announcement($courseInfo, $id)
- {
- $session_id = api_get_session_id();
- $item_visibility = api_get_item_visibility(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- $session_id
- );
- if ($item_visibility == '1') {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'invisible',
- api_get_user_id()
- );
- } else {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'visible',
- api_get_user_id()
- );
- }
- return true;
- }
- /**
- * Deletes an announcement
- * @param array $courseInfo the course array
- * @param int $id the announcement id
- */
- public static function delete_announcement($courseInfo, $id)
- {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'delete',
- api_get_user_id()
- );
- }
- /**
- * Deletes all announcements by course
- * @param array $courseInfo the course array
- */
- public static function delete_all_announcements($courseInfo)
- {
- $announcements = self::get_all_annoucement_by_course(
- $courseInfo,
- api_get_session_id()
- );
- if (!empty($announcements)) {
- foreach ($announcements as $annon) {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $annon['id'],
- 'delete',
- api_get_user_id()
- );
- }
- }
- }
- /**
- * @param string $title
- * @param int $courseId
- * @param int $sessionId
- * @param int $visibility 1 or 0
- *
- * @return mixed
- */
- public static function getAnnouncementsByTitle(
- $title,
- $courseId,
- $sessionId = 0,
- $visibility = 1
- ) {
- $dql = "SELECT a
- FROM ChamiloCourseBundle:CAnnouncement a
- JOIN ChamiloCourseBundle:CItemProperty ip
- WITH a.id = ip.ref AND a.cId = ip.course
- WHERE
- ip.tool = 'announcement' AND
- a.cId = :course AND
- a.sessionId = :session AND
- a.title like :title AND
- ip.visibility = :visibility
- ORDER BY a.displayOrder DESC";
- $qb = Database::getManager()->createQuery($dql);
- $result = $qb->execute(
- [
- 'course' => $courseId,
- 'session' => $sessionId,
- 'visibility' => $visibility,
- 'title' => "%$title%",
- ]
- );
- return $result;
- }
- /**
- * @param int $announcementId
- * @param int $courseId
- * @param int $userId
- *
- * @return array
- */
- public static function getAnnouncementInfoById(
- $announcementId,
- $courseId,
- $userId
- ) {
- if (api_is_allowed_to_edit(false, true) ||
- (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
- ) {
- $dql = "SELECT a, ip
- FROM ChamiloCourseBundle:CAnnouncement a
- JOIN ChamiloCourseBundle:CItemProperty ip
- WITH a.id = ip.ref AND a.cId = ip.course
- WHERE
- a.id = :announcement AND
- ip.tool = 'announcement' AND
- a.cId = :course
- ORDER BY a.displayOrder DESC";
- } else {
- $group_list = GroupManager::get_group_ids($courseId, api_get_user_id());
- if (empty($group_list)) {
- $group_list[] = 0;
- }
- if (api_get_user_id() != 0) {
- $dql = "SELECT a, ip
- FROM ChamiloCourseBundle:CAnnouncement a
- JOIN ChamiloCourseBundle:CItemProperty ip
- WITH a.id = ip.ref AND a.cId = ip.course
- WHERE
- a.id = :announcement AND
- ip.tool='announcement' AND
- (
- ip.toUser = $userId OR
- ip.group IN ('0', '".implode("', '", $group_list)."') OR
- ip.group IS NULL
- ) AND
- ip.visibility = '1' AND
- ip.course = :course
- ORDER BY a.displayOrder DESC";
- } else {
- $dql = "SELECT a, ip
- FROM ChamiloCourseBundle:CAnnouncement a
- JOIN ChamiloCourseBundle:CItemProperty ip
- WITH a.id = ip.ref AND a.cId = ip.course
- WHERE
- a.id = :announcement AND
- ip.tool = 'announcement' AND
- (ip.group = '0' OR ip.group IS NULL) AND
- ip.visibility = '1' AND
- ip.course = :course";
- }
- }
- $qb = Database::getManager()->createQuery($dql);
- $result = $qb->execute(
- [
- 'announcement' => $announcementId,
- 'course' => $courseId,
- ]
- );
- return [
- 'announcement' => $result[0],
- 'item_property' => $result[1]
- ];
- }
- /**
- * Displays one specific announcement
- * @param int $id, the id of the announcement you want to display
- *
- * @return string
- */
- public static function displayAnnouncement($id)
- {
- if ($id != strval(intval($id))) {
- return null;
- }
- global $charset;
- $html = '';
- $result = self::getAnnouncementInfoById(
- $id,
- api_get_course_int_id(),
- api_get_user_id()
- );
- /** @var CAnnouncement $announcement */
- $announcement = $result['announcement'];
- /** @var CItemProperty $itemProperty */
- $itemProperty = $result['item_property'];
- if (empty($announcement) || empty($itemProperty)) {
- return '';
- }
- $title = $announcement->getTitle();
- $content = $announcement->getContent();
- $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">";
- $html .= "<tr><td><h2>".$title."</h2></td></tr>";
- if (api_is_allowed_to_edit(false, true) ||
- (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
- ) {
- $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$id."\">".
- Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>";
- if ($itemProperty->getVisibility() === 1) {
- $image_visibility = 'visible';
- $alt_visibility = get_lang('Hide');
- } else {
- $image_visibility = 'invisible';
- $alt_visibility = get_lang('Visible');
- }
- global $stok;
- $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=showhide&id=".$id."&sec_token=".$stok."\">".
- Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>";
- if (api_is_allowed_to_edit(false, true)) {
- $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;\">".
- Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).
- "</a>";
- }
- $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>";
- }
- $toUser = $itemProperty->getToUser();
- $toUserId = !empty($toUser) ? $toUser->getId() : 0;
- $content = self::parse_content(
- $toUserId,
- $content,
- api_get_course_id(),
- api_get_session_id()
- );
- $html .= "<tr><td>$content</td></tr>";
- $html .= "<tr>";
- $html .= "<td class=\"announcements_datum\">".get_lang('LastUpdateDate')." : ";
- $lastEdit = $itemProperty->getLasteditDate();
- $html .= Display::dateToStringAgoAndLongDate($lastEdit);
- $html .= "</td></tr>";
- if (api_is_allowed_to_edit(false, true)) {
- $sent_to = self::sent_to('announcement', $id);
- $sent_to_form = self::sent_to_form($sent_to);
- $html .= Display::tag(
- 'td',
- get_lang('SentTo').': '.$sent_to_form,
- ['class' => 'announcements_datum']
- );
- }
- $attachment_list = self::get_attachment($id);
- if (count($attachment_list) > 0) {
- $html .= "<tr><td>";
- $realname = $attachment_list['path'];
- $user_filename = $attachment_list['filename'];
- $full_file_name = 'download.php?'.api_get_cidreq().'&file='.$realname;
- $html .= '<br/>';
- $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
- $html .= '<a href="'.$full_file_name.' "> '.$user_filename.' </a>';
- $html .= ' - <span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
- if (api_is_allowed_to_edit(false, true)) {
- $html .= Display::url(
- Display::return_icon(
- 'delete.png',
- get_lang('Delete'),
- '',
- 16
- ),
- api_get_self()."?".api_get_cidreq()."&action=delete_attachment&id_attach=".$attachment_list['id']."&sec_token=".$stok
- );
- }
- $html .= '</td></tr>';
- }
- $html .= "</table>";
- return $html;
- }
- /**
- * @param array $courseInfo
- *
- * @return int
- */
- public static function get_last_announcement_order($courseInfo)
- {
- if (empty($courseInfo)) {
- return 0;
- }
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $courseId = $courseInfo['real_id'];
- $sql = "SELECT MAX(display_order)
- FROM $tbl_announcement
- WHERE c_id = $courseId ";
- $res_max = Database::query($sql);
- $order = 0;
- if (Database::num_rows($res_max)) {
- $row_max = Database::fetch_array($res_max);
- $order = intval($row_max[0]) + 1;
- }
- return $order;
- }
- /**
- * Store an announcement in the database (including its attached file if any)
- * @param array $courseInfo
- * @param int $sessionId
- * @param string $title Announcement title (pure text)
- * @param string $newContent Content of the announcement (can be HTML)
- * @param array $sentTo Array of users and groups to send the announcement to
- * @param array $file uploaded file $_FILES
- * @param string $file_comment Comment describing the attachment
- * @param string $end_date
- * @param bool $sendToUsersInSession
- * @param int $authorId
- *
- * @return int false on failure, ID of the announcement on success
- */
- public static function add_announcement(
- $courseInfo,
- $sessionId,
- $title,
- $newContent,
- $sentTo,
- $file = [],
- $file_comment = null,
- $end_date = null,
- $sendToUsersInSession = false,
- $authorId = 0
- ) {
- if (empty($courseInfo)) {
- return false;
- }
- $courseId = $courseInfo['real_id'];
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $authorId = empty($authorId) ? api_get_user_id() : $authorId;
- if (empty($end_date)) {
- $end_date = api_get_utc_datetime();
- }
- $order = self::get_last_announcement_order($courseInfo);
- // store in the table announcement
- $params = [
- 'c_id' => $courseId,
- 'content' => $newContent,
- 'title' => $title,
- 'end_date' => $end_date,
- 'display_order' => $order,
- 'session_id' => (int) $sessionId
- ];
- $last_id = Database::insert($tbl_announcement, $params);
- if (empty($last_id)) {
- return false;
- } else {
- $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
- Database::query($sql);
- if (!empty($file)) {
- self::add_announcement_attachment_file(
- $last_id,
- $file_comment,
- $_FILES['user_upload']
- );
- }
- // store in item_property (first the groups, then the users
- if (empty($sentTo) ||
- (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone')
- ) {
- // The message is sent to EVERYONE, so we set the group to 0
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $last_id,
- 'AnnouncementAdded',
- $authorId,
- '0',
- null,
- null,
- null,
- $sessionId
- );
- } else {
- $send_to = CourseManager::separateUsersGroups($sentTo);
- $batchSize = 20;
- $em = Database::getManager();
- // Storing the selected groups
- if (is_array($send_to['groups']) &&
- !empty($send_to['groups'])
- ) {
- $counter = 1;
- foreach ($send_to['groups'] as $group) {
- $groupInfo = GroupManager::get_group_properties($group);
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $last_id,
- 'AnnouncementAdded',
- $authorId,
- $groupInfo
- );
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear();
- }
- $counter++;
- }
- }
- // Storing the selected users
- if (is_array($send_to['users'])) {
- $counter = 1;
- foreach ($send_to['users'] as $user) {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $last_id,
- 'AnnouncementAdded',
- $authorId,
- '',
- $user
- );
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear();
- }
- $counter++;
- }
- }
- }
- if ($sendToUsersInSession) {
- self::addAnnouncementToAllUsersInSessions($last_id);
- }
- return $last_id;
- }
- }
- /**
- * @param $title
- * @param $newContent
- * @param $to
- * @param $to_users
- * @param array $file
- * @param string $file_comment
- * @param bool $sendToUsersInSession
- *
- * @return bool|int
- */
- public static function add_group_announcement(
- $title,
- $newContent,
- $to,
- $to_users,
- $file = [],
- $file_comment = '',
- $sendToUsersInSession = false
- ) {
- $courseInfo = api_get_course_info();
- // Database definitions
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $order = self::get_last_announcement_order($courseInfo);
- $now = api_get_utc_datetime();
- $courseId = api_get_course_int_id();
- // store in the table announcement
- $params = [
- 'c_id' => $courseId,
- 'content' => $newContent,
- 'title' => $title,
- 'end_date' => $now,
- 'display_order' => $order,
- 'session_id' => api_get_session_id()
- ];
- $last_id = Database::insert($tbl_announcement, $params);
- // Store the attach file
- if ($last_id) {
- $sql = "UPDATE $tbl_announcement SET id = iid
- WHERE iid = $last_id";
- Database::query($sql);
- if (!empty($file)) {
- self::add_announcement_attachment_file(
- $last_id,
- $file_comment,
- $file
- );
- }
- // Store in item_property (first the groups, then the users)
- //if (!isset($to_users)) {
- if (isset($to_users[0]) && $to_users[0] === 'everyone') {
- // when no user is selected we send it to everyone
- $send_to = CourseManager::separateUsersGroups($to);
- // storing the selected groups
- if (is_array($send_to['groups'])) {
- foreach ($send_to['groups'] as $group) {
- $groupInfo = GroupManager::get_group_properties($group);
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $last_id,
- 'AnnouncementAdded',
- api_get_user_id(),
- $groupInfo
- );
- }
- }
- } else {
- $send_to_groups = CourseManager::separateUsersGroups($to);
- $send_to_users = CourseManager::separateUsersGroups($to_users);
- $to_groups = $send_to_groups['groups'];
- $to_users = $send_to_users['users'];
- // storing the selected users
- if (is_array($to_users) && is_array($to_groups)) {
- foreach ($to_groups as $group) {
- $groupInfo = GroupManager::get_group_properties($group);
- foreach ($to_users as $user) {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $last_id,
- 'AnnouncementAdded',
- api_get_user_id(),
- $groupInfo,
- $user
- );
- }
- }
- }
- }
- if ($sendToUsersInSession) {
- self::addAnnouncementToAllUsersInSessions($last_id);
- }
- }
- return $last_id;
- }
- /**
- * This function stores the announcement item in the announcement table
- * and updates the item_property table
- *
- * @param int $id id of the announcement
- * @param string $title
- * @param string $newContent
- * @param array $to users that will receive the announcement
- * @param mixed $file attachment
- * @param string $file_comment file comment
- * @param bool $sendToUsersInSession
- */
- public static function edit_announcement(
- $id,
- $title,
- $newContent,
- $to,
- $file = [],
- $file_comment = '',
- $sendToUsersInSession = false
- ) {
- $courseInfo = api_get_course_info();
- $courseId = api_get_course_int_id();
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $id = intval($id);
- $params = [
- 'title' => $title,
- 'content' => $newContent
- ];
- Database::update(
- $tbl_announcement,
- $params,
- ['c_id = ? AND id = ?' => [$courseId, $id]]
- );
- // save attachment file
- $row_attach = self::get_attachment($id);
- $id_attach = 0;
- if ($row_attach) {
- $id_attach = intval($row_attach['id']);
- }
- if (!empty($file)) {
- if (empty($id_attach)) {
- self::add_announcement_attachment_file(
- $id,
- $file_comment,
- $file
- );
- } else {
- self::edit_announcement_attachment_file(
- $id_attach,
- $file,
- $file_comment
- );
- }
- }
- // We remove everything from item_property for this
- $sql = "DELETE FROM $tbl_item_property
- WHERE c_id = $courseId AND ref='$id' AND tool='announcement'";
- Database::query($sql);
- if ($sendToUsersInSession) {
- self::addAnnouncementToAllUsersInSessions($id);
- }
- // store in item_property (first the groups, then the users
- if (!is_null($to)) {
- // !is_null($to): when no user is selected we send it to everyone
- $send_to = CourseManager::separateUsersGroups($to);
- // storing the selected groups
- if (is_array($send_to['groups'])) {
- foreach ($send_to['groups'] as $group) {
- $groupInfo = GroupManager::get_group_properties($group);
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'AnnouncementUpdated',
- api_get_user_id(),
- $groupInfo
- );
- }
- }
- // storing the selected users
- if (is_array($send_to['users'])) {
- foreach ($send_to['users'] as $user) {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'AnnouncementUpdated',
- api_get_user_id(),
- 0,
- $user
- );
- }
- }
- // Send to everyone
- if (isset($to[0]) && $to[0] === 'everyone') {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'AnnouncementUpdated',
- api_get_user_id(),
- 0
- );
- }
- } else {
- // the message is sent to everyone, so we set the group to 0
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $id,
- 'AnnouncementUpdated',
- api_get_user_id(),
- 0
- );
- }
- }
- /**
- * @param int $announcementId
- */
- public static function addAnnouncementToAllUsersInSessions($announcementId)
- {
- $courseCode = api_get_course_id();
- $courseInfo = api_get_course_info();
- $sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
- if (!empty($sessionList)) {
- foreach ($sessionList as $sessionInfo) {
- $sessionId = $sessionInfo['id'];
- $userList = CourseManager::get_user_list_from_course_code(
- $courseCode,
- $sessionId
- );
- if (!empty($userList)) {
- foreach ($userList as $user) {
- api_item_property_update(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $announcementId,
- "AnnouncementUpdated",
- api_get_user_id(),
- 0,
- $user['user_id'],
- 0,
- 0,
- $sessionId
- );
- }
- }
- }
- }
- }
- /**
- * @param int $insert_id
- * @return bool
- */
- public static function update_mail_sent($insert_id)
- {
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- if ($insert_id != strval(intval($insert_id))) {
- return false;
- }
- $insert_id = intval($insert_id);
- $courseId = api_get_course_int_id();
- // store the modifications in the table tbl_annoucement
- $sql = "UPDATE $tbl_announcement SET email_sent='1'
- WHERE c_id = $courseId AND id = $insert_id";
- Database::query($sql);
- }
- /**
- * Gets all announcements from a user by course
- * @param string course db
- * @param int user id
- * @return array html with the content and count of announcements or false otherwise
- */
- public static function get_all_annoucement_by_user_course($course_code, $user_id)
- {
- $course_info = api_get_course_info($course_code);
- $courseId = $course_info['real_id'];
- if (empty($user_id)) {
- return false;
- }
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- if (!empty($user_id) && is_numeric($user_id)) {
- $user_id = (int) $user_id;
- $sql = "SELECT DISTINCT
- announcement.title,
- announcement.content,
- display_order
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement' AND
- (
- ip.insert_user_id='$user_id' AND
- (ip.to_group_id='0' OR ip.to_group_id IS NULL)
- )
- AND ip.visibility='1'
- AND announcement.session_id = 0
- ORDER BY display_order DESC";
- $rs = Database::query($sql);
- $num_rows = Database::num_rows($rs);
- $content = '';
- $i = 0;
- $result = [];
- if ($num_rows > 0) {
- while ($myrow = Database::fetch_array($rs)) {
- $content .= '<strong>'.$myrow['title'].'</strong><br /><br />';
- $content .= $myrow['content'];
- $i++;
- }
- $result['content'] = $content;
- $result['count'] = $i;
- return $result;
- }
- return false;
- }
- return false;
- }
- /**
- * Returns announcement info from its id
- *
- * @param int $courseId
- * @param int $id
- * @return array
- */
- public static function get_by_id($courseId, $id)
- {
- $id = intval($id);
- $courseId = $courseId ? intval($courseId) : api_get_course_int_id();
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $sql = "SELECT DISTINCT
- announcement.id,
- announcement.title,
- announcement.content,
- ip.to_group_id
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON
- announcement.id = ip.ref AND
- announcement.c_id = ip.c_id
- WHERE
- announcement.c_id = $courseId AND
- ip.tool='announcement' AND
- announcement.id = $id
- ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- return Database::fetch_array($result);
- }
- return [];
- }
- /**
- * this function gets all the groups of the course,
- * not including linked courses
- */
- public static function get_course_groups()
- {
- $session_id = api_get_session_id();
- if ($session_id != 0) {
- $new_group_list = CourseManager::get_group_list_of_course(
- api_get_course_id(),
- $session_id,
- 1
- );
- } else {
- $new_group_list = CourseManager::get_group_list_of_course(
- api_get_course_id(),
- 0,
- 1
- );
- }
- return $new_group_list;
- }
- /**
- * This tools loads all the users and all the groups who have received
- * a specific item (in this case an announcement item)
- * @param string $tool
- * @param int $id
- * @return array
- */
- public static function load_edit_users($tool, $id)
- {
- $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $tool = Database::escape_string($tool);
- $id = intval($id);
- $courseId = api_get_course_int_id();
- $sql = "SELECT * FROM $table
- WHERE c_id = $courseId AND tool='$tool' AND ref = $id";
- $result = Database::query($sql);
- $to = [];
- while ($row = Database::fetch_array($result)) {
- $to_group = $row['to_group_id'];
- switch ($to_group) {
- // it was send to one specific user
- case null:
- $to[] = "USER:".$row['to_user_id'];
- break;
- // it was sent to everyone
- case 0:
- return "everyone";
- break;
- default:
- $to[] = "GROUP:".$row['to_group_id'];
- }
- }
- return $to;
- }
- /**
- * constructs the form to display all the groups and users the message has been sent to
- * @param array $sent_to_array
- * input:
- * $sent_to_array is a 2 dimensional array containing the groups and the users
- * the first level is a distinction between groups and users:
- * $sent_to_array['groups'] * and $sent_to_array['users']
- * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
- * containing all the id's of the groups (resp. users) who have received this message.
- * @return string
- * @author Patrick Cool <patrick.cool@>
- */
- public static function sent_to_form($sent_to_array)
- {
- // we find all the names of the groups
- $group_names = self::get_course_groups();
- // we count the number of users and the number of groups
- if (isset($sent_to_array['users'])) {
- $number_users = count($sent_to_array['users']);
- } else {
- $number_users = 0;
- }
- if (isset($sent_to_array['groups'])) {
- $number_groups = count($sent_to_array['groups']);
- } else {
- $number_groups = 0;
- }
- $total_numbers = $number_users + $number_groups;
- // starting the form if there is more than one user/group
- $output = [];
- if ($total_numbers > 1) {
- // outputting the name of the groups
- if (is_array($sent_to_array['groups'])) {
- foreach ($sent_to_array['groups'] as $group_id) {
- $output[] = $group_names[$group_id]['name'];
- }
- }
- if (isset($sent_to_array['users'])) {
- if (is_array($sent_to_array['users'])) {
- foreach ($sent_to_array['users'] as $user_id) {
- $user_info = api_get_user_info($user_id);
- $output[] = $user_info['complete_name_with_username'];
- }
- }
- }
- } else {
- // there is only one user/group
- if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
- $user_info = api_get_user_info($sent_to_array['users'][0]);
- $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
- }
- if (isset($sent_to_array['groups']) and
- is_array($sent_to_array['groups']) and
- isset($sent_to_array['groups'][0]) and
- $sent_to_array['groups'][0] !== 0
- ) {
- $group_id = $sent_to_array['groups'][0];
- $output[] = " ".$group_names[$group_id]['name'];
- }
- if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
- $output[] = " ".get_lang('Everybody');
- }
- }
- if (!empty($output)) {
- $output = array_filter($output);
- if (count($output) > 0) {
- $output = implode(', ', $output);
- }
- return $output;
- }
- }
- /**
- * Returns all the users and all the groups a specific announcement item
- * has been sent to
- * @param string The tool (announcement, agenda, ...)
- * @param int ID of the element of the corresponding type
- * @return array Array of users and groups to whom the element has been sent
- */
- public static function sent_to($tool, $id)
- {
- $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $tool = Database::escape_string($tool);
- $id = (int) $id;
- $sent_to_group = [];
- $sent_to = [];
- $courseId = api_get_course_int_id();
- $sql = "SELECT to_group_id, to_user_id
- FROM $table
- WHERE c_id = $courseId AND tool = '$tool' AND ref=".$id;
- $result = Database::query($sql);
- while ($row = Database::fetch_array($result)) {
- // if to_user_id <> 0 then it is sent to a specific user
- if ($row['to_user_id'] <> 0) {
- $sent_to_user[] = $row['to_user_id'];
- continue;
- }
- // if to_group_id is null then it is sent to a specific user
- // if to_group_id = 0 then it is sent to everybody
- if ($row['to_group_id'] != 0) {
- $sent_to_group[] = $row['to_group_id'];
- }
- }
- if (isset($sent_to_group)) {
- $sent_to['groups'] = $sent_to_group;
- }
- if (isset($sent_to_user)) {
- $sent_to['users'] = $sent_to_user;
- }
- return $sent_to;
- }
- /**
- * Show a list with all the attachments according to the post's id
- * @param int $announcementId
- * @return array with the post info
- * @author Arthur Portugal
- * @version November 2009, dokeos 1.8.6.2
- */
- public static function get_attachment($announcementId)
- {
- $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
- $announcementId = intval($announcementId);
- $courseId = api_get_course_int_id();
- $row = [];
- $sql = 'SELECT id, path, filename, comment
- FROM '.$table.'
- WHERE c_id = ' . $courseId.' AND announcement_id = '.$announcementId;
- $result = Database::query($sql);
- if (Database::num_rows($result) != 0) {
- $row = Database::fetch_array($result, 'ASSOC');
- }
- return $row;
- }
- /**
- * This function add a attachment file into announcement
- * @param int announcement id
- * @param string file comment
- * @param array uploaded file $_FILES
- * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
- */
- public static function add_announcement_attachment_file(
- $announcement_id,
- $file_comment,
- $file
- ) {
- $courseInfo = api_get_course_info();
- $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
- $return = 0;
- $announcement_id = intval($announcement_id);
- $courseId = api_get_course_int_id();
- if (is_array($file) && $file['error'] == 0) {
- // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
- $courseDir = $courseInfo['path'].'/upload/announcements';
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $updir = $sys_course_path.$courseDir;
- // Try to add an extension to the file if it hasn't one
- $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
- // user's file name
- $file_name = $file['name'];
- if (!filter_extension($new_file_name)) {
- $return = -1;
- echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
- } else {
- $new_file_name = uniqid('');
- $new_path = $updir.'/'.$new_file_name;
- // This file is copy here but its cleaned in api_mail_html in api.lib.php
- copy($file['tmp_name'], $new_path);
- $params = [
- 'c_id' => $courseId,
- 'filename' => $file_name,
- 'comment' => $file_comment,
- 'path' => $new_file_name,
- 'announcement_id' => $announcement_id,
- 'size' => intval($file['size']),
- ];
- $insertId = Database::insert($table, $params);
- if ($insertId) {
- $sql = "UPDATE $table SET id = iid
- WHERE iid = $insertId";
- Database::query($sql);
- }
- $return = 1;
- }
- }
- return $return;
- }
- /**
- * This function edit a attachment file into announcement
- * @param int attach id
- * @param array uploaded file $_FILES
- * @param string file comment
- * @return int
- */
- public static function edit_announcement_attachment_file(
- $id_attach,
- $file,
- $file_comment
- ) {
- $courseInfo = api_get_course_info();
- $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
- $return = 0;
- $courseId = api_get_course_int_id();
- if (is_array($file) && $file['error'] == 0) {
- // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
- $courseDir = $courseInfo['path'].'/upload/announcements';
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $updir = $sys_course_path.$courseDir;
- // Try to add an extension to the file if it hasn't one
- $new_file_name = add_ext_on_mime(
- stripslashes($file['name']),
- $file['type']
- );
- // user's file name
- $file_name = $file ['name'];
- if (!filter_extension($new_file_name)) {
- $return = -1;
- echo Display::return_message(
- get_lang('UplUnableToSaveFileFilteredExtension'),
- 'error'
- );
- } else {
- $new_file_name = uniqid('');
- $new_path = $updir.'/'.$new_file_name;
- copy($file['tmp_name'], $new_path);
- $safe_file_comment = Database::escape_string($file_comment);
- $safe_file_name = Database::escape_string($file_name);
- $safe_new_file_name = Database::escape_string($new_file_name);
- $id_attach = intval($id_attach);
- $sql = "UPDATE $table SET
- filename = '$safe_file_name',
- comment = '$safe_file_comment',
- path = '$safe_new_file_name',
- size ='".intval($file['size'])."'
- WHERE c_id = $courseId AND id = '$id_attach'";
- $result = Database::query($sql);
- if ($result === false) {
- $return = -1;
- echo Display::return_message(
- get_lang('UplUnableToSaveFile'),
- 'error'
- );
- } else {
- $return = 1;
- }
- }
- }
- return $return;
- }
- /**
- * This function delete a attachment file by id
- * @param integer $id attachment file Id
- * @return bool
- */
- public static function delete_announcement_attachment_file($id)
- {
- $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
- $id = intval($id);
- $courseId = api_get_course_int_id();
- if (empty($courseId) || empty($id)) {
- return false;
- }
- $sql = "DELETE FROM $table
- WHERE c_id = $courseId AND id = $id";
- Database::query($sql);
- return true;
- }
- /**
- * @param array $courseInfo
- * @param int $sessionId
- * @param int $id
- * @param bool $sendToUsersInSession
- * @param bool $sendToDrhUsers
- */
- public static function sendEmail(
- $courseInfo,
- $sessionId,
- $id,
- $sendToUsersInSession = false,
- $sendToDrhUsers = false
- ) {
- $email = AnnouncementEmail::create($courseInfo, $sessionId, $id);
- $email->send($sendToUsersInSession, $sendToDrhUsers);
- }
- /**
- * @param $stok
- * @param $announcement_number
- * @param bool $getCount
- * @param null $start
- * @param null $limit
- * @param string $sidx
- * @param string $sord
- * @param string $titleToSearch
- * @param int $userIdToSearch
- * @param int $userId
- * @param int $courseId
- * @param int $sessionId
- * @return array
- */
- public static function getAnnouncements(
- $stok,
- $announcement_number,
- $getCount = false,
- $start = null,
- $limit = null,
- $sidx = '',
- $sord = '',
- $titleToSearch = '',
- $userIdToSearch = 0,
- $userId = 0,
- $courseId = 0,
- $sessionId = 0
- ) {
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $user_id = $userId ?: api_get_user_id();
- $group_id = api_get_group_id();
- $session_id = $sessionId ?: api_get_session_id();
- if (empty($courseId)) {
- $courseInfo = api_get_course_info();
- $courseId = $courseInfo['real_id'];
- } else {
- $courseId = (int) $courseId;
- $courseInfo = api_get_course_info($courseId);
- }
- if (empty($courseInfo)) {
- return [];
- }
- $condition_session = api_get_session_condition(
- $session_id,
- true,
- true,
- 'announcement.session_id'
- );
- $group_memberships = GroupManager::get_group_ids(
- $courseId,
- api_get_user_id()
- );
- $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement');
- $select = ' DISTINCT
- announcement.*,
- ip.visibility,
- ip.to_group_id,
- ip.insert_user_id,
- ip.insert_date,
- ip.lastedit_date';
- $groupBy = ' GROUP BY announcement.iid';
- if ($getCount) {
- $groupBy = '';
- $select = ' COUNT(DISTINCT announcement.iid) count';
- }
- $searchCondition = '';
- if (!empty($titleToSearch)) {
- $titleToSearch = Database::escape_string($titleToSearch);
- $searchCondition .= " AND (title LIKE '%$titleToSearch%')";
- }
- if (!empty($userIdToSearch)) {
- $userIdToSearch = intval($userIdToSearch);
- $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)";
- }
- $allowOnlyGroup = api_get_configuration_value('hide_base_course_announcements_in_group');
- $extraGroupCondition = '';
- if ($allowOnlyGroup) {
- $extraGroupCondition = " AND ip.to_group_id = $group_id ";
- }
- if (api_is_allowed_to_edit(false, true) ||
- ($allowUserEditSetting && !api_is_anonymous())
- ) {
- // A.1. you are a course admin with a USER filter
- // => see only the messages of this specific user + the messages of the group (s)he is member of.
- //if (!empty($user_id)) {
- if (0) {
- if (is_array($group_memberships) &&
- count($group_memberships) > 0
- ) {
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool = 'announcement' AND
- (
- ip.to_user_id = $user_id OR
- ip.to_group_id IS NULL OR
- ip.to_group_id IN (0, ".implode(", ", $group_memberships).")
- ) AND
- ip.visibility IN ('1', '0')
- $condition_session
- $searchCondition
- ORDER BY display_order DESC";
- } else {
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool ='announcement' AND
- (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND
- ip.visibility IN ('1', '0')
- $condition_session
- $searchCondition
- ORDER BY display_order DESC";
- }
- } elseif ($group_id != 0) {
- // A.2. you are a course admin with a GROUP filter
- // => see only the messages of this specific group
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- ip.tool='announcement' AND
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.visibility<>'2' AND
- (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
- $condition_session
- $searchCondition
- $extraGroupCondition
- $groupBy
- ORDER BY display_order DESC";
- //GROUP BY ip.ref
- } else {
- // A.3 you are a course admin without any group or user filter
- // A.3.a you are a course admin without user or group filter but WITH studentview
- // => see all the messages of all the users and groups without editing possibilities
- if (isset($isStudentView) && $isStudentView == 'true') {
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- ip.tool='announcement' AND
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.visibility='1'
- $condition_session
- $searchCondition
- $groupBy
- ORDER BY display_order DESC";
- //GROUP BY ip.ref
- } else {
- // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
- // => see all the messages of all the users and groups with editing possibilities
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- ip.tool = 'announcement' AND
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- (ip.visibility='0' OR ip.visibility='1')
- $condition_session
- $searchCondition
- $groupBy
- ORDER BY display_order DESC";
- }
- }
- } else {
- // STUDENT
- if (is_array($group_memberships) && count($group_memberships) > 0) {
- if ($allowUserEditSetting && !api_is_anonymous()) {
- if ($group_id == 0) {
- // No group
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$user_id."' OR (
- (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) OR
- (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
- )
- ) ";
- } else {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")
- )";
- $cond_user_id .= $extraGroupCondition;
- }
- } else {
- if ($group_id == 0) {
- $cond_user_id = " AND (
- (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)."))
- ) ";
- } else {
- $cond_user_id = " AND (
- (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."))
- )";
- $cond_user_id .= $extraGroupCondition;
- }
- }
- $sql = "SELECT $select
- FROM $tbl_announcement announcement INNER JOIN
- $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement'
- $cond_user_id
- $condition_session
- $searchCondition
- AND ip.visibility='1'
- $groupBy
- ORDER BY display_order DESC";
- } else {
- if ($user_id) {
- if ($allowUserEditSetting && !api_is_anonymous()) {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".api_get_user_id()."' OR
- ((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))
- ) ";
- } else {
- $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) ) ";
- }
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement'
- $cond_user_id
- $condition_session
- $searchCondition
- AND ip.visibility='1'
- AND announcement.session_id IN(0, ".$session_id.")
- $groupBy
- ORDER BY display_order DESC";
- } else {
- if (($allowUserEditSetting && !api_is_anonymous())) {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
- )";
- } else {
- $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL ";
- }
- $sql = "SELECT $select
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement'
- $cond_user_id
- $condition_session
- $searchCondition AND
- ip.visibility='1' AND
- announcement.session_id IN ( 0,".api_get_session_id().")
- $groupBy
- ";
- }
- }
- }
- if (!is_null($start) && !is_null($limit)) {
- $start = intval($start);
- $limit = intval($limit);
- $sql .= " LIMIT $start, $limit";
- }
- $result = Database::query($sql);
- if ($getCount) {
- $result = Database::fetch_array($result, 'ASSOC');
- return $result['count'];
- }
- $iterator = 1;
- $bottomAnnouncement = $announcement_number;
- $displayed = [];
- $results = [];
- $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq();
- $emailIcon = '<i class="fa fa-envelope-o" title="'.get_lang('AnnounceSentByEmail').'"></i>';
- $attachmentIcon = '<i class="fa fa-paperclip" title="'.get_lang('Attachment').'"></i>';
- $editIcon = Display::return_icon(
- 'edit.png',
- get_lang('Edit'),
- '',
- ICON_SIZE_SMALL
- );
- $deleteIcon = Display::return_icon(
- 'delete.png',
- get_lang('Delete'),
- '',
- ICON_SIZE_SMALL
- );
- $isTutor = false;
- if (!empty($group_id)) {
- $groupInfo = GroupManager::get_group_properties(api_get_group_id());
- //User has access in the group?
- $isTutor = GroupManager::is_tutor_of_group(
- api_get_user_id(),
- $groupInfo
- );
- }
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- if (!in_array($row['id'], $displayed)) {
- $sent_to_icon = '';
- // the email icon
- if ($row['email_sent'] == '1') {
- $sent_to_icon = ' '.$emailIcon;
- }
- $groupReference = ($row['to_group_id'] > 0) ? ' <span class="label label-info">'.get_lang('Group').'</span> ' : '';
- $title = $row['title'].$groupReference.$sent_to_icon;
- $item_visibility = api_get_item_visibility(
- $courseInfo,
- TOOL_ANNOUNCEMENT,
- $row['id'],
- $session_id
- );
- $row['visibility'] = $item_visibility;
- // show attachment list
- $attachment_list = self::get_attachment($row['id']);
- $attachment_icon = '';
- if (count($attachment_list) > 0) {
- $attachment_icon = ' '.$attachmentIcon;
- }
- /* TITLE */
- $user_info = api_get_user_info($row['insert_user_id']);
- $username = sprintf(get_lang("LoginX"), $user_info['username']);
- $username_span = Display::tag(
- 'span',
- $user_info['complete_name'],
- ['title' => $username]
- );
- $title = Display::url(
- $title.$attachment_icon,
- $actionUrl.'&action=view&id='.$row['id']
- );
- // we can edit if : we are the teacher OR the element belongs to
- // the session we are coaching OR the option to allow users to edit is on
- if (api_is_allowed_to_edit(false, true) ||
- (api_is_session_general_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $row['id'])) ||
- (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) ||
- ($row['to_group_id'] == $group_id && $isTutor)
- ) {
- $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$row['id']."\">".$editIcon."</a>";
- if ($row['visibility'] == 1) {
- $image_visibility = "visible";
- $alt_visibility = get_lang('Hide');
- } else {
- $image_visibility = "invisible";
- $alt_visibility = get_lang('Visible');
- }
- $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$row['id']."&sec_token=".$stok."\">".
- Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>";
- // DISPLAY MOVE UP COMMAND only if it is not the top announcement
- if ($iterator != 1) {
- $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$row["id"]."&sec_token=".$stok."\">".
- Display::return_icon('up.gif', get_lang('Up'))."</a>";
- } else {
- $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
- }
- if ($iterator < $bottomAnnouncement) {
- $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$row["id"]."&sec_token=".$stok."\">".
- Display::return_icon('down.gif', get_lang('Down'))."</a>";
- } else {
- $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
- }
- if (api_is_allowed_to_edit(false, true)) {
- $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$row['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, api_get_system_encoding()))."')) return false;\">".
- $deleteIcon."</a>";
- }
- $iterator++;
- } else {
- $modify_icons = Display::url(
- Display::return_icon('default.png'),
- $actionUrl.'&action=view&id='.$row['id']
- );
- }
- $announcement = [
- 'id' => $row['id'],
- 'title' => $title,
- 'username' => $username_span,
- 'insert_date' => api_convert_and_format_date(
- $row['insert_date'],
- DATE_TIME_FORMAT_LONG
- ),
- 'lastedit_date' => api_convert_and_format_date(
- $row['lastedit_date'],
- DATE_TIME_FORMAT_LONG
- ),
- 'actions' => $modify_icons
- ];
- $results[] = $announcement;
- }
- $displayed[] = $row['id'];
- }
- return $results;
- }
- /**
- * @return int
- */
- public static function getNumberAnnouncements()
- {
- // Maximum title messages to display
- $maximum = '12';
- // Database Table Definitions
- $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
- $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $session_id = api_get_session_id();
- $courseInfo = api_get_course_info();
- $courseId = $courseInfo['real_id'];
- $userId = api_get_user_id();
- $condition_session = api_get_session_condition(
- $session_id,
- true,
- true,
- 'announcement.session_id'
- );
- if (api_is_allowed_to_edit(false, true)) {
- // check teacher status
- if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
- if (api_get_group_id() == 0) {
- $group_condition = '';
- } else {
- $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0 OR ip.to_group_id IS NULL)";
- }
- $sql = "SELECT
- announcement.*,
- ip.visibility,
- ip.to_group_id,
- ip.insert_user_id
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool = 'announcement' AND
- ip.visibility <> '2'
- $group_condition
- $condition_session
- GROUP BY ip.ref
- ORDER BY display_order DESC
- LIMIT 0, $maximum";
- }
- } else {
- // students only get to see the visible announcements
- if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
- $group_memberships = GroupManager::get_group_ids(
- $courseInfo['real_id'],
- $userId
- );
- if ((api_get_course_setting('allow_user_edit_announcement') &&
- !api_is_anonymous())
- ) {
- if (api_get_group_id() == 0) {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$userId."' OR (
- ip.to_user_id='".$userId."' OR
- ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
- ip.to_group_id IS NULL
- )
- )
- ";
- } else {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$userId."'OR
- ip.to_group_id IN (0, ".api_get_group_id().") OR
- ip.to_group_id IS NULL
- )";
- }
- } else {
- if (api_get_group_id() == 0) {
- $cond_user_id = " AND (
- ip.to_user_id='".$userId."' OR
- ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
- ip.to_group_id IS NULL
- ) ";
- } else {
- $cond_user_id = " AND (
- ip.to_user_id='".$userId."' OR
- ip.to_group_id IN (0, ".api_get_group_id().") OR
- ip.to_group_id IS NULL
- ) ";
- }
- }
- // the user is member of several groups => display personal announcements AND
- // his group announcements AND the general announcements
- if (is_array($group_memberships) && count($group_memberships) > 0) {
- $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement' AND
- ip.visibility='1'
- $cond_user_id
- $condition_session
- GROUP BY ip.ref
- ORDER BY display_order DESC
- LIMIT 0, $maximum";
- } else {
- // the user is not member of any group
- // this is an identified user => show the general announcements AND his personal announcements
- if ($userId) {
- if ((api_get_course_setting('allow_user_edit_announcement') &&
- !api_is_anonymous())
- ) {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".$userId."' OR
- ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
- ) ";
- } else {
- $cond_user_id = " AND ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
- }
- $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement' AND
- ip.visibility='1'
- $cond_user_id
- $condition_session
- GROUP BY ip.ref
- ORDER BY display_order DESC
- LIMIT 0, $maximum";
- } else {
- if (api_get_course_setting('allow_user_edit_announcement')) {
- $cond_user_id = " AND (
- ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
- ) ";
- } else {
- $cond_user_id = " AND ip.to_group_id='0' ";
- }
- // the user is not identiefied => show only the general announcements
- $sql = "SELECT
- announcement.*,
- ip.visibility,
- ip.to_group_id,
- ip.insert_user_id
- FROM $tbl_announcement announcement
- INNER JOIN $tbl_item_property ip
- ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
- WHERE
- announcement.c_id = $courseId AND
- ip.c_id = $courseId AND
- ip.tool='announcement' AND
- ip.visibility='1' AND
- ip.to_group_id='0'
- $condition_session
- GROUP BY ip.ref
- ORDER BY display_order DESC
- LIMIT 0, $maximum";
- }
- }
- }
- }
- $result = Database::query($sql);
- return Database::num_rows($result);
- }
- }
|