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;