<?php

require_once('../../config.php');
require_once('edit_form.php');
require_once('lib.php');
$site = get_site();
global $DB, $USER, $PAGE;

require_login();

//if already respond to question redirect to site frontpage

$PAGE->set_context(context_system::instance());
$data = array();
$siteurl = new moodle_url('/index.php');
$PAGE->set_url('/local/askquestion/index.php');

$_SESSION['currenturl'] = $PAGE->url; //Added by Desmond on 27Oct2017

if (isloggedin() && !is_siteadmin($USER->id) && !isguestuser($USER->id)) {
    
	$editform = new askquestion_edit_form(NULL);	
	
	if ($fromform = $editform->get_submitted_data()) {
        $fromdata = (array) $fromform;
		//$editform->validation($fromdata, null);
		if (!$errors = $editform->validation($fromdata, null)) {
			//echo 'No Errors - All Good';
			//redirect($_SESSION['currenturl']);  //Commented by Desmond on 27Oct2017
		}else{
			redirect($_SESSION['currenturl']); 
		}/*else {
            $max_attempt_allowed = get_config('local_askquestion', 'max_attempt_allowed_in_security_question');
            if ($_SESSION['how_many_times_failed_in_answering'] >= $max_attempt_allowed) {
                $logouturl = new moodle_url('/login/logout.php', array('sesskey' => sesskey()));
                $userobj = new stdClass();
                $userobj->id = $USER->id;
                $userobj->suspended = 1;
                 $return = $DB->update_record('user', $obj);
                send_account_suspend_email_to_user();
                echo $errors;
                redirect($logouturl);
            }
        }*/
		
    } else {
		
        $PAGE->set_pagelayout('base');
        $PAGE->set_url('/local/askquestion/index.php');
        $PAGE->set_context(context_system::instance());

        $heading = get_string('security_ques', 'local_askquestion');
        $title = "$site->shortname: " . $heading;

        $PAGE->navbar->add($heading);
        $PAGE->set_title($title);
        $PAGE->set_heading($heading);

        echo $OUTPUT->heading($heading);
		if(isset($_SESSION['errors'])){
			echo ($_SESSION['errors']['securityquestion']);
			unset($_SESSION['errors']);
		}
        $editform->display();
    }
}
?>
<style>
.mform .fitem
.fitemtitle{
	width:30% !important;
	text-align: left !important;
}
</style>