123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class SystemAnnouncementManager
- */
- class SystemAnnouncementManager
- {
- const VISIBLE_GUEST = 'visible_guest';
- const VISIBLE_STUDENT = 'visible_student';
- const VISIBLE_TEACHER = 'visible_teacher';
- // Requires DB change
- const VISIBLE_DRH = 'visible_drh';
- const VISIBLE_SESSION_ADMIN = 'visible_session_admin';
- const VISIBLE_STUDENT_BOSS = 'visible_boss';
- /**
- * @return array
- */
- public static function getVisibilityList()
- {
- $extraRoles = self::newRolesActivated();
- $visibleToUsers = [
- self::VISIBLE_TEACHER => get_lang('Teacher'),
- self::VISIBLE_STUDENT => get_lang('Student'),
- self::VISIBLE_GUEST => get_lang('Guest')
- ];
- if ($extraRoles) {
- $visibleToUsers[self::VISIBLE_DRH] = get_lang('DRH');
- $visibleToUsers[self::VISIBLE_SESSION_ADMIN] = get_lang('SessionAdministrator');
- $visibleToUsers[self::VISIBLE_STUDENT_BOSS] = get_lang('StudentBoss');
- }
- return $visibleToUsers;
- }
- /**
- * @param string $visibility
- * @return string
- */
- public static function getVisibilityCondition($visibility)
- {
- $list = self::getVisibilityList();
- $visibilityCondition = " AND ".self::VISIBLE_GUEST." = 1 ";
- if (in_array($visibility, array_keys($list))) {
- $visibilityCondition = " AND $visibility = 1 ";
- }
- return $visibilityCondition;
- }
- /**
- * Displays all announcements
- * @param string $visibility VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
- * @param int $id The identifier of the announcement to display
- */
- public static function display_announcements($visibility, $id = -1)
- {
- $user_selected_language = api_get_interface_language();
- $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
- $userGroup = new UserGroup();
- $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(), 0);
- $groups = array();
- foreach ($temp_user_groups as $user_group) {
- $groups = array_merge($groups, array($user_group['id']));
- $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
- }
- $groups_string = '('.implode($groups, ',').')';
- $now = api_get_utc_datetime();
- $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y %h:%i:%s') AS display_date
- FROM $db_table
- WHERE
- (lang='$user_selected_language' OR lang IS NULL) AND
- (('$now' BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
- $sql .= self::getVisibilityCondition($visibility);
- if (count($groups) > 0) {
- $sql .= " OR id IN (
- SELECT announcement_id FROM $tbl_announcement_group
- WHERE group_id in $groups_string
- ) ";
- }
- $current_access_url_id = 1;
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- }
- $sql .= " AND access_url_id = '$current_access_url_id' ";
- $sql .= " ORDER BY date_start DESC LIMIT 0,7";
- $announcements = Database::query($sql);
- if (Database::num_rows($announcements) > 0) {
- $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
- $query_string = ereg_replace('&$', '', $query_string);
- $url = api_get_self();
- echo '<div class="system_announcements">';
- echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
- echo '<div style="margin:10px;text-align:right;"><a href="news_list.php">'.get_lang('More').'</a></div>';
- while ($announcement = Database::fetch_object($announcements)) {
- if ($id != $announcement->id) {
- if (strlen($query_string) > 0) {
- $show_url = 'news_list.php#'.$announcement->id;
- } else {
- $show_url = 'news_list.php#'.$announcement->id;
- }
- $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
- echo '<a name="'.$announcement->id.'"></a>
- <div class="system_announcement">
- <div class="system_announcement_title"><a name="ann'.$announcement->id.'" href="'.$show_url.'">'.$announcement->title.'</a></div><div class="system_announcement_date">'.$display_date.'</div>
- </div>';
- } else {
- echo '<div class="system_announcement">
- <div class="system_announcement_title">'
- .$announcement->display_date.'
- <a name="ann'.$announcement->id.'" href="'.$url.'?'.$query_string.'#ann'.$announcement->id.'">'.$announcement->title.'</a>
- </div>';
- }
- echo '<br />';
- }
- echo '</div>';
- }
- return;
- }
- /**
- * @param string $visibility
- * @param int $id
- * @param int $start
- * @param string $user_id
- * @return string
- */
- public static function displayAllAnnouncements($visibility, $id = -1, $start = 0, $user_id = '')
- {
- $user_selected_language = api_get_interface_language();
- $start = intval($start);
- $userGroup = new UserGroup();
- $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
- $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(), 0);
- $groups = array();
- foreach ($temp_user_groups as $user_group) {
- $groups = array_merge($groups, array($user_group['id']));
- $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id']));
- }
- // Checks if tables exists to not break platform not updated
- $groups_string = '('.implode($groups, ',').')';
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $now = api_get_utc_datetime();
- $sql = "SELECT * FROM $table
- WHERE
- (lang = '$user_selected_language' OR lang IS NULL) AND
- ( '$now' >= date_start AND '$now' <= date_end) ";
- $sql .= self::getVisibilityCondition($visibility);
- if (count($groups) > 0) {
- $sql .= " OR id IN (
- SELECT announcement_id FROM $tbl_announcement_group
- WHERE group_id in $groups_string
- ) ";
- }
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- $sql .= " AND access_url_id IN ('1', '$current_access_url_id')";
- }
- if (!isset($_GET['start']) || $_GET['start'] == 0) {
- $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
- } else {
- $sql .= " ORDER BY date_start DESC LIMIT ".($start + 1).",20";
- }
- $announcements = Database::query($sql);
- $content = '';
- if (Database::num_rows($announcements) > 0) {
- $content .= '<div class="system_announcements">';
- $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>';
- $content .= '<table align="center">';
- $content .= '<tr>';
- $content .= '<td>';
- $content .= self::display_arrow($user_id);
- $content .= '</td>';
- $content .= '</tr>';
- $content .= '</table>';
- $content .= '<table align="center" border="0" width="900px">';
- while ($announcement = Database::fetch_object($announcements)) {
- $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG);
- $content .= '<tr><td>';
- $content .= '<a name="'.$announcement->id.'"></a>
- <div class="system_announcement">
- <h2>'.$announcement->title.'</h2><div class="system_announcement_date">'.$display_date.'</div>
- <br />
- <div class="system_announcement_content">'
- .$announcement->content.'
- </div>
- </div><br />';
- $content .= '</tr></td>';
- }
- $content .= '</table>';
- $content .= '<table align="center">';
- $content .= '<tr>';
- $content .= '<td>';
- $content .= self::display_arrow($user_id);
- $content .= '</td>';
- $content .= '</tr>';
- $content .= '</table>';
- $content .= '</div>';
- }
- return $content;
- }
- /**
- * @param int $user_id
- * @return string
- */
- public static function display_arrow($user_id)
- {
- $start = (int) $_GET['start'];
- $nb_announcement = self::count_nb_announcement($start, $user_id);
- $next = ((int) $_GET['start'] + 19);
- $prev = ((int) $_GET['start'] - 19);
- $content = '';
- if (!isset($_GET['start']) || $_GET['start'] == 0) {
- if ($nb_announcement > 20) {
- $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
- }
- } else {
- echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
- if ($nb_announcement > 20) {
- $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
- }
- }
- return $content;
- }
- /**
- * @param int $start
- * @param string $user_id
- * @return int
- */
- public static function count_nb_announcement($start = 0, $user_id = '')
- {
- $start = intval($start);
- $user_selected_language = api_get_interface_language();
- $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $sql = 'SELECT id FROM '.$db_table.'
- WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
- $visibility = self::getCurrentUserVisibility();
- $sql .= self::getVisibilityCondition($visibility);
- $current_access_url_id = 1;
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- }
- $sql .= " AND access_url_id = '$current_access_url_id' ";
- $sql .= 'LIMIT '.$start.', 21';
- $announcements = Database::query($sql);
- $i = 0;
- while ($rows = Database::fetch_array($announcements)) {
- $i++;
- }
- return $i;
- }
- /**
- * Get all announcements
- * @return array An array with all available system announcements (as php
- * objects)
- */
- public static function get_all_announcements()
- {
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $now = api_get_utc_datetime();
- $sql = "SELECT *, IF ( '$now' >= date_start AND '$now' <= date_end, '1', '0') AS visible
- FROM $table";
- $current_access_url_id = 1;
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- }
- $sql .= " WHERE access_url_id = '$current_access_url_id' ";
- $sql .= " ORDER BY date_start ASC";
- $result = Database::query($sql);
- $announcements = array();
- while ($announcement = Database::fetch_object($result)) {
- $announcements[] = $announcement;
- }
- return $announcements;
- }
- /**
- * Adds an announcement to the database
- *
- * @param string $title Title of the announcement
- * @param string $content Content of the announcement
- * @param string $date_start Start date (YYYY-MM-DD HH:II: SS)
- * @param string $date_end End date (YYYY-MM-DD HH:II: SS)
- * @param array $visibility
- * @param string $lang The language for which the announvement should be shown. Leave null for all langages
- * @param int $send_mail Whether to send an e-mail to all users (1) or not (0)
- * @param bool $add_to_calendar
- * @param bool $sendEmailTest
- *
- * @return mixed insert_id on success, false on failure
- */
- public static function add_announcement(
- $title,
- $content,
- $date_start,
- $date_end,
- $visibility,
- $lang = '',
- $send_mail = 0,
- $add_to_calendar = false,
- $sendEmailTest = false
- ) {
- $original_content = $content;
- $a_dateS = explode(' ', $date_start);
- $a_arraySD = explode('-', $a_dateS[0]);
- $a_arraySH = explode(':', $a_dateS[1]);
- $date_start_to_compare = array_merge($a_arraySD, $a_arraySH);
- $a_dateE = explode(' ', $date_end);
- $a_arrayED = explode('-', $a_dateE[0]);
- $a_arrayEH = explode(':', $a_dateE[1]);
- $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH);
- $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
- Display::addFlash(Display::return_message(get_lang('InvalidStartDate'), 'warning'));
- return false;
- }
- if (($date_end_to_compare[1] ||
- $date_end_to_compare[2] ||
- $date_end_to_compare[0]) &&
- !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
- ) {
- Display::addFlash(Display::return_message(get_lang('InvalidEndDate'), 'warning'));
- return false;
- }
- if (strlen(trim($title)) == 0) {
- Display::addFlash(Display::return_message(get_lang('InvalidTitle'), 'warning'));
- return false;
- }
- $start = api_get_utc_datetime($date_start);
- $end = api_get_utc_datetime($date_end);
- //Fixing urls that are sent by email
- //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
- //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
- $content = str_replace('src=\"'.api_get_path(REL_HOME_PATH), 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
- $content = str_replace('file='.api_get_path(REL_HOME_PATH), 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
- $lang = is_null($lang) ? '' : $lang;
- $current_access_url_id = 1;
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- }
- $params = [
- 'title' => $title,
- 'content' => $content,
- 'date_start' => $start,
- 'date_end' => $end,
- 'lang' => $lang,
- 'access_url_id' => $current_access_url_id
- ];
- foreach ($visibility as $key => $value) {
- $params[$key] = $value;
- }
- $resultId = Database::insert($db_table, $params);
- if ($resultId) {
- if ($sendEmailTest) {
- self::send_system_announcement_by_email(
- $title,
- $content,
- $visibility,
- $lang,
- true
- );
- } else {
- if ($send_mail == 1) {
- self::send_system_announcement_by_email(
- $title,
- $content,
- $visibility,
- $lang
- );
- }
- }
- if ($add_to_calendar) {
- $agenda = new Agenda('admin');
- $agenda->addEvent(
- $date_start,
- $date_end,
- false,
- $title,
- $original_content
- );
- }
- return $resultId;
- }
- return false;
- }
- /**
- * Makes the announcement id visible only for groups in groups_array
- * @param int $announcement_id
- * @param array $group_array array of group id
- * @return bool
- **/
- public static function announcement_for_groups($announcement_id, $group_array)
- {
- $tbl_announcement_group = Database::get_main_table(
- TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS
- );
- //first delete all group associations for this announcement
- $res = Database::query(
- "DELETE FROM $tbl_announcement_group
- WHERE announcement_id=".intval($announcement_id)
- );
- if ($res === false) {
- return false;
- }
- foreach ($group_array as $group_id) {
- if (intval($group_id) != 0) {
- $sql = "INSERT INTO $tbl_announcement_group SET
- announcement_id=".intval($announcement_id).",
- group_id=".intval($group_id);
- $res = Database::query($sql);
- if ($res === false) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Gets the groups of this announce
- * @param int announcement id
- * @return array array of group id
- **/
- public static function get_announcement_groups($announcement_id)
- {
- $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
- $tbl_group = Database::get_main_table(TABLE_USERGROUP);
- //first delete all group associations for this announcement
- $sql = "SELECT
- g.id as group_id,
- g.name as group_name
- FROM $tbl_group g , $tbl_announcement_group ag
- WHERE
- announcement_id =".intval($announcement_id)." AND
- ag.group_id = g.id";
- $res = Database::query($sql);
- $groups = Database::fetch_array($res);
- return $groups;
- }
- /**
- * Updates an announcement to the database
- *
- * @param integer $id of the announcement
- * @param string $title title of the announcement
- * @param string $content content of the announcement
- * @param array $date_start start date (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
- * @param array $date_end end date of (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
- * @param array $visibility
- * @return bool True on success, false on failure
- */
- public static function update_announcement(
- $id,
- $title,
- $content,
- $date_start,
- $date_end,
- $visibility,
- $lang = null,
- $send_mail = 0,
- $sendEmailTest = false
- ) {
- $em = Database::getManager();
- $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id);
- if (!$announcement) {
- return false;
- }
- $a_dateS = explode(' ', $date_start);
- $a_arraySD = explode('-', $a_dateS[0]);
- $a_arraySH = explode(':', $a_dateS[1]);
- $date_start_to_compare = array_merge($a_arraySD, $a_arraySH);
- $a_dateE = explode(' ', $date_end);
- $a_arrayED = explode('-', $a_dateE[0]);
- $a_arrayEH = explode(':', $a_dateE[1]);
- $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH);
- $lang = is_null($lang) ? '' : $lang;
- if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) {
- echo Display::return_message(get_lang('InvalidStartDate'));
- return false;
- }
- if (($date_end_to_compare[1] ||
- $date_end_to_compare[2] ||
- $date_end_to_compare[0]) &&
- !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])
- ) {
- echo Display::return_message(get_lang('InvalidEndDate'));
- return false;
- }
- if (strlen(trim($title)) == 0) {
- echo Display::return_message(get_lang('InvalidTitle'));
- return false;
- }
- $start = api_get_utc_datetime($date_start);
- $end = api_get_utc_datetime($date_end);
- //Fixing urls that are sent by email
- //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content);
- //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content);
- $content = str_replace('src=\"'.api_get_path(REL_HOME_PATH), 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
- $content = str_replace('file='.api_get_path(REL_HOME_PATH), 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), $content);
- if ($sendEmailTest) {
- self::send_system_announcement_by_email(
- $title,
- $content,
- null,
- null,
- $lang,
- $sendEmailTest
- );
- } else {
- if ($send_mail == 1) {
- self::send_system_announcement_by_email(
- $title,
- $content,
- $visibility,
- $lang
- );
- }
- }
- $dateStart = new DateTime($start, new DateTimeZone('UTC'));
- $dateEnd = new DateTime($end, new DateTimeZone('UTC'));
- $announcement
- ->setLang($lang)
- ->setTitle($title)
- ->setContent($content)
- ->setDateStart($dateStart)
- ->setDateEnd($dateEnd)
- //->setVisibleTeacher($visible_teacher)
- //->setVisibleStudent($visible_student)
- //->setVisibleGuest($visible_guest)
- ->setAccessUrlId(api_get_current_access_url_id());
- $em->merge($announcement);
- $em->flush();
- // Update visibility
- $list = self::getVisibilityList();
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- foreach ($list as $key => $title) {
- $value = isset($visibility[$key]) && $visibility[$key] ? 1 : 0;
- $sql = "UPDATE $table SET $key = '$value' WHERE id = $id";
- Database::query($sql);
- }
- return true;
- }
- /**
- * Deletes an announcement
- * @param int $id The identifier of the announcement that should be
- * @return bool True on success, false on failure
- */
- public static function delete_announcement($id)
- {
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $id = intval($id);
- $sql = "DELETE FROM $table WHERE id =".$id;
- $res = Database::query($sql);
- if ($res === false) {
- return false;
- }
- return true;
- }
- /**
- * Gets an announcement
- * @param int $id The identifier of the announcement that should be
- * @return object Object of class StdClass or the required class, containing the query result row
- */
- public static function get_announcement($id)
- {
- $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $id = intval($id);
- $sql = "SELECT * FROM ".$db_table." WHERE id = ".$id;
- $announcement = Database::fetch_object(Database::query($sql));
- return $announcement;
- }
- /**
- * Change the visibility of an announcement
- * @param int $id
- * @param int $user For who should the visibility be changed
- * @param bool $visible
- * @return bool True on success, false on failure
- */
- public static function set_visibility($id, $user, $visible)
- {
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $id = (int) $id;
- $list = array_keys(self::getVisibilityList());
- $user = trim($user);
- $visible = (int) $visible;
- if (!in_array($user, $list)) {
- return false;
- }
- $field = $user;
- $sql = "UPDATE $table SET ".$field." = '".$visible."'
- WHERE id='".$id."'";
- $res = Database::query($sql);
- if ($res === false) {
- return false;
- }
- return true;
- }
- /**
- * Send a system announcement by e-mail to all teachers/students depending on parameters
- * @param string $title
- * @param string $content
- * @param array $visibility
- * @param string $language Language (optional, considered for all languages if left empty)
- * @param bool $sendEmailTest
- * @return bool True if the message was sent or there was no destination matching.
- * False on database or e-mail sending error.
- */
- public static function send_system_announcement_by_email(
- $title,
- $content,
- $visibility,
- $language = null,
- $sendEmailTest = false
- ) {
- $content = str_replace(array('\r\n', '\n', '\r'), '', $content);
- $now = api_get_utc_datetime();
- $teacher = $visibility['visible_teacher'];
- $student = $visibility['visible_student'];
- if ($sendEmailTest) {
- MessageManager::send_message_simple(api_get_user_id(), $title, $content);
- return true;
- }
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- if (api_is_multiple_url_enabled()) {
- $current_access_url_id = api_get_current_access_url_id();
- $url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
- $url_condition = " INNER JOIN $url_rel_user uu ON uu.user_id = u.user_id ";
- }
- if ($teacher <> 0 && $student == 0) {
- $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
- WHERE status = '1' ";
- }
- if ($teacher == 0 && $student <> 0) {
- $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
- WHERE status = '5' ";
- }
- if ($teacher <> 0 && $student <> 0) {
- $sql = "SELECT DISTINCT u.user_id FROM $user_table u $url_condition
- WHERE 1 = 1 ";
- }
- if (!empty($language)) {
- //special condition because language was already treated for SQL insert before
- $sql .= " AND language = '".Database::escape_string($language)."' ";
- }
- if (api_is_multiple_url_enabled()) {
- $sql .= " AND access_url_id = '".$current_access_url_id."' ";
- }
- // Sent to active users.
- $sql .= " AND email <>'' AND active = 1 ";
- // Expiration date
- $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') ";
- if ((empty($teacher) || $teacher == '0') && (empty($student) || $student == '0')) {
- return true;
- }
- $result = Database::query($sql);
- if ($result === false) {
- return false;
- }
- $message_sent = false;
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- MessageManager::send_message_simple($row['user_id'], $title, $content);
- $message_sent = true;
- }
- // Minor validation to clean up the attachment files in the announcement
- if (!empty($_FILES)) {
- $attachments = $_FILES;
- foreach ($attachments as $attachment) {
- unlink($attachment['tmp_name']);
- }
- }
- return $message_sent; //true if at least one e-mail was sent
- }
- /**
- * Displays announcements as an slideshow
- * @param string $visible see self::VISIBLE_* constants
- * @param int $id The identifier of the announcement to display
- *
- * @return string
- */
- public static function displayAnnouncementsSlider($visible, $id = null)
- {
- $user_selected_language = Database::escape_string(api_get_interface_language());
- $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $cut_size = 500;
- $now = api_get_utc_datetime();
- $sql = "SELECT * FROM $table
- WHERE
- (lang = '$user_selected_language' OR lang = '') AND
- ('$now' >= date_start AND '$now' <= date_end) ";
- $sql .= self::getVisibilityCondition($visible);
- if (isset($id) && !empty($id)) {
- $id = intval($id);
- $sql .= " AND id = $id ";
- }
- if (api_is_multiple_url_enabled()) {
- $current_url_id = api_get_current_access_url_id();
- $sql .= " AND access_url_id IN ('1', '$current_url_id') ";
- }
- $sql .= " ORDER BY date_start DESC";
- $result = Database::query($sql);
- $announcements = [];
- if (Database::num_rows($result) > 0) {
- while ($announcement = Database::fetch_object($result)) {
- $announcementData = [
- 'id' => $announcement->id,
- 'title' => $announcement->title,
- 'content' => $announcement->content,
- 'readMore' => null
- ];
- if (empty($id)) {
- if (api_strlen(strip_tags($announcement->content)) > $cut_size) {
- $announcementData['content'] = cut($announcement->content, $cut_size);
- $announcementData['readMore'] = true;
- }
- }
- $announcements[] = $announcementData;
- }
- }
- if (count($announcements) === 0) {
- return null;
- }
- $template = new Template(null, false, false);
- $template->assign('announcements', $announcements);
- $layout = $template->get_template('announcement/slider.tpl');
- return $template->fetch($layout);
- }
- /**
- * Get the HTML code for an announcement
- * @param int $announcementId The announcement ID
- * @param int $visibility The announcement visibility
- * @return string The HTML code
- */
- public static function displayAnnouncement($announcementId, $visibility)
- {
- $selectedUserLanguage = Database::escape_string(api_get_interface_language());
- $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
- $now = api_get_utc_datetime();
- $whereConditions = [
- "(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage,
- "AND (? >= date_start AND ? <= date_end) " => [$now, $now],
- "AND id = ? " => intval($announcementId)
- ];
- $condition = self::getVisibilityCondition($visibility);
- $whereConditions[$condition] = 1;
- if (api_is_multiple_url_enabled()) {
- $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id();
- }
- $announcement = Database::select(
- '*',
- $announcementTable,
- [
- 'where' => $whereConditions,
- 'order' => 'date_start',
- ],
- 'first'
- );
- $template = new Template(null, false, false);
- $template->assign('announcement', $announcement);
- $layout = $template->get_template('announcement/view.tpl');
- return $template->fetch($layout);
- }
- /**
- * @return bool
- */
- public static function newRolesActivated()
- {
- /* In order to use this option you need to run this SQL changes :
- ALTER TABLE sys_announcement ADD COLUMN visible_drh INT DEFAULT 0;
- ALTER TABLE sys_announcement ADD COLUMN visible_session_admin INT DEFAULT 0;
- ALTER TABLE sys_announcement ADD COLUMN visible_boss INT DEFAULT 0;
- */
- return api_get_configuration_value('system_announce_extra_roles');
- }
- /**
- * @return string
- */
- public static function getCurrentUserVisibility()
- {
- if (api_is_anonymous()) {
- return SystemAnnouncementManager::VISIBLE_GUEST;
- }
- if (self::newRolesActivated()) {
- if (api_is_student_boss()) {
- return SystemAnnouncementManager::VISIBLE_STUDENT_BOSS;
- }
- if (api_is_session_admin()) {
- return SystemAnnouncementManager::VISIBLE_SESSION_ADMIN;
- }
- if (api_is_drh()) {
- return SystemAnnouncementManager::VISIBLE_DRH;
- }
- if (api_is_allowed_to_create_course()) {
- return SystemAnnouncementManager::VISIBLE_TEACHER;
- } else {
- return SystemAnnouncementManager::VISIBLE_STUDENT;
- }
- } else {
- // Default behaviour
- return api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
- }
- }
- }
|