<?php pravin_get_todo(); ?> in your template. Contains a dashboard widget that shows the contents of the to-do list only to predetermined roles.
Version: 0.2.2
Author: Pravin Paratey
Author URI: http://www.DustyAnt.com
*/
/* Copyright 2007 Pravin Paratey (pravinp@gmail.com); Dashboard widget, email notification and role capability via options setting added by Mark Lee, marcusantoniouslee1@yahoo.ca, April 2009
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
*/
class pravin {
// --------------------------------------------------------------------
// Responsible for installing the plugin
// --------------------------------------------------------------------
function todo_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
// Check if the table is already present
if($wpdb->get_var("SHOW TABLES LIKE '" . $table_name ."'") != $table_name) {
$sql = "CREATE TABLE $table_name (
task_id bigint(20) not null auto_increment,
task_desc text not null default '',
task_owner bigint(20) not null,
assigned_by bigint(30) not null,
date_due bigint(11) not null default '0',
date_created bigint(11) not null default '0',
priority smallint(3) not null default '1',
notes text not null default '',
status tinyint(1) not null default '0',
unique key id(task_id)
);";
$results = $wpdb->query($sql);
$table_name = $wpdb->prefix . 'pravin_todo_options';
$sql = "CREATE TABLE $table_name (
option_id smallint(3) not null,
show_limit smallint(3) default '1',
show_completed tinyint(1) default '0',
show_duetoday tinyint(1) default '0',
show_spectrum tinyint(1) default '1',
hot_color tinytext,
cold_color tinytext,
format_date_due tinytext,
format_date_creat tinytext,
sort_field1 tinytext,
sort_order1 tinytext,
sort_field2 tinytext,
sort_order2 tinytext,
show_role_option tinytext,
show_task_id tinyint(1) default '0',
show_task_owner tinyint(1) default '1',
show_assigned_by tinyint(1) default '0',
show_date_due tinyint(1) default '1',
show_date_creat tinyint(1) default '0',
show_priority tinyint(1) default '1',
show_notes tinyint(0) default '0',
column_order text default '',
timezone_offset tinytext,
unique key id(option_id)
);";
$results = $wpdb->query($sql);
// Add initial data
$sql = "INSERT INTO `$table_name` (show_limit, sort_field1, sort_order1, sort_field2, sort_order2, show_role_option) " .
" VALUES ('5', 'status', 'DESC', 'date_due', 'DESC', 'users')";
$results = $wpdb->query($sql);
}
}
// --------------------------------------------------------------------
// Adds the ToDo page under Manage
// --------------------------------------------------------------------
function todo_addpages() {
add_management_page('Manage your ToDo list', 'ToDo - Add', 1, 'todo', array('pravin', 'todo_addoption'));
//=======================================================================
// Start create Capability. It works but maybe it should be somewhere else
// ======================================================================
add_filter('capabilities_list', 'todo_caps_list');
function todo_caps_list($caps) {
$caps[] = 'add_assignment';
return $caps;
}
// =======================================================================
// End create capability
// =======================================================================
}
// --------------------------------------------------------------------
// Responsible for rendering the ToDo page under Manage
// --------------------------------------------------------------------
function todo_addoption()
{
global $wpdb;
$output_html = '
ToDo List
<$ToDoList$>
Add a ToDo
Enter your to-do using this form. The information saved will be displayed in the list above and an email will be sent to the person given the task. Do a brief smmary in the "Task" field, select a deadline date and make sure to select the correct user. Information in the "Notes" text area will be appended to the "Task" in the body of the email message, so you may try to make the idea flow by giving details about the task.
You must visit the ToDo Settings page to change the list of names from which you may choose a recipient.
Display Options
Advanced
If you are upgrading from v0.1 to v0.2, use this to DROP your table. You will lose all ToDo data and settings! After you press the button, you will get an error. It is expected. Deactivate the plugin and activate it again to recreate your tables.
';
$table_name = $wpdb->prefix . 'pravin_todo';
$sql = "select * from $table_name order by status ASC, date_due ASC ";
$results = $wpdb->get_results($sql);
$alt = 0;
$todolist = '
';
}
$assigned_by = $wpdb->get_var("SELECT display_name FROM `$wpdb->users` WHERE ID = $result->assigned_by LIMIT 1");
$assigned_to = $wpdb->get_var("SELECT display_name FROM `$wpdb->users` WHERE ID = $result->task_owner LIMIT 1");
// 'F jS, Y @ H:i'
$todolist .= '
' . gmdate('M j, Y', $result->date_due) . '
'.
'
' .$result->task_desc . '
' .
$assigned_to . '
' .
$result->priority . '
' .
'
' . $assigned_by . '
' .
'
';
}
}
$output_html = str_replace('<$ToDoList$>', $todolist, $output_html);
$month_array = array('January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December');
$cur_month = date('n');
$month_html = '';
for($i=1; $i < 13; $i++)
{
$month_html .= '';
}
$month_html = '' .
' , ' .
' @ ' .
' : ' .
' hrs';
$output_html = str_replace('<$ToDoAdd$>', $month_html, $output_html);
// Get option list of all users...
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo_options';
$user_group = $wpdb->get_var("SELECT show_role_option FROM $table_name LIMIT 1");
// $role = $user_group;
if ($user_group == 'users'){
$users = $wpdb->get_results("SELECT * FROM `$wpdb->users`");
$userlist = '';
foreach($users as $user)
{
$userlist .= '';
}
$output_html = str_replace('<$UserList$>', $userlist, $output_html);
}
// ...or users from roles using DE Groot's select users with role. ###########################################
else {
$this_role = "'[[:<:]]".$user_group."[[:>:]]'";
$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
$users_of_this_role = $wpdb->get_results($query);
if ($users_of_this_role)
{
foreach($users_of_this_role as $user)
{
$curuser = get_userdata($user->ID);
$userlist .= '';
}
}
} //end else
//DE Groot's select role END
$output_html = str_replace('<$UserList$>', $userlist, $output_html);
// Get Show Completed
$table_name = $wpdb->prefix . 'pravin_todo_options';
$op = $wpdb->get_var("SELECT show_limit FROM $table_name LIMIT 1");
$output_html = str_replace('<$ShowLimit$>', $op, $output_html);
$op = $wpdb->get_var("SELECT show_completed FROM $table_name LIMIT 1");
$option_html = '';
if(1 == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$ShowCompleted$>', $option_html, $output_html);
// Get Due Today
$op = $wpdb->get_var("SELECT show_duetoday FROM $table_name LIMIT 1");
$option_html = '';
if(1 == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$ShowDueToday$>', $option_html, $output_html);
// Get sort field 1
$op = $wpdb->get_var("SELECT sort_field1 FROM $table_name LIMIT 1");
$option_html = '';
if('date_due' == $op)
{
$option_html = '';
}
else if('priority' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortBy1$>', $option_html, $output_html);
// Get sort field 2
$op = $wpdb->get_var("SELECT sort_field2 FROM $table_name LIMIT 1");
$option_html = '';
if('date_due' == $op)
{
$option_html = '';
}
else if('priority' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortBy2$>', $option_html, $output_html);
// Get Sort Order 1
$op = $wpdb->get_var("SELECT sort_order1 FROM $table_name LIMIT 1");
$option_html = '';
if('asc' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortOrder1$>', $option_html, $output_html);
// Get Sort Order 2
$op = $wpdb->get_var("SELECT sort_order2 FROM $table_name LIMIT 1");
$option_html = '';
if('asc' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortOrder2$>', $option_html, $output_html);
echo $output_html;
}
}
// --------------------------------------------------------------------
// Dashboard widget
// --------------------------------------------------------------------
//Pre-check user status and role and show only to allowed roles. This can be removed to allow dashboard widget to be seen by all logged in users.
function hide_show_assignboard(){
if(is_user_logged_in) { // only if there is a user logged in
require (ABSPATH . WPINC . '/pluggable.php');
$logged_user = wp_get_current_user();
$user_roles = $logged_user->roles; // an array of roles
foreach($user_roles as $user_role)
{
if($user_role=='administrator' || $user_role=='intern' ||$user_role=='editor')
include('dashboard.php');}
}
}
hide_show_assignboard();
// --------------------------------------------------------------------
// End dashboard widget
// --------------------------------------------------------------------
// ---------------------------------------------------
// Insert settings menu and page
// ---------------------------------------------------
add_action('admin_menu', 'todo_plugin_menu');
function todo_plugin_menu() {
add_options_page('ToDo Options', 'ToDo Settings', 8, 'todo.php', 'todo_plugin_options');
}
include ('todo_settings.php');
// --------------------------------------------------------------------
// Widgetize!
// --------------------------------------------------------------------
function widget_pravin_todo() {
function widget_todo($args) {
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args);
// Each widget can store its own options. We keep strings here.
$title = $options['title'];
echo $before_widget . $before_title . 'ToDo' . $after_title;
echo pravin_get_todo();
echo $after_widget;
}
// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget(array('ToDo Plugin', 'widgets'), 'widget_todo');
}
// --------------------------------------------------------------------
// Called when user clicks activate in the plugin menu
// --------------------------------------------------------------------
if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
if (defined('WPINC') && strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) > 0) {
add_action('init', array('pravin', 'todo_install'));
}
}
// --------------------------------------------------------------------
// Insert the mt_add_pages() sink into the plugin hook list for 'admin_menu'
// --------------------------------------------------------------------
if ( current_user_can('add_assignment') )
add_action('admin_menu', array('pravin', 'todo_addpages'));
// Run our code later in case this loads prior to any required plugins.
add_action('widgets_init', 'widget_pravin_todo');
// --------------------------------------------------------------------
// Handle any add/delete/update requests
// --------------------------------------------------------------------
$name = $_POST["operation"];
if('add' == $name) {
// get current user :(
global $current_user;
$current_user = wp_get_current_user();
global $wpdb;
$assigned_by = ($current_user->ID);
$table_name = $wpdb->prefix . 'pravin_todo';
$date_due = mktime($_POST["hour"], $_POST["minute"], 0, $_POST["month"], $_POST["day"], $_POST["year"]);
$sql = "INSERT INTO `$table_name` (task_desc, task_owner, assigned_by, date_due, date_created, priority, notes) VALUES( '" .
$wpdb->escape($_POST["task"]) . "' , '" .
$_POST['taskowner'] . "' , '" .
$assigned_by . "', '" .
$date_due . "', '" .
time() . "', '" .
$_POST['priority'] . "', '" .
$wpdb->escape($_POST['notes']) . "')";
$results = $wpdb->query($sql);
//====================================================
//Email the task
//====================================================
include ('notify.php');
mail_todo();
// ==================================================
// End Function email the task
// ==================================================
}
else if('update' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$sql = '';
$dowhat = $_POST["dowhat"];
$id = $_POST["id"];
if('delete' == $dowhat)
{
$sql = "DELETE FROM $table_name WHERE task_id=$id";
}
else if('done' == $dowhat)
{
$sql = "UPDATE $table_name SET status='1' WHERE task_id=$id";
}
else
{
$sql = "UPDATE $table_name SET status='0' WHERE task_id=$id";
}
$results = $wpdb->query($sql);
}
else if('drop' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$table_options = $table_name . '_options';
$wpdb->query("DROP TABLE `$table_name`, `$table_options`;");
}
else if('options' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo_options';
$wpdb->query("UPDATE $table_name SET show_limit='" . $_POST['limit'] .
"', sort_field1='" . $_POST['sortby1'] .
"', sort_order1='" . $_POST['sortorder1'] .
"', sort_field2='" . $_POST['sortby2'] .
"', sort_order2='" . $_POST['sortorder2'] .
"', show_completed='" . $_POST['showcompleted'] .
"', show_duetoday='" . $_POST['showduetoday'] . "'");
}
// update roles list
else if('showroleoption' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo_options';
$wpdb->query("UPDATE $table_name SET show_role_option='" . $_POST['usergroups'] . "'");
}
// end update roles list
function pravin_get_todo()
{
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$table_name_options = $wpdb->prefix . 'pravin_todo_options';
$sql = "SELECT * from `$table_name_options` LIMIT 1";
$options = $wpdb->get_results($sql);
$option = $options[0];
$where_clause = '';
if($option->show_completed == '0')
{
$where_clause = " WHERE status='0' ";
}
$sql = "SELECT * from `$table_name` $where_clause ORDER BY $option->sort_field1 $option->sort_order1, $option->sort_field2 $option->sort_order2 LIMIT $option->show_limit";
$results = $wpdb->get_results($sql);
$output_html = '