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:
";
- echo " Please note before quitting a team:"; - echo "
"; - echo ""; - 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 " -