<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('account_type')){
    function account_type($number = "", $is_supper_admin = "", $children_org = '') {
        if ($is_supper_admin == 1) {
            $result = array(
                array('id'=> 0, 'name'=> 'supper admin'),
                array('id'=> 1, 'name'=> 'admin'),
                array('id'=> 2, 'name'=> 'organization'),
                array('id'=> 3, 'name'=> 'teacher'),
                array('id'=> 4, 'name'=> 'student'),
            );
        } else
        if($number == 1) {
            $result = array(
                array('id'=> 1, 'name'=> 'admin'),
                array('id'=> 2, 'name'=> 'organization'),
                array('id'=> 3, 'name'=> 'teacher'),
                array('id'=> 4, 'name'=> 'student')
            );
        } else if ($number == 2) {
            if ($children_org == array()) {
                $result = array(
                    array('id'=> 3, 'name'=> 'teacher'),
                    array('id'=> 4, 'name'=> 'student')
                );
            } else {
                $result = array(
                    array('id'=> 1, 'name'=> 'admin'),
                    array('id'=> 2, 'name'=> 'organization'),
                    array('id'=> 3, 'name'=> 'teacher'),
                    array('id'=> 4, 'name'=> 'student')
                );
            }
        } else if ($number == 3) {
            $result = array(
                array('id'=> 4, 'name'=> 'student')
            );
        }
        return $result;
    }
}
if(!function_exists('name_account_type')) {
    function name_account_type($number) {
        switch($number) {
            case 1:
                $name = 'admin';
            break;
            case 2:
                $name = 'organization';
            break;
            case 3:
                $name = 'teacher';
            break;
            case 4:
                $name = 'student';
            break;
            default:
                $name = 'not_found';
        }
        return $name;
    }
}

if( !function_exists('all_child_organization')){
    function all_child_organization($organization_id) {
        $CI	=&	get_instance();
		$CI->load->database();
        $sql = 'SELECT  id ';
        $sql .= ' FROM (SELECT * FROM organization ORDER BY parent_id, id) C, (SELECT @pv := ' .$organization_id. ') C2 ';
        $sql .= ' WHERE FIND_IN_SET(parent_id, @pv) AND LENGTH(@pv := CONCAT(@pv, ",", id))';
        $query = $CI->db->query($sql);
        $result = $query->result_array();
        $arr = array();
        foreach($result as $res):
            array_push($arr, $res['id']);
        endforeach;
        return $arr;
    }
}
?>