Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('BASEPATH') OR exit('No direct script access allowed');
- class User_model extends CI_Model
- {
- function set_parent_permission($parent_ids,$class_id)
- {
- // $this->db->select('users.id as id',FALSE);
- // $this->db->select('users.type as type',FALSE);
- // $this->db->from('users');
- // $this->db->where_in('users.id', array_keys($parent_ids));
- // $users = $this->db->get()->result_array();
- // $update = array();
- // foreach($users as $u => $user)
- // {
- // $type = $user['type'];
- // $active = $parent_ids[$user['id']];
- // $new_type = "";
- // if($active == "1")
- // {
- // if($type == "3")
- // {
- // $new_type = "5";
- // }
- // else if($type == "4")
- // {
- // $new_type = "6";
- // }
- // else if($type == "1")
- // {
- // $new_type = "7";
- // }
- // }
- // else if($active == "0")
- // {
- // if($type == "5")
- // {
- // $new_type = "3";
- // }
- // else if($type == "6")
- // {
- // $new_type = "4";
- // }
- // else if($type == "7")
- // {
- // $new_type = "1";
- // }
- // }
- // $update[] = array("id"=>$user['id'],"type"=>$new_type);
- // }
- $new_pta = array();
- foreach($parent_ids as $u => $user)
- {
- $data = array();
- $type = $parent_ids[$u];
- $data['user_id'] = $u;
- $data['class_id'] = $class_id;
- $data['vaad_type'] = $type;
- $data['year'] = CURRENT_YEAR;
- if($type == '0')
- {
- $data['active'] = '0';
- }
- else
- {
- $data['active'] = '1';
- }
- if($data['active'] == '0')
- {
- $res = $this->db->update('classes_vaad',$data,array('user_id'=>$data['user_id'],'class_id'=>$data['class_id']));
- if(!$res)
- {
- //return false;
- }
- }
- else
- {
- $query = $this->db->get_where('classes_vaad',array('user_id'=>$data['user_id'],'class_id'=>$data['class_id'], 'active'=>'0'));
- $count = $query->num_rows(); //counting result from query
- if ($count === 0)
- {
- $query = $this->db->get_where('classes_vaad',array('user_id'=>$data['user_id'],'class_id'=>$data['class_id'], 'active'=>'1'));
- $count = $query->num_rows(); //counting result from query
- if ($count === 0)
- {
- $res = $this->db->insert('classes_vaad', $data);
- $new_pta[$data['user_id']] = '1';
- }
- else
- {
- //return false;
- }
- }
- else
- {
- $res = $this->db->update('classes_vaad',$data,array('user_id'=>$data['user_id'],'class_id'=>$data['class_id']));
- $new_pta[$data['user_id']] = '1';
- }
- if(!$res)
- {
- //return false;
- }
- }
- }
- return $new_pta;
- }
- function updatepassword($id,$old_password,$new_password,$api_key)
- {
- $this->db->select('password',FALSE);
- $this->db->from('users');
- $this->db->where('users.id', $id);
- $password = $this->db->get()->row()->password;
- if(sha1($old_password) !== $password)
- {
- return false;
- }
- else
- {
- $res = $this->check_password($new_password);
- if(is_string($res))
- {
- return $res;
- }
- else
- {
- $this->db->update('users',array('password'=>sha1($new_password),'api_key'=>$api_key),array('id'=>$id));
- if($this->db->affected_rows() > 0)
- {
- return true;
- }
- else
- {
- return '-1';
- }
- }
- }
- }
- function send_pta_push($parent_ids,$class_id,$app_name)
- {
- $tokens = $this->token_model->get_teachers_tokens($parent_ids);
- $gan_class_name = $this->class_model->get_class_gan_name($class_id);
- $class_name = $gan_class_name['class_name'];
- $loc_args = array($class_name);
- $acme = array("cls_id" => $class_id);
- if($app_name == GB || $app_name == null)
- {
- $text = PUSH_PTA_HE;
- }
- else
- {
- $text = PUSH_PTA_EN;
- }
- $text = str_replace('CLASS_NAME',$class_name,$text);
- $users_ids = array();
- foreach ($parent_ids as $p=>$p_id)
- {
- $users_ids[(string)$p_id] = array("cls_id" => $class_id);
- }
- // file_put_contents("/opt/app/current/testFile.txt","daa". print_r($users_ids, true));
- $this->message_model->send_push($tokens,$users_ids,$text,$class_id,PTA1_KEY,$loc_args,$acme,null,$app_name);
- }
- function compare_old_password($mail,$old_pass)
- {
- $this->db->select('password',FALSE);
- $this->db->from('users');
- $this->db->where('users.mail', $mail);
- $password = $this->db->get()->row()->password;
- if(sha1($old_pass) !== $password)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- function update_fail_attempts()
- {
- $ip = $this->get_ip_address();
- $query = $this->db->get_where('users_login_attempts',array('ip'=>$ip));
- $count = $query->num_rows(); //counting result from query
- if ($count === 0) // not exist
- {
- $res = $this->db->insert('users_login_attempts', array('ip'=>$ip,'attempts'=>1,'time'=>date("Y-m-d H:i:s")));
- return true;
- }
- else
- {
- $attempts = $query->row()->attempts;
- $attempts ++;
- $data['attempts'] = $attempts;
- if($attempts >= BLOCK_ATTEMPTS)
- {
- $data['time'] = date("Y-m-d H:i:s");
- }
- $res = $this->db->update('users_login_attempts',$data,array('ip'=>$ip));
- if($attempts >= BLOCK_ATTEMPTS)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- function check_block_time()
- {
- $ip = $this->get_ip_address();
- $query = $this->db->get_where('users_login_attempts',array('ip'=>$ip));
- if ($count === 0) // not exist
- {
- return true;
- }
- else
- {
- $attempts = $query->row()->attempts;
- if($attempts >= BLOCK_ATTEMPTS)
- {
- // if block time didnt pass --> error message
- $attempt_time = $query->row()->time;
- $now = date("Y-m-d H:i:s");
- $attempt_time_time = strtotime($attempt_time);
- $now_time = strtotime($now);
- $diff = $now_time - $attempt_time_time;
- if(intval($diff) < intval(BLOCK_TIME))
- {
- return false;
- }
- else
- {
- //else delete from table and return true
- $this->db->delete('users_login_attempts',array('ip'=>$ip));
- return true;
- }
- }
- else
- {
- return true;
- }
- }
- }
- function get_kids_2014_no_2015()
- {
- $this->db->select('distinct kids.id',FALSE);
- $this->db->select('users.id as user_id',FALSE);
- $this->db->select('users.first_name as user_name',FALSE);
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('users.mail',FALSE);
- $this->db->select('users.mail_key as mail_key',FALSE);
- $this->db->select('users.first_name',FALSE);
- $this->db->select('kids_classes_2014.year as class_year_2014',FALSE);
- $this->db->select('tokens.type',FALSE);
- // $this->db->select('kids_classes_2015.year as class_year_2015',FALSE);
- $this->db->from('kids');
- $this->db->join('kids_classes as kids_classes_2014', 'kids.id = kids_classes_2014.kid_id and kids_classes_2014.active = 1 and kids_classes_2014.year = 2014');
- $this->db->join('classes', 'kids_classes_2014.class_id = classes.id');
- $this->db->join('gans', 'classes.gan_id = gans.id');
- $this->db->join('users', 'users.id = kids.parent_id');
- $this->db->join('tokens', 'tokens.user_id = users.id');
- // $this->db->join('kids_classes as kids_classes_2015', 'kids.id = kids_classes_2015.kid_id and kids_classes_2015.year = 2015','left');
- // $this->db->where('kids_classes_2014.class_id', '1839');
- $this->db->where('tokens.type', 'android');
- // $this->db->where('kids_classes_2015.class_id', '1839');
- // $this->db->where('users.id', '3977');
- $kids = $this->db->get()->result_array();
- $kids_new = array();
- foreach($kids as $kid)
- {
- $query = $this->db->get_where('kids_classes',array('kid_id' => $kid['id'],'year' => '2015'));
- if($query->num_rows() == 0) // not in 2015 - it's good
- {
- $kids_new[] = $kid;
- }
- }
- $kids_new_mail = array();
- foreach($kids_new as $kid_new)
- {
- $kids_new_mail[$kid_new['mail']][] = $kid_new;
- }
- $arr = array();
- foreach($kids_new_mail as $kid_new_mail)
- {
- $new_arr = array();
- for ($i=0; $i < count($kid_new_mail); $i++)
- {
- $new_arr['email'] = $kid_new_mail[$i]['mail'];
- $new_arr['code'] = $kid_new_mail[$i]['mail_key'];
- $new_arr['name'] = $kid_new_mail[$i]['user_name'];
- $new_arr['gan_name'.$i] = $kid_new_mail[$i]['gan_name'];
- $new_arr['class_name'.$i] = $kid_new_mail[$i]['class_name'];
- $new_arr['kid_name'.$i] = $kid_new_mail[$i]['kid_name'];
- }
- if(count($kid_new_mail) < 3)
- {
- for ($i=count($kid_new_mail); $i < 3; $i++)
- {
- $new_arr['gan_name'.$i] = ' ';
- $new_arr['class_name'.$i] = ' ';
- $new_arr['kid_name'.$i] = ' ';
- }
- }
- $arr[] = $new_arr;
- }
- // foreach($kids_new as $kid_new)
- // {
- // $this->db->where('id', $kid_new['user_id']);
- // $mail_key = md5(uniqid(rand(), true));
- // $this->db->update('users', array("mail_key" => $mail_key));
- // }
- $count = count($kids_new);
- // return array('count'=>$count,'kids'=>$kids_new);
- return $arr;
- }
- function update_last_seen($id)
- {
- $date_str = date("Y-m-d H:i:s");
- $this->db->select('last_seen',FALSE);
- $this->db->select('type',FALSE);
- $query = $this->db->get_where('users',array('id' => $id));
- $type = $query->row()->type;
- if($type == "2" && $id == "12022")
- {
- $last_seen = $query->row()->last_seen;
- $last_seen_date = strtotime($last_seen);
- $date = strtotime($date_str);
- // $diff = $last_seen_date->diff($date);
- if((intval($date) - intval($last_seen_date)) >= 600) // more then 10 sec
- {
- return false;
- }
- else
- {
- $this->update_last_seen_no_check($id,$date_str);
- return true;
- }
- }
- else
- {
- $this->update_last_seen_no_check($id,$date_str);
- return true;
- }
- }
- function update_last_seen_no_check($id,$date_str)
- {
- $this->db->where('id', $id);
- $this->db->update('users', array("last_seen" => $date_str));
- }
- function register_retention($mail,$token_id)
- {
- $query = $this->db->get_where('retention',array('mail' => $mail,'token_id' => $token_id));
- if($query->num_rows() == 0)
- {
- $date = date("Y-m-d H:i:s");
- $this->db->insert('retention', array('mail' => $mail,'token_id' => $token_id,"active" => 1,'created' => $date));
- return $this->db->insert_id();
- }
- else
- {
- return false;
- }
- }
- function delete_retention($mail)
- {
- $this->db->where('mail', $mail);
- $this->db->update('retention', array("active" => 0));
- return $this->db->affected_rows() > 0;
- }
- function encrypt_password()
- {
- $this->db->from('users');
- $query = $this->db->get();
- foreach ($query->result() as $row)
- {
- $id = $row->id;
- $password = $row->password;
- $new_password = sha1($password);
- $this->db->where('id', $id);
- $data = array(
- 'password' => $new_password
- );
- $this->db->update('users', $data);
- }
- }
- function insert($data)
- {
- $this->db->insert('users', $data);
- }
- function forgetpass($mail,$os=null,$key)
- {
- $this->db->select('password',FALSE);
- $query = $this->db->get_where('users',array('mail' => $mail));
- if($query->num_rows() > 0)
- {
- $res = $query->result_array();
- $pass = $query->row()->password;
- $this->db->where('mail', $mail);
- $data = array(
- 'password_key' => $key
- );
- $this->db->update('users', $data);
- return $pass;
- }
- else
- {
- if($os != null)
- {
- $pass = "emptypassword";
- return $pass;
- }
- else
- {
- return false;
- }
- }
- }
- function login($mail,$password)
- {
- $this->db->select('id',FALSE);
- $this->db->select('first_name',FALSE);
- $this->db->select('last_name',FALSE);
- $this->db->select('mobile',FALSE);
- $this->db->select('phone',FALSE);
- $this->db->select('address',FALSE);
- $this->db->select('cityName',FALSE);
- $this->db->select('type',FALSE);
- $this->db->select('active',FALSE);
- $this->db->select('activation',FALSE);
- $query = $this->db->get_where('users',array('mail' => $mail, 'password' => sha1($password)));
- if($query->num_rows() > 0)
- {
- $res = $query->result_array();
- $type = $query->row()->type;
- if($type == '1')
- {
- $res[0]['active'] = '0';
- $id = $query->row()->id;
- $this->db->select('COUNT(distinct kids_classes.kid_id) AS count',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id ');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id ');
- $this->db->where('users.id', $id);
- $this->db->where('kids_classes.active', 1);
- $query2 = $this->db->get();
- $count = $query2->row()->count;
- if($count > 0)
- {
- $res[0]['active'] = '1';
- }
- return $res;
- }
- else
- {
- return $res;
- }
- }
- else
- {
- return '';
- }
- }
- function post_login($query,$token_id,$gmt,$app_name,$data,$os_name,$app_version,$object_id)
- {
- $user = false;
- if($query)
- {
- $user = $this->login_new( $query );
- //error_log("*** POST LOGIN2 user " . print_r($user, TRUE));
- if($user)
- {
- //create token
- // $token_id = $this->post('token_id');
- // $gmt = $this->post('gmt');
- // $app_name = $this->post('app_name');
- $user_id_key = isset($user["parent_id"]) ? "parent_id" : "teacher_id";
- $user_id = isset($user["parent_id"]) ? $user["parent_id"] : $user["teacher_id"];
- $user_data = array("app_name" => $app_name);
- $this->updateuser($user_id,$user_data);
- // $data = array(
- // 'device_vendor' => $this->post('device_vendor'),
- // 'device_model' => $this->post('device_model'),
- // 'os_name' => $this->post('os_name'),
- // 'os_version' => $this->post('os_version'),
- // 'scale' => $this->post('scale'),
- // 'app_version' => $this->post('app_version'),
- // 'user_id' => $user_id,
- // 'active' => '1',
- // 'gmt' => $this->post('gmt')
- // );
- // $this->token_model->update_token($token_id,array("user_id" => $user_id, "active" => '1', "gmt" => $gmt));
- if($object_id != "")
- {
- $this->token_model->update_parse_token_login((string)$object_id,(string)$user_id,(string)$token_id);
- }
- $data['user_id'] = $user_id;
- if($token_id == '')
- {
- $token_id = $this->token_model->create_token($data);
- $user['token_id'] = $token_id;
- }
- else
- {
- $update_res = $this->token_model->update_token($token_id,$data);
- if(!$update_res)
- {
- file_put_contents('/opt/app/current/log_update_token.txt', print_r(array("token_id"=>$token_id,"data"=>$data), true), FILE_APPEND);
- }
- }
- $user[$user_id_key] = $this->util_model->get_encrypt_user_id($user_id);
- $user['update'] = (string)$this->version_model->get_update($os_name,$app_version);
- $user['user_id'] = $user_id;
- // 200 being the HTTP response code
- }
- else
- {
- $user = false;
- // $this->response(array("errors"=>array("code"=>"9000","msg"=>"Wrong User Name or Password")), 400);
- }
- }
- else
- {
- $user = false;
- // $this->response(array("errors"=>array("code"=>"9000","msg"=>"Wrong User Name or Password")), 400);
- }
- return $user;
- }
- function user_exists($mail,$password,$migrate=false)
- {
- $double_check = 0;
- $password_to_check = sha1($password);
- while ($double_check < 2)
- {
- $query = $this->db->get_where('users',array('mail' => $mail, 'password' => $password_to_check ));
- if($query->num_rows() > 0)
- {
- return $query;
- }
- else
- {
- if(!$migrate)
- {
- return false;
- }
- else
- {
- $password_to_check = $password;
- $double_check++;
- }
- }
- }
- return false;
- }
- function user_exists_migrate_android($user_id)
- {
- $query = $this->db->get_where('users',array('id' => $user_id ));
- if($query->num_rows() > 0)
- {
- return $query;
- }
- else
- {
- return false;
- }
- }
- function login_new($query)
- {
- // $query = $this->db->get_where('users',array('mail' => $mail, 'password' => sha1($password) ));
- // $query = $this->user_exists($mail,$password);
- // if($query)
- // {
- // $res = $query->result_array();
- // $activation = $query->row()->activation;
- // if($activation != null)
- // {
- // return -1;
- // }
- $api_key = $query->row()->api_key;
- $id = $query->row()->id;
- if($api_key == null)
- {
- $api_key = md5(uniqid(rand(), true));
- $this->updateuser($id,array('api_key' => $api_key));
- }
- $type = $query->row()->type;
- if($type == '2')
- {
- $idStr = " as teacher_id";
- $fnStr = " as teacher_first_name";
- $lnStr = " as teacher_last_name";
- $mobileStr = " as teacher_mobile";
- $phoneStr = " as teacher_phone";
- $addressStr = " as teacher_address";
- $cityStr = " as teacher_city";
- $mailStr = " as teacher_mail";
- }
- else
- {
- $idStr = " as parent_id";
- $fnStr = " as parent_first_name";
- $lnStr = " as parent_last_name";
- $mobileStr = " as parent_mobile";
- $phoneStr = " as parent_phone";
- $addressStr = " as parent_address";
- $cityStr = " as parent_city";
- $mailStr = " as parent_mail";
- }
- $this->db->select('id'.$idStr,FALSE);
- $this->db->select('mail'.$mailStr,FALSE);
- $this->db->select('first_name'.$fnStr,FALSE);
- $this->db->select('last_name'.$lnStr,FALSE);
- // $this->db->select('area_code',FALSE);
- $this->db->select('mobile_phone'.$mobileStr,FALSE);
- $this->db->select('home_phone'.$phoneStr,FALSE);
- $this->db->select('address'.$addressStr,FALSE);
- $this->db->select('cityName'.$cityStr,FALSE);
- $this->db->select('city',FALSE);
- $this->db->select('type',FALSE);
- $this->db->select('api_key',FALSE);
- $this->db->from('users');
- // $this->db->where(array('mail' => $mail, 'password' => sha1($password)));
- // $this->db->where('mail', $mail);
- // $this->db->where('password', sha1($password));
- $this->db->where('id', $id);
- $query = $this->db->get();
- $user = reset($query->result_array());
- // if($user['type'] == 2)
- // {
- $user['current_year'] = CURRENT_YEAR;
- // }
- // $user_ac = $user['area_code'];
- // if($user_ac > 0)
- // {
- // if($user['type'] == 2)
- // {
- // $phone_index = 'teacher_phone';
- // $mobile_index = 'teacher_mobile';
- // }
- // else
- // {
- // $phone_index = 'parent_phone';
- // $mobile_index = 'parent_mobile';
- // }
- // // if($user['type'] == 2)
- // // {
- // // $phone = $user['teacher_phone'];
- // // $mobile = $user['teacher_mobile'];
- // // }
- // // else
- // // {
- // // $phone = $user['parent_phone'];
- // // $mobile = $user['parent_mobile'];
- // // }
- // $phone = $user[$phone_index];
- // $mobile = $user[$mobile_index];
- // $res = $this->set_mobile_phone($user_ac,$mobile,$phone);
- // $user[$mobile_index] = $res['mobile'];
- // $user[$phone_index] = $res['phone'];
- // }
- // unset($user['area_code']);
- //city from old users
- $city_id = $user['city'];
- $cityName = $user['parent_city'];
- if($user['type'] == 2)
- {
- $cityName = $user['teacher_city'];
- }
- if($city_id > 0 && ($cityName == null || $cityName == "0"))
- {
- if($user['type'] == 2)
- {
- $user['teacher_city'] = $this->city_model->get_name($city_id);
- }
- else
- {
- $user['parent_city'] = $this->city_model->get_name($city_id);
- }
- }
- unset($user['city']);
- return $user;
- // }
- // else
- // {
- // return false;
- // }
- }
- function fix_users_mobile_phone()
- {
- $this->db->from('users');
- $this->db->where('users.id >=','10933');
- $users = $this->db->get()->result_array();
- foreach($users as $u=>$user)
- {
- $arr = $this->set_mobile_phone($user['area_code'],$user['mobile'],$user['phone']);
- // $users[$u]['mobile'] = $arr['mobile'];
- // $users[$u]['phone'] = $arr['phone'];
- $this->db->where('id', $user['id']);
- $this->db->update('users', $arr);
- }
- // $this->db->update_batch('users',$users,'id');
- }
- function fix_users_mobile_gan()
- {
- $this->db->from('gans');
- $this->db->where('id >=','10933');
- $gans = $this->db->get()->result_array();
- foreach($gans as $g=>$gan)
- {
- $arr = $this->set_mobile_phone($gan['area_code'],$gan['mobile'],$gan['phone'],false);
- // $users[$u]['mobile'] = $arr['mobile'];
- // $users[$u]['phone'] = $arr['phone'];
- $this->db->where('id', $gan['id']);
- $this->db->update('gans', $arr);
- }
- // $this->db->update_batch('users',$users,'id');
- }
- function set_mobile_phone($area_code,$mobile,$phone_num,$users=true)
- {
- $this->db->from('phones');
- $phonesarr = $this->db->get()->result_array();
- //if phone starts with 050,054...
- $phone_start_with_ac = null;
- foreach ($phonesarr as $phone)
- {
- if($this->startsWith($phone_num,$phone['name']))
- {
- $phone_start_with_ac = $phone['id'];
- }
- }
- if($phone_start_with_ac >= 12)
- {
- $mobile = $phone_num;
- $phone_num = null;
- }
- // else if ($phone_start_with_ac < 12)
- // {
- // }
- else if($phone_start_with_ac == null)
- {
- $phones = array();
- foreach ($phonesarr as $phone)
- {
- $phones[$phone['id']] = $phone['name'];
- }
- $ac_name = $phones[$area_code];
- if($area_code >= 12)
- {
- if(!$this->startsWith($mobile,"0"))
- {
- $mobile = $ac_name.$phone_num;
- $phone_num = null;
- }
- }
- else
- {
- if(!$this->startsWith($phone_num,"0"))
- {
- $phone_num = $ac_name.$phone_num;
- }
- else
- {
- $phone_ac = substr($phone_num, 0, 3);
- $phone_ac_id = $this->phone_model->get_id($phone_ac);
- if($phone_ac_id >=12) //mobile
- {
- $mobile = $phone_num;
- $phone_num = null;
- }
- }
- }
- }
- if($users)
- {
- return array("mobile_phone"=>$mobile,"home_phone"=>$phone_num);
- }
- else
- {
- $number = ($phone_num == null) ? $mobile : $phone_num;
- return array("gan_phone"=>$number);
- }
- }
- function getKids($id)
- {
- $this->db->select('gans.id as gan_id',FALSE);
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('gans.pic as pic',FALSE);
- $this->db->select('gans.max as gan_max',FALSE);
- $this->db->select('gans.code as gan_code',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('classes.id as class_id',FALSE);
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('kids.birth_date as birth_date',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->select('kids.pic as kid_pic',FALSE);
- $this->db->select('kids_classes.active as kid_active',FALSE);
- $this->db->select('users.first_name as first_name',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
- $this->db->join('classes', 'kids_classes.class_id = classes.id');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id');
- $this->db->where('users.id', $id);
- // $this->db->group_by('kids.id');
- $query = $this->db->get();
- return $query->result_array();
- }
- function get_user_kids_new($parent_id){
- $q='SELECT B.kid_name,
- B.kid_bd ,
- B.kid_id ,
- B.kid_pic ,
- B.kid_gender ,
- B.gan_id ,
- B.gan_name ,
- B.gan_max ,
- B.gan_address ,
- B.gan_phone ,
- B.gan_city ,
- B.gan_code ,
- B.class_name ,
- B.class_id ,
- B.teacher_id ,
- B.teacher_mail ,
- B.teacher_first_name ,
- B.teacher_last_name ,
- B.teacher_mobile ,
- B.teacher_phone ,
- B.kid_active ,
- B.class_year ,
- C.sent_messages-C.read_messages as unread_messaages ,
- B.vaad_type
- FROM
- (SELECT A.*,`gans`.`name` as gan_name,
- gans.max as gan_max,
- gans.address as gan_address,
- gans.gan_phone,
- gans.cityName as gan_city,
- gans.code as gan_code,
- classes.name as class_name,
- users.id as teacher_id,
- users.mail as teacher_mail,
- users.first_name as teacher_first_name,
- users.last_name as teacher_last_name,
- users.mobile_phone as teacher_mobile,
- users.home_phone as teacher_phone,
- classes_vaad.vaad_type
- FROM (
- SELECT `kids`.`name` as kid_name,
- kids.birth_date as kid_bd,
- `kids`.`id` as kid_id,
- `kids`.`pic` as kid_pic,
- `kids`.`parent_id`,
- `kids`.`active`,
- `kids`.`gender` as kid_gender,
- `kids_classes`.`gan_id`,
- `kids_classes`.`class_id` ,
- `kids_classes`.`year` ,
- kids_classes.active as kid_active,
- kids_classes.year as class_year
- FROM `kids`
- LEFT JOIN `kids_classes`
- ON `kids`.`id`=`kids_classes`.`kid_id`
- WHERE `kids`.`parent_id`= '.$parent_id.'
- ) A
- LEFT JOIN `gans` ON `gans`.`id`=A.gan_id
- LEFT JOIN `classes` ON classes.id= A.class_id
- LEFT JOIN users ON users.gan_id=A.gan_id
- LEFT JOIN `classes_vaad` ON classes_vaad.user_id=A.parent_id AND A.class_id=classes_vaad.class_id
- ) B
- LEFT JOIN
- (SELECT messages.class_id,
- count(messages.id) as sent_messages,
- count(read_messages.message_id) as read_messages
- FROM messages
- LEFT JOIN read_messages ON messages.id = read_messages.message_id
- AND read_messages.user_id='.$parent_id.'
- WHERE messages.year='.CURRENT_YEAR.'
- AND messages.active=1
- GROUP BY messages.class_id
- ) C
- ON B.class_id = C.class_id
- ORDER BY B.class_year DESC, B.kid_active ASC
- ';
- $re = $this->db->query($q);
- $kids = $re->result_array();
- $q1 = 'SELECT album_seen_pictures.user_id,albums.class_id, count(pictures.id) as sent_pics,`album_seen_pictures`.seen_pics FROM albums
- LEFT JOIN pictures ON albums.id=pictures.album_id AND albums.active=1 AND pictures.tmb=1 AND pictures.full=1
- LEFT JOIN `album_seen_pictures` ON `albums`.`id` = `album_seen_pictures`.`album_id`
- WHERE albums.year='.CURRENT_YEAR.' AND album_seen_pictures.user_id ='.$parent_id.'
- GROUP BY class_id';
- $re1 = $this->db->query($q1);
- $media = $re1->result_array();
- if(!empty($media)){
- foreach($kids as $k=>$kid){
- foreach($media as $m=>$me){
- if($parent_id == $me['user_id'] AND $kid['class_id'] == $me['class_id'] ){
- $kids[$k]['unseen_photos'] = abs((int)$me['sent_pics'] - (int)$me['seen_pics']);
- break;
- }
- }
- }
- }
- $new_kids_current_year = array();
- $new_kids_last_year = array();
- foreach ($kids as $k => $kid)
- {
- $kid['type'] = "1";
- if($kid['class_year'] == CURRENT_YEAR)
- {
- $new_kids_current_year[] = $kid;
- }
- else
- {
- $new_kids_last_year[] = $kid;
- }
- }
- foreach ($new_kids_current_year as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- $v = $kid;
- unset($new_kids_current_year[$k]);
- $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
- $new_kids_current_year[$last_key+1] = $v;
- }
- }
- $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
- $new_kids = array();
- foreach ($new_kids_1 as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $class_id = $kid['class_id'];
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- if(
- isset($new_kids[$kid['kid_id']]['class_year']) &&
- ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
- isset($new_kids[$kid['kid_id']]['kid_active']) &&
- ($new_kids[$kid['kid_id']]['kid_active']) != '0'
- )
- {
- }
- else
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
- {
- if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- if($kid['kid_active'] != '0')
- {
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $history = $this->set_kid_array_values($kid,true);
- $new_kids[$kid['kid_id']]['history'][] = $history;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
- {
- $kid = $this->set_kid_array_values($kid);
- $new_kids[$kid['kid_id']] = $kid;
- }
- }
- return array_values($new_kids);
- }
- function get_user_kids($parent_id)
- {
- $this->db->select('users.type as type',FALSE);
- $this->db->from('users');
- $this->db->where('users.id', $parent_id);
- $query = $this->db->get();
- $type = $query->row()->type;
- // $this->db->from('phones');
- // $phonesarr = $this->db->get()->result_array();
- // $phones = array();
- // foreach ($phonesarr as $phone)
- // {
- // $phones[$phone['id']] = $phone['name'];
- // }
- // $this->db->select('kids.name as kid_name',FALSE);
- // $this->db->select('kids.birth_date as kid_bd',FALSE);
- // $this->db->select('kids.id as kid_id',FALSE);
- // $this->db->select('kids.pic as kid_pic',FALSE);
- // // $this->db->select('kids.active as kid_active',FALSE);
- // $this->db->select('kids.gender as kid_gender',FALSE);
- // // $this->db->select('kids.class_id as class_id',FALSE);
- // $this->db->from('kids');
- // $this->db->where('parent_id', $parent_id);
- // $query = $this->db->get();
- // $kids = $query->result_array();
- // foreach ($kids as $k => $kid)
- // {
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('kids.birth_date as kid_bd',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->select('kids.pic as kid_pic',FALSE);
- // $this->db->select('kids.active as kid_active',FALSE);
- $this->db->select('kids.gender as kid_gender',FALSE);
- $this->db->select('gans.id as gan_id',FALSE);
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('gans.max as gan_max',FALSE);
- $this->db->select('gans.address as gan_address',FALSE);
- // $this->db->select('phones.name as gan_ac',FALSE);
- // $this->db->select('gans.area_code as gan_ac',FALSE);
- $this->db->select('gans.gan_phone as gan_phone',FALSE);
- $this->db->select('gans.cityName as gan_city',FALSE);
- $this->db->select('gans.city as gan_city_id',FALSE);
- $this->db->select('gans.code as gan_code',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('classes.id as class_id',FALSE);
- $this->db->select('users.id as teacher_id',FALSE);
- $this->db->select('users.mail as teacher_mail',FALSE);
- $this->db->select('users.first_name as teacher_first_name',FALSE);
- $this->db->select('users.last_name as teacher_last_name',FALSE);
- $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
- $this->db->select('users.home_phone as teacher_phone',FALSE);
- // $this->db->select('users.area_code as teacher_ac',FALSE);
- $this->db->select('kids_classes.active as kid_active',FALSE);
- $this->db->select('kids_classes.year as class_year',FALSE);
- $this->db->order_by("kids_classes.year", "desc");
- $this->db->order_by("kids_classes.active", "asc");
- $this->db->from('kids');
- // $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id','left');
- $this->db->join('classes', 'kids_classes.class_id = classes.id','left');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id','left');
- $this->db->join('users', 'users.gan_id = gans.id','left');
- // $this->db->join('phones', 'gans.area_code = phones.id','left');
- // $this->db->where('kids_classes.kid_id', $kid['kid_id']);
- // $this->db->where('kids_classes.year', '2015');
- $this->db->where('kids.parent_id', $parent_id);
- $query = $this->db->get();
- $kids = $query->result_array();
- $new_kids_current_year = array();
- $new_kids_last_year = array();
- foreach ($kids as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR)
- {
- $new_kids_current_year[] = $kid;
- }
- else
- {
- $new_kids_last_year[] = $kid;
- }
- }
- foreach ($new_kids_current_year as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- $v = $kid;
- unset($new_kids_current_year[$k]);
- $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
- $new_kids_current_year[$last_key+1] = $v;
- }
- }
- $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
- $new_kids = array();
- foreach ($new_kids_1 as $k => $kid)
- {
- // if(isset($kid['gan_ac']))
- // {
- // $kids[$k]['gan_phone'] = $phones[$kid['gan_ac']].$kid['gan_phone'];
- // unset($kids[$k]['gan_ac']);
- // }
- // if($kid['teacher_ac'] > 0)
- // {
- // $res = $this->set_mobile_phone($kid['teacher_ac'],$kid['teacher_mobile'],$kid['teacher_phone']);
- // $kids[$k]['teacher_mobile'] = $res['mobile'];
- // $kids[$k]['teacher_phone'] = $res['phone'];
- // }
- // unset($kids[$k]['teacher_ac']);
- //gan city - old users
- //city from old users
- $gan_city_id = $kid['gan_city_id'];
- $gan_city = $kid['gan_city'];
- if($gan_city_id > 0 && ($gan_city == null || $gan_city == "0"))
- {
- $new_kids_1[$k]['gan_city'] = $this->city_model->get_name($gan_city_id);
- }
- unset($kid['gan_city_id']);
- $kid['type'] = $type;
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $class_id = $kid['class_id'];
- // $arr = $this->message_model->get_messages($class_id,CURRENT_YEAR,true);
- // $messages = $arr['messages'];
- $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
- $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
- // if(count($messages)>0)
- // {
- // $unread_messages = $this->message_model->get_count_unread_messgae($parent_id,$messages);
- // }
- // $photos = $this->album_model->get_class_total_pic($class_id);
- // if($photos > 0)
- // {
- // $unseen_photos = $this->album_model->get_count_unseen_pics_for_class($parent_id,$class_id,$photos);
- // }
- // if($unread_messages > 0)
- // {
- $kid['unread_messages'] = (string)$unread_messages;
- // }
- // if($unseen_photos > 0)
- // {
- $kid['unseen_photos'] = (string)$unseen_photos;
- // }
- $kid['vaad_type'] = $this->get_parent_type($parent_id,$class_id,$type);
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- if(
- isset($new_kids[$kid['kid_id']]['class_year']) &&
- ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
- isset($new_kids[$kid['kid_id']]['kid_active']) &&
- ($new_kids[$kid['kid_id']]['kid_active']) != '0'
- )
- {
- }
- else
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
- {
- if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- if($kid['kid_active'] != '0')
- {
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $history = $this->set_kid_array_values($kid,true);
- $new_kids[$kid['kid_id']]['history'][] = $history;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
- {
- $kid = $this->set_kid_array_values($kid);
- $new_kids[$kid['kid_id']] = $kid;
- }
- }
- return array_values($new_kids);
- // $kids[$k]['gan'] = $gan;
- // $gans = reset($gans);
- // $current_gan = null;
- // $history = null;
- // foreach ($kids as $k=>$kid)
- // {
- // $kids[$k]['current_year'] = CURRENT_YEAR;
- // if($kid['class_year'] == CURRENT_YEAR)
- // {
- // $current_gan = $kid;
- // }
- // else
- // {
- // if($kid['kid_active'] == "1")
- // {
- // // $history[] = $gan;//array("class_id" =>$gan['class_id'],"class_year" =>$gan['class_year']);
- // $kids[$k]['history'] = $kids;
- // }
- // }
- // }
- //get unread messages for the cuurent gan
- // if($current_gan['kid_active'] == "1")
- // {
- // $class_id = $current_gan['class_id'];
- // // $arr = $this->message_model->get_messages($class_id,CURRENT_YEAR,true);
- // // $messages = $arr['messages'];
- // $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
- // $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
- // // if(count($messages)>0)
- // // {
- // // $unread_messages = $this->message_model->get_count_unread_messgae($parent_id,$messages);
- // // }
- // // $photos = $this->album_model->get_class_total_pic($class_id);
- // // if($photos > 0)
- // // {
- // // $unseen_photos = $this->album_model->get_count_unseen_pics_for_class($parent_id,$class_id,$photos);
- // // }
- // // if($unread_messages > 0)
- // // {
- // $current_gan['unread_messages'] = (string)$unread_messages;
- // // }
- // // if($unseen_photos > 0)
- // // {
- // $current_gan['unseen_photos'] = (string)$unseen_photos;
- // // }
- // }
- // $kids[$k] = array_merge((array)$kid, (array)$current_gan);
- // $kids[$k]['current_year'] = CURRENT_YEAR;
- // $kids[$k]['history'] = $history;
- // }
- // return $new_kids;
- }
- function get_parent_type($parent_id,$class_id,$type)
- {
- /*vaad*/
- $this->db->select('classes_vaad.vaad_type as vaad_type',FALSE);
- $this->db->from('classes_vaad');
- $this->db->where('classes_vaad.user_id', $parent_id);
- $this->db->where('classes_vaad.class_id', $class_id);
- $this->db->where('classes_vaad.active', 1);
- $query = $this->db->get();
- $count = $query->num_rows();
- if($count > 0)
- {
- return $query->row()->vaad_type;
- }
- return '0';
- /*vaad*/
- }
- function set_kid_array_values($kid,$history=false)
- {
- if($history)
- {
- unset($kid['kid_name']);
- unset($kid['kid_bd']);
- // unset($kid['kid_id']);
- unset($kid['kid_pic']);
- unset($kid['kid_gender']);
- }
- else
- {
- unset($kid['gan_id']);
- unset($kid['gan_name']);
- unset($kid['gan_max']);
- unset($kid['gan_address']);
- unset($kid['gan_phone']);
- unset($kid['gan_city']);
- unset($kid['gan_code']);
- unset($kid['class_name']);
- unset($kid['class_id']);
- unset($kid['teacher_id']);
- unset($kid['teacher_mail']);
- unset($kid['teacher_first_name']);
- unset($kid['teacher_last_name']);
- unset($kid['teacher_mobile']);
- unset($kid['teacher_phone']);
- unset($kid['kid_active']);
- unset($kid['class_year']);
- }
- return $kid;
- }
- function getKids2014($id)
- {
- $this->db->select('gans.id as gan_id',FALSE);
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('gans.pic as pic',FALSE);
- $this->db->select('gans.max as gan_max',FALSE);
- $this->db->select('gans.code as gan_code',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('classes.id as class_id',FALSE);
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('kids.birth_date as birth_date',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->select('kids.pic as kid_pic',FALSE);
- $this->db->select('kids_classes.active as kid_active',FALSE);
- $this->db->select('kids_classes.year as year',FALSE);
- $this->db->select('users.first_name as first_name',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
- $this->db->join('classes', 'kids_classes.class_id = classes.id');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id');
- $this->db->where('users.id', $id);
- // $this->db->group_by('kids.id');
- $kids = $this->db->get()->result_array();
- $kids_new = array();
- $res = array();
- foreach ($kids as $kid)
- {
- $kids_new[$kid['kid_id']][] = $kid;
- }
- foreach ($kids_new as $kid_new)
- {
- $new_year = false;
- foreach ($kid_new as $kid)
- {
- if($kid['year'] == "2015")
- {
- $new_year = true;
- }
- }
- if(!$new_year)
- {
- $res[] = reset($kid_new);
- }
- // if(count($kid_new)==1)
- // {
- // $res[] = reset($kid_new);
- // }
- }
- return $res;
- }
- function startsWith($haystack, $needle)
- {
- return $needle === "" || strpos($haystack, $needle) === 0;
- }
- function getReadGansDetailsFn($mail)
- {
- $this->db->select('users.first_name as first_name',FALSE);
- $this->db->from('users');
- $this->db->join('gans', 'gans.id = users.gan_id');
- $this->db->where('users.mail', $mail);
- $query = $this->db->get();
- return $query->row()->first_name;
- }
- function getReadGansDetailsLn($mail)
- {
- $this->db->select('users.last_name as last_name',FALSE);
- $this->db->from('users');
- $this->db->join('gans', 'gans.id = users.gan_id');
- $this->db->where('users.mail', $mail);
- $query = $this->db->get();
- return $query->row()->last_name;
- }
- function getReadGansDetailsCode($mail)
- {
- $this->db->select('gans.code as code',FALSE);
- $this->db->from('users');
- $this->db->join('gans', 'gans.id = users.gan_id');
- $this->db->where('users.mail', $mail);
- $query = $this->db->get();
- return $query->row()->code;
- }
- //
- function editkiddetails($id,$name,$class_id,$bd,$gan_id,$path)
- {
- $this->db->where('id', $id);
- if( strcmp($path, "") != 0)
- {
- $data = array(
- 'birth_date' => $bd,
- 'name' => $name,
- 'gan_id' => $gan_id,
- 'class_id' => $class_id,
- 'pic' => $id
- );
- }
- else
- {
- $data = array(
- 'birth_date' => $bd,
- 'name' => $name,
- 'gan_id' => $gan_id,
- 'class_id' => $class_id
- );
- }
- return $this->db->update('kids', $data);
- }
- function registerkid($id,$name,$class_id,$bd,$gan_id)
- {
- $data = array(
- 'parent_id' => $id,
- 'birth_date' => $bd,
- 'name' => $name,
- 'gan_id' => $gan_id,
- 'class_id' => $class_id
- );
- $bool = $this->db->insert('kids', $data);
- $kid_id = $this->db->insert_id();
- $data_kid_class = array(
- "class_id" => $class_id,
- "gan_id" => $gan_id,
- "kid_id" => $kid_id,
- "year" => "2015",
- "date" => date("Y-m-d")
- );
- $this->db->insert('kids_classes', $data_kid_class);
- return $kid_id;
- }
- function updatekidpic($id,$name=null)
- {
- $this->db->where('id', $id);
- $data = array();
- if($name == null)
- {
- $data['pic'] = $id;
- }
- else
- {
- $data['pic'] = $name;
- }
- $this->db->update('kids', $data);
- }
- function kidconfirmed($id)
- {
- $this->db->select('active');
- $this->db->where('id', $id);
- $query = $this->db->get('kids');
- return $query->row()->active;
- }
- function updateclass($kid_id,$class_id_param,$user_id=null)
- {
- // check if already kid_id,class_id,current_year exist
- if($class_id_param != "")
- {
- $this->db->where('kid_id', $kid_id);
- $this->db->where('class_id', $class_id_param);
- $this->db->where('year', CURRENT_YEAR);
- $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
- $query = $this->db->get('kids_classes');
- if($query->num_rows() > 0)
- {
- return false;
- }
- }
- $this->db->select('class_id',FALSE);
- $this->db->select('active',FALSE);
- $this->db->where('kid_id', $kid_id);
- // $this->db->where('class_id', $class);
- $this->db->where('year', CURRENT_YEAR);
- $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
- $query = $this->db->get('kids_classes');
- if($query->num_rows() > 0)
- {
- $class_id = $query->row()->class_id;
- $active = $query->row()->active;
- $this->db->where('kid_id', $kid_id);
- $this->db->where('class_id', $class_id);
- $this->db->where('active', $active);
- $data_kid_class = array(
- "active" => 0,
- // "year" => CURRENT_YEAR,
- "date" => date("Y-m-d H:i:s")
- );
- $res = $this->db->update('kids_classes', $data_kid_class); // deactivate older classes
- $this->retention_model->delete_token_retention_after_active_kid($class_id, $user_id); // update old classes
- if($class_id_param == "")
- {
- return $res;
- }
- }
- // return false;
- // if( strcmp($class_id_param, "") != 0)
- // {
- return $this->setclass($kid_id, $class_id_param, "1");
- // }
- // else
- // {
- // return $res;
- // }
- }
- // function setclass($id,$class)
- // {
- // $this->db->where('kid_id', $id);
- // $this->db->where('class_id', $class);
- // $this->db->where('year', CURRENT_YEAR);
- // $query = $this->db->get('kids_classes');
- // if($query->num_rows() > 0)
- // {
- // $this->db->where('kid_id', $id);
- // $this->db->where('class_id', $class);
- // $this->db->where('year', CURRENT_YEAR);
- // $data_kid_class = array(
- // "active" => 2,
- // "year" => CURRENT_YEAR,
- // "date" => date("Y-m-d H:i:s")
- // );
- // $res = $this->db->update('kids_classes', $data_kid_class);
- // return $res;
- // }
- // else
- // {
- // return $this->add_class($id, $class);
- // }
- // }
- function set_class_batch_by_year($kids,$year)
- {
- $kids_insert = array();
- foreach($kids as $class_id => $kids_arr)
- {
- foreach($kids_arr as $kid_id)
- {
- $kid = array("kid_id"=>$kid_id, "class_id"=>$class_id);
- $kids_insert[] = $kid;
- }
- }
- foreach($kids_insert as $kid)
- {
- $res = $this->setclass($kid['kid_id'],$kid['class_id'],'1',$year);
- if($res)
- {
- //send push
- }
- }
- }
- function setclass($id, $class, $active, $year=CURRENT_YEAR)
- {
- // if($active == '1')
- // {
- // $query = $this->db->get_where('kids_classes',array('kid_id' => $id,
- // 'class_id' => $class,
- // 'active' => $active,
- // 'year' => $year
- // ));
- // if($query->num_rows() > 0)
- // {
- // return false;
- // }
- // }
- $this->db->where('kid_id', $id);
- $this->db->where('class_id', $class);
- $this->db->where('year', $year);
- $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
- $query = $this->db->get('kids_classes');
- if($query->num_rows() > 0)
- {
- return false;
- }
- $this->db->select('gan_id');
- $this->db->where('id', $class);
- $query = $this->db->get('classes');
- $gan_id = $query->row()->gan_id;
- $data_kid_class = array(
- "active" => $active,
- "class_id" => $class,
- "gan_id" => $gan_id,
- "kid_id" => $id,
- "year" => $year,
- "date" => date("Y-m-d H:i:s")
- );
- $res = $this->db->insert('kids_classes', $data_kid_class);
- //update kid
- // $this->db->where('id', $id);
- // $data = array(
- // 'active' => 0,
- // 'gan_id' => $gan_id,
- // 'class_id' => $class
- // );
- // $this->db->update('kids', $data);
- // if($res)
- // {
- // }
- return $res;
- }
- function create_kid($id,$name,$gender,$bd,$path,$gan_code)
- {
- $date = date("Y-m-d H:i:s");
- $query = $this->db->get_where('kids',array('name' => $name,
- 'gender' => $gender,
- // 'birth_date' => $bd,
- 'parent_id' => $id
- ));
- if($query->num_rows() > 0)
- {
- $created = $query->row()->created;
- $time_date = strtotime($date);
- $time_created = strtotime($created);
- if($time_date - $time_created <= 600)
- {
- return $query->row()->id;
- }
- }
- $data = array(
- 'parent_id' => $id,
- 'birth_date' => $bd,
- 'gender' => $gender,
- // 'active' => $active,
- 'created' => $date,
- 'name' => $name
- );
- $this->db->insert('kids', $data);
- $kid_id = $this->db->insert_id();
- if( strcmp($path, "") != 0)
- {
- $this->db->where('id', $kid_id);
- $data = array(
- 'pic' => $kid_id
- );
- $this->db->update('kids', $data);
- }
- if ($kid_id)
- {
- return $kid_id;
- }
- else
- {
- return false;
- }
- }
- function registerkiddetails($id,$name,$class_id,$bd,$gan_id,$path)
- {
- $data = array(
- 'parent_id' => $id,
- 'birth_date' => $bd,
- 'name' => $name,
- 'gan_id' => $gan_id,
- 'class_id' => $class_id
- );
- $bool = $this->db->insert('kids', $data);
- $kid_id = $this->db->insert_id();
- $data_kid_class = array(
- "active" => 2,
- "class_id" => $class_id,
- "gan_id" => $gan_id,
- "kid_id" => $kid_id,
- "year" => "2015",
- "date" => date("Y-m-d")
- );
- $bool = $this->db->insert('kids_classes', $data_kid_class);
- if( strcmp($path, "") != 0)
- {
- $this->db->where('id', $kid_id);
- $data = array(
- 'pic' => $kid_id
- );
- $this->db->update('kids', $data);
- }
- if ($bool)
- {
- return $kid_id;
- }
- }
- function getParentsByGanenetId($id)
- {
- //get gan_id
- $this->db->select('gan_id');
- $this->db->where('id', $id);
- $query = $this->db->get('users');
- $gan_id = $query->row()->gan_id;
- $this->db->select('distinct users.*',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id ');
- $this->db->where('kids.gan_id', $gan_id);
- $query = $this->db->get();
- return $query->result_array();
- }
- function getParentsByClassId($id)
- {
- $this->db->select('distinct users.*',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id ');
- $this->db->where('kids.class_id', $id);
- $query = $this->db->get();
- return $query->result_array();
- }
- function mailexistRetention($mail)
- {
- $this->db->where('mail',$mail);
- $query = $this->db->get('retention');
- if ($query->num_rows() > 0){
- return true;
- }
- else{
- return false;
- }
- }
- function mailexistNew($mail)
- {
- $this->db->where('mail',$mail);
- //$this->db->where('activation',NULL);
- $query = $this->db->get('users');
- if ($query->num_rows() > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- function mailexist($mail)
- {
- $this->db->select('mail',FALSE);
- $this->db->from('users');
- $this->db->where('users.mail', $mail);
- $query = $this->db->get();
- return $query->result_array();
- /*$classes = array(
- '4' => $mail ,
- '2' => $mail
- );
- for ($i = 1; $i <= 2; $i++)
- {
- if (array_key_exists($i, $classes)) {
- echo $i;
- }
- }*/
- }
- function getuserpersonaldetails($id)
- {
- $this->db->select('id',FALSE);
- $this->db->select('mail',FALSE);
- $this->db->select('password',FALSE);
- $this->db->select('first_name',FALSE);
- $this->db->select('last_name',FALSE);
- $this->db->select('area_code',FALSE);
- $this->db->select('phone',FALSE);
- $this->db->select('address',FALSE);
- $this->db->select('city',FALSE);
- $this->db->select('country',FALSE);
- $this->db->select('state',FALSE);
- $this->db->select('cityName',FALSE);
- $this->db->from('users');
- $this->db->where('users.id', $id);
- $query = $this->db->get();
- return $query->result_array();
- }
- function edituserpersonaldetails($id,$mail,$password,$first_name,$last_name,$address,$area_code,$phone,$city,$country,$state,$cityName)
- {
- $this->db->where('id', $id);
- $data = array(
- 'mail' => $mail,
- // 'password' => sha1($password),
- 'first_name' => $first_name,
- 'last_name' => $last_name,
- 'address' => $address,
- 'area_code' => $area_code,
- 'phone' => $phone,
- 'country' => $country,
- 'state' => $state,
- 'cityName' => $cityName,
- 'city' => $city
- );
- return $this->db->update('users', $data);
- }
- function updateuser($id,$post_data)
- // function updateuser($id,$first_name,$last_name,$address,$phone,$mobile,$city)
- {
- $data = $this->util_model->filldata($post_data);
- // if($first_name != "")
- // {
- // $user['first_name'] = $first_name;
- // }
- // if($last_name != "")
- // {
- // $user['last_name'] = $last_name;
- // }
- // if($address != "")
- // {
- // $user['address'] = $address;
- // }
- // if($phone != "")
- // {
- // $user['phone'] = $phone;
- // }
- // if($mobile != "")
- // {
- // $user['mobile'] = $mobile;
- // }
- // if($city != "")
- // {
- // $user['cityName'] = $city;
- // }
- // $user['area_code'] = 0;
- // $user = array(
- // 'phone' => $phone,
- // 'first_name' => $first_name,
- // 'last_name' => $last_name,
- // 'mobile' => $mobile,
- // 'address' => $address,
- // 'city' => $city
- // );
- $this->db->where('id', $id);
- return $this->db->update('users', $data);
- // return $this->db->update('users', $user);
- }
- function editkids($kids)
- {
- $count = count($kids);
- for ($i = 1; $i <= $count; $i++)
- {
- $arr = $kids[$i];
- $id = $arr["id"] ;
- $this->db->where('id', $id);
- $query = $this->db->update('kids', $arr);
- }
- }
- function send_setclass_push($kid_id,$class_id,$app_name)
- {
- //send notification to teacher
- $kid_name = $this->parent_model->get_kid_name($kid_id);
- $class_name = $this->class_model->get_class_name($class_id);
- // $text = $kid_name . ' ' . 'נרשמ/ה לכיתת ' . $class_name . ' ' . 'ומחכה לאישורך';
- if($app_name == GB || $app_name == null)
- {
- $text = PUSH_SET_CLASS_HE;
- }
- else
- {
- $text = PUSH_SET_CLASS_EN;
- }
- $text = str_replace('KID_NAME',$kid_name,$text);
- $text = str_replace('CLASS_NAME',$class_name,$text);
- $arr = $this->get_teacher_by_class($class_id);
- $res = reset($arr);
- $user_id = $res['user_id'];
- $gan_id = $res['gan_id'];
- $badge = $this->parent_model->get_count_waiting_parents($gan_id);
- //send push
- $tokens = $this->token_model->get_user_tokens($user_id);
- $loc_args = array($kid_name,$class_name);
- $acme = array("cls_id" => $class_id);
- $user_id_array = array();
- $user_id_array[$user_id] = array("cls_id" => $class_id);
- $this->message_model->send_push($tokens,$user_id_array,$text,$class_id,SET_CLASS_LOC_KEY,$loc_args,$acme,$badge,$app_name);
- }
- function update_seen_albums($user_id,$album_id,$num_seen_photos,$year)
- {
- //update only views count (seen_albums)
- $this->db->where('user_id',$user_id);
- $this->db->where('album_id',$album_id);
- $query = $this->db->get('seen_albums');
- if ($query->num_rows() == 0)
- {
- $data = array('user_id'=>$user_id,
- 'album_id'=>$album_id,
- 'year'=>$year,
- 'views'=>1
- );
- $query = $this->db->insert('seen_albums', $data);
- if (!$query)
- {
- return false;
- }
- }
- else
- {
- $res = reset($query->result_array());
- $views = $res['views'];
- $views ++;
- $this->db->where('user_id', $user_id);
- $this->db->where('album_id', $album_id);
- $data = array(
- 'views' => $views
- );
- $query = $this->db->update('seen_albums', $data);
- if (!$query)
- {
- return false;
- }
- }
- if($num_seen_photos != "")
- {
- //update also num pic count (album_seen_pictures)
- $this->db->where('user_id',$user_id);
- $this->db->where('album_id',$album_id);
- $query = $this->db->get('album_seen_pictures');
- if ($query->num_rows() == 0)
- {
- $data = array('user_id'=>$user_id,
- 'album_id'=>$album_id,
- 'year'=>$year,
- 'seen_pics'=>$num_seen_photos
- );
- $query = $this->db->insert('album_seen_pictures', $data);
- if (!$query)
- {
- return false;
- }
- }
- else
- {
- $this->db->where('user_id', $user_id);
- $this->db->where('album_id', $album_id);
- $data = array(
- 'seen_pics' => $num_seen_photos
- );
- $query = $this->db->update('album_seen_pictures', $data);
- if (!$query)
- {
- return false;
- }
- }
- }
- return true;
- }
- function createuser($data)
- {
- $query = $this->db->insert('users', $data);
- $id = $this->db->insert_id();
- return $id;
- }
- function check_password($pass)
- {
- $arr = str_split($pass);
- if(count($arr) < 6)
- {
- return '9010';
- }
- else if($this->HasConsec($arr))
- {
- return "9011";
- }
- else if (!preg_match('/[A-Za-z]/', $pass) || !preg_match('/[0-9]/', $pass))
- {
- return "9012";
- }
- else
- {
- return true;
- }
- }
- function HasConsec($array)
- {
- $consec = 0;
- $res = false;
- $cb = false;
- foreach ($array as $im)
- {
- if ($cb !== false && $cb == $im)
- {
- $consec ++;
- if($consec == 2)
- {
- return true;
- }
- }
- else
- {
- if($consec == 1)
- {
- $consec = 0;
- }
- }
- $cb = $im + 1;
- }
- return $res;
- }
- function register($user,$first_name,$last_name,$kids,$gan,$classes,$mail,$pass,$phone,$area_code,$address,$city,$country,$state,$cityName,$activation)
- {
- $bool = true;
- $ip = $this->get_ip_address();
- if($cityName == "")
- {
- $cityName = null;
- }
- $data = array(
- 'first_name' => $first_name ,
- 'last_name' => $last_name ,
- 'mail' => $mail,
- 'password' => sha1($pass),
- 'phone' => $phone,
- 'area_code' => $area_code,
- 'address' => $address,
- 'city' => $city,
- 'country' => $country,
- 'state' => $state,
- 'cityName' => $cityName,
- 'type' => $user,
- 'activation' => $activation,
- 'ip' => $ip,
- 'date' => date("Y-m-d H:i:s")
- );
- if($user != 1) // ganenet
- {
- $code = rand(1000, 9999);
- $flag_code = $this->gan_model->getCountGanCode($code);
- while($flag_code)
- {
- $code = rand(1000, 9999);
- $flag_code = $this->gan_model->getCountGanCode($code);
- }
- $gan['active'] = 1;
- $gan['code'] = $code;
- $gan['date'] = date("Y-m-d H:i:s");
- if($gan['area_code'] == '0')
- {
- $gan['area_code'] = '1';
- }
- $query = $this->db->insert('gans', $gan);
- $gan_id = $this->db->insert_id();
- if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
- {
- $bool = false;
- return $bool;
- }
- else
- {
- $this->db->where('id', $gan_id);
- /*if( strcmp($gan["pic"], "default") == 0)
- {
- $pic = "default";
- }
- else
- {
- $pic = 'gan_'.$gan_id;
- }*/
- if (array_key_exists('pic', $gan))
- {
- $pic = 'gan_'.$gan_id;
- $dataGan = array(
- 'pic' => $pic
- );
- $this->db->update('gans', $dataGan);
- }
- // $this->db->order_by("id", "desc");
- // $query = $this->db->get('gans', 1, 0);
- // $id = $query->row()->id;
- $data['gan_id'] = $gan_id;
- $data['active'] = 1;
- /*$count = count($classes);
- for ($i = 1; $i <= $count; $i++)
- {
- if (array_key_exists(strval($i), $classes)) {
- $arr = $classes[$i];
- $arr["gan_id"] = $gan_id;
- $query = $this->db->insert('classes', $arr);
- if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
- {
- $bool = false;
- }
- }
- }*/
- foreach ($classes as $key => $value)
- {
- $arr = $value;
- $arr["gan_id"] = $gan_id;
- $query = $this->db->insert('classes', $arr);
- if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
- {
- $bool = false;
- }
- }
- }
- }
- $query = $this->db->insert('users', $data);
- // if ($this->db->_error_message())
- // {
- // return false;
- // }
- $id = $this->db->insert_id();
- if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
- {
- $bool = false;
- return $bool;
- }
- $kid_ids = "";
- if($user == 1) // parent
- {
- // $this->db->order_by("id", "desc");
- // $query = $this->db->get('users', 1, 0);
- // $id = $query->row()->id;
- $count = count($kids);
- for ($i = 1; $i <= $count; $i++)
- {
- $arr = $kids[$i];
- $arr["parent_id"] = $id;
- $query = $this->db->insert('kids', $arr);
- $kid_id = $this->db->insert_id();
- $data_kid_class = array(
- "class_id" => $arr["class_id"],
- "gan_id" => $arr["gan_id"],
- "kid_id" => $kid_id,
- "year" => "2015",
- "date" => date("Y-m-d")
- );
- $this->db->insert('kids_classes', $data_kid_class);
- $kid_ids .= $kid_id;
- $kid_ids .= ",";
- if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
- {
- $bool = false;
- }
- else
- {
- if (array_key_exists('pic', $arr))
- {
- $this->db->where('id', $kid_id);
- $data = array(
- 'pic' => $kid_id
- );
- $this->db->update('kids', $data);
- }
- }
- }
- $kid_ids = substr($kid_ids, 0, -1);
- }
- if($bool)
- {
- if($user != 1) // ganenet
- {
- return $id.'-'.$code;
- }
- else
- {
- return $kid_ids;
- }
- }
- }
- /**
- * Get user ip address
- */
- function get_ip_address()
- {
- foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
- if (array_key_exists($key, $_SERVER) === true) {
- foreach (explode(',', $_SERVER[$key]) as $ip) {
- if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
- return $ip;
- }
- }
- }
- }
- }
- function getLastUserId($id)
- {
- // $this->db->order_by("id", "desc");
- // $query = $this->db->get('users', 1, 0);
- // return $query->row()->id;
- $this->db->select('parent_id',FALSE);
- $this->db->from('kids');
- $this->db->where('kids.id', $id);
- $query = $this->db->get();
- return $query->row()->parent_id;
- }
- function getLastGanId()
- {
- $this->db->order_by("id", "desc");
- $query = $this->db->get('gans', 1, 0);
- return $query->row()->id;
- }
- function getLastRegisteredKidsIds($kids)
- {
- $str_ids = '';
- $len = sizeof($kids);
- // for ( $counter = 0; $counter < $len; $counter += 1)
- // {
- $this->db->order_by("id", "desc");
- $query = $this->db->get('kids', $len, $counter);
- foreach ($query->result() as $row)
- {
- $str_ids = $row->id . $str_ids;
- $str_ids = ',' . $str_ids;
- }
- // }
- $str_ids = substr( $str_ids, 1 );
- return $str_ids;
- }
- function deactivate($mail)
- {
- $this->db->where('mail', $mail);
- // $this->db->where('activation', $key);
- $data = array(
- 'activation' => '111'
- );
- $this->db->update('users', $data);
- }
- function activate($key,$mail)
- {
- $this->db->where('mail', $mail);
- $this->db->where('activation', $key);
- $data = array(
- 'activation' => null
- );
- return $this->db->update('users', $data);
- }
- /*function activate($key,$mail)
- {
- $this->db->where('mail', $mail);
- $this->db->where('activation', $key);
- $data = array(
- 'activation' => null
- );
- $this->db->update('users', $data);
- $this->db->select('type');
- $this->db->from('users');
- $this->db->where('mail', $mail);
- $query = $this->db->get();
- return $query->row()->type;
- }*/
- function forget_key_exist($key)
- {
- $query = $this->db->get_where('users',array('password_key'=>$key));
- $count = $query->num_rows(); //counting result from query
- if ($count === 0)
- {
- return false;
- }
- return true;
- }
- function forget($key,$mail,$password)
- {
- $this->db->where('mail', $mail);
- $this->db->where('password_key', $key);
- $data = array(
- 'password' => sha1($password),
- 'password_key' => NULL
- );
- $this->db->update('users', $data);
- return $this->db->affected_rows() > 0;
- }
- function delete_teacher($id)
- {
- $this->db->select('gan_id');
- $this->db->where('id', $id);
- $query = $this->db->get('users');
- $gan_id = $query->row()->gan_id;
- $this->db->where('gan_id', $gan_id);
- $this->db->delete('classes');
- $this->db->where('id', $gan_id);
- $this->db->delete('gans');
- $this->db->where('id', $id);
- $this->db->delete('users');
- }
- function get_kid_class($kid_id)
- {
- $this->db->select('kids_classes.class_id as class_id');
- $this->db->select('classes.name as class_name');
- $this->db->select('users.first_name as teacher_first_name');
- $this->db->select('users.last_name as teacher_last_name');
- $this->db->select('gans.name as gan_name');
- $this->db->from('kids_classes');
- $this->db->join('classes', 'kids_classes.class_id = classes.id');
- $this->db->join('users', 'users.gan_id = classes.gan_id');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id');
- $this->db->where('kids_classes.kid_id', $kid_id);
- $this->db->where('kids_classes.year', CURRENT_YEAR);
- $query = $this->db->get();
- return reset($query->result_array());
- }
- function get_users_in_class($class_id,$type)
- {
- if($type != TEACHER_TYPE)
- {
- $this->db->select('distinct(users.id) as user_id');
- //$this->db->select('kids.id as kid_id');
- $this->db->from('classes');
- $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
- $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('users', 'users.id = kids.parent_id');
- $this->db->where('classes.id', $class_id);
- $users = $this->db->get()->result_array();
- }
- else
- {
- $this->db->select('distinct(users.id) as user_id');
- $this->db->from('classes');
- $this->db->join('gans', 'gans.id = classes.gan_id');
- $this->db->join('users', 'users.gan_id = gans.id');
- $this->db->where('classes.id', $class_id);
- $users = $this->db->get()->result_array();
- }
- // $users = array_merge($users, $teachers);
- $res = array();
- foreach ($users as $u=>$user)
- {
- $res[] = $user['user_id'];
- }
- return $res;
- }
- function get_users_in_class_test($class_id,$users_ids=null)
- {
- $this->db->select('distinct(users.id) as user_id');
- $this->db->select('kids.id as kid_id');
- $this->db->from('classes');
- $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
- $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('users', 'users.id = kids.parent_id');
- $this->db->where('classes.id', $class_id);
- if($users_ids != null)
- {
- $this->db->where_in('users.id', $users_ids);
- }
- $users = $this->db->get()->result_array();
- $this->db->select('distinct(users.id) as user_id');
- $this->db->from('classes');
- $this->db->join('gans', 'gans.id = classes.gan_id');
- $this->db->join('users', 'users.gan_id = gans.id');
- $this->db->where('classes.id', $class_id);
- $teachers = $this->db->get()->result_array();
- $users = array_merge($users, $teachers);
- $ids = array();
- $kids_ids = array();
- foreach ($users as $u=>$user)
- {
- if(!in_array($user['user_id'],$ids))
- {
- $ids[] = $user['user_id'];
- $kids_ids[$user['user_id']] = $user['kid_id'];
- }
- }
- // $res = array();
- // $res['kids_ids'] = $kids_ids;
- // $res['ids'] = $ids;
- return $kids_ids;
- }
- function get_kids_parents($kids)
- {
- $this->db->select('distinct(users.id) as user_id',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->from('kids');
- $this->db->join('users', 'kids.parent_id = users.id');
- $this->db->where_in('kids.id', $kids);
- $parents = $this->db->get()->result_array();
- $res = array();
- foreach ($parents as $p=>$parent)
- {
- $res[$parent['user_id']] = $parent['kid_id'];
- }
- return $res;
- }
- function get_kids_parents2($kids)
- {
- $this->db->select('kids.parent_id as user_id',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->from('kids');
- $this->db->join('users', 'kids.parent_id = users.id');
- $this->db->where_in('kids.id', $kids);
- $parents = $this->db->get()->result_array();
- $res = array();
- error_log("*** DB *** " . print_r($parents, TRUE));
- foreach ($parents as $p=>$parent)
- {
- $res[$parent['user_id']] = $parent['kid_id'];
- }
- return $res;
- }
- function get_kids_parents_mails($kids)
- {
- $this->db->select('distinct(users.id) as user_id',FALSE);
- $this->db->select('users.mail as user_mail',FALSE);
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('kids.birth_date as kid_birth_date',FALSE);
- $this->db->from('kids');
- $this->db->join('users', 'kids.parent_id = users.id');
- $this->db->where_in('kids.id', $kids);
- $this->db->where('users.app_name', '1');
- $parents = $this->db->get()->result_array();
- $res = array();
- foreach ($parents as $p=>$parent)
- {
- $res[$p] = $parent;
- $year = date('Y');
- $kid_bd = $parent['kid_birth_date'];
- $kid_year = date('Y', strtotime($kid_bd));
- $res[$p]['kid_age'] = intval($year) - intval($kid_year);
- }
- return $res;
- }
- function get_kid_parent($kid_id)
- {
- $this->db->select('users.id as id');
- $this->db->select('users.first_name as first_name');
- $this->db->select('users.type as type');
- $this->db->select('users.mail as mail');
- $this->db->select('kids.name as name');
- $this->db->select('users.app_name as app_name');
- $this->db->select('gans.name as gan_name');
- $this->db->from('kids');
- $this->db->join('users', 'users.id = kids.parent_id');
- $this->db->join('kids_classes', 'kids_classes.kid_id = kids.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
- $this->db->join('gans', 'gans.id = kids_classes.gan_id');
- $this->db->where('kids.id', $kid_id);
- $query = $this->db->get();
- return reset($query->result_array());
- }
- function udpate_mail_key($id)
- {
- $key = md5(uniqid(rand(), true));
- $data = array(
- 'mail_key' => $key
- );
- $this->db->where('id', $id);
- $this->db->update('users', $data);
- }
- function get_users_kids_2014()
- {
- // $this->db->select('gans.name as gan_name',FALSE);
- // $this->db->select('classes.name as class_name',FALSE);
- // $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('users.first_name as first_name',FALSE);
- $this->db->select('users.mail as mail',FALSE);
- $this->db->select('users.id as id',FALSE);
- $this->db->select('users.mail_key as mail_key',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
- $this->db->join('classes', 'kids_classes.class_id = classes.id');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id');
- $this->db->where('kids_classes.active', 1);
- $this->db->where('kids_classes.year', 2014);
- $this->db->group_by('users.id');
- $users = $this->db->get()->result_array();
- foreach ($users as $u=>$user)
- {
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->from('users');
- $this->db->join('kids', 'kids.parent_id = users.id');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
- $this->db->join('classes', 'kids_classes.class_id = classes.id');
- $this->db->join('gans', 'kids_classes.gan_id = gans.id');
- $this->db->where('users.id', $user['id']);
- $this->db->where('kids_classes.active', 1);
- $this->db->where('kids_classes.year', 2014);
- $kids = $this->db->get()->result_array();
- foreach ($kids as $k=>$kid)
- {
- foreach ($kid as $key=>$value)
- {
- $kid[$key] = str_replace('"', "", $kid[$key]);
- $users[$u][$key.$k] = $kid[$key];
- }
- }
- if(count($kids) == 1)
- {
- $users[$u]['gan_name1'] = " ";
- $users[$u]['class_name1'] = " ";
- $users[$u]['kid_name1'] = " ";
- $users[$u]['gan_name2'] = " ";
- $users[$u]['class_name2'] = " ";
- $users[$u]['kid_name2'] = " ";
- }
- else if(count($kids) == 2)
- {
- $users[$u]['gan_name2'] = " ";
- $users[$u]['class_name2'] = " ";
- $users[$u]['kid_name2'] = " ";
- }
- }
- $res = array();
- foreach ($users as $u=>$user)
- {
- unset($user['id']);
- $res[] = $user;
- }
- return $res;
- }
- function userkey($key)
- {
- $this->db->select('id');
- $this->db->where('mail_key', $key);
- $this->db->from('users');
- $query = $this->db->get();
- return $query->row()->id;
- }
- function get_teacher_by_gan($gan_id)
- {
- $this->db->select('id');
- $this->db->where('gan_id', $gan_id);
- $this->db->from('users');
- $query = $this->db->get();
- return $query->row()->id;
- }
- function get_teacher_by_class($class_id)
- {
- $this->db->select('users.id as user_id');
- $this->db->select('gans.id as gan_id');
- $this->db->where('classes.id', $class_id);
- $this->db->from('users');
- $this->db->join('gans', 'gans.id = users.gan_id');
- $this->db->join('classes', 'gans.id = classes.gan_id');
- return $this->db->get()->result_array();
- }
- function get_teacher_name_by_class($class_id)
- {
- $this->db->select('users.first_name as teacher_first_name');
- $this->db->select('users.last_name as teacher_last_name');
- $this->db->select('users.mail as teacher_mail');
- $this->db->select('gans.name as gan_name');
- $this->db->where('classes.id', $class_id);
- $this->db->from('users');
- $this->db->join('gans', 'gans.id = users.gan_id');
- $this->db->join('classes', 'gans.id = classes.gan_id');
- return reset($this->db->get()->result_array());
- }
- function get_parents_classes_messages($parents_ids)
- {
- $this->db->select('distinct(messages.id) as message_id');
- $this->db->select('classes.id as class_id');
- $this->db->select('users.id as user_id');
- $this->db->from('messages');
- $this->db->join('classes', 'messages.class_id = classes.id');
- $this->db->join('kids_classes', 'kids_classes.class_id = classes.id');
- $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('users', 'kids.parent_id = users.id');
- $this->db->where_in('users.id', $parents_ids);
- $this->db->where('kids_classes.active', '1');
- $this->db->where('messages.year', CURRENT_YEAR);
- // $this->db->limit(50);
- $messages = $this->db->get()->result_array();
- $classes = array();
- $users = array();
- foreach ($messages as $m => $message)
- {
- $users[$message['user_id']][] = $message;
- }
- $res = array();
- foreach ($users as $u => $user)
- {
- $arr = array("user_id" => $u,"messages"=>$user);
- $res[] = $arr;
- }
- return $res;
- }
- function get_parents_classes_pics($parents_ids)
- {
- $this->db->select('COUNT(distinct pictures.id) as count_pic');
- $this->db->select('users.id as user_id');
- $this->db->from('pictures');
- $this->db->join('albums', 'albums.id = pictures.album_id');
- $this->db->join('classes', 'albums.class_id = classes.id');
- $this->db->join('kids_classes', 'kids_classes.class_id = classes.id');
- $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('users', 'kids.parent_id = users.id');
- $this->db->where_in('users.id', $parents_ids);
- $this->db->where('kids_classes.active', '1');
- $this->db->where('pictures.active', '1');
- $this->db->where('albums.year', CURRENT_YEAR);
- $this->db->group_by('users.id');
- $pictures = $this->db->get()->result_array();
- $users = array();
- foreach ($pictures as $p => $picture)
- {
- $users[$picture['user_id']][] = $picture['count_pic'];
- }
- return $pictures;
- }
- function get_user_app($user_id)
- {
- $this->db->select('app_name');
- $this->db->where('id', $user_id);
- $this->db->from('users');
- $query = $this->db->get();
- return $query->row()->app_name;
- }
- function get_user_app_mail($mail)
- {
- $this->db->select('app_name');
- $this->db->where('mail', $mail);
- $this->db->from('users');
- $query = $this->db->get();
- return $query->row()->app_name;
- }
- function is_user_teacher($user_id)
- {
- $this->db->select('type');
- $this->db->where('id', $user_id);
- $this->db->from('users');
- $query = $this->db->get();
- return ($query->row()->type == "2");
- }
- function create_user_push($push_id,$token_id)
- {
- $res = $this->db->insert('users_push', array('push_id'=>$push_id,'token_id'=>$token_id,'date'=>date("Y-m-d H:i:s")));
- }
- function is_user_pta_in_class_or_teacher($user_id,$class_id)
- {
- // return 0 - parent, return 1 - teacher, return 2 - PTA
- $this->db->select('type');
- $this->db->where('id', $user_id);
- $this->db->from('users');
- $query = $this->db->get();
- if($query->row()->type != "2") //parent
- {
- $this->db->select('active');
- $this->db->where('user_id', $user_id);
- $this->db->where('class_id', $class_id);
- $this->db->from('classes_vaad');
- $query = $this->db->get();
- if($query->num_rows == 0)
- {
- return 0;
- }
- $active = $query->row()->active;
- if($active == "1")
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 1;
- }
- }
- function get_permissions_for_user($user_id)
- {
- $res = array();
- $this->db->select('users.type');
- $this->db->select('users.gan_id');
- $this->db->from('users');
- $this->db->where('users.id', $user_id);
- $user = reset($this->db->get()->result_array());
- if($user['type'] == '2')
- {
- $res = $this->get_permissions_for_gan($user['gan_id']);
- }
- return $res;
- }
- function get_permissions_for_gan($gan_id)
- {
- $this->db->from('gans_permissions');
- $this->db->where('gans_permissions.active', '1');
- $this->db->where('gans_permissions.gan_id', $gan_id);
- $permissions = $this->db->get()->result_array();
- $res = array();
- foreach ($permissions as $permission)
- {
- if($permission['permission_type'] == "1")
- {
- $res['like_forbidden'] = true;
- }
- else if($permission['permission_type'] == "2")
- {
- $res['comment_forbidden'] = true;
- }
- }
- return $res;
- }
- function get_users_mails_in_class($class_id)
- {
- $this->db->select('distinct(users.mail) as user_mail');
- $this->db->select('users.first_name as first_name');
- $this->db->from('classes');
- $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
- $this->db->join('kids', 'kids.id = kids_classes.kid_id');
- $this->db->join('users', 'users.id = kids.parent_id');
- $this->db->where('classes.id', $class_id);
- $mails = $this->db->get()->result_array();
- return $mails;
- }
- function get_user_kids_2($parent_id)
- {
- $this->db->select('users.type as type',FALSE);
- $this->db->from('users');
- $this->db->where('users.id', $parent_id);
- $query = $this->db->get();
- $type = $query->row()->type;
- $this->db->select('kids.name as kid_name',FALSE);
- $this->db->select('kids.birth_date as kid_bd',FALSE);
- $this->db->select('kids.id as kid_id',FALSE);
- $this->db->select('kids.pic as kid_pic',FALSE);
- $this->db->select('kids.gender as kid_gender',FALSE);
- // $this->db->select('gans.id as gan_id',FALSE);
- // $this->db->select('gans.name as gan_name',FALSE);
- // $this->db->select('gans.max as gan_max',FALSE);
- // $this->db->select('gans.address as gan_address',FALSE);
- // $this->db->select('gans.gan_phone as gan_phone',FALSE);
- // $this->db->select('gans.cityName as gan_city',FALSE);
- // $this->db->select('gans.city as gan_city_id',FALSE);
- // $this->db->select('gans.code as gan_code',FALSE);
- // $this->db->select('classes.name as class_name',FALSE);
- // $this->db->select('classes.id as class_id',FALSE);
- // $this->db->select('users.id as teacher_id',FALSE);
- // $this->db->select('users.mail as teacher_mail',FALSE);
- // $this->db->select('users.first_name as teacher_first_name',FALSE);
- // $this->db->select('users.last_name as teacher_last_name',FALSE);
- // $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
- // $this->db->select('users.home_phone as teacher_phone',FALSE);
- $this->db->select('kids_classes.active as kid_active',FALSE);
- $this->db->select('kids_classes.year as class_year',FALSE);
- $this->db->select('kids_classes.class_id as class_id',FALSE);
- $this->db->order_by("kids_classes.year", "desc");
- $this->db->order_by("kids_classes.active", "asc");
- $this->db->from('kids');
- $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id','left');
- // $this->db->join('classes', 'kids_classes.class_id = classes.id','left');
- // $this->db->join('gans', 'kids_classes.gan_id = gans.id','left');
- // $this->db->join('users', 'users.gan_id = gans.id','left');
- $this->db->where('kids.parent_id', $parent_id);
- $query = $this->db->get();
- $kids = $query->result_array();
- if(count($kids) == 0)
- {
- return array();
- }
- $classes_ids = array();
- foreach ($kids as $k => $kid)
- {
- if(!in_array($kid['class_id'], $classes_ids))
- {
- $classes_ids[] = $kid['class_id'];
- }
- }
- /*************************************************************/
- $this->db->select('gans.id as gan_id',FALSE);
- $this->db->select('gans.name as gan_name',FALSE);
- $this->db->select('gans.max as gan_max',FALSE);
- $this->db->select('gans.address as gan_address',FALSE);
- $this->db->select('gans.gan_phone as gan_phone',FALSE);
- $this->db->select('gans.cityName as gan_city',FALSE);
- $this->db->select('gans.city as gan_city_id',FALSE);
- $this->db->select('gans.code as gan_code',FALSE);
- $this->db->select('classes.name as class_name',FALSE);
- $this->db->select('classes.id as class_id',FALSE);
- $this->db->select('users.id as teacher_id',FALSE);
- $this->db->select('users.mail as teacher_mail',FALSE);
- $this->db->select('users.first_name as teacher_first_name',FALSE);
- $this->db->select('users.last_name as teacher_last_name',FALSE);
- $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
- $this->db->select('users.home_phone as teacher_phone',FALSE);
- $this->db->from('classes');
- $this->db->join('gans', 'classes.gan_id = gans.id');
- $this->db->join('users', 'users.gan_id = gans.id');
- $this->db->where_in('classes.id', $classes_ids);
- $this->db->group_by('classes.id');
- $query = $this->db->get();
- $classes = $query->result_array();
- foreach ($kids as $k => $kid)
- {
- foreach ($classes as $c => $class)
- {
- if($kid['class_id'] == $class['class_id'])
- {
- $kids[$k] = array_merge($kids[$k],$class);
- }
- }
- }
- /*************************************************************/
- $new_kids_current_year = array();
- $new_kids_last_year = array();
- foreach ($kids as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR)
- {
- $new_kids_current_year[] = $kid;
- }
- else
- {
- $new_kids_last_year[] = $kid;
- }
- }
- foreach ($new_kids_current_year as $k => $kid)
- {
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- $v = $kid;
- unset($new_kids_current_year[$k]);
- $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
- $new_kids_current_year[$last_key+1] = $v;
- }
- }
- $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
- $new_kids = array();
- foreach ($new_kids_1 as $k => $kid)
- {
- $gan_city_id = $kid['gan_city_id'];
- $gan_city = $kid['gan_city'];
- if($gan_city_id > 0 && ($gan_city == null || $gan_city == "0"))
- {
- $new_kids_1[$k]['gan_city'] = $this->city_model->get_name($gan_city_id);
- }
- unset($kid['gan_city_id']);
- $kid['type'] = $type;
- if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $class_id = $kid['class_id'];
- $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
- $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
- $kid['unread_messages'] = (string)$unread_messages;
- $kid['unseen_photos'] = (string)$unseen_photos;
- $kid['vaad_type'] = $this->get_parent_type($parent_id,$class_id,$type);
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
- {
- $kid['current_year'] = CURRENT_YEAR;
- $new_kids[$kid['kid_id']] = $kid;
- }
- else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
- {
- if(
- isset($new_kids[$kid['kid_id']]['class_year']) &&
- ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
- isset($new_kids[$kid['kid_id']]['kid_active']) &&
- ($new_kids[$kid['kid_id']]['kid_active']) != '0'
- )
- {
- }
- else
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
- {
- if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
- {
- $kid_details = $this->set_kid_array_values($kid,false);
- $new_kids[$kid['kid_id']] = $kid_details;
- }
- if($kid['kid_active'] != '0')
- {
- $permissions = $this->get_permissions_for_gan($kid['gan_id']);
- $kid = array_merge($kid, $permissions);
- $history = $this->set_kid_array_values($kid,true);
- $new_kids[$kid['kid_id']]['history'][] = $history;
- }
- }
- else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
- {
- $kid = $this->set_kid_array_values($kid);
- $new_kids[$kid['kid_id']] = $kid;
- }
- }
- return array_values($new_kids);
- }
- function generateAuthToken($userid) {
- $token = hash('sha256', $userid . rand() . time());
- return $token;
- }
- function updateAuthToken($userid, $newToken) {
- if(isset($userid) && isset($newToken)) {
- $this->db->where('id', $userid);
- $data = array(
- 'auth_token' => $newToken
- );
- $this->db->update('users', $data);
- }
- }
- function checkPermissions($authToken, $method_name, $method_full, $anonymous_functions, $teacher_functions, $unsigned_functions, $args) {
- if(!isset($method_name) || $method_name == "") {
- error_log("*** No method");
- return false;
- }
- if(in_array($method_name, $anonymous_functions)) {
- error_log("*** passing anonymous function: " . $method_name);
- return true;
- }
- //$authToken = 'bla'; //'ivo123';
- if(!isset($authToken) || $authToken == "") {
- error_log("*** No auth token");
- return false;
- }
- if(($authToken == '7caae84146a63264b33rfu562677062f9dfa3c70e3311eac4edd23d85ea542de' && $_SERVER['REMOTE_ADDR'] == '188.226.178.75') || ($method_name == 'getfavorite' && $_SERVER['REMOTE_ADDR'] == '188.226.178.75')) {
- error_log("*** Allowing dashboard calls");
- return true;
- }
- $this->db->select('id',FALSE);
- $this->db->select('mail',FALSE);
- $this->db->select('gan_id',FALSE);
- $this->db->select('type',FALSE);
- $this->db->select('active',FALSE);
- $this->db->select('activation',FALSE);
- $this->db->select('auth_token',FALSE);
- $query = $this->db->get_where('users',array('auth_token' => $authToken));
- if($query->num_rows() <= 0) {
- error_log("*** No user for token: " . $authToken);
- return false;
- }
- $user = reset($query->result_array());
- //error_log(print_r($user, TRUE));
- if($user["type"] != 2 && in_array($method_name, $teacher_functions)) {
- error_log("*** User with auth token " . $authToken . " is NOT a teacher");
- return false;
- }
- $query->free_result();
- $classes = Array();
- $kids = Array();
- if($method_name != "getkindergarten" && isset($args["user_id"]) && $args["user_id"] != "") {
- $user_id = $args["user_id"];
- if(!in_array($method_name, $unsigned_functions)) {
- $user_id = $this->util_model->get_decrypt_user_id($user_id);
- }
- if($user_id != $user["id"]) {
- error_log("*** Wrong caller id");
- return false;
- }
- }
- // load classes
- if($user["gan_id"] != null) {
- $this->db->select('id', FALSE);
- $query = $this->db->get_where('classes', Array('gan_id' => $user["gan_id"]));
- if($query->num_rows() > 0) {
- foreach ($query->result_array() as $row) {
- $classes[] = $row["id"];
- }
- }
- $query->free_result();
- if(count($classes) > 0) {
- $this->db->select('kid_id',FALSE);
- $this->db->distinct();
- $this->db->from('kids_classes');
- $this->db->where_in('class_id', $classes);
- $query = $this->db->get();
- if($query->num_rows() > 0) {
- foreach ($query->result_array() as $row) {
- $kids[] = $row["kid_id"];
- }
- }
- $query->free_result();
- }
- } else {
- $this->db->select('id', FALSE);
- $query = $this->db->get_where('kids', Array('parent_id' => $user["id"]));
- if($query->num_rows() > 0) {
- foreach ($query->result_array() as $row) {
- $kids[] = $row["id"];
- }
- }
- $query->free_result();
- if(count($kids) > 0) {
- $this->db->select('class_id',FALSE);
- $this->db->distinct();
- $this->db->from('kids_classes');
- $this->db->where_in('kid_id', $kids);
- $query = $this->db->get();
- if($query->num_rows() > 0) {
- foreach ($query->result_array() as $row) {
- $classes[] = $row["class_id"];
- }
- }
- $query->free_result();
- }
- }
- //error_log(print_r($classes, TRUE));
- //error_log(print_r($kids, TRUE));
- if(isset($args["class_id"]) && $args["class_id"] != "" && $method_name != 'setclass' && !in_array($args["class_id"], $classes)) {
- error_log("*** Wrong class");
- return false;
- }
- if(isset($args["kid_id"]) && $args["kid_id"] != "" && !in_array($args["kid_id"], $kids)) {
- error_log("*** Wrong kid");
- return false;
- }
- if(isset($args["gan_id"]) && $args["gan_id"] != "" && $args["gan_id"] != $user["gan_id"]) {
- error_log("*** Wrong gan id");
- return false;
- }
- if(isset($args["parent_id"]) && $args["parent_id"] != "") {
- $parent_id = $args["parent_id"];
- if(!in_array($method_name, $unsigned_functions)) {
- $parent_id = $this->util_model->get_decrypt_user_id($parent_id);
- }
- if($parent_id != $user["id"]) {
- error_log("*** Wrong parent id");
- return false;
- }
- }
- if(isset($args["album_id"]) && $args["album_id"] != "") {
- $this->db->select('class_id', FALSE);
- $query = $this->db->get_where('albums',array('id' => $args["album_id"]));
- if($query->num_rows() <= 0) {
- error_log("*** No such album: " . $args["album_id"]);
- return false;
- }
- $album = reset($query->result_array());
- if(!in_array($album["class_id"], $classes)) {
- error_log("*** No permissions for album: " . $args["album_id"]);
- return false;
- }
- $query->free_result();
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement