From e10d31f2c8ffebbe32b56eca9d69eaf06bf54a97 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 1 Mar 2011 19:57:05 +0000 Subject: [PATCH] Updated the api for api/project/mentor/add and api/project/mentor/edit. Both now accept an array of mentor arrays (now referred to as 'mentors' instead of 'mentor'). /edit will only accept an array, but /add will accept that or no parameters to generate a fresh empty record. --- api.php | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/api.php b/api.php index 0854b00..63a9a42 100644 --- a/api.php +++ b/api.php @@ -1068,7 +1068,7 @@ switch($request[0]) { switch($request[2]){ /* APIDOC: project/mentor/add description(add a project mentor to the current project) - post(mentor array optional) + post(mentors array optional) return(mentor array (if none passed in, otherwise nothing)) */ case 'add': @@ -1078,18 +1078,23 @@ switch($request[0]) { break; } - $result = addMentor($_SESSION['registration_id']); - if($_POST['mentor']) { - $md=json_decode($_POST['mentor']); - $md['id']=$result['id']; - $result = saveMentorData($md); - if($result == 'ok'){ + if($_POST['mentors']) { + $mentors = json_decode($_POST['mentors']); + $errors = array(); + foreach($mentors as $md){ + $result = addMentor($_SESSION['registration_id']); + $md['id']=$result['id']; + $result = saveMentorData($md); + if($result != 'ok') $errors[] = $result; + } + if(!count($errors)){ $ret['status'] = 'ok'; }else{ $ret['status'] = 'error'; - $ret['error'] = $result; + $ret['error'] = '(' . implode('), (', $errors) . ')'; } } else { + $result = addMentor($_SESSION['registration_id']); if(is_array($result)){ $ret['status'] = 'ok'; $ret['mentor'] = $result; @@ -1101,21 +1106,30 @@ switch($request[0]) { break; /* APIDOC: project/mentor/edit - post(mentor array) - description(edit a project mentor) + post(mentors array) + description(edit a list of project mentors) */ case 'edit': - if(!array_key_exists('mentor', $_POST)){ + if(!array_key_exists('mentors', $_POST)){ $ret['status'] = "error"; - $ret['error'] = "mentor array parameter required"; + $ret['error'] = "mentors array parameter required"; break; } - $result = saveMentorData(json_decode($_POST['mentor'])); - if($result == 'ok'){ + $errors = array(); + $mentorList = json_decode($_POST['mentors']); + foreach($mentorList as $mentor){ + if(!is_array($mentor)){ + $errors[] = "Invalid mentor data: $mentor"; + continue; + } + $result = saveMentorData($mentor); + if($result != 'ok') $errors[] = $result; + } + if(!count($errors)){ $ret['status'] = 'ok'; }else{ $ret['status'] = 'error'; - $ret['error'] = $result; + $ret['error'] = '(' . implode('), (', $errors) . ')'; } break;