<commit_before><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Memberships_model extends CI_Model {
	
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
		
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// Memberships
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	

 	/**
	 * get_user_memberships
	 * Gets a paginated list of users that can be filtered via the user search form, filtering by the users email and first and last names.
	 */
	function get_user_memberships($user_id, $type='All') {
		if ($type == 'All') {
			$this->db->select("membership_names.*,membership_types.name as type, membership_types.number as max_users, count(distinct membership_users.user_id) as user_count, max(membership_credits.end) as expires, membership_types.paypal_button, membership_types.paypal_subscription_button");
			$this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
			$this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
			$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
			$this->db->group_by("membership_names.id");
			$this->db->order_by("expires","desc");
			$this->db->where('membership_names.owner_id', $user_id);
			$this->db->where('membership_credits.end is not null');
			$query = $this->db->get("membership_names");
			$memberships = $query->result_array();
			foreach ($memberships as $key => $membership) {
				$memberships[$key]['credits'] = $this->db->get_where("membership_credits",array('membership_id' => $membership['id']))->result_array();
				$this->db->select("membership_users.id,membership_users.user_id,concat(user_profiles.first_name,' ',user_profiles.last_name) as name",FALSE);
				$this->db->join("user_profiles","user_profiles.user_id = membership_users.user_id","left");
				$this->db->where("membership_users.membership_id", $membership['id']);
				$memberships[$key]['users'] = $this->db->get("membership_users")->result_array();
			}
			return $memberships;
		} else {
		    $this->db->select("membership_names.*,concat(user_profiles.first_name,' ',user_profiles.last_name) as owner_name, membership_types.name as membership_type, max(membership_credits.end) as expires", FALSE);
		    $this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
			$this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id","left");
			$this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
			$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
			$this->db->group_by("membership_names.id");
			$this->db->order_by("expires","desc");
			$this->db->where('membership_users.user_id', $user_id);
			$this->db->where('membership_credits.start <','NOW()', FALSE);
			$this->db->where('membership_credits.end >', 'NOW()', FALSE);
			$this->db->where('membership_credits.end is not null');
			$query = $this->db->get("membership_names");
			return $query->result_array();
		}
	}

	function get_all_memberships() {
		$this->db->select("membership_names.*,membership_names.id as membership_id,membership_types.name as type, CONCAT_WS(' ',user_profiles.first_name,user_profiles.last_name ) as owner, max(membership_credits.end) as expires");
		$this->db->from("membership_names");
		$this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
		$this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id");
		$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
		$this->db->group_by("membership_names.id");
		$this->db->where("membership_credits.end is not null");
		$this->db->order_by("owner","ASC");
		$query = $this->db->get();
		return $query->result_array();
	}

    function get_membership($membership_id)
    {
        $this->db->select("membership_names.*,membership_types.name as type,membership_types.price as type_price, CONCAT_WS(' ',user_profiles.first_name,user_profiles.last_name ) as owner_name, membership_types.number as max_users, count(distinct membership_users.user_id) as user_count, membership_types.paypal_button, membership_types.paypal_subscription_button");
        $this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
        $this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id","left");
        $this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
        $this->db->where('membership_names.id',$membership_id);
        $membership = $this->db->get("membership_names")->row_array();
        $membership['credits'] = $this->db->get_where("membership_credits",array('membership_id' => $membership['id']))->result_array();
        $this->db->select("membership_users.id,membership_users.user_id,concat(user_profiles.first_name,' ',user_profiles.last_name) as name",FALSE);
        $this->db->join("user_profiles","user_profiles.user_id = membership_users.user_id","left");
        $this->db->where("membership_users.membership_id", $membership['id']);
        $membership['users'] = $this->db->get("membership_users")->result_array();
        
        return $membership;
    }

    function get_membership_types() {
	$this->db->where("disabled",0);
        $query = $this->db->get("membership_types");
        return $query->result_array();
    }
    
    function get_membership_type($type_id) {
	$this->db->where("disabled",0);
	    $this->db->where("id", $type_id);
        $query = $this->db->get("membership_types");
        return $query->row_array();
    }
    
    function membership_type_list() {
        $query = $this->db->get("membership_types");
		$data = $query->result_array();
		$result = array();
		foreach ($data as $item) {
			$result[$item['id']] = $item['name'];
		}
		return $result;
    }
    
    function get_membership_counts() {
	$active = $this->db->select("count(distinct user_id) as active from current_memberships",FALSE)->get()->row_array()['active'];
	$max = $this->db->select("count(distinct owner_id) as max from membership_names",FALSE)->get()->row_array()['max'];
	$total = $this->db->select("count(distinct user_id) as total from user_profiles",FALSE)->get()->row_array()['total'];
        //$query = $this->db->select("count(distinct c.user_id) as active,count(distinct m.owner_id) as max,count(distinct p.user_id) as total from current_memberships as c, membership_names as m, user_profiles as p", FALSE)->get();
        return array("active"=>$active,"max"=>$max,"total"=>$total);<commit_msg>Added code to update type in model<commit_after><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Memberships_model extends CI_Model {
	
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
		
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// Memberships
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	

 	/**
	 * get_user_memberships
	 * Gets a paginated list of users that can be filtered via the user search form, filtering by the users email and first and last names.
	 */
	function get_user_memberships($user_id, $type='All') {
		if ($type == 'All') {
			$this->db->select("membership_names.*,membership_types.name as type, membership_types.number as max_users, count(distinct membership_users.user_id) as user_count, max(membership_credits.end) as expires, membership_types.paypal_button, membership_types.paypal_subscription_button");
			$this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
			$this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
			$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
			$this->db->group_by("membership_names.id");
			$this->db->order_by("expires","desc");
			$this->db->where('membership_names.owner_id', $user_id);
			$this->db->where('membership_credits.end is not null');
			$query = $this->db->get("membership_names");
			$memberships = $query->result_array();
			foreach ($memberships as $key => $membership) {
				$memberships[$key]['credits'] = $this->db->get_where("membership_credits",array('membership_id' => $membership['id']))->result_array();
				$this->db->select("membership_users.id,membership_users.user_id,concat(user_profiles.first_name,' ',user_profiles.last_name) as name",FALSE);
				$this->db->join("user_profiles","user_profiles.user_id = membership_users.user_id","left");
				$this->db->where("membership_users.membership_id", $membership['id']);
				$memberships[$key]['users'] = $this->db->get("membership_users")->result_array();
			}
			return $memberships;
		} else {
		    $this->db->select("membership_names.*,concat(user_profiles.first_name,' ',user_profiles.last_name) as owner_name, membership_types.name as membership_type, max(membership_credits.end) as expires", FALSE);
		    $this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
			$this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id","left");
			$this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
			$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
			$this->db->group_by("membership_names.id");
			$this->db->order_by("expires","desc");
			$this->db->where('membership_users.user_id', $user_id);
			$this->db->where('membership_credits.start <','NOW()', FALSE);
			$this->db->where('membership_credits.end >', 'NOW()', FALSE);
			$this->db->where('membership_credits.end is not null');
			$query = $this->db->get("membership_names");
			return $query->result_array();
		}
	}

	function get_all_memberships() {
		$this->db->select("membership_names.*,membership_names.id as membership_id,membership_types.name as type, CONCAT_WS(' ',user_profiles.first_name,user_profiles.last_name ) as owner, max(membership_credits.end) as expires");
		$this->db->from("membership_names");
		$this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
		$this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id");
		$this->db->join("membership_credits","membership_credits.membership_id = membership_names.id","left");
		$this->db->group_by("membership_names.id");
		$this->db->where("membership_credits.end is not null");
		$this->db->order_by("owner","ASC");
		$query = $this->db->get();
		return $query->result_array();
	}

    function get_membership($membership_id)
    {
        $this->db->select("membership_names.*,membership_types.name as type,membership_types.price as type_price, CONCAT_WS(' ',user_profiles.first_name,user_profiles.last_name ) as owner_name, membership_types.number as max_users, count(distinct membership_users.user_id) as user_count, membership_types.paypal_button, membership_types.paypal_subscription_button");
        $this->db->join("membership_types","membership_types.id = membership_names.type_id","left");
        $this->db->join("user_profiles","user_profiles.user_id = membership_names.owner_id","left");
        $this->db->join("membership_users","membership_users.membership_id = membership_names.id","left");
        $this->db->where('membership_names.id',$membership_id);
        $membership = $this->db->get("membership_names")->row_array();
        $membership['credits'] = $this->db->get_where("membership_credits",array('membership_id' => $membership['id']))->result_array();
        $this->db->select("membership_users.id,membership_users.user_id,concat(user_profiles.first_name,' ',user_profiles.last_name) as name",FALSE);
        $this->db->join("user_profiles","user_profiles.user_id = membership_users.user_id","left");
        $this->db->where("membership_users.membership_id", $membership['id']);
        $membership['users'] = $this->db->get("membership_users")->result_array();
        
        return $membership;
    }

    function get_membership_types() {
	$this->db->where("disabled",0);
        $query = $this->db->get("membership_types");
        return $query->result_array();
    }
    
    function get_membership_type($type_id) {
	$this->db->where("disabled",0);
	    $this->db->where("id", $type_id);
        $query = $this->db->get("membership_types");
        return $query->row_array();
    }
    
    function membership_type_list() {
        $query = $this->db->get("membership_types");
		$data = $query->result_array();
		$result = array();
		foreach ($data as $item) {
			$result[$item['id']] = $item['name'];
		}
		return $result;
    }
    
    function update_type($type_id,$type_data) {
        $this->db->where("id",$type_id);
        $this->db->update("membership_types",$type_data);
    }
    
    function get_membership_counts() {
	$active = $this->db->select("count(distinct user_id) as active from current_memberships",FALSE)->get()->row_array()['active'];
	$max = $this->db->select("count(distinct owner_id) as max from membership_names",FALSE)->get()->row_array()['max'];
	$total = $this->db->select("count(distinct user_id) as total from user_profiles",FALSE)->get()->row_array()['total'];
        //$query = $this->db->select("count(distinct c.user_id) as active,count(distinct m.owner_id) as max,count(distinct p.user_id) as total from current_memberships as c, membership_names as m, user_profiles as p", FALSE)->get();
        return array("active"=>$active,"max"=>$max,"total"=>$total);
{'fraction_non_alphanumeric': 0.2053192230035508, 'fraction_numerical': 0.00027849335097124555, 'mean_word_length': 5.8465204957102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 285, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '398617', 'n_tokens_mistral': 4899, 'n_tokens_neox': 4594, 'n_words': 647}