<?php
/****************************
 * File : category.php
 * Author : Cygnusa
 * @2018
 ****************************/

//SECURITY
if (!defined('APPLICATION_PATH')) {
    exit('No direct script access allowed');
}

class category
{

    //FUNCTION LISTS
    public function __construct($parent)
    {
        //GET PAGE MESSAGE
        $this->page_data = globalfunc::getMessage($parent, $parent->lang, 'category');
        if (count($parent->params) == 0) {

            $this->categoryfunc($parent);
        } else {
            $qry_params = explode("?", $parent->params[0]);
            $parameter_val = strtolower($qry_params[0]);

            if (strtolower($parent->device_type) == 'app') {
                switch ($parameter_val) {

                    default:
                        $this->categoryfunc($parent);
                        break;
                }
            } else if (strtolower($parent->device_type) == 'web') {
                switch ($parameter_val) {

                    default:
                        $this->categoryfunc($parent);
                        break;
                }
            }
        }

        $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
    }

    //FUNCTION
    public function categoryfunc($parent)
    {
        switch ($parent->get_request_method()) {
            case "GET":$this->categoryList($parent);
                break;

            default:
                $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
                break;
        }
    }

    /* /////////////////////////////// BANNER LIST //////////////////////////////////////////////// */
    private function categoryList($parent)
    {
        //BANNER LIST
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 0);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //LOG
        $logstr = 'CATEGORY';
        globalfunc::insertUserLog($parent, $logstr);

        $query_category = $parent->selectQuery(TABLE_CATEGORY, "`cateid`, `catename`", "1=1 ORDER BY cateid ", "");

        $userid = $parent->current_user_id;
        $check_user_str = "SELECT  userid,segment,role FROM " . TABLE_USER . "  WHERE  status='1' AND is_deleted='0' AND userid='" . $userid . "' LIMIT 1";
        $check_user_arr = $parent->getQueryValue($check_user_str);
        if (!count($check_user_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error2"]), 406);
        }

        if ($check_user_arr[0]->role > 7 && $check_user_arr[0]->role < 14) {
            $user_filter_str = projectfunc::getUserfiltervalue($parent, array('role', 'zone', 'city', 'dealer_code'), $userid);
        } else {
            $user_filter_str = projectfunc::getUserfiltervalue($parent, array('role'), $userid);
        }
        $user_cond = str_replace("filter_", "c.filter_", $user_filter_str);

        if ($check_user_arr[0]->role > 7 && $check_user_arr[0]->role < 14) {
            switch ($check_user_arr[0]->segment) {
                case "1":$user_cond .= " AND t.segment IN (0,1)  AND c.segment IN (0,1)";
                    break;
                case "2":$user_cond .= " AND t.segment IN (0,2)  AND c.segment IN (0,2)";
                    break;
                default:$user_cond .= " AND t.segment IN (0)  AND c.segment IN (0)";
                    break;
            }
        }

        $user_cond .= ($parent->current_user_role == 15) ? " AND t.assettopicid=61" : "AND t.assettopicid!=61";

        if ($query_category['nr'] > 0) {
            foreach ($query_category['result'] as $ckey => $res_cat) {

                $get_course_str = "SELECT COUNT(c.`contentid`) as totcontent FROM " . TABLE_ASSET_CONTENT . " as c JOIN " . TABLE_ASSET_TOPIC . " as t ON t.assettopicid=c.assettopicid WHERE t.status='1' AND t.is_deleted='0' AND c.status='1' AND c.is_deleted='0' AND t.cateid='" . $res_cat->cateid . "' " . $user_cond;

                $get_course_arr = $parent->getQueryValue($get_course_str);
                $total_content = (count($get_course_arr) > 0) ? $get_course_arr[0]->totcontent : '0';

                //GET USER COMPLETED CONTENT COUNT
                $get_user_completed_str = "SELECT COUNT(c.`contentid`) as totcontent FROM " . TABLE_ASSET_CONTENT . " as c JOIN " . TABLE_ASSET_TOPIC . " as t ON t.assettopicid=c.assettopicid JOIN (SELECT * FROM " . TABLE_USER_COMPLETE_CONTENT . " WHERE userid='" . $userid . "' AND status='2') as a ON a.contentid=c.contentid WHERE t.status='1' AND t.is_deleted='0' AND c.status='1' AND c.is_deleted='0' AND t.cateid='" . $res_cat->cateid . "' " . $user_cond;
                $get_user_completed_arr = $parent->getQueryValue($get_user_completed_str);
                $user_content = (count($get_user_completed_arr) > 0) ? $get_user_completed_arr[0]->totcontent : '0';

                $query_category['result'][$ckey]->total = ($total_content > 0) ? $total_content : '0';
                $query_category['result'][$ckey]->completed = ($user_content > 0) ? $user_content : '0';
                $query_category['result'][$ckey]->percentage = ($total_content > 0) ? round(($user_content / $total_content) * 100) : '0';

            }
        }

        $parent->response(array('response' => 'success', 'data' => $query_category['result'], 'total' => $query_category['nr'], 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

}
