function ObtenerEstructura($param)
{
    global $CFG;
    /// save log registering request parameters
    $data_to_log->ISBN = $param->ISBN;
    add_to_log(2, 10, serialize($data_to_log));
    $ret = new ObtenerEstructuraResponse();
    $auth = UserAuthentication($GLOBALS["HTTP_RAW_POST_DATA"]);
    if ($auth->Codigo == '1') {
        //default return
        $ret->ObtenerEstructuraResult->Codigo = "0";
        $ret->ObtenerEstructuraResult->Descripcion = "no s'ha trobat llibre";
        if (isset($param)) {
            $sSql = "Select * from {$CFG->prefix}books where isbn = '{$param->ISBN}'";
            if ($books = get_recordset_sql($sSql)) {
                $arrLibros = array();
                $arrLibros = RetornaEstructuraLibros($books);
                if (count($arrLibros) > 0) {
                    $ret->ObtenerEstructuraResult->Codigo = "1";
                    $ret->ObtenerEstructuraResult->Descripcion = "procés correcte";
                    $lib = new libro();
                    $lib = $arrLibros[0];
                    $ret->ObtenerEstructuraResult->Libros->libro = $lib;
                }
            }
        }
    } else {
        $ret->ObtenerEstructuraResult->Codigo = $auth->Codigo;
        $ret->ObtenerEstructuraResult->Descripcion = $auth->Descripcion;
    }
    /// save log registering method response
    add_to_log(2, 20, serialize($ret->ObtenerEstructuraResult));
    return $ret;
}
function AutenticarUsuarioContenido($usrcontent)
{
    global $CFG;
    //save log registering the request data
    add_to_log(1, 10, serialize($usrcontent));
    $result = new AutenticarUsuarioContenidoResponse();
    $auth = UserAuthentication($GLOBALS["HTTP_RAW_POST_DATA"]);
    if ($auth->Codigo == '1') {
        //if book exists
        if ($book = get_record("books_credentials", 'isbn', $usrcontent->ISBN)) {
            //if credential of the book exists
            if ($book_credential = get_record("books_credentials", 'isbn', $usrcontent->ISBN, 'credentials', $usrcontent->Credencial)) {
                $result->AutenticarUsuarioContenidoResult->Codigo = $book_credential->code;
                $result->AutenticarUsuarioContenidoResult->Descripcion = $book_credential->description;
                $result->AutenticarUsuarioContenidoResult->URL = $book_credential->url;
                //XTEC *********** AFEGIT -> Check if isset parameter Rol and if one off the tow allowed values
                //2011.05.16  @mmartinez
                if (isset($usrcontent->Rol)) {
                    $alloweb_values = array("ESTUDIANTE", "PROFESOR");
                    if (!in_array($usrcontent->Rol, $alloweb_values)) {
                        $result->AutenticarUsuarioContenidoResult->Codigo = "-7";
                        $result->AutenticarUsuarioContenidoResult->Descripcion = "Rol incorrecte. El valor del rol és incorrecte";
                        $result->AutenticarUsuarioContenidoResult->URL = "http://www.xtec.cat/error.html";
                        return $result;
                    }
                } else {
                    $usrcontent->Rol = "ESTUDIANTE";
                }
                //*********** FI
                if ($book_credential->success == 1) {
                    /// get the absolute book path
                    $path = $CFG->wwwroot . '/data/books/';
                    if ($usrcontent->IdUnidad == '' && $usrcontent->IdActividad == '') {
                        if (!($bookpath = get_record('books', 'isbn', $usrcontent->ISBN))) {
                            //save log error becouse the ISBN it's not found in db
                            add_to_log(1, '1-200', serialize(array('ISBN' => $usrcontent->ISBN)), true);
                        } else {
                            /// manipulate the manifest to set href's absolutes
                            if ($bookpath->format == 'scorm') {
                                if (!manifest_manipulation($bookpath->path)) {
                                    add_to_log(1, '1-201', serialize(array('ISBN' => $usrcontent->ISBN, 'path' => $bookpath->path)), true);
                                }
                            }
                            /// set the absolute path to the manifest
                            $result->AutenticarUsuarioContenidoResult->URL = $path . $bookpath->path;
                        }
                    } else {
                        if ($usrcontent->IdUnidad != '' && $usrcontent->IdActividad == '') {
                            if (!($bookpath = get_record('books', 'isbn', $usrcontent->ISBN))) {
                                //save log error becouse the ISBN it's not found in db
                                add_to_log(1, '1-210', serialize(array('ISBN' => $usrcontent->ISBN)), true);
                            } else {
                                if (!($unitpath = get_record('books_units', 'bookid', $bookpath->id, 'code', $usrcontent->IdUnidad))) {
                                    //save log error becouse the Unit code it's not found in db
                                    add_to_log(1, '1-211', serialize(array('ISBN' => $usrcontent->ISBN, 'unitcode' => $usrcontent->IdUnidad)), true);
                                    $result->AutenticarUsuarioContenidoResult->Codigo = "-5";
                                    $result->AutenticarUsuarioContenidoResult->Descripcion = "L'identificador de la unitat no és vàlid";
                                    $result->AutenticarUsuarioContenidoResult->URL = "http://www.xtec.cat/error.html";
                                    return $result;
                                } else {
                                    /// manipulate the manifest to set href's absolutes
                                    if ($bookpath->format == 'scorm') {
                                        if (!manifest_manipulation($unitpath->path)) {
                                            add_to_log(1, '1-212', serialize(array('ISBN' => $usrcontent->ISBN, 'unitcode' => $usrcontent->IdUnidad, 'path' => $unitpath->path)), true);
                                        }
                                    }
                                    /// set the absolute path to the manifest
                                    $result->AutenticarUsuarioContenidoResult->URL = $path . $unitpath->path;
                                }
                            }
                        } else {
                            if ($usrcontent->IdUnidad != '' && $usrcontent->IdActividad != '') {
                                if (!($bookpath = get_record('books', 'isbn', $usrcontent->ISBN))) {
                                    //save log error becouse the ISBN it's not found in db
                                    add_to_log(1, '1-220', serialize(array('ISBN' => $usrcontent->ISBN)), true);
                                } else {
                                    if (!($unitpath = get_record('books_units', 'bookid', $bookpath->id, 'code', $usrcontent->IdUnidad))) {
                                        //save log error becouse the Unit code it's not found in db
                                        add_to_log(1, '1-221', serialize(array('ISBN' => $usrcontent->ISBN, 'unitcode' => $usrcontent->IdUnidad)), true);
                                        $result->AutenticarUsuarioContenidoResult->Codigo = "-5";
                                        $result->AutenticarUsuarioContenidoResult->Descripcion = "L'identificador de la unitat no és vàlid";
                                        $result->AutenticarUsuarioContenidoResult->URL = "http://www.xtec.cat/error.html";
                                        return $result;
                                    } else {
                                        if (!($activitypath = get_record('books_activities', 'bookid', $bookpath->id, 'unitid', $unitpath->id, 'code', $usrcontent->IdActividad))) {
                                            add_to_log(1, '1-222', serialize(array('ISBN' => $usrcontent->ISBN, 'unitcode' => $usrcontent->IdUnidad, 'activitycode' => $usrcontent->IdActividad, 'path' => $activitypath->path)), true);
                                            $result->AutenticarUsuarioContenidoResult->Codigo = "-6";
                                            $result->AutenticarUsuarioContenidoResult->Descripcion = "L'identificador de la activitat no és vàlid ";
                                            $result->AutenticarUsuarioContenidoResult->URL = "http://www.xtec.cat/error.html";
                                            return $result;
                                        } else {
                                            /// manipulate the manifest to set href's absolutes
                                            if ($bookpath->format == 'scorm') {
                                                if (!manifest_manipulation($activitypath->path)) {
                                                    add_to_log(1, '1-223', serialize(array('ISBN' => $usrcontent->ISBN, 'unitcode' => $usrcontent->IdUnidad, 'path' => $activitypath->path)), true);
                                                }
                                            }
                                            /// set the absolute path to the manifest
                                            $result->AutenticarUsuarioContenidoResult->URL = $path . $activitypath->path;
                                        }
                                    }
                                }
                            } else {
                                add_to_log(1, '1-204', serialize(array('ISBN' => $book_credencial->ISBN)));
                                $result->AutenticarUsuarioContenidoResult->URL = $book_credential->url;
                            }
                        }
                    }
                    //GAP
                    //********** AFEGIT XTEC - if URL generated correctly, generates the token and saves the data in the session table
                    if ($result->AutenticarUsuarioContenidoResult->Codigo == 1) {
                        if (isset($bookpath->format) and $bookpath->format == 'webcontent' and !isset($_GET['wsdl'])) {
                            $session = new stdClass();
                            $session->token = str_replace('.', '', uniqid('', true));
                            $session->isbn = $usrcontent->ISBN;
                            $session->userid = $usrcontent->IdUsuario;
                            $session->nameape = $usrcontent->NombreApe;
                            $session->groupid = $usrcontent->IdGrupo;
                            $session->courseid = $usrcontent->IdCurso;
                            $session->centerid = $usrcontent->IdCentro;
                            $session->wsurltracking = $usrcontent->URLResultado;
                            $session->lmscontentid = $usrcontent->IdContenidoLMS;
                            $session->unitid = $usrcontent->IdUnidad;
                            $session->activityid = $usrcontent->IdActividad;
                            $session->addtime = time();
                            $session->expiretime = time() + 86400;
                            //expire in 24 hours
                            $session->urlcontent = $result->AutenticarUsuarioContenidoResult->URL . "?token={$session->token}";
                            $session = addslashes_object($session);
                            $result->AutenticarUsuarioContenidoResult->URL = $result->AutenticarUsuarioContenidoResult->URL . "?token={$session->token}";
                            insert_record("sessions", $session);
                        }
                    }
                }
                //**********
            } else {
                $result->AutenticarUsuarioContenidoResult->Codigo = '-2';
                $result->AutenticarUsuarioContenidoResult->Descripcion = 'El codi de llicencia no es vàlid.';
                $result->AutenticarUsuarioContenidoResult->URL = 'http://www.xtec.cat/error.html';
            }
        } else {
            $result->AutenticarUsuarioContenidoResult->Codigo = '-3';
            $result->AutenticarUsuarioContenidoResult->Descripcion = 'El ISBN del producte no es vàlid.';
            $result->AutenticarUsuarioContenidoResult->URL = 'http://www.xtec.cat/error.html';
        }
    } else {
        $result->AutenticarUsuarioContenidoResult->Codigo = $auth->Codigo;
        $result->AutenticarUsuarioContenidoResult->Descripcion = $auth->Descripcion;
        $result->AutenticarUsuarioContenidoResult->URL = $auth->url;
    }
    /// save log registering method response
    add_to_log(1, 20, serialize($result->AutenticarUsuarioContenidoResult));
    return $result;
}