<?php

include_once ".\\module.php";
session_start();
if (!isset($_SESSION['userid'])) {
    redirect_url('login.php?p=index.php');
}
$mongodb = getMongoDbConnection();
$database = $mongodb->test;
$collection = $database->response;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 10;
if (isset($_GET['from'])) {
    $from = $_GET['from'];
}
if (isset($_GET['to'])) {
    $to = $_GET['to'];
}
$skip = ($page - 1) * $limit;
$next = $page + 1;
$prev = $page - 1;
if (isset($from) && isset($to)) {
    $from = $from . " 00:00:00";
    $to = $to . " 00:00:00";
    $search_query = array('timestamp' => array('$gte' => $from, '$lte' => $to));
    $cursor = $collection->find($search_query)->skip($skip)->limit($limit);
} else {
    if (isset($_GET['search'])) {
        $search_string = $_GET['search'];
        $cursor = $collection->find(array('$text' => array('$search' => $search_string)), array('score' => array('$meta' => 'textScore')));
        $cursor = $cursor->sort(array('score' => array('$meta' => 'textScore')))->limit($limit)->skip($skip);
        ?>
				<script type="text/javascript">
					alert(logged out successfully);
				</script>
			<?php 
        // die();
    }
}
if (isset($_SESSION['userid'])) {
    if (isset($_GET['p'])) {
        redirect_url($_GET['p']);
    }
    redirect_url('index.php');
} else {
    if (isset($_POST['user']) && isset($_POST['passwd'])) {
        $conn = getMongoDbConnection() or die('Error connecting to db');
        $dbName = $conn->selectDB('test');
        $collection = $dbName->users;
        $query = array('_id' => $_POST['user'], 'password' => $_POST['passwd']);
        $cursor = $collection->findOne($query);
        // echo "<pre>";
        // var_dump($cursor);
        // foreach ($cursor as $key => $value) {
        // 	print_r($value);
        // }
        if ($cursor) {
            // echo "set";
            $_SESSION['userid'] = $_POST['user'];
            if (isset($_GET['p'])) {
                redirect_url($_GET['p']);
            } else {