Location: PHPKode > projects > Internet Task Management System > itms_mysql_build_0111/reminder.php
<?php

/*
 * ITMS ValleyData source file version 1.0 May 11, 2001
 *
 * This file is run by lynx daily
 * For every ITMS user this file must:
 * send an email reminder to this person about tasks which are overdue
 * or tasks which are due before the next reminder this person has set in
 * his/her preferences if he/she is supposed to get a reminder today.
 *
 *
 *
 * Internet Task Management System: An online system used for recording information about and assigning tasks and processes.
 * Copyright (C) 2001  ValleyData Programming Group
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * See file named "gpl.txt" included with source code or
 * visit http://www.gnu.org/copyleft/gpl.txt on the internet.
 */

include("config.php");
include("error_handler.php");
include("db_tools.php");
include("toolbox.php");

if(isset($save)) //to delete tasks for those html emailers
{
    db_open();
    db_use();
    $query = "SELECT PT.date_assigned, PT.period, PT.period_unit, PT.due_date, ".
			 "PT.tid, PT.title, PT.notify, PT.info, ".
		     "U1.name AS assigner, U2.name AS assignedTo, U1.email, U1.html FROM pending_tasks PT, ".
		     "users U1, users U2 WHERE PT.uid = '$uid' AND PT.assigner = U1.uid AND PT.uid = U2.uid";
    $result = db_query($query); // Get all details of user's pending tasks 
	$assignedTo_email = get_user_email($uid);
    while($row = db_fetch_row($result))
    {
        $tid = $row["tid"];
        $cbdone = "done$tid";
        if(isset($$cbdone))
        {
            if($row["notify"] == 1)
            {
                //send notification
				if(isset($row["email"]))
				{
					notifyComplete(
							$row["title"], 
							$row["email"], 
							$row["assignedTo"],
							$assignedTo_email,
							$row["info"],
                            $row["due_date"],
						    get_priority_string($row["priority"]),
							$row["html"]);
				}
            }
			if($row["period"] != "0" && $row["period"] != "") // If this is a periodic task...
			{
				$due_date = $row["due_date"];//sample: 2001-03-11 18:59:56 
				$due_year = substr($due_date, 0, 4);
				$due_month = substr($due_date, 5, 2);
				$due_day = substr($due_date, 8, 2);
				$due_hour = substr($due_date, 11, 2);
				$due_min = substr($due_date, 14, 2);
				$assigned_day = substr($row["date_assigned"], 8, 2);
				
				switch($row["period_unit"])
				{
					case "0"://days
						$due_day += $row["period"];
						break;
					case "1"://weeks
						$due_day += ($row["period"] * 7);
						break;
					case "2"://months
						$month_num = $due_month + $row["period"];
						$days_to_add = get_days_in_month(mktime($due_hour, $due_min, 0, $due_month, $due_day, $due_year));
						/*
						$next_month_days = get_days_in_month(mktime($due_hour, $due_min, 0, $due_month + 1, 1, $due_year));
						if($days_to_add > $next_month_days)
							$days_to_add = $next_month_days;
						*/
						($assigned_day > $due_day) ? $max_day = $assigned_day : $max_day = $due_day;
						for($i = $due_month+1; $i <= $month_num; $i++)
						{
							$curr_day = date('d', mktime($due_hour, $due_min, 0, $month_num-1, $days_to_add, $due_year));
							$next_month_days = get_days_in_month(mktime($due_hour, $due_min, 0, $i, 1, $due_year));
							if($max_day > $curr_day)
							{
								$curr_day = $max_day;
							}
							if($curr_day > $next_month_days)
							{
								$curr_day = $next_month_days;
							}
							$days_to_add += $curr_day;
						}
						$due_day = $days_to_add;
						break;
				}
				$new_due = mktime($due_hour, $due_min, 0, $due_month, $due_day, $due_year);
				$new_due = date('Y-m-d H:i:s', $new_due);
				$query = "UPDATE pending_tasks SET date_assigned = '$due_date', due_date='$new_due'  WHERE tid = '$tid'"; // Sets the new due date
			}
			else
			{
				$query = "DELETE FROM pending_tasks WHERE tid = '$tid'";
            }
            db_open();
            db_use();
            $task_name = get_pending_task_name($tid);
			if(db_query($query)) // Update or delete pending task
            {
                message_box("$task_name completed.");
            }
        }// end if(isset($$cbdone))
    }// end while($row = db_fetch_row($result))
    print("<form method=\"post\">\n");
    print("<input type=\"button\" value=\"Close\" onclick=\"parent.window.close()\"></form>\n");
}

else if($key != $EXECUTE_KEY) // This is an aribtrary number defined in config.php that matches reminder.cron
{
    die("Bad key supplied for reminder.php");
}

else // If the key is correct
{
    $time = time();

    //first: get the date and time
    $date_array = getdate($time);

    $weekday = $date_array["weekday"];
    $wday = $date_array["wday"];

    $date_string = date('Y-m-d H:i:s');

    //second: get a list of all the users
    $all_users_array = get_all_users();

    //foreach user:
    foreach($all_users_array as $user)
    {
        $user_num = $user["uid"];
        $reads_html = $user["html"];
        // check to see if this user is supposed to get reminders today
        if(is_reminder_day($wday, $user_num))
        {
            // get his/her next reminder day
            $days_until_next_reminder = get_days_until_next_reminder($wday, $user_num);

            //construct a date based on the next_reminder_day
            $newtime_stamp = add_days($days_until_next_reminder, $time);

            $next_reminder_date = date('Y-m-d H:i:s', $newtime_stamp);

            // get all overdue tasks
            db_open();
            db_use();
            $query_tasks = "SELECT * FROM pending_tasks WHERE uid = '$user_num' AND due_date < '$date_string' ORDER BY due_date";
            $result = db_query($query_tasks);
            while($row = db_fetch_row($result))
            {
                $overdue = "true";
                $OTS = "Overdue Tasks:\n" .
                    "<form name=\"save\" METHOD=\"POST\" ACTION=\"" . $ITMS_ROOT . "reminder.php\">" .
                    "<table border=\"1\">\n<tr><td>Task Name</td>\n<td>Due Date</td>\n<td>Assigned " .
                    "By</td>\n<td>Description</td>\n<td>Priority</td>\n<td>Task Done?</td></tr>\n";

                $title = $row["title"];
                $due_date = convert_date($row["due_date"]);
                $assigner = $row["assigner"];
                $assigner = get_user_name($assigner);
                $info = nl2br($row["info"]);
                $priority = get_priority_string($row["priority"]);
                
                $overdue_html .= "<tr><td>$title</td>\n<td>$due_date</td>\n".
                    "<td>$assigner</td>\n<td>$info</td>\n<td>$priority</td>\n".
                    "<td><INPUT TYPE=\"checkbox\" NAME=\"done" . $row["tid"] . "\">Task Done</td></tr>\n";
                $OTE = "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" .
                       "<td><INPUT TYPE=\"hidden\" name=\"save\" value=\"save\">".
                       "<INPUT TYPE=\"hidden\" name=\"uid\" value=\"$user_num\">".
                       "<INPUT TYPE=\"submit\" value=\"Complete Selected Tasks\" name=\"save\">".
                       "</td></tr></table></form>\n";

                $overdue_text .= "$title, assigned by: $assigner is due: $due_date, with priority: $priority, $info.\n";
            }

            // get all tasks due before the next reminder day
            
            db_open();
            db_use();
            $query_tasks = "SELECT * FROM pending_tasks WHERE uid = '$user_num'" . 
                           "AND due_date BETWEEN '$date_string' AND '$next_reminder_date' ORDER BY due_date";
            $result = db_query($query_tasks);
            while($row = db_fetch_row($result))
            {
                $remind = "true";
                $RTS = "Tasks Due before next Reminder:\n" .
                    "<form name=\"save\" METHOD=\"POST\" ACTION=\"" . $ITMS_ROOT . "reminder.php\">" .
                    "<table border=\"1\">\n<tr>\n<td>Task Name</td>\n<td>Due Date</td>\n<td>Assigned " . "By</td>\n<td>Description</td>\n<td>Priority</td>\n<td>Task Done?</td>\n</tr>\n";

                $title = $row["title"];
                $due_date = convert_date($row["due_date"]);
                $assigner = $row["assigner"];
                $assigner = get_user_name($assigner);
                $info = nl2br($row["info"]);
                $priority = get_priority_string($row["priority"]);

                $reminder_html .= "<tr>\n<td>$title</td>\n<td>$due_date</td>\n" .
                    "<td>$assigner</td>\n<td>$info</td>\n<td>$priority</td>\n".
                    "<td><INPUT TYPE=\"checkbox\" NAME=\"done" . $row["tid"] . "\">Task Done</td></tr>\n";
                $RTE = "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" .
                       "<td><INPUT TYPE=\"hidden\" name=\"save\" value=\"save\">".
                       "<INPUT TYPE=\"hidden\" name=\"uid\" value=\"$user_num\">".
                       "<INPUT TYPE=\"submit\" value=\"Complete Selected Tasks\" name=\"save\">".
                       "</td></tr></table></form>\n";

                $reminder_text .= "$title, assigned by: $assigner is due: $due_date, with priority: $priority, $info.\n";
            }
            
            $html = "<html>\n<body>\n$OTS $overdue_html $OTE $RTS $reminder_html $RTE\n</body>\n<html>";
            
            if(isset($overdue) || isset($remind))
            {
                $to = get_user_email($user_num);
                $subject = "ITMS Task Reminder";
                $headers = "Content-Type: text/html;";
                // send an email reminder to this person if there are overdue or reminder-needing tasks
                if($reads_html == "1")
                {
                    $body = $html;
                    mail(stripslashes($to), stripslashes($subject), stripslashes($body), stripslashes($headers));
                }
                else
                {
                    if(isset($overdue))
                        $text = "Overdue Tasks:\n$overdue_text\n";
                    if(isset($remind))
                        $text .= "Tasks Due before next Reminder:\n$reminder_text";
                    $body = $text;
                    mail(stripslashes($to), stripslashes($subject), stripslashes($body), stripslashes($headers));
                }
            }
        }//end if(reminder day)
        unset($overdue);
        unset($remind);
        unset($overdue_html);
        unset($reminder_html);
        unset($overdue_text);
        unset($reminder_text);
    }//end foreach user

}//end else

// Returns true if the specified day is one of the user's preferred reminder days.
function is_reminder_day($wday, $user_num)
{
    $ret = false;
    $pref_array = array();

    db_open();
    db_use();
    $query_prefs = "SELECT * FROM users WHERE uid = '$user_num'";
    $result = db_query($query_prefs);
    $row = db_fetch_row($result);
    array_push($pref_array, $row["remindS"]);
    array_push($pref_array, $row["remindM"]);
    array_push($pref_array, $row["remindT"]);
    array_push($pref_array, $row["remindW"]);
    array_push($pref_array, $row["remindR"]);
    array_push($pref_array, $row["remindF"]);
    array_push($pref_array, $row["remindA"]);

    if($pref_array[$wday] == 1)
    {
        $ret = true;
    }
    return $ret;
}

// Returns the number of days until the user's next reminder
function get_days_until_next_reminder($wday, $user_num)
{
    $wday++;
    if($wday >= 7)
        $wday = 0;
    $pref_array = array();

    db_open();
    db_use();
    $query_prefs = "SELECT * FROM users WHERE uid = '$user_num'";
    $result = db_query($query_prefs);
    $row = db_fetch_row($result);
    array_push($pref_array, $row["remindS"]);
    array_push($pref_array, $row["remindM"]);
    array_push($pref_array, $row["remindT"]);
    array_push($pref_array, $row["remindW"]);
    array_push($pref_array, $row["remindR"]);
    array_push($pref_array, $row["remindF"]);
    array_push($pref_array, $row["remindA"]);

    $max = 1;
    $ret = 1;
    while($pref_array[$wday] == 0)
    {
        $ret++;
        $max++;
        $wday++;
        if($wday >= 7) //need to wrap
        {
            $wday = 0;
        }
        if($max >= 7) //they haven't set any email preferences
        {
            $ret = $max;
            break;
        }
    }
    return $ret;
}

// Returns a time stamp corresponding to a date in the future - doesn't really add days to your life :(
function add_days ($days, $date) 
{
    $date_time_array  = getdate($date);
        
    $hours =  $date_time_array["hours"];
    $minutes =  $date_time_array["minutes"];
    $seconds =  $date_time_array["seconds"];
    $month =  $date_time_array["mon"];
    $day =  $date_time_array["mday"];
    $year =  $date_time_array["year"];

    $day+=$days;
    $timestamp =  mktime($hours ,$minutes, $seconds,$month ,$day, $year);

    return $timestamp;
}

?>
Return current item: Internet Task Management System