// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with INTUITEL for Moodle Adaptor.  If not, see <http://www.gnu.org/licenses/>.
/**
 * REST interface.
 *
 * @package    block_intuitel
 * @author Juan Pablo de Castro, Elena Verdú.
 * @copyright  2015 Intuitel Consortium
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
use intuitel\Intuitel;
use intuitel\IntuitelXMLSerializer;
use intuitel\idFactory;
use intuitel\IntuitelController;
use intuitel\ProtocolErrorException;
require_once "../../config.php";
require_once 'locallib.php';
require_once 'model/exceptions.php';
block_intuitel_disable_moodle_page_exception_handler();
$xml = block_intuitel_get_input_message('POST|GET');
list($validated, $useridsvalidated, $response) = IntuitelController::ProcessAuthRequest($xml);
if ($validated) {
    $_SESSION['user_validated'] = true;
    $_SESSION['user_ids_validated'] = $useridsvalidated;
} else {
    session_destroy();
}
header('Content-type: text/xml');
echo $response;
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with INTUITEL for Moodle Adaptor.  If not, see <http://www.gnu.org/licenses/>.
/**
 * REST interface.
 *
 * @package    block_intuitel
 * @author Juan Pablo de Castro, Elena Verdú.
 * @copyright  2015 Intuitel Consortium
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
use intuitel\IntuitelXMLSerializer;
use intuitel\IntuitelController;
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once 'locallib.php';
require_once "model/LOFactory.php";
require_once "model/intuitelLO.php";
require_once 'model/serializer.php';
require_once 'model/exceptions.php';
block_intuitel_disable_moodle_page_exception_handler();
block_intuitel_check_access();
$params = array();
$serializer = new IntuitelXMLSerializer();
$xml = block_intuitel_get_input_message();
global $log;
$log->LogDebug("USE_PERF request received: {$xml}");
$response = IntuitelController::ProcessUsePerfRequest($xml);
header('Content-type: text/xml');
$log->LogDebug("USE_PERF response sent: {$response}");
echo $response;
require_once "../../model/intuitelController.php";
require_once '../../model/intuitelLO.php';
//require_once('DumbIntuitel.php');
require_once 'SmartyIntuitel.php';
// test this with: http://localhost/moodle2/blocks/intuitel/tests/mockrest/intuitel.php?xml=%3CINTUITEL%3E%3CLearner%20uId=%22pepe%22/%3E%3C/INTUITEL%3E
// http://localhost/moodle2/blocks/intuitel/tests/mockrest/intuitel.php?xml=%3CINTUITEL%3E%3CLearner%20uId=%22pepe%22%20loId=%22wqHKFQmiYXEq4tE6y4BztVFIdzsIe2d7localhost-CO2%22%20debugcourse=%222%22/%3E%3C/INTUITEL%3E
// http://localhost/moodle2/blocks/intuitel/tests/mockrest/intuitel.php?debugresponse=true&userid=3&xml=%3CINTUITEL%3E%3CLearner%20uId=%22pepe%22%20loId=%22wqHKFQmiYXEq4tE6y4BztVFIdzsIe2d7localhost-CO2%22%20debugcourse=%222%22/%3E%3C/INTUITEL%3E
// accept: debugcourse with native courseid to simulare Lore Loids
//          debugresponse to enable HTML page formating
//          userid to use native userid in the request.
$debugresponse = optional_param('debugresponse', false, PARAM_ALPHANUM);
if ($debugresponse == false) {
    block_intuitel_disable_moodle_page_exception_handler();
}
$native_userid = optional_param('userid', null, PARAM_INTEGER);
$query = block_intuitel_get_input_message();
$intuitelMsg = IntuitelController::getIntuitelXML($query);
//sleep(4);
if ($intuitelMsg->Learner) {
    if ($native_userid) {
        // Debugging backdoor
        $learnerid = Intuitel::getIDFactory()->getUserId($native_userid);
    } else {
        $learnerid = (string) $intuitelMsg->Learner['uId'];
    }
    $loId = new LOId((string) $intuitelMsg->Learner['loId']);
    $nativeCourseId = (string) $intuitelMsg->Learner['debugcourse'];
    $adaptor = Intuitel::getAdaptorInstanceForCourse();
    $idFactory = Intuitel::getIdFactory();
    if ($nativeCourseId) {
        global $PAGE;