Admin: add config setting "allow_careers_in_global_announcements"

BT#16262
pull/3075/head
Julio Montoya 6 years ago
parent 758dd1a585
commit 5af4e17a8f
  1. 78
      main/admin/system_announcements.php
  2. 23
      main/inc/ajax/career.ajax.php
  3. 4
      main/inc/ajax/user_manager.ajax.php
  4. 124
      main/inc/lib/system_announcements.lib.php
  5. 7
      main/install/configuration.dist.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[] = '<script>
function showCareer() {
$("#promotion").show();
var url = "'.$url.'";
var id = $(\'#career_id\').val();
$.getJSON(
url, {
"career_id" : id,
"a" : "get_promotions"
}
)
.done(function(data) {
$("#promotion_id").empty();
$("#promotion_id").append(
$("<option>", {value: "0", text: "'.addslashes(get_lang('All')).'"})
);
$.each(data, function(index, value) {
$("#promotion_id").append(
$("<option>", {value: value.id, text: value.name})
);
});
$("#promotion_id").selectpicker("refresh");
});
}
</script>';
// Displaying the header.
Display :: display_header($tool_name);
Display::display_header($tool_name);
if ($action != 'add' && $action != 'edit') {
echo '<div class="actions">';
echo '<a href="?action=add">'.Display::return_icon('add.png', get_lang('AddAnnouncement'), [], 32).'</a>';
@ -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('<div id="promotion" style="display:'.$display.';">');
$form->addSelect(
'promotion_id',
get_lang('Promotion'),
$options,
['id' => 'promotion_id']
);
$form->addHtml('</div>');
}
$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(

@ -0,0 +1,23 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Responses to AJAX calls.
*/
require_once __DIR__.'/../global.inc.php';
api_protect_admin_script();
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
switch ($action) {
case 'get_promotions':
$careerId = isset($_REQUEST['career_id']) ? (int) $_REQUEST['career_id'] : 0;
$career = new Promotion();
$promotions = $career->get_all_promotions_by_career_id($careerId);
echo json_encode($promotions);
break;
}

@ -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);

@ -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;
}

@ -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

Loading…
Cancel
Save