Location: PHPKode > projects > Auxrames > 0.9.5/functions.php
<?php
/**
 * \file functions.php
 * \brief PHP functions nedded by Aurames
 *
 * PHP version 5
 *
 * @category PHP
 * @package  Auxrames
 * @author   Jean-Michel Leyrie <jean-hide@address.com>
 * @license  http://www.gnu.org/licenses/gpl-3.0.html GPLv3
 * @link     http://www.tech.viveris.com
 */
require_once "./config.php";


/** ------------------ LDAP users Functions ----------------- **/


/** \brief Get unknown user from LDAP to database
*
* Only return users with tasks (old or not)
* 
* @todo : Remove users who are no more in databse
* @return none
*/
function syncUsersLDAP()
{
    $users = getUsersLDAP();
    $db = dbConnect();
    
    for ($i=0; $i<count($users);$i++) {
        $sql = 'SELECT COUNT(*) FROM `users` WHERE `id`='.$users[$i][0].'
                AND `name`="'.$users[$i][1].'"';
        $result = mysql_query($sql)
            or die ("$i Unable to check user presence: " . mysql_error());
            
        if (mysql_result($result, 0) == 0) {
            echo "<br>User ".$users[$i][1]." not yet in DB, add in progress...<br>";
            $uidnumber = preg_replace('/ /','',$users[$i][0]);
            $login = preg_replace('/ /','',$users[$i][1]);
            $sql = 'INSERT INTO `users` (`id`, `name`) VALUES ('.$uidnumber.',
                   "'.$login.'")';
            
            $result = mysql_query($sql)
                or die ('Unable to insert user: ' . mysql_error());
        }
    };
    //mysql_close();
}

/** \brief Get all users from LDAP
*
* @return Array with users
*/
function getUsersLDAP()
{
    $connection = ldap_connect(CONF_LDAPSRV)
        or die(ldap_error($connection));

    ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3)
        or die(ldap_error($connection));
        
    ldap_bind($connection, CONF_LDAPBINDACCOUNT, CONF_LDAPBINDPASS) 
        or die(ldap_error($connection));

    $ldapObject = array(CONF_LDAPUSERSID, CONF_LDAPUSERSLOGIN);
   
    $result = ldap_list(
        $connection,
        CONF_LDAPUSERSDN, CONF_LDAPUSERSFILTER,
        $ldapObject
    )
        or die (ldap_error($connection));

    $usersArray=ldap_get_entries($connection, $result);
    $row = array ($usersArray[0][CONF_LDAPUSERSID][0], $usersArray[0][CONF_LDAPUSERSLOGIN][0]);
    $arrayIdLogin = array();
    array_push($arrayIdLogin, $row);
    for ($i=1; $i<$usersArray["count"]; $i++) {
        $row = array($usersArray[$i][CONF_LDAPUSERSID][0], $usersArray[$i][CONF_LDAPUSERSLOGIN][0]);
        array_push($arrayIdLogin, $row);
    }
    sort($arrayIdLogin);
    
    return $arrayIdLogin;
}

/** ------------------ MySQL users Functions ----------------- **/

/** \brief Function to connect to database
*
* @return Connection handler
*/
function dbConnect()
{
    $db = mysql_connect(CONF_MYSQLSERVER, CONF_MYSQLID, CONF_MYSQLPASS)
        or die("Unable to connect to db : " . mysql_error());
    mysql_select_db(CONF_MYSQLDB, $db)
        or die("Unable to select db : " . mysql_error());
    return $db;
}

/** \brief Function to get users list
*
* @return String : Users list
*/
function getUsersListMySQL()
{
    $db = dbConnect();

    $result = mysql_query(CONF_USERSQUERY)
        or die ('Unable to get users list: ' . mysql_error());

    $users=array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($users, $row);
    }

    //mysql_close();
    
    return $users;
}

/** \brief Function to get user name
*
* @param [in] $id User's ID
* 
* @return String : User's name
*/
function getUserNameMYSQL($id)
{
    $db = dbConnect();
    
    $result = mysql_query(CONF_USERNAME.$id);

    if (mysql_num_rows($result)==0) {
        $userName = null;
    } else {
        $userName = mysql_result($result, 0);
    }
    //mysql_close();
    
    return $userName;
}

/** \brief Function to get user id
*
* @param [in] $userName User's name
* 
* @return String : User's id
*/
function getUserIdMYSQL($userName)
{
    $db = dbConnect();
    $sql = CONF_USERID.$userName.'"';
    
    $result = mysql_query(CONF_USERID.$userName.'"');
    $userName = mysql_result($result, 0);
    //mysql_close();
    
    return $userName;
}

/** \brief Function to always display user in MMI
*
* @param [in] $id            User's id
* @param [in] $alwaysDisplay Set(1) or reset(0)
* 
* @return None
*/
function setAlwaysDisplayMySQL($id,$alwaysDisplay)
{
    $db = dbConnect();
    $sql = "UPDATE `users` SET `display` = $alwaysDisplay
        WHERE `users`.`id` = $id LIMIT 1;";
    $result = mysql_query($sql)
        or die ('Unable to insert user: ' . mysql_error());
    //mysql_close();
}

/** \brief Function to set location of an user
*
* @param [in] $locset 0->reset; 1->set location
* @param [in] $id     User's ID
* @param [in] $loc    Location register as defined in configuration file
* 
* @return none
*/
function setLocationMYSQL($locset,$id,$loc)
{
    // Get old location register
    $locationRegister = getLocation($id);
    
    // Update old location register
    if ($locset) {
        $locationRegister = $locationRegister | pow(2, $loc);
    } else {
        $locationRegister = $locationRegister & ~pow(2, $loc);
    }
    $sql = "UPDATE `users` SET `location` = $locationRegister
        WHERE `users`.`id` = $id LIMIT 1;";
    $result = mysql_query($sql)
        or die ('Unable to modify location: ' . mysql_error());
    //mysql_close();
}

/** \brief Function to get location of an user
*
* @param [in] $id User's ID
* 
* @return Difference location as defined in configuration file
*/
function getLocationMYSQL($id)
{
    $db = dbConnect();
    // Get location register
    $sql = "SELECT location FROM `users` WHERE `users`.`id` = $id";
    $result = mysql_query($sql);
    $locationRegister = mysql_result($result, 0);
    return $locationRegister;
}

/** ------------------ Users Functions ----------------- **/

/** \brief Wrapper function to always display user in MMI
*
* @param [in] $id            User's id
* @param [in] $alwaysDisplay Set(1) or reset(0)
* 
* @return None
*/
function setAlwaysDisplay($id, $alwaysDisplay)
{
    setAlwaysDisplayMySQL($id, $alwaysDisplay);
}

/** \brief Wrapper function to get user id
*
* @param [in] $userName User's name
* 
* @return String : User's id
*/
function getUserId($userName)
{
    if (CONF_USERDB == "LDAP") {
        die ('Error, LDAP user management not yet implemented');
    } elseif (CONF_USERDB == "MYSQL") {
        return getUserIdMYSQL($userName);
    } else {
        die ('Error, no user management mode defined in config file');
    }
}

/** \brief Wrapper function to get users list
*
* @return String : Users list
*/
function getUsersList()
{
    if (CONF_USERDB == "LDAP") {
        die ('Error, LDAP user management not yet implemented');
    } elseif (CONF_USERDB == "MYSQL") {
        return getUsersListMySQL();
    
    } else {
        die ('Error, no user management mode defined in config file');
    }
}

/** \brief Wrapper function to get user name
*
* @param [in] $id User's ID
* 
* @return String : User's name
*/
function getUserName($id)
{
    if (CONF_USERDB == "LDAP") {
        die ('Error, LDAP user management not yet implemented');
    } elseif (CONF_USERDB == "MYSQL") {
        return getUserNameMYSQL($id);
    
    } else {
        die ('Error, no user management mode defined in config file');
    }
}

/** \brief Wrapper function to set location of an user
*
* @param [in] $locset 0->reset; 1->set location
* @param [in] $id     User's ID
* @param [in] $loc    Location register as defined in configuration file
* 
* @return none
*/
function setLocation($locset, $id, $loc)
{
    setLocationMYSQL($locset, $id, $loc);
}

/** \brief Wrapper function to get location of an user
*
* @param [in] $id User's ID
* 
* @return Difference location as defined in configuration file
*/
function getLocation($id)
{
    return getLocationMYSQL($id);
}

/** ------------------ Dates Functions ----------------- **/

/** \brief Compute difference between two dates (unix time stamp)
*
* @param [in] $a First date
* @param [in] $b Second date
* 
* @return Difference between $a and $b
*/
function dateDiff($a,$b)
{
    $date1 = intval(substr($a, 0, 4))*12+intval(substr($a, 4, 2));
    $date2 = intval(substr($b, 0, 4))*12+intval(substr($b, 4, 2));

    return $date1-$date2;
}

/** \brief Compute number of day remaining in a month
*
* @param [in] $date Begin date
* 
* @return Number of days from $date to end of month
*/
function daysRemainingInMonth($date)
{
    $startMonth = idate('m', strtotime($date));
    $startYear  = idate('Y', strtotime($date));
    $startMonth = mktime(0, 0, 0, $startMonth, 1, $startYear); 
    $startDay   = idate('d', strtotime($date));
    $firstMonthNbOfDays = intval(date("t", $startMonth));
    $firstMonthNbOfDays = $firstMonthNbOfDays - $startDay;
    
    return $firstMonthNbOfDays;
}

/** \brief Compute number of day in a month
*
* @param [in] $month Month
* @param [in] $year  Year of the month
* 
* @return Number of days
*/
function daysInMonth($month,$year)
{
    $m= mktime(0, 0, 0, $month, 1, $year);
    return intval(date("t", $m));
}

/** \brief Compute number of month between two dates
*
* @param [in] $startDate Start date
* @param [in] $stopDate  Stop date
* 
* @return Number of month
*/
function nbOfMonth($startDate,$stopDate)
{
    $startMonth = idate('m', strtotime($startDate));
    $startYear  = idate('Y', strtotime($startDate));
    $stopMonth  = idate('m', strtotime($stopDate));
    $stopYear   = idate('Y', strtotime($stopDate));
    
    $nbOfMonth = $stopMonth - ($startYear -$stopYear)*12 - $startMonth +1;
    
    return $nbOfMonth;
}

/** \brief Get older event to determine max date
*
* @return Older event in an array
*/
function getOlderEvent()
{
    $db = dbConnect();
    $sql = "SELECT MAX(stopDate) FROM tasks";
    $result = mysql_query($sql)
        or die ('Unable to get older event: ' . mysql_error());
    $olderEvent = mysql_fetch_array($result, MYSQL_NUM);
    //mysql_close();
    return ($olderEvent[0]);
}

/** \brief Look of two duration are overlapping
*
* @param [in] $startDate1 Start date 1
* @param [in] $stopDate1  End date 1
* @param [in] $startDate2 Start date 2
* @param [in] $stopDate2  End date 2
* 
* @return False if duration are not overlapping, true instead
*/
function isOverlapping($startDate1, $stopDate1,$startDate2, $stopDate2)
{
    if ((strtotime($stopDate1)<strtotime($startDate2))
        OR (strtotime($stopDate2)<strtotime($startDate1))
    ) {
        return false;
    } else {
        return true;
    }
}

/** \brief Get duration between two dates
*
* @param [in] $startDate Start date
* @param [in] $stopDate  End date
* 
* @return Number of days between the two dates
*/
function getDuration($startDate, $stopDate)
{
    $nbjours = ceil(strtotime($stopDate)/(60*60*24)-1) -
               ceil(strtotime($startDate)/(60*60*24)-1);
    return $nbjours;
}

/** ------------------ Core Event Functions ----------------- **/


/** \brief Smooth brigthness dependings on workload
*
* @param [in] $workload workload percent to apply [0;100]
* 
* @return Brightness [0;1]
*/
function smoothWorkload($workload)
{
    if ($workload < 25) {
        $workload = 40;
   } elseif ($workload < 50) {
        $workload = 60;
    } elseif ($workload < 75) {
        $workload = 80;
    } else {
        $workload = 100;
    }
    return $workload/100;
}

/** \brief Get calendar for a user
*
* @param [in] $userId    User's ID
* @param [in] $beginDate Begin date for calendar computation
* 
* @todo : Rework this awfull function !!
* @return Array with the whole planning
*/
function computePlanning($userId,$beginDate)
{
    $tasks = getTasks($userId);

    if (count($tasks) == 0) {
        return null;
    }

    $index = 0;
    $nonWorkingTime = 0;
    
    for ($i=1; $i<count($tasks); $i=$i+1) {
        if (strtotime($tasks[$i]['startDate']) <= strtotime($beginDate)) {
            $tasks[$i]['startDate'] = date("Y-m-d H:i:s", strtotime($beginDate));
        }
    }

    // if first task begins before begin date
    if (strtotime($tasks[0]['startDate']) <= strtotime($beginDate)) {
        $tasks[0]['startDate'] = date("Y-m-d H:i:s", $beginDate);
        
        $planning[$index]['duration'] = getDuration(
            $beginDate, $tasks[0]['stopDate']
        );
                        
        if ($tasks[0]['holidays'] == 1) {
            $planning[$index]['work'] = 2;
        } else {
            $planning[$index]['work'] = 1;
        }
        $planning[$index]['name']=$tasks[0]['name'];
        $planning[$index]['info']=$tasks[0]['info'];
        $planning[$index]['startDate']=date(CONF_DATE, strtotime($beginDate));
        $planning[$index]['stopDate']=date(
            CONF_DATE, strtotime($tasks[0]['stopDate'])
        );
        $planning[$index]['manager']=getUserName($tasks[0]['manager']);
        $planning[$index]['id']=$tasks[0]['id'];
        $planning[$index]['workload']=$tasks[0]['workload'];
        $planning[$index]['brightness']=smoothWorkload($tasks[0]['workload']);
        
        $index++;
    } else {
        $planning[$index]['overlapping'] = 0;
        $planning[$index]['duration'] = getDuration(
            date("Y-m-d H:i:s", time()), $tasks[0]['startDate']
        );
        $planning[$index]['work'] = 0;
        $planning[$index]['startDate']=date(CONF_DATE, time());
        $planning[$index]['stopDate']=date(
            CONF_DATE, strtotime($tasks[0]['startDate'])
        );
        $index++;

        $planning[$index]['overlapping'] = 0;
        $planning[$index]['duration'] = getDuration(
            $tasks[0]['startDate'], $tasks[0]['stopDate']
        );
        $planning[$index]['work'] = 1;
        $planning[$index]['name']=$tasks[0]['name'];
        $planning[$index]['info']=$tasks[0]['info'];
        $planning[$index]['startDate']=date(
            CONF_DATE, strtotime($tasks[0]['startDate'])
        );
        $planning[$index]['stopDate']=date(
            CONF_DATE, strtotime($tasks[0]['stopDate'])
        );
        $planning[$index]['manager']=getUserName($tasks[0]['manager']);
        $planning[$index]['id']=$tasks[0]['id'];
        $planning[$index]['workload']=$tasks[0]['workload'];
        $planning[$index]['brightness']=smoothWorkload($tasks[0]['workload']);
        
        if ($tasks[0]['holidays'] == 1) {
            $planning[$index]['work'] = 2;
        } else {
            $planning[$index]['work'] = 1;
        }
        $index++;
    }
    
    for ($i=1; $i<count($tasks); $i=$i+1) {
        $overlap = isOverlapping(
            $tasks[$i-1]["startDate"],
            $tasks[$i-1]["stopDate"],
            $tasks[$i]["startDate"],
            $tasks[$i]["stopDate"]
        );
        if ($overlap) {
            $planning[$index]['overlapping'] = 1;
            $planning[$index]['overlappingdelay'] = getDuration(
                date("Y-m-d H:i:s", time()), $tasks[$i]['startDate']
            );
            $planning[$index]['duration'] = getDuration(
                $tasks[$i]['startDate'], $tasks[$i]['stopDate']
            );
            $planning[$index]['work'] = 1;
            $planning[$index]['name']=$tasks[$i]['name'];
            $planning[$index]['info']=$tasks[$i]['info'];
            $planning[$index]['startDate']=date(
                CONF_DATE, strtotime($tasks[$i]['startDate'])
            );
            $planning[$index]['stopDate']=date(
                CONF_DATE, strtotime($tasks[$i]['stopDate'])
            );
            $planning[$index]['manager']=getUserName($tasks[$i]['manager']);
            $planning[$index]['id']=$tasks[$i]['id'];
            $planning[$index]['workload']=$tasks[$i]['workload'];
            $planning[$index]['brightness']=smoothWorkload($tasks[$i]['workload']);
            
            if ($tasks[$i]['holidays'] == 1) {
                $planning[$index]['work'] = 2;
            } else {
                $planning[$index]['work'] = 1;
            }
            $index++;
        } else {
            $planning[$index]['overlapping'] = 0;
            $planning[$index]['duration'] = getDuration(
                $tasks[$i-1]['stopDate'], $tasks[$i]['startDate']
            );
            $planning[$index]['work'] = 0;
            $planning[$index]['startDate']=date(
                CONF_DATE, strtotime($tasks[$i-1]['stopDate'])
            );
            $planning[$index]['stopDate']=date(
                CONF_DATE, strtotime($tasks[$i]['startDate'])
            );
            $index++;

            $planning[$index]['overlapping'] = 0;
            $planning[$index]['duration'] = getDuration(
                $tasks[$i]['startDate'], $tasks[$i]['stopDate']
            );
            $planning[$index]['work'] = 1;
            $planning[$index]['name']=$tasks[$i]['name'];
            $planning[$index]['info']=$tasks[$i]['info'];
            $planning[$index]['startDate']=date(
                CONF_DATE, strtotime($tasks[$i]['startDate'])
            );
            $planning[$index]['stopDate']=date(
                CONF_DATE, strtotime($tasks[$i]['stopDate'])
            );
            $planning[$index]['manager']=getUserName($tasks[$i]['manager']);
            
            $planning[$index]['id']=$tasks[$i]['id'];
            $planning[$index]['workload']=$tasks[$i]['workload'];
            $planning[$index]['brightness']=smoothWorkload($tasks[$i]['workload']);
            
            if ($tasks[$i]['holidays'] == 1) {
                $planning[$index]['work'] = 2;
            } else {
                $planning[$index]['work'] = 1;
            }
            $index++;
        }
    }
    return $planning;
}

/** \brief Get whole calendar between two dates
*
* @param [in] $startDate Start date
* @param [in] $stopDate  Stop date
* 
* @return Array with the whole calendar
*/
function getCalendar($startDate, $stopDate)
{
    $firstMonthNbOfDays = daysRemainingInMonth($startDate);
    $nbOfMonth = nbOfMonth($startDate, $stopDate);
    $index = 0;
    $calendar[$index]['month'] = date("M", strtotime($startDate));
    $calendar[$index++]['nbDays']= $firstMonthNbOfDays;
    
    if ($nbOfMonth == 2) {
        $calendar[$index]['month']    = date("M", strtotime($stopDate));
        $calendar[$index++]['nbDays'] = idate('d', strtotime($stopDate));
    } elseif ($nbOfMonth > 2) {
        $month = idate('m', strtotime($startDate));
        $year  = idate('Y', strtotime($startDate));
        $month = ($month + 1) % 12;
        if ($month == 1) {
            $year++;
        }
        
        for ($j=2 ; $j<$nbOfMonth ; $j++) {
            $calendar[$index]['month']=date("M", mktime(0, 0, 0, $month, 1, $year));
            
            $calendar[$index++]['nbDays'] = daysInMonth($month, $year);
            
            $month = ($month + 1) % 12;
            if ($month == 1) {
                $year++;
            }
        }
        $calendar[$index]['month']    = date("M", strtotime($stopDate));
        $calendar[$index++]['nbDays'] = idate('d', strtotime($stopDate));
    }
    
    return $calendar;
}

/** \brief Insert a projects into database
*
* Unused
*
* @param [in] $projectName Name of the project
* 
* @return none
*/
function insertProject($projectName)
{
    $db = dbConnect();
    $sql = "INSERT INTO `projects` (`id`, `name`) VALUES (NULL, '$projectName');";
    $result = mysql_query($sql)
        or die ('Unable to insert project: ' . mysql_error());
        
    //mysql_close();
}

/** \brief Get all projects from database
*
* Unused
*
* @return none
*/
function getProjects()
{
    $db = dbConnect();
    $result = mysql_query("SELECT * FROM `projects`") 
        or die ('Unable to get project : ' . mysql_error());
    $projects=array();
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        array_push($projects, $row);
    }

    mysql_free_result($result);
    //mysql_close();
    
    return $projects;
}


/** \brief Get all users from database. 
*
* Only return users with tasks (old or not)
* 
* @return none
*/
function getUsers()
{
    $db = dbConnect();

    $result = mysql_query(CONF_USERSWITHTASKS)
        or die ('Unable to get tasks : ' . mysql_error());
        
    $users=array();
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        array_push($users, $row[0]);
    }
    
    mysql_free_result($result);
    //mysql_close();
    
    return $users;
}

/** \brief Test if a task already exists in the database
*
* @param [in] $name      Name of the task to look for
* @param [in] $userId    User's id of the task to look for
* @param [in] $startDate Start date of the task to look for
* @param [in] $stopDate  Stop date of the task to look for
* @param [in] $manager   Manager id of the task to look for
* @param [in] $info      Additionnal information of the task to look for
* 
* @return none
*/
function testExistingTask($name, $userId,$startDate,$stopDate, $manager,$info)
{
    $startDate = date("Y-m-d H:i:s", strtotime($startDate));
    $stopDate  = date("Y-m-d H:i:s", strtotime($stopDate));
    $db = dbConnect();
    $sql = "SELECT * FROM `tasks` WHERE  `name`=\"$name\" AND `manager`=$manager AND
           `idUser`=$userId AND `startDate`=\"$startDate\" AND
           `stopDate`=\"$stopDate\" AND `info`=\"$info\"";
    
    $result = mysql_query($sql)
        or die ('Unable to get existing tasks : ' . mysql_error());

    $ExistingTasks = array();
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($ExistingTasks, $row);
    }
    //mysql_close();
    
    if (count($ExistingTasks) != 0) {
        return true;
    } else {
        return false;
    }
    
    //mysql_close();
}

/** \brief insert a tasks in the database
*
* @param [in] $name      New name
* @param [in] $userId    Task's user
* @param [in] $startDate Start date
* @param [in] $stopDate  Stop date
* @param [in] $manager   Manager id
* @param [in] $holidays  Set if task is hollidays or not
* @param [in] $info      Information from drop down list
* 
* @return none
*/
function insertTask ($name, $userId,$startDate,$stopDate, $manager, $holidays, $workload,$info)
{
    $startDate = date("Y-m-d H:i:s", strtotime($startDate));
    $stopDate  = date("Y-m-d H:i:s", strtotime($stopDate));
    
    if (false == testExistingTask($name, $userId, $startDate, $stopDate, $manager,$info)) {
        $db = dbConnect();
        $sql = "INSERT INTO `tasks` (`name`,`manager`,`idUser`,`startDate`,`stopDate`
                ,`holidays`,`workload`,`info`) VALUES ('$name','$manager', '$userId','$startDate',
                '$stopDate','$holidays','$workload','$info');";
        $result = mysql_query($sql)
            or die ('Unable to insert task : ' . mysql_error());

        //mysql_close();
    } else {
        echo "Task already exists<br>";
    }
}

/** \brief Update a tasks
*
* @param [in] $id        Id of the task to update
* @param [in] $name      New name
* @param [in] $startDate New start date
* @param [in] $stopDate  New stop date
* @param [in] $managerId New manager id
* @param [in] $holidays  Set if task is hollidays or not (to be implemented)
* 
* @todo : implement holidays field update
* @return none
*/
function updateTask ($id, $name, $startDate,$stopDate, $managerId, $holidays, $workload,$info)
{
    $startDate = date("Y-m-d H:i:s", strtotime($startDate));
    $stopDate  = date("Y-m-d H:i:s", strtotime($stopDate));
    
    $db = dbConnect();

    $sql = "UPDATE `tasks` SET `name`='$name',
        `startDate`='$startDate',`stopDate`='$stopDate',
        `workload`='$workload',`manager`='$managerId',
        `info`='$info'
        WHERE `tasks`.`id` = $id LIMIT 1;";
    
    $result = mysql_query($sql)
        or die ('Unable to update task : ' . mysql_error());

    //mysql_close();
    
}

/** \brief Delete a tasks
*
* @param [in] $taskId Id of the task as in the database
* 
* @return none
*/
function deleteTask($taskId)
{
    $db = dbConnect();
    $sql ="DELETE FROM `tasks` WHERE `id`=$taskId";
    $result = mysql_query($sql)
        or die ('Unable to remove tasks : ' . mysql_error());

    //mysql_close();
}
/** \brief Get all tasks for a userId
*
* @param [in] $userId Id of the user as in the database
* 
* @return Array with user's tasks
*/
function getTasks ($userId=-1)
{
    $db = dbConnect();
    $date = date("Y-m-d H:i:s");
    if ($userId == -1) {
        $sql ="SELECT * FROM `tasks` WHERE `stopDate`>'$date' ORDER by startDate";
    } else {
        $sql ="SELECT * FROM `tasks` WHERE `idUser`=$userId AND
              `stopDate`>'$date' ORDER by startDate";
    }
    $result = mysql_query($sql)
        or die ('Unable to get tasks : ' . mysql_error());
        
    $userTasks = array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if (strtotime($row['stopDate'])> time()) {
            array_push($userTasks, $row);
        }
    }
    mysql_free_result($result);
    
    if ($userId==-1) {
        // Add username is list is asked for all users
        for ($i=0; $i < count($userTasks); $i++ ) {
            $userTasks[$i]['userName'] = getUserName($userTasks[$i]['idUser']);
        }
        sksort($userTasks,"userName",true);    
    }
    
    return $userTasks;
}

/** \brief Get all tasks for a userId
*
* @param [in] $array          array to sort
* @param [in] $subkey         subkey reference form the sort
* @param [in] $sort_ascending sort ascending or not
* 
* Get from serpro at gmail dot com, http://fr2.php.net/ksort
*
* @return Array sort
*/
function sksort(&$array, $subkey="id", $sort_ascending=false) {

    if (count($array))
        $temp_array[key($array)] = array_shift($array);

    foreach($array as $key => $val){
        $offset = 0;
        $found = false;
        foreach($temp_array as $tmp_key => $tmp_val)
        {
            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
            {
                $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
                                            array($key => $val),
                                            array_slice($temp_array,$offset)
                                          );
                $found = true;
            }
            $offset++;
        }
        if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
    }

    if ($sort_ascending) $array = array_reverse($temp_array);

    else $array = $temp_array;
}

/** \brief Delete old tasks
*
* Delete from database all task with end date inferior from current date.
* 
* @return none
*/
function deleteOldTaks()
{
    $db = dbConnect();
    $sql = 'DELETE FROM `tasks` WHERE stopDate<\''.date(CONF_DATEMYSQL, time()).'\'';
    
    $result = mysql_query($sql)
        or die ('Unable to remove old tasks : ' . mysql_error());
    //mysql_close();
}

?>
Return current item: Auxrames