Added the necessary code for updating the grade category that a project belongs in whenever it or it's participants are modified.

This commit is contained in:
jacob 2011-03-02 22:41:26 +00:00
parent 1749d8c67b
commit 0c6f1c3b70
4 changed files with 46 additions and 8 deletions

View File

@ -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;

View File

@ -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;
}
?>

View File

@ -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,9 +453,11 @@ 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']}'");
@ -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);
@ -504,6 +507,16 @@ function joinProject($registration_number, $email){
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';
}

View File

@ -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 */