<?php

use app\libraries\Tenants;

defined('BASEPATH') or exit('No direct script access allowed');

class Note_model extends MY_Model {

    use Tenants;

    public $short = "n";
    public $table;
    public $tbl;
    public $_sql = '';

//    public $database;

    public function __construct() {
        parent::__construct();
        $this->database = 'default';
        $this->tbl = 'note';
        $this->table = $this->tbl . ' ' . $this->short;
		$tenantno = $this->session->userdata('tenant_no');
		$sql_pattern = sprintf("SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `%snote` `n`
JOIN `%sunit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `{{dbname}}`.`%scourse` `c` ON `u`.`course_id` = `c`.`id`
JOIN `{{dbname}}`.`%scourse_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `{{dbname}}`.`%scourse_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` = '{{tenant}}'", $this->table_prefix, $this->table_prefix, $this->table_prefix, $this->table_prefix, $this->table_prefix);
		$sqlArr = [];
        $sqlArr[] = str_replace(array('{{dbname}}', '{{tenant}}'), array($this->database_prefix . 'main', 'main'), $sql_pattern);
		if ($tenantno !== 'main') {
			$sqlArr[] = str_replace(array('{{dbname}}', '{{tenant}}'), array($this->database_prefix . 'main', $tenantno), $sql_pattern);
		}
		$this->_sql = implode(" union ", $sqlArr);
    }

    public function getByIds($noteids) {
    	$this->db->select('title, filename, content, FROM_UNIXTIME(create_time,"%Y-%m-%d") as createdate, tenant_no', false);
    	$this->db->from($this->table_prefix . $this->tbl);
    	$this->db->where('status', 1);
    	$this->db->where_in('id', $noteids);
    	return $this->db->get()->result_array();
//        $sql = 'Select title,filename,content,FROM_UNIXTIME(create_time,"%Y-%m-%d") as createdate, tenant_no From ?note Where id in ? and status = ?';
//        $data = $this->db->query($sql, array($this->table_prefix, explode(',', $noteids), 1))->result_array();
//        return $data;
    }

    public function totalCount() {
        $db = $this->load->database('default', TRUE);
        $db->from($this->table_prefix . $this->table);
        $tenantno = $this->session->userdata('tenant_no');
        if ($this->session->userdata('tenant_no') === 'main') {
            return $db->count_all_results();
        } else {
/*
            $sql = "SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='$tenantno'
union 
SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='main'
";
*/
			$sql = $this->_sql;
            $db = $this->load->database($this->database, TRUE);
            $data = $db->query($sql)->result_array();
            return count($data);
        }
    }

    public function filteredCount($param) {
        $fields = [
            $this->short . '.id',
            $this->short . '.status',
            $this->short . '.tenant_no',
            $this->short . '.title AS note_name',
            'u.title As `unit_name`',
            'cc.name AS `category_name`',
            'subcc.name AS `subcategory_name`',
            'c.name AS `course_name`',
        ];
        $orders = [];
        foreach ($param['order'] as $order) {
            $colIdx = $order['column'];
            $colName = str_replace(array('unit_name', 'category_name', 'subcategory_name', 'course_name'), array('u.title', 'cc.name', 'subcc.name', 'c.name'), $param['columns'][$colIdx]['data']);
            $dir = strtoupper($order['dir']);
            $orders[] = ($colName === 'u.title' || $colName === 'cc.name' || $colName === 'subcc.name' || $colName === 'c.name' || $colName === 'note_name') ? $colName . ' ' . $dir : $this->short . "." . $colName . ' ' . $dir;
        }
        $tenant_no = $this->database_prefix . $this->session->userdata('tenant_no');
        $unit_table = sprintf("%sunit u", $this->table_prefix);
        $course_table = sprintf("%s.%scourse c", $tenant_no, $this->table_prefix);
        $category_table = sprintf("%s.%scourse_category cc", $tenant_no, $this->table_prefix);
        $subcategory_table = sprintf("%s.%scourse_category_sub subcc", $tenant_no, $this->table_prefix);
        $joins = [
            $unit_table => $this->short . '.unit_id=u.id',
            $course_table => 'u.course_id = c.id',
            $category_table => 'c.course_category_id = cc.id',
            $subcategory_table => 'c.course_category_sub_id = subcc.id',
        ];
        $db = $this->load->database($this->database, TRUE);
        $tenantno = $this->session->userdata('tenant_no');
        $db->distinct();
        $db->select(implode(', ', $fields));
        $db->from($this->table_prefix . $this->table);
//        if ($this->database == 'default') {
        list($dummy, $shortname) = explode(" ", $this->table);
//        $db->like($shortname . '.tenant_no', $tenantno, 'none');
        $db->where_in($shortname . '.tenant_no', ['main', $tenantno]);
//        }
        foreach ($joins as $joinTbl => $condition) {
            $db->join($joinTbl, $condition);
        }
        $search = [];
        foreach ($param['columns'] as $column) {
            if ($column['searchable'] === "true" && !empty($column['search']['value'])) {
                $colName = str_replace(array('unit_name', 'category_name', 'subcategory_name', 'course_name'), array('u.title', 'cc.name', 'subcc.name', 'c.name'), $column['data']);
                $searchfield = ($colName === 'u.title' || $colName === 'cc.name' || $colName === 'subcc.name' || $colName === 'c.name') ? $colName : $this->short . "." . $colName;
                $search[$searchfield] = $column['search']['value'];
            }
        }
        if (!empty($search)) {
            foreach ($search as $k => $v) {
//                 log_message('debug','@filtercount@ k:'.$k.';v:'.$v);
                if ($v !== '') {
                    $db->like($search, 'none');
                }
            }
        }
        if ($this->session->userdata('tenant_no') === 'main') {
            return count($db->get()->result_array());
        } else {
/*
            $sql = "SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='$tenantno'
union 
SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='main'
";
*/
			$sql = $this->_sql;
            $db = $this->load->database($this->database, TRUE);
            $data = $db->query($sql)->result_array();
            if (!empty($search)) {
                foreach ($search as $k => $v) {
//                log_message('debug','@getfromfilter@ k:'.$k.';v:'.$v);// k:u.subcc.name;v:K1
                    if ($v !== '') {
                        $key = str_replace(['cc.name', 'c.name', 'subcc.name', 'u.title'], ['category_name', 'course_name', 'subcategory_name', 'unit_name'], $k);
                        foreach ($data as $i => $d) {
                            if ($data[$i][$key] !== $v) {
                                unset($data[$i]);
                            }
                        }
                    }
                }
            }
            return count($data);
        }
    }

    public function listing($param) {
        $fields = [
            $this->short . '.id',
            $this->short . '.status',
            $this->short . '.tenant_no',
            $this->short . '.title AS note_name',
            'u.title As `unit_name`',
            'u.status As ustatus',
            'cc.name AS `category_name`',
            'cc.status As categorystatus',
            'subcc.name AS `subcategory_name`',
            'subcc.status As substatus',
            'c.name AS `course_name`',
            'c.status As cstatus'
        ];
        $orders = [];
        foreach ($param['order'] as $order) {
            $colIdx = $order['column'];
            $colName = str_replace(array('unit_name', 'category_name', 'subcategory_name', 'course_name'), array('u.title', 'cc.name', 'subcc.name', 'c.name'), $param['columns'][$colIdx]['data']);
            $dir = strtoupper($order['dir']);
            $orders[] = ($colName === 'u.title' || $colName === 'cc.name' || $colName === 'subcc.name' || $colName === 'c.name' || $colName === 'note_name') ? $colName . ' ' . $dir : $this->short . "." . $colName . ' ' . $dir;
        }
//        log_message('debug',implode(',',$orders));
        $start = $param['start'];
        $length = $param['length'];
        $tenant_no = $this->database_prefix . $this->session->userdata('tenant_no');
        $unit_table = sprintf("%sunit u", $this->table_prefix);
        $course_table = sprintf("%s.%scourse c", $tenant_no, $this->table_prefix);
        $category_table = sprintf("%s.%scourse_category cc", $tenant_no, $this->table_prefix);
        $subcategory_table = sprintf("%s.%scourse_category_sub subcc", $tenant_no, $this->table_prefix);
        $joins = [
            $unit_table => $this->short . '.unit_id=u.id',
            $course_table => 'u.course_id = c.id',
            $category_table => 'c.course_category_id = cc.id',
            $subcategory_table => 'c.course_category_sub_id = subcc.id',
        ];
        $search = [];
        foreach ($param['columns'] as $column) {
            if ($column['searchable'] === "true" && !empty($column['search']['value'])) {
                $colName = str_replace(array('unit_name', 'category_name', 'subcategory_name', 'course_name'), array('u.title', 'cc.name', 'subcc.name', 'c.name'), $column['data']);
                $searchfield = ($colName === 'u.title' || $colName === 'cc.name' || $colName === 'subcc.name' || $colName === 'c.name') ? $colName : $this->short . "." . $colName;
                $search[$searchfield] = $column['search']['value'];
            }
        }
        $result = $this->getFromDBFilter($this->database, $this->table, $fields, $start, $length, $orders, $search, $joins);
        $data = [];
        foreach ($result as $row) {
            $data[] = [
                'id' => trim($row['id']),
                'tenant_no' => $this->tenantname[$row['tenant_no']],
                'note_name' => trim($row['note_name']),
                'unit_name' => $row['ustatus'] == 0 ? '<span class="notinuse">' . $row['unit_name'] . '</span>' : trim($row['unit_name']),
                'category_name' => $row['categorystatus'] == 0 ? '<span class="notinuse">' . $row['category_name'] . '</span>' : trim($row['category_name']),
                'subcategory_name' => $row['substatus'] == 0 ? '<span class="notinuse">' . $row['subcategory_name'] . '</span>' : trim($row['subcategory_name']),
                'course_name' => $row['cstatus'] == 0 ? '<span class="notinuse">' . $row['course_name'] . '</span>' : trim($row['course_name']),
                'status' => $this->status[$row['status']],
                'details' => '<button class="details" data-id="' . trim($row['id']) . '">詳情</button>',
            ];
        }
//        log_message('debug','data count:'.count($data));
        return $data;
    }

    //overwrite
    public function getFromDBFilter($database, $table, $fields, $start, $length, $orders, $search, $joins = [], $keywordfieldname = '') {
        $db = $this->load->database($database, TRUE);
        $tenantno = $this->session->userdata('tenant_no');
        $db->distinct();
        $db->select(implode(', ', $fields));
        $db->from($this->table_prefix . $table);
        list($dummy, $shortname) = explode(" ", $table);
        $db->where_in($shortname . '.tenant_no', ['main', $tenantno]);

        if (count($joins) > 0) {
            foreach ($joins as $joinTbl => $condition) {
                $db->join($joinTbl, $condition);
            }
        }

//        log_message('debug','not empty search?'.!empty($search));
        if (!empty($search)) {
            foreach ($search as $k => $v) {
//                log_message('debug','@getfromfilter@ k:'.$k.';v:'.$v);// k:u.subcc.name;v:K1
                if ($v !== '') {
                    $db->where($search);
                }
            }
        }

        $db->order_by(implode(', ', $orders));
        if ($start > -1) {
            $db->limit($length, $start);
        }
        $data = $db->get()->result_array();
        if ($this->session->userdata('tenant_no') === 'main') {
            return $data;
        } else {
/*
            $sql = "SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_$tenantno`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='$tenantno'
union 
SELECT DISTINCT `n`.`id`, `n`.`status`, `n`.`tenant_no`, `n`.`title` AS `note_name`, `u`.`title` As `unit_name`, `u`.`status` As `ustatus`, `cc`.`name` AS `category_name`, `cc`.`status` As `categorystatus`, `subcc`.`name` AS `subcategory_name`, `subcc`.`status` As `substatus`, `c`.`name` AS `course_name`, `c`.`status` As `cstatus`
FROM `mas_note` `n`
JOIN `mas_unit` `u` ON `n`.`unit_id`=`u`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course` `c` ON `u`.`course_id` = `c`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category` `cc` ON `c`.`course_category_id` = `cc`.`id`
JOIN `atwebshk_ci_mastery_main`.`mas_course_category_sub` `subcc` ON `c`.`course_category_sub_id` = `subcc`.`id`
WHERE `n`.`tenant_no` ='main'
";
*/
			$sql = $this->_sql;
            $db = $this->load->database($this->database, TRUE);
            $data = $db->query($sql)->result_array();
            if (!empty($search)) {
                foreach ($search as $k => $v) {
//                log_message('debug','@getfromfilter@ k:'.$k.';v:'.$v);// k:u.subcc.name;v:K1
                    if ($v !== '') {
                        $key = str_replace(['cc.name', 'c.name', 'subcc.name', 'u.title'], ['category_name', 'course_name', 'subcategory_name', 'unit_name'], $k);
                        foreach ($data as $i => $d) {
                            if ($data[$i][$key] !== $v) {
                                unset($data[$i]);
                            }
                        }
                    }
                }
            }
            $orderby = implode(', ', $orders);
            $orderkey = explode(' ', $orderby)[0];
            $direction = explode(' ', $orderby)[1];
//            log_message('debug', 'orderkey:' . $orderkey . ';direction:' . $direction);//orderkey:u.title;direction:ASC
            if ($orderkey === 'n.tenant_no') {
                usort($data, array($this, 'cmpbytenantno' . $direction));
            }
            if ($orderkey === 'note_name') {
                usort($data, array($this, 'cmpbynote' . $direction));
            }
            if ($orderkey === 'u.title') {
                usort($data, array($this, 'cmpbyunit' . $direction));
            }
            if ($orderkey === 'cc.name') {
                usort($data, array($this, 'cmpbyccname' . $direction));
            }
            if ($orderkey === 'c.name') {
                usort($data, array($this, 'cmpbycname' . $direction));
            }
            if ($orderkey === 'subcc.name') {
                usort($data, array($this, 'cmpbysubccname' . $direction));
            }
            if ($orderkey === 'n.status') {
                usort($data, array($this, 'cmpbystatus' . $direction));
            }

            if ($start > -1) {
                $data = array_slice($data, $start, $length);
            }
            return $data;
        }
    }

    private function cmpbystatusASC($a, $b) {
        return strcmp($a['status'], $b['status']);
    }

    private function cmpbystatusDESC($a, $b) {
        return strcmp($b['status'], $a['status']);
    }

    private function cmpbysubccnameASC($a, $b) {
        return strcmp($a['subcategory_name'], $b['subcategory_name']);
    }

    private function cmpbysubccnameDESC($a, $b) {
        return strcmp($b['subcategory_name'], $a['subcategory_name']);
    }

    private function cmpbycnameASC($a, $b) {
        return strcmp($a['course_name'], $b['course_name']);
    }

    private function cmpbycnameDESC($a, $b) {
        return strcmp($b['course_name'], $a['course_name']);
    }

    private function cmpbyccnameASC($a, $b) {
        return strcmp($a['category_name'], $b['category_name']);
    }

    private function cmpbyccnameDESC($a, $b) {
        return strcmp($b['category_name'], $a['category_name']);
    }

    private function cmpbytenantnoASC($a, $b) {
        return strcmp($a['tenant_no'], $b['tenant_no']);
    }

    private function cmpbytenantnoDESC($a, $b) {
        return strcmp($b['tenant_no'], $a['tenant_no']);
    }

    private function cmpbyunitASC($a, $b) {
        return strcmp($a['unit_name'], $b['unit_name']);
    }

    private function cmpbyunitDESC($a, $b) {
        return strcmp($b['unit_name'], $a['unit_name']);
    }

    private function cmpbynoteASC($a, $b) {
        return strcmp($a['note_name'], $b['note_name']);
    }

    private function cmpbynoteDESC($a, $b) {
        return strcmp($b['note_name'], $a['note_name']);
    }

    public function insert($params) {
        $id = $this->insertDB('default', $this->tbl, $params);

        /* Auto generate note_code */
        $unitCode = sprintf("%05d", $params['unit_id']);
        $_digit = 5;
        $codePrefix = 'NOTE';
        $sql = sprintf("UPDATE `%snote` SET `note_code` = UCASE(" .
                "   CONCAT ('%s', " .
                "       LPAD(" .
                "           CONVERT(" .
                "               SUBSTRING(" .
                "                   IFNULL((SELECT * FROM (SELECT MAX(`note_code`) FROM `%snote` WHERE `tenant_no` = '%s' AND `note_code` LIKE '%s%%') AS `temp`), 0)," .
                "               -%s)," .
                "           UNSIGNED INTEGER) + 1," .
                "       %s, 0)" .
                "   )) " .
                "WHERE `id` = %s", $this->table_prefix, $codePrefix . $unitCode, $this->table_prefix, $params['tenant_no'], $codePrefix . $unitCode, $_digit, $_digit, $id);
        $this->db->query($sql);
        $this->db->where('id', $id);
        $this->db->select('note_code');
        $newCode = $this->db->get($this->table_prefix . 'note')->result_array()[0]['note_code'];
        if (!is_null($params['ext'])) {
            $update = [];
            $update['filename'] = $newCode . "." . $params['ext'];
            $this->db->where('id', $id);
            $this->db->update($this->table_prefix . 'note', $update);
        }
        return $newCode;
    }

    public function all() {
        $fields = [$this->short . '.id', $this->short . '.name', $this->short . '.status'];
        return $this->getAll($this->database, $this->table, $fields);
    }

    public function one($id) {
//    	log_message('debug','id:'.$id);
        $data = $this->getOne($this->database, $this->tbl, [], $id);
        $this->load->model('Unit_model');
//		log_message('debug','unitid:'.$data['unit_id']);
        $unit = $this->Unit_model->one($data['unit_id']);
//        log_message('debug','unittitle:'.$unit['title']);//ok
        $this->load->model('Course_model');
//        log_message('debug','course id:'.$unit['course_id']);
        $course = $this->Course_model->detailfortenants($unit,['course_category_id', 'course_category_sub_id']);
//        log_message('debug','catid:'.$course['course_category_id']);
        $data = array_merge(['course_id' => $unit['course_id']], $data);
        return array_merge($data, $course);
    }

    public function save($params, $id) {
        $this->saveDB($this->database, $this->tbl, $params, $id);
    }

    public function deleteMultiple($ids) {
        $this->deleteRelatedDB($this->database, array('note_price', $this->tbl), array('note_id', 'id'), $ids);
    }

    public function deleteAll() {
        $this->deleteAllDB($this->database, $this->tbl);
    }

    public function getAllIds() {
        $ids = [];
        $all = $this->all();
        foreach ($all as $k => $v) {
            $ids[] = $v['id'];
        }
        return $ids;
    }

    public function getUnits($courseId, $tenantno = '') {
        $this->load->model('Unit_model');
        return $this->Unit_model->allfornotes($courseId, $tenantno);
    }

    public function getCategories() {
        $this->load->model('Course_category_model');
        return $this->Course_category_model->allfortenants();
    }

    public function getSubCategories($categoryId) {
        $this->load->model('Course_subcategory_model');
        return $this->Course_subcategory_model->allfortenants($categoryId);
    }

    public function getCourses($categoryId, $subcategoryId) {
        $this->load->model('Course_model');
        return $this->Course_model->allfortenants($categoryId, $subcategoryId);
    }

    public function getAllUnits_filter($tenant) {
        $this->load->model('Unit_model');
        return $this->Unit_model->allforfilter($tenant);
    }

    public function getAllUnits($tenantno) {
        $this->load->model('Unit_model');
        return $this->Unit_model->all('', $tenantno);
    }

    public function getAllCourses_filter($tenant) {
        $this->load->model('Course_model');
        return $this->Course_model->allforfilter($tenant);
    }

    public function getAllCourses() {
        $this->load->model('Course_model');
        return $this->Course_model->all();
    }

    public function getAllCategories_filter() {
        $this->load->model('Course_category_model');
        return $this->Course_category_model->allforfilter();
    }

    public function getAllCategories() {
        $this->load->model('Course_category_model');
        return $this->Course_category_model->all();
    }

    public function getAllSubCategories_filter() {
        $this->load->model('Course_subcategory_model');
        return $this->Course_subcategory_model->allforfilter();
    }

    public function getAllSubCategories() {
        $this->load->model('Course_subcategory_model');
        return $this->Course_subcategory_model->all();
    }

}
