From 5af4e17a8f71e1d26ffaeeb9dd093760b5a534a1 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 21 Jan 2020 15:53:16 +0100 Subject: [PATCH] Admin: add config setting "allow_careers_in_global_announcements" BT#16262 --- main/admin/system_announcements.php | 78 +++++++++++++- main/inc/ajax/career.ajax.php | 23 ++++ main/inc/ajax/user_manager.ajax.php | 4 +- main/inc/lib/system_announcements.lib.php | 124 +++++++++++++++------- main/install/configuration.dist.php | 7 +- 5 files changed, 192 insertions(+), 44 deletions(-) create mode 100644 main/inc/ajax/career.ajax.php diff --git a/main/admin/system_announcements.php b/main/admin/system_announcements.php index 5434bc5a17..8aa6434a01 100755 --- a/main/admin/system_announcements.php +++ b/main/admin/system_announcements.php @@ -24,6 +24,8 @@ $action_todo = false; // Access restrictions api_protect_admin_script(true); +$allowCareers = api_get_configuration_value('allow_careers_in_global_announcements'); + // Setting breadcrumbs. $interbreadcrumb[] = [ 'url' => 'index.php', @@ -54,9 +56,37 @@ if (!empty($action)) { } else { $tool_name = get_lang('SystemAnnouncements'); } +$url = api_get_path(WEB_AJAX_PATH).'career.ajax.php'; + +$htmlHeadXtra[] = ''; // Displaying the header. -Display :: display_header($tool_name); +Display::display_header($tool_name); if ($action != 'add' && $action != 'edit') { echo '
'; echo ''.Display::return_icon('add.png', get_lang('AddAnnouncement'), [], 32).''; @@ -80,6 +110,7 @@ switch ($action) { if ($action == 'make_visible') { $status = true; } + SystemAnnouncementManager::set_visibility( $_GET['id'], $_GET['person'], @@ -127,6 +158,10 @@ switch ($action) { $values[$key] = $data[$key]; } } + if ($allowCareers) { + $values['career_id'] = $announcement->career_id; + $values['promotion_id'] = $announcement->promotion_id; + } $values['lang'] = $announcement->lang; $values['action'] = 'edit'; @@ -175,6 +210,39 @@ if ($action_todo) { ['id' => 'range'] ); + if ($allowCareers) { + $career = new Career(); + $careerList = $career->get_all(); + $list = array_column($careerList, 'name', 'id'); + + $form->addSelect( + 'career_id', + get_lang('Career'), + $list, + ['onchange' => 'javascript: showCareer();', 'placeholder' => get_lang('SelectAnOption'), 'id' => 'career_id'] + ); + + $display = 'none;'; + $options = []; + if (isset($values['promotion_id'])) { + $promotion = new Promotion(); + $promotion = $promotion->get($values['promotion_id']); + if ($promotion) { + $options = [$promotion['id'] => $promotion['name']]; + $display = 'block'; + } + } + + $form->addHtml('
'); + $form->addSelect( + 'promotion_id', + get_lang('Promotion'), + $options, + ['id' => 'promotion_id'] + ); + $form->addHtml('
'); + } + $group = []; foreach ($visibleList as $key => $name) { $group[] = $form->createElement( @@ -245,7 +313,9 @@ if ($action_todo) { $values['lang'], $sendMail, empty($values['add_to_calendar']) ? false : true, - empty($values['send_email_test']) ? false : true + empty($values['send_email_test']) ? false : true, + isset($values['career_id']) ? $values['career_id'] : 0, + isset($values['promotion_id']) ? $values['promotion_id'] : 0 ); if ($announcement_id !== false) { @@ -276,7 +346,9 @@ if ($action_todo) { $visibilityResult, $values['lang'], $sendMail, - $sendMailTest + $sendMailTest, + isset($values['career_id']) ? $values['career_id'] : 0, + isset($values['promotion_id']) ? $values['promotion_id'] : 0 )) { if (isset($values['group'])) { SystemAnnouncementManager::announcement_for_groups( diff --git a/main/inc/ajax/career.ajax.php b/main/inc/ajax/career.ajax.php new file mode 100644 index 0000000000..93f0746d19 --- /dev/null +++ b/main/inc/ajax/career.ajax.php @@ -0,0 +1,23 @@ +get_all_promotions_by_career_id($careerId); + echo json_encode($promotions); + + break; +} diff --git a/main/inc/ajax/user_manager.ajax.php b/main/inc/ajax/user_manager.ajax.php index e158e55ee1..bdae7830b8 100755 --- a/main/inc/ajax/user_manager.ajax.php +++ b/main/inc/ajax/user_manager.ajax.php @@ -252,6 +252,8 @@ switch ($action) { case 'user_by_role': api_block_anonymous_users(false); + $status = isset($_REQUEST['status']) ? (int) $_REQUEST['status'] : DRH; + $criteria = new Criteria(); $criteria ->where( @@ -262,7 +264,7 @@ switch ($action) { ) ) ->andWhere( - Criteria::expr()->eq('status', DRH) + Criteria::expr()->eq('status', $status) ); $users = UserManager::getRepository()->matching($criteria); diff --git a/main/inc/lib/system_announcements.lib.php b/main/inc/lib/system_announcements.lib.php index b0f1cae813..8b4c8e46df 100755 --- a/main/inc/lib/system_announcements.lib.php +++ b/main/inc/lib/system_announcements.lib.php @@ -249,6 +249,8 @@ class SystemAnnouncementManager * @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 + * @param int $careerId + * @param int $promotionId * * @return mixed insert_id on success, false on failure */ @@ -261,7 +263,9 @@ class SystemAnnouncementManager $lang = '', $send_mail = 0, $add_to_calendar = false, - $sendEmailTest = false + $sendEmailTest = false, + $careerId = 0, + $promotionId = 0 ) { $original_content = $content; $a_dateS = explode(' ', $date_start); @@ -336,6 +340,11 @@ class SystemAnnouncementManager 'access_url_id' => $current_access_url_id, ]; + if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { + $params['career_id'] = (int) $careerId; + $params['promotion_id'] = (int) $promotionId; + } + foreach ($visibility as $key => $value) { $params[$key] = $value; } @@ -345,19 +354,15 @@ class SystemAnnouncementManager if ($resultId) { if ($sendEmailTest) { self::send_system_announcement_by_email( - $title, - $content, + $resultId, $visibility, - $lang, true ); } else { if ($send_mail == 1) { self::send_system_announcement_by_email( - $title, - $content, - $visibility, - $lang + $resultId, + $visibility ); } } @@ -454,6 +459,8 @@ class SystemAnnouncementManager * @param array $lang * @param int $send_mail * @param bool $sendEmailTest + * @param int $careerId + * @param int $promotionId * * @return bool True on success, false on failure */ @@ -466,7 +473,9 @@ class SystemAnnouncementManager $visibility, $lang = null, $send_mail = 0, - $sendEmailTest = false + $sendEmailTest = false, + $careerId = 0, + $promotionId = 0 ) { $em = Database::getManager(); $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id); @@ -525,26 +534,6 @@ class SystemAnnouncementManager $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')); @@ -565,12 +554,40 @@ class SystemAnnouncementManager // Update visibility $list = self::getVisibilityList(); $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); + + if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { + $params = []; + $params['career_id'] = (int) $careerId; + $params['promotion_id'] = (int) $promotionId; + Database::update( + $table, + $params, + ['id = ? ' => $id] + ); + + } + 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); } + if ($sendEmailTest) { + self::send_system_announcement_by_email( + $id, + $visibility, + true + ); + } else { + if ($send_mail == 1) { + self::send_system_announcement_by_email( + $id, + $visibility + ); + } + } + return true; } @@ -584,7 +601,7 @@ class SystemAnnouncementManager public static function delete_announcement($id) { $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); - $id = intval($id); + $id = (int) $id; $sql = "DELETE FROM $table WHERE id =".$id; $res = Database::query($sql); if ($res === false) { @@ -604,7 +621,7 @@ class SystemAnnouncementManager public static function get_announcement($id) { $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); - $id = intval($id); + $id = (int) $id; $sql = "SELECT * FROM ".$table." WHERE id = ".$id; $announcement = Database::fetch_object(Database::query($sql)); @@ -646,22 +663,28 @@ class SystemAnnouncementManager /** * 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 + * @param int $id + * @param array $visibility + * @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, + $id, $visibility, - $language = null, $sendEmailTest = false ) { + $announcement = self::get_announcement($id); + + if (empty($announcement)) { + return false; + } + + $title = $announcement->title; + $content = $announcement->content; + $language = $announcement->lang; + $content = str_replace(['\r\n', '\n', '\r'], '', $content); $now = api_get_utc_datetime(); $teacher = $visibility['visible_teacher']; @@ -712,6 +735,29 @@ class SystemAnnouncementManager // Expiration date $sql .= " AND (expiration_date = '' OR expiration_date IS NULL OR expiration_date > '$now') "; + // @todo check if other filters will apply for the career/promotion option. + if (isset($announcement->career_id) && !empty($announcement->career_id)) { + $promotion = new Promotion(); + $promotionList = $promotion->get_all_promotions_by_career_id($announcement->career_id); + if (isset($announcement->promotion_id) && !empty($announcement->promotion_id)) { + $promotionList = []; + $promotionList[] = $promotion->get($announcement->promotion_id); + } + if (!empty($promotionList)) { + foreach ($promotionList as $promotion) { + $sessionList = SessionManager::get_all_sessions_by_promotion($promotion['id']); + foreach ($sessionList as $session) { + $users = SessionManager::get_users_by_session($session['id'], 0); + foreach ($users as $user) { + MessageManager::send_message_simple($user['user_id'], $title, $content); + } + } + } + } + + return true; + } + if ((empty($teacher) || $teacher == '0') && (empty($student) || $student == '0')) { return true; } diff --git a/main/install/configuration.dist.php b/main/install/configuration.dist.php index f8b457a687..8ea66eb917 100755 --- a/main/install/configuration.dist.php +++ b/main/install/configuration.dist.php @@ -1356,9 +1356,14 @@ ALTER TABLE notification_event ADD COLUMN event_id INT NULL; // create new user text extra field called 'notification_event' to save the persistent settings. // $_configuration['notification_event'] = false; -//Add help text to put 2 names in registration form +// Add help text to put 2 names in registration form //$_configuration['registration_add_helptext_for_2_names'] = false; +// Allow career/promotions in global announcements +// ALTER TABLE sys_announcement ADD COLUMN career_id INT DEFAULT 0; +// ALTER TABLE sys_announcement ADD COLUMN promotion_id INT DEFAULT 0; +//$_configuration['allow_careers_in_global_announcements'] = false; + // KEEP THIS AT THE END // -------- Custom DB changes // Add user activation by confirmation email