@ -1,4 +1,5 @@
<?php
require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
/* For licensing terms, see /license.txt */
define('VISIBLE_GUEST', 1);
define('VISIBLE_STUDENT', 2);
@ -20,8 +21,19 @@ class SystemAnnouncementManager
{
$user_selected_language = api_get_interface_language();
$db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
$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') ";
$tbl_announcement_group = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS);
$temp_user_groups = GroupPortalManager::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, GroupPortalManager::get_parent_groups($user_group['id']));
}
$groups_string = '('.implode($groups,',').')';
$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') ";
switch ($visible) {
case VISIBLE_GUEST :
$sql .= " AND visible_guest = 1 ";
@ -33,6 +45,8 @@ class SystemAnnouncementManager
$sql .= " AND visible_teacher = 1 ";
break;
}
$sql .= " OR id IN (SELECT announcement_id FROM $tbl_announcement_group "
." WHERE group_id in $groups_string) ";
$sql .= " ORDER BY date_start DESC LIMIT 0,7";
$announcements = Database::query($sql);
@ -229,7 +243,7 @@ class SystemAnnouncementManager
* @param int Whether the announcement should be visible to anonymous users (1) or not (0)
* @param string The language for which the announvement should be shown. Leave null for all langages
* @param int Whether to send an e-mail to all users (1) or not (0)
* @return bool True on success, false on failure
* @return mixed insert_id on success, false on failure
*/
public static function add_announcement($title, $content, $date_start, $date_end, $visible_teacher = 0, $visible_student = 0, $visible_guest = 0, $lang = null, $send_mail=0)
{
@ -277,8 +291,51 @@ class SystemAnnouncementManager
Debug::log_s(mysql_error());
return false;
}
return true ;
return Database::insert_id() ;
}
/**
* Makes the announcement id visible only foir groups in groups_array
* @param int announcement id
* @param array array of group id
**/
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) {
Debug::log_s(mysql_error());
return false;
}
foreach ($group_array as $group_id) {
if (intval($group_id) != 0 ) {
$res = Database::query("INSERT into $tbl_announcement_group set announcement_id=".intval($announcement_id)
.", group_id=".intval($group_id));
if ($res === false) {
Debug::log_s(mysql_error());
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_MAIN_GROUP);
//first delete all group associations for this announcement
$res = Database::query("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");
$groups = Database::fetch_array($res);
return $groups;
}
/**
* Updates an announcement to the database
* @param integer $id : id of the announcement
@ -423,4 +480,4 @@ class SystemAnnouncementManager
}
}
}
?>
?>