From 0c6f1c3b7052afbfe12cd8db051fefb00522c870 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 2 Mar 2011 22:41:26 +0000 Subject: [PATCH] Added the necessary code for updating the grade category that a project belongs in whenever it or it's participants are modified. --- api.php | 5 +++-- projects.inc.php | 17 ++++++++++++++++- register_participants.inc.php | 23 ++++++++++++++++++----- user.inc.php | 9 +++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/api.php b/api.php index 9cecb47..03be176 100644 --- a/api.php +++ b/api.php @@ -1051,13 +1051,14 @@ switch($request[0]) { description(remove the current user from an existing project. If no other users are in the project, then it is deleted.) */ case 'remove': - if(!array_key_exists('registration_id', $_SESSION)){ + $regId = getRegistrationsId($_SESSION['users_id']); + if(!$regId){ $ret['status'] = 'error'; $ret['error'] = 'current user not associated with a project'; break; } - $result = removeProject($_SESSION['registration_id']); + $result = removeProject($regId); if($result != 'ok'){ $ret['status'] = "error"; $ret['error'] = $result; diff --git a/projects.inc.php b/projects.inc.php index b19e498..14790b5 100644 --- a/projects.inc.php +++ b/projects.inc.php @@ -290,6 +290,21 @@ function project_load($pid) return $proj; } - +function getProjectCategory($registrations_id){ + global $conference; + $q=mysql_query("SELECT MAX(grade) AS maxgrade FROM users WHERE registrations_id='$registrations_id'"); + $gradeinfo=mysql_fetch_object($q); + $projectcategories_id = null; + //now lets grab all the age categories, so we can choose one based on the max grade + $q=mysql_query("SELECT * FROM projectcategories WHERE conferences_id='".$conference['id']."' ORDER BY id"); + while(($r=mysql_fetch_object($q)) && $projectcategories_id === null) + { + if($gradeinfo->maxgrade >= $r->mingrade && $gradeinfo->maxgrade <= $r->maxgrade) + { + $projectcategories_id = $r->id; + } + } + return $projectcategories_id; +} ?> diff --git a/register_participants.inc.php b/register_participants.inc.php index 682aade..7226433 100644 --- a/register_participants.inc.php +++ b/register_participants.inc.php @@ -25,6 +25,7 @@ // legacy - functions previously included here have been split off into participants.inc.php, // but expected in this file by other code require_once("participant.inc.php"); +require_once("projects.inc.php"); function generateProjectNumber($registration_id) { @@ -452,10 +453,12 @@ function getProject($userId){ // add a project function addProject($registrations_id){ global $conference; + // get the appropriate category for this project + $projCategory = getProjectCategory($registrations_id); mysql_query(" - INSERT INTO projects(registrations_id, conferences_id) - VALUES ('" . $registrations_id . "','" . $conference['id']."') - "); + INSERT INTO projects(registrations_id, conferences_id, projectcategories_id) + VALUES ('" . $registrations_id . "','" . $conference['id']."', '$projCategory') + "); //now query the one we just inserted $q = mysql_query("SELECT * FROM projects WHERE registrations_id='$registrations_id' AND conferences_id='{$conference['id']}'"); if(mysql_error()) { @@ -476,7 +479,7 @@ function joinProject($registration_number, $email){ return 'register_participants.inc.php::joinProject -> you are already registered for a project'; } - // let's avoid an SQL naughtiness + // let's avoid any SQL naughtiness $email = mysql_real_escape_string($email); $registration_number = intval($registration_number); @@ -503,6 +506,16 @@ function joinProject($registration_number, $email){ if(mysql_error()){ return "register_participants.inc.php::joinProject -> " . mysql_error(); } + + // finally, we can update the project's age category if necessary + $category = getProjectCategory($registration_id); + if($category !== null){ + mysql_query("UPDATE projects SET projectcategories_id = $category WHERE registrations_id = $registration_id"); + if(mysql_error()){ + return "register_participants.inc.php::joinProject -> " . mysql_error(); + } + } + return 'ok'; } @@ -690,5 +703,5 @@ function getMentors($registrations_id){ $returnval[] = $row; } return $returnval; -} +} ?> diff --git a/user.inc.php b/user.inc.php index 8feb5e9..bdf552e 100644 --- a/user.inc.php +++ b/user.inc.php @@ -869,7 +869,16 @@ function user_save(&$u) if(mysql_error() != '') return "SQLERR7: " . mysql_error(); } + if($u['orig']['grade'] != $u['grade'] && $u['registrations_id']){ + // their grade has changed and they're connected to a registration. Let's update + // the registration's grade level to match if necessary + $category = getProjectCategory($u['registrations_id']); + if($category !== null){ + mysql_query("UPDATE projects SET projectcategories_id = $category WHERE registrations_id = $registration_id"); + if(mysql_error() != '') return "SQLERR8: " . mysql_error(); + } + } /* Record all the data in orig that we saved so subsequent * calls to user_save don't try to overwrite data already * saved to the database */