<?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 == 'questionlist.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']=='change_status' && isset($_REQUEST['question_id'])){
    if(isset($_REQUEST['status'])){

        $questionDetail = $questionObj->fromId($_REQUEST['question_id']);
        if($questionDetail->question_status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        if($_REQUEST['status'] == '1')
        {
            $new_status = 'Active';
        }else{
            $new_status = 'Inactive';
        }
        $questionObj->changeStatus($_REQUEST['question_id']);

        $question_detail = " <b>Question Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$question_detail);
        echo RECORD_STATUS;
    }
    return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['question_id'])){
    $questionObj->deleteFromId($_REQUEST['question_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("question_status" => '1');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $questionObj->fromId($val_actchk);
            if($old_data->question_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->question_status != '1')
            {
                $new_status = 'Active';
                $questiondata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$questiondata);
            }
        }
        $rs = $questionObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("question_status" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $questionObj->fromId($val_actchk);
            if($old_data->question_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->question_status != '0')
            {
                $new_status = 'Inactive';
                $questiondata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$questiondata);
            }
        }
        
        $rs = $questionObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        
        $chk_del = implode(",",$_POST['check']);
        
        $questiondata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$questiondata);

        $rs = $questionObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}
if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['question_type']) && $_REQUEST['question_type'] != '')
    {
        $where .= " AND question_type LIKE '%".$_REQUEST['question_type']."%'";
    }
    if(isset($_REQUEST['question_title']) && $_REQUEST['question_title'] != '')
    {
        $where .= " AND question_title LIKE '%".$_REQUEST['question_title']."%'";
    }
    if(isset($_REQUEST['question_date']) && $_REQUEST['question_date'] != '')
    {
        $where .= " AND question_created_at LIKE '%".$_REQUEST['question_date']."%'";
    }
    if(isset($_REQUEST['question_status']) && $_REQUEST['question_status']!=""){
         
         $where .= " and question_status='".$_REQUEST['question_status']."' "; 
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND (question_title LIKE '%".$_REQUEST['search']['value']."%' OR question_type LIKE '%".$_REQUEST['search']['value']."%' OR question_created_at LIKE '%".$_REQUEST['search']['value']."%' )";
    }    
        
    $orderFields = array('','question_type','question_title');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" question_id desc ");
    }

    $data    = array('where' => $where,'orderBy'=>$orderby,'limit' => $limit);

    $questionDetails = $questionObj->getAllRecords($data);
    $recordsTotal   =   $questionObj->getAllCount($data);

        
    if ($questionDetails && count($questionDetails)>0){
        foreach ($questionDetails as $key => $question){

            if($question->question_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$question->question_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$question->question_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }

            // $action = '<a href="questionadd.php?act=edit&question_id='.$question->question_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
            //      <a id="delete-confirm" onclick="delete_entry('.$question->question_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';

             $action = '<a href="questionadd.php?act=edit&question_id='.$question->question_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

             $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$question->question_id.'" class="check_class" />',  
                 $question->question_type,
                 $question->question_title,
                 $question->question_example,
                 date("Y-m-d H:i:s",  strtotime($question->question_created_at)),
                 date("Y-m-d H:i:s",  strtotime($question->question_updated_at)),
                 "<div id='td_status_".$question->question_id."'>".$status."</div>",
                 $action
                );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>