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 ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    }
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    }
    exit(0);
}
include_once 'service_db_operations.php';
include_once 'map_format_parser.php';
$request_method = $_SERVER['REQUEST_METHOD'];
$student_id = isset($_GET['student_id']) ? $_GET['student_id'] : 1;
$map_id = isset($_GET['id']) ? $_GET['id'] : null;
$json_string = file_get_contents('php://input');
$json_string = check_table_format($student_id, $json_string, $map_id);
//http://www.w3schools.com/tags/ref_httpmessages.asp
$valid_request = $request_method == "GET" || $request_method == "POST" || $request_method == "PUT" || $request_method == "DELETE";
if (!$valid_request) {
    deliver_response(405, "Request Method Not Allowed.");
} else {
    if (does_student_exist($student_id)) {
        switch ($request_method) {
            case "GET":
                if ($map_id != null) {
                    if (!does_map_exist_by_id($student_id, $map_id)) {
                        deliver_response(200, "Map with id={$map_id} does not exist");
                        break;
                    }
                    $map = get_user_maps($student_id, $map_id);
                    $map = xml_maps_to_json($map);