<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$questionObj =  new  question();

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'questionadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;

}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['question_id'])){
    $questionDetail = $questionObj->fromId($_REQUEST['question_id']);
}


if(isset($_POST['submit'])){
    	$data=array(
          'question_title'=>trim($_POST['question_title']),
          'question_example'=>trim($_POST['question_example']),
          'question_type'=>trim($_POST['question_type']),
	);
        if(isset($_POST['question_status'])){
            $data['question_status'] 			= '1';
        }else{
            $data['question_status'] 			= '0';
        }

        $questionDetail = $questionObj->fromId($_REQUEST['question_id']);
        if(!empty($questionDetail))
        {
            $questiondata = "<b>Edit Record</b> ";
            if($questionDetail->question_type != trim($_POST['question_type']))
            {   

                $questiondata .= " <b>Type</b>: "." ".$questionDetail->question_type." <b>To</b> ".$_POST['question_type']." and ";
            
                $chages_data = true;
            }

            if($questionDetail->question_title != trim($_POST['question_title']))
            {
                $questiondata .= " <b>Title</b>: "." ".$questionDetail->question_title." <b>To</b> ".$_POST['question_title']." and ";
            
                $chages_data = true;
            }

            if($questionDetail->question_example != trim($_POST['question_example']))
            {
                $questiondata .= " <b>Example</b>: "." ".$questionDetail->question_example." <b>To</b> ".$_POST['question_example']." and ";
            
                $chages_data = true;
            }
            

            if($questionDetail->question_status != $data['question_status'])
            {
                
                if($data['question_status'] == '1')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

                if($questionDetail->question_status == '1')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

                $questiondata .= " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." and ";
            
                $chages_data = true;
            }  
        } 

        if (isset($_REQUEST['question_id'])){
            $data['question_updated_at']=date('Y-m-d H:i:s');
            $id=$_REQUEST['question_id'];
            $rs=$questionObj->updateRecords($data,$id);
            if ($rs){

                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($questiondata," and"));
                }
                $successMsg = base64_encode(RECORD_UPDATE);			
                //?&successMsg='.$successMsg
                header('Location:questionlist.php');
                exit;
            }
	}else{
           
            $data['question_created_at']=date('Y-m-d H:i:s');

            $questiondata = "<b>Add Record</b> ";
            $questiondata.= " <b>Question Data</b>: ".json_encode($data);

            $rs = $questionObj->addRecords($data);
            if($rs){

                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$questiondata);

                $successMsg = base64_encode(RECORD_ADD);			
                header('Location:questionlist.php?&successMsg='.$successMsg);
                exit;
            }
	}
}

// Include Template 
include_once('../libs/routs.php')

?>