Location: PHPKode > projects > ConPortal > conportal/inc/announcements/db.php
<?php
/*
 *  ConPortal - Pomona College ITS scheduling appplication
 *  Copyright (C) 2005-2006  Pomona College
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*  Functions in this file:
 *    Data Retrieval Functions:
 *      getAllUnexpiredAnnouncments()
 *      getAnnouncmentDetails($pid)
 *      getExpiredAnnouncements($startDate,$endDate)
 *      getUnexpiredAnnouncmentsForGroup($groupId)
 *    Data Storage Functions
 *      createAnnouncment($subject,$message,$expiration,$author,$group)
 *      expireAnnouncment($pid)
 *      extendAnnouncment($pid,$newDate)
 */

//Takes an announcement PID and gets all the information about it.
//Returns an associative array of the information
function getAnnouncmentDetails($pid)
{
    $query = 'select subject, UNIX_TIMESTAMP(publishDate) as publishDate, '.
             'message, UNIX_TIMESTAMP(expiration) as expiration, '.
             'author, targetGroup from announcements '.
             'where pid=%d';
    $details = safeQuery($query,$pid);
    return mysql_fetch_assoc($details);
}

//Gets all unexpired announcements
//Returns an array of PIDs
function getAllUnexpiredAnnouncments()
{
    $query = 'select pid from announcements where expiration>=NOW()';
    $announcements = safeQuery($query);
    $numRows = mysql_num_rows($announcements);
    $a = array();
    for ($i = 0; $i<$numRows; $i++)
    {
        $temp = mysql_fetch_array($announcements);
        $a[] = $temp[0];
    }
    return $a;
}

//Gets all unexpired announcements
//Returns an array of PIDs
function getUnexpiredAnnouncmentsForGroup($groupId)
{
    $query = 'select pid from announcements where targetGroup = %d && '.
             'expiration>=NOW()';
    $results = safeQuery($query,$groupId);
    $numRows = mysql_num_rows($results);
    $a = array();
    for ($i = 0; $i<$numRows; $i++)
    {
        $temp = mysql_fetch_array($results);
        $a[] = $temp[0];
    }
    return $a;
}

//Gets expired announcements between start and end date.
//Returns an array of PIDs
function getExpiredAnnouncements($startDate,$endDate)
{
    $query = 'select pid from announcements where expiration >= '.
             'FROM_UNIXTIME(%d) and expiration <= FROM_UNIXTIME(%d)';
    $results = safeQuery($query,$startDate,$endDate);
    $numRows = mysql_num_rows($results);
    $a = array();
    for ($i = 0; $i<$numRows; $i++)
    {
        $temp = mysql_fetch_array($results);
        $a[] = $temp[0];
    }
    return $a;
}

//Creates an appointment
//Returns the PID of the newly created appointment
function createAnnouncment($subject,$message,$expiration,$author,$group)
{
    $query = 'insert into announcements set subject="%s", message="%s", '.
             'expiration=FROM_UNIXTIME(%d), author=%d, targetGroup=%d, '.
             'publishDate=NOW()';
    safeQuery($query,mysql_real_escape_string($subject),
                     mysql_real_escape_string($message),
                     $expiration, $author, $group);
    return mysql_insert_id();
}

//Adjusts the end date of an annoucement to today-1
//Returns void
function expireAnnouncment($pid)
{
    $query = 'update announcements set expiration=DATE_SUB(NOW(), interval 1 day) '.
             'where pid = %d';
    safeQuery($query,$pid);
}

//Adjusts expiration
//NO CHECKING IS DONE TO ENSURE DATE IS IN FUTURE
//Returns void
function extendAnnouncment($pid,$newDate)
{
    $query = 'update announcements set expiration=FROM_UNIXTIME(%d) '.
             'where pid = %d';
    safeQuery($query,$newDate,$pid);
}

?>
Return current item: ConPortal