From 43a21e6efd202b8cca1da4bec2807e90814d21bb Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 21 Nov 2004 18:56:30 +0000 Subject: [PATCH] *** empty log message *** svn path=/trunk/boinc/; revision=4618 --- checkin_notes | 13 +++++ html/inc/team.inc | 64 +++++++++-------------- html/inc/util.inc | 7 +++ html/user/team_create_action.php | 2 +- html/user/team_edit_action.php | 7 +-- html/user/team_edit_form.php | 9 +--- html/user/team_email_list.php | 8 +-- html/user/team_join_action.php | 7 ++- html/user/team_join_form.php | 16 ++---- html/user/team_lookup.php | 7 +-- html/user/team_manage.php | 10 ++-- html/user/team_quit_action.php | 22 +++----- html/user/team_quit_form.php | 49 ++++++++--------- html/user/team_remove_inactive_action.php | 40 +++++--------- html/user/team_remove_inactive_form.php | 16 ++---- 15 files changed, 111 insertions(+), 166 deletions(-) diff --git a/checkin_notes b/checkin_notes index 5bb8403a5e..9fb45ae8e4 100755 --- a/checkin_notes +++ b/checkin_notes @@ -19947,3 +19947,16 @@ David 20 Nov 2004 cs_prefs.C win/ wingui_mainwindow.cpp + +David 21 Nov 2004 + - Change team-related PHP code so that it recomputes + "nusers" by doing a select count(*) when a user + quits or joins a team. + Also cleaned up skanky PHP code. + + html/ + inc/ + team.inc + util.inc + user/ + team*.php diff --git a/html/inc/team.inc b/html/inc/team.inc index 20522289cc..ac1f54e2de 100644 --- a/html/inc/team.inc +++ b/html/inc/team.inc @@ -114,22 +114,17 @@ function display_team_page($team, $offset, $sort_by) { // requires that the team exist function require_team($team) { if (!$team) { - page_head("Error"); - echo "Team does not exist."; - page_tail(); - exit(); + error_page("No such team."); } } // requires that the user is logged in as the founder of // the team trying to be edited +// function require_founder_login($user, $team) { require_team($team); if ($user->id != $team->userid) { - page_head("Permission denied"); - echo "Only a team's founder may edit a team."; - page_tail(); - exit(); + error_page("Only a team's founder may edit a team."); } } @@ -170,39 +165,15 @@ function show_team_row($team, $i) { } function user_join_team($team, $user) { - if ($user->teamid != 0) { - $query_team_other = sprintf( - "select * from team where id = %d", - $user->teamid - ); - $result_team_other = mysql_query($query_team_other); - $first_team = mysql_fetch_object($result_team_other); - $first_nusers = $first_team->nusers; - $first_new_nusers = $first_nusers - 1; - if ($first_new_nusers > 0) { - $query_team_table_other = sprintf( - "update team set nusers = %d where id = %d", - $first_new_nusers, - $first_team->id - ); - } else { - // no more users in this team: disband it. - $query_team_table_other = sprintf( - "delete from team where id = %d", - $first_team->id - ); - } - $result_team_table_other = mysql_query($query_team_table_other); + $old_teamid = $user->teamid; + $res = mysql_query("update user set teamid=$team->id where id=$user->id"); + if ($old_teamid != 0) { + $old_team = lookup_team($old_teamid); + team_update_nusers($old_team); } - $query_user_table = sprintf( - "update user set teamid = %d where id = %d", - $team->id, - $user->id - ); - $result_user_table = mysql_query($query_user_table); - $result_team_table = mysql_query("update team set nusers=nusers+1 where id = $team->id"); - if ($result_user_table && $result_team_table) return true; - else return false; + team_update_nusers($team); + if ($res) return true; + return false; } function team_edit_form($team, $label, $url) { @@ -295,4 +266,17 @@ function team_inactive_ndays($team, $ndays) { return false; } +function team_update_nusers($team) { + $res = mysql_query("select count(*) as total from user where teamid=$team->id"); + $comp = mysql_fetch_object($res); + if (!$comp) return; + $n = $comp->total; + if ($n > 0) { + mysql_query("update team set nusers=$n where id=$team->id"); + } else { + echo "
Team is empty - deleting team.\n"; + mysql_query("delete from team where id=$team->id"); + } +} + ?> diff --git a/html/inc/util.inc b/html/inc/util.inc index 5ea470c381..613b1b5ec2 100644 --- a/html/inc/util.inc +++ b/html/inc/util.inc @@ -155,6 +155,13 @@ function profile_error_page($str) { page_tail(); } +function error_page($msg) { + page_head("Unable to handle request"); + echo $msg; + page_tail(); + exit(); +} + function date_str($x) { if ($x == 0) return "---"; // return date("g:i A, l M j", $when); diff --git a/html/user/team_create_action.php b/html/user/team_create_action.php index ef7f8d19f4..99266ce5b8 100644 --- a/html/user/team_create_action.php +++ b/html/user/team_create_action.php @@ -32,7 +32,7 @@ $team_result = mysql_query("select * from team where id = $teamid"); $new_team = mysql_fetch_object($team_result); mysql_free_result($team_result); - user_join_team($new_team,$user); + user_join_team($new_team, $user); Header("Location: team_display.php?teamid=$teamid"); } else { page_head("Error"); diff --git a/html/user/team_edit_action.php b/html/user/team_edit_action.php index 036a208d56..29f3d0c0ae 100644 --- a/html/user/team_edit_action.php +++ b/html/user/team_edit_action.php @@ -8,12 +8,7 @@ $user = get_logged_in_user(); $teamid = $_POST["teamid"]; - $query = "select * from team where id = $teamid"; - $result = mysql_query($query); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } + $team = lookup_team($teamid); require_founder_login($user, $team); $team_url = ereg_replace("\"", "'", $_POST["url"]); diff --git a/html/user/team_edit_form.php b/html/user/team_edit_form.php index ba7876ec74..c5f7012b18 100644 --- a/html/user/team_edit_form.php +++ b/html/user/team_edit_form.php @@ -8,14 +8,9 @@ require_once("../inc/team.inc"); $user = get_logged_in_user(); $teamid = $_GET["teamid"]; - - $query = "select * from team where id = $teamid"; - $result = mysql_query($query); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } + $team = lookup_team($teamid); require_founder_login($user, $team); + $team_name = ereg_replace("\"", "'", $team->name); $team_name_html = ereg_replace("\"", "'", $team->name_html); $team_url = ereg_replace("\"", "'", $team->url); diff --git a/html/user/team_email_list.php b/html/user/team_email_list.php index b7f40a7097..b5d61530b6 100644 --- a/html/user/team_email_list.php +++ b/html/user/team_email_list.php @@ -9,13 +9,7 @@ require_once("../inc/team.inc"); $user = get_logged_in_user(); $teamid = $_GET["teamid"]; - - $result = mysql_query("select * from team where id=$teamid"); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } - + $team = lookup_team($teamid); require_founder_login($user, $team); page_head("$team->name Email List"); diff --git a/html/user/team_join_action.php b/html/user/team_join_action.php index 5344f27285..9320324c2a 100644 --- a/html/user/team_join_action.php +++ b/html/user/team_join_action.php @@ -14,18 +14,17 @@ page_head("Unable to add $user->name"); echo "You are already a member of $team->name."; } else { - $success = user_join_team($team,$user); + $success = user_join_team($team, $user); if ($success == true) { page_head("Joined $team->name"); echo "You have joined id>$team->name. "; } else { - page_head("Error"); - echo "Couldn't join team - please try later.\n"; + error_page("Couldn't join team - please try later."); } } -page_tail(); + page_tail(); ?> diff --git a/html/user/team_join_form.php b/html/user/team_join_form.php index f26e26b73a..3d972a8f71 100644 --- a/html/user/team_join_form.php +++ b/html/user/team_join_form.php @@ -4,18 +4,12 @@ require_once("../inc/db.inc"); require_once("../inc/util.inc"); require_once("../inc/team.inc"); -db_init(); -$user = get_logged_in_user(); -$id = $_GET["id"]; + db_init(); + $user = get_logged_in_user(); + $teamid = $_GET["id"]; - $query = "select * from team where id = $id"; - $result = mysql_query($query); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } + $team = lookup_team($teamid); $team_name = $team->name; - $team_id = $team->id; page_head("Join $team_name"); echo "

Please note:


- +
"; diff --git a/html/user/team_lookup.php b/html/user/team_lookup.php index 337e25d75a..3be1697f64 100644 --- a/html/user/team_lookup.php +++ b/html/user/team_lookup.php @@ -32,9 +32,10 @@ } echo ""; if ($too_many) { - echo "This is only a partial list of the possible teams you "; - echo "were searching for. You will need to narrow your search "; - echo "criteria to get more accurate results.
"; + echo " + More than 100 teams match your search. + The first 100 are shown.
+ "; } } echo "End of results
"; diff --git a/html/user/team_manage.php b/html/user/team_manage.php index b81a6e270c..59ccda0cb9 100644 --- a/html/user/team_manage.php +++ b/html/user/team_manage.php @@ -20,11 +20,11 @@ if ($team->userid != $user->id) { } echo " - + "; page_tail(); diff --git a/html/user/team_quit_action.php b/html/user/team_quit_action.php index 492f9e5c72..2778e08c65 100644 --- a/html/user/team_quit_action.php +++ b/html/user/team_quit_action.php @@ -6,25 +6,15 @@ db_init(); $user = get_logged_in_user(); $teamid = $_POST["id"]; - $team = lookup_team($teamid); if ($user->teamid == $team->id) { - $query_user_table = "update user set teamid = 0 where id = $user->id"; - $result_user_table = mysql_query($query_user_table); - $query_team_table = "update team set nusers=nusers-1 where id=$team->id"; - $result_team_table = mysql_query($query_team_table); - if ($result_user_table && $result_team_table) { - $team_name = $team->name; - page_head("Quit $team_name"); - echo "

Removed from team

"; - echo "You have been removed from id>$team_name"; - } else { - page_head("Error"); - echo "Couldn't quit team - please try later.\n"; - } + mysql_query("update user set teamid=0 where id=$user->id"); + team_update_nusers($team); + page_head("Quit $team->name"); + echo "You have been removed from id>$team->name"; } else { - page_head("Unable to remove $user->name"); - echo "$user->name is not a member of $team_name.\n"; + page_head("Unable to quit team"); + echo "Team doesn't exist, or you don't belong to it.\n"; } page_tail(); diff --git a/html/user/team_quit_form.php b/html/user/team_quit_form.php index 65d9a3219e..d560eac259 100644 --- a/html/user/team_quit_form.php +++ b/html/user/team_quit_form.php @@ -8,31 +8,28 @@ db_init(); $user = get_logged_in_user(true); $id = $user->teamid; - $result = mysql_query("select * from team where id=$id"); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } - $team_name = $team->name; - $team_id = $team->id; - page_head("Quit $team_name"); - echo "

Quit $team_name

"; - echo ""; - echo "
"; - echo "

Please note before quitting a team:"; - echo "

    "; - echo "
  • If you quit a team, you may rejoin later, "; - echo "or join any other team you desire "; - echo "
  • Quitting a team does not affect your personal credit "; - echo "statistics in any way."; - echo "
"; - echo "

"; - echo "
"; - echo "
"; - echo ""; - echo ""; - echo "
"; - echo "
"; - page_tail(); +$result = mysql_query("select * from team where id=$id"); +if ($result) { + $team = mysql_fetch_object($result); + mysql_free_result($result); +} +$team_name = $team->name; +$team_id = $team->id; +page_head("Quit $team_name"); +echo " + Please note before quitting a team: + +

+
+ + +
+"; +page_tail(); } ?> diff --git a/html/user/team_remove_inactive_action.php b/html/user/team_remove_inactive_action.php index f0c28b8cdb..ec62d992b9 100644 --- a/html/user/team_remove_inactive_action.php +++ b/html/user/team_remove_inactive_action.php @@ -7,48 +7,32 @@ db_init(); $user = get_logged_in_user(); - $query = sprintf( - "select * from team where id = %d", - $_POST["id"] - ); - $result = mysql_query($query); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); + $teamid = $_POST["id"]; + $team = lookup_team($teamid); + if (!team) { + error_page("No such team"); } require_founder_login($user, $team); page_head("Removing users from $team->name"); - $nmembers = 0; + $ndel = 0; for ($i=0; $i<$_POST["ninactive_users"]; $i++) { if ($_POST["remove_$i"] != 0) { - $query = sprintf( - "select * from user where id = %d", - $_POST["remove_$i"] - ); - $result = mysql_query($query); - $user = mysql_fetch_object($result); + $userid = $_POST["remove_$i"]; + $user = lookup_user_id($userid); if ($user->teamid != $team->id) { echo "
$user->name is not a member of $team->name"; } else { - $query_user_table = sprintf( - "update user set teamid = 0 where id = %d", - $_POST["remove_$i"] - ); - $nmembers++; - $result_user_table = mysql_query($query_user_table); + $query = "update user set teamid=0 where id=$userid"; + $result_user_table = mysql_query($query); echo "
$user->name has been removed"; + $ndel++; } } } - $new_nusers = $team->nusers - $nmembers; - if ($new_nusers > 0) { - $query = "update team set nusers = $new_nusers where id = $team->id"; - } else { - $query = "delete from team where id = $team->id"; - echo "

The team has been disbanded because there are no more members."; + if ($ndel) { + team_update_nusers($team); } - $result = mysql_query($query); page_tail(); diff --git a/html/user/team_remove_inactive_form.php b/html/user/team_remove_inactive_form.php index 2daaf510d8..6eb9a2c575 100644 --- a/html/user/team_remove_inactive_form.php +++ b/html/user/team_remove_inactive_form.php @@ -7,22 +7,14 @@ db_init(); $user = get_logged_in_user(); $teamid = $_GET["teamid"]; - - $result = mysql_query("select * from team where id = $teamid"); - if ($result) { - $team = mysql_fetch_object($result); - mysql_free_result($result); - } + $team = lookup_team($teamid); require_founder_login($user, $team); - $team_name = $team->name; - $team_id = $team->id; $nusers = $team->nusers; - page_head("Remove Members from $team_name"); + page_head("Remove Members from $team->name"); echo " -

Remove members from $team_name

Removing a member will subtract their credit from team totals
- + id> "; start_table(); echo " @@ -33,7 +25,7 @@ "; - $result = mysql_query("select * from user where teamid = $team_id"); + $result = mysql_query("select * from user where teamid = $team->id"); $ninactive_users = 0; while ($user = mysql_fetch_object($result)) {