function from_db_to_json($conceptmap) { $map = array(); foreach ($conceptmap as $key => $value) { $type = gettype($conceptmap->{$key}); if ($key == "mapxml") { $attr = $conceptmap->{$key}; libxml_use_internal_errors(true); $sxe = simplexml_load_string($attr); if ($sxe === false) { // echo "Failed loading the original XML\n"; // foreach(libxml_get_errors() as $error) { // echo "Line: $error->line($error->column) $error->message<br>"; // } $attr = simplexml_load_string("<map>" . $attr . "</map>"); } else { $attr = simplexml_load_string($attr); } $map[$key] = xml_maps_to_json($attr[0]); $result = json_decode($map[$key], true); // super_echo($result); foreach ($result as $node_key => $node_value) { $map[$node_key] = $result[$node_key]; } } else { $map[$key] = $conceptmap->{$key}; } } unset($map['mapxml']); unset($map['timecreated']); unset($map['timemodified']); $map['id'] = $conceptmap->id; $map['title'] = $map['name']; unset($map['name']); foreach ($map as $key => $value) { if (is_array($map[$key])) { $arrays = $map[$key]; foreach ($arrays as $key1 => $value1) { $map[$key1] = $value1; } unset($map[$key]); } } $map_arr = check_table_format($map, $map['id'], $map['name']); $map = json_encode($map_arr); return $map; }
if (!does_map_exist_by_id($student_id, $map_id)) { deliver_response(200, "Map with id={$map_id} does not exist"); break; } if ($map_id != $json_arr['id']) { deliver_response(200, "You cannot update map's id"); break; } if (!does_map_exist_by_id($student_id, $map_id)) { deliver_response(200, "Map with id={$map_id} does not exist"); break; } if (is_array($json_arr)) { update_map($student_id, $map_id, $json_string); $map = get_user_maps($student_id, $map_id); $map = xml_maps_to_json($map); $map = json_decode($map, true); deliver_response(200, "The map has been updated", $map); } else { deliver_response(400, "The request cannot be fulfilled due to bad syntax"); } break; case "DELETE": if (!does_map_exist_by_id($student_id, $map_id)) { deliver_response(200, "Map with id={$map_id} does not exist"); break; } delete_map($student_id, $map_id); deliver_response(200, "The map has been deleted"); break; default:
function update_map($student_id, $map_id, $json_string) { $json_arr = json_decode($json_string, true); $maps = get_user_maps($student_id); $maps = xml_maps_to_json($maps); $maps = json_decode($maps, true); foreach ($maps as &$map) { if ($map['id'] == $map_id) { foreach ($json_arr as $key => $value) { if (!empty($json_arr[$key])) { $map[$key] = $json_arr[$key]; } } delete_map($student_id, $map_id); $map = json_encode($map); add_map($student_id, $map); break; } } }