Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 94.37 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class User_model extends CI_Model
  4. {
  5. function set_parent_permission($parent_ids,$class_id)
  6. {
  7. // $this->db->select('users.id as id',FALSE);
  8. // $this->db->select('users.type as type',FALSE);
  9.  
  10. // $this->db->from('users');
  11.  
  12. // $this->db->where_in('users.id', array_keys($parent_ids));
  13.  
  14. // $users = $this->db->get()->result_array();
  15.  
  16. // $update = array();
  17.  
  18. // foreach($users as $u => $user)
  19. // {
  20. // $type = $user['type'];
  21. // $active = $parent_ids[$user['id']];
  22.  
  23. // $new_type = "";
  24.  
  25. // if($active == "1")
  26. // {
  27. // if($type == "3")
  28. // {
  29. // $new_type = "5";
  30. // }
  31. // else if($type == "4")
  32. // {
  33. // $new_type = "6";
  34. // }
  35. // else if($type == "1")
  36. // {
  37. // $new_type = "7";
  38. // }
  39. // }
  40. // else if($active == "0")
  41. // {
  42. // if($type == "5")
  43. // {
  44. // $new_type = "3";
  45. // }
  46. // else if($type == "6")
  47. // {
  48. // $new_type = "4";
  49. // }
  50. // else if($type == "7")
  51. // {
  52. // $new_type = "1";
  53. // }
  54. // }
  55.  
  56. // $update[] = array("id"=>$user['id'],"type"=>$new_type);
  57. // }
  58.  
  59. $new_pta = array();
  60.  
  61. foreach($parent_ids as $u => $user)
  62. {
  63. $data = array();
  64. $type = $parent_ids[$u];
  65.  
  66. $data['user_id'] = $u;
  67. $data['class_id'] = $class_id;
  68. $data['vaad_type'] = $type;
  69. $data['year'] = CURRENT_YEAR;
  70.  
  71. if($type == '0')
  72. {
  73. $data['active'] = '0';
  74. }
  75. else
  76. {
  77. $data['active'] = '1';
  78. }
  79.  
  80. if($data['active'] == '0')
  81. {
  82. $res = $this->db->update('classes_vaad',$data,array('user_id'=>$data['user_id'],'class_id'=>$data['class_id']));
  83. if(!$res)
  84. {
  85. //return false;
  86. }
  87. }
  88. else
  89. {
  90. $query = $this->db->get_where('classes_vaad',array('user_id'=>$data['user_id'],'class_id'=>$data['class_id'], 'active'=>'0'));
  91. $count = $query->num_rows(); //counting result from query
  92.  
  93. if ($count === 0)
  94. {
  95. $query = $this->db->get_where('classes_vaad',array('user_id'=>$data['user_id'],'class_id'=>$data['class_id'], 'active'=>'1'));
  96. $count = $query->num_rows(); //counting result from query
  97.  
  98. if ($count === 0)
  99. {
  100. $res = $this->db->insert('classes_vaad', $data);
  101. $new_pta[$data['user_id']] = '1';
  102. }
  103. else
  104. {
  105. //return false;
  106. }
  107. }
  108. else
  109. {
  110. $res = $this->db->update('classes_vaad',$data,array('user_id'=>$data['user_id'],'class_id'=>$data['class_id']));
  111. $new_pta[$data['user_id']] = '1';
  112. }
  113.  
  114. if(!$res)
  115. {
  116. //return false;
  117. }
  118. }
  119. }
  120.  
  121. return $new_pta;
  122. }
  123.  
  124. function updatepassword($id,$old_password,$new_password,$api_key)
  125. {
  126. $this->db->select('password',FALSE);
  127.  
  128. $this->db->from('users');
  129.  
  130. $this->db->where('users.id', $id);
  131.  
  132. $password = $this->db->get()->row()->password;
  133.  
  134. if(sha1($old_password) !== $password)
  135. {
  136. return false;
  137. }
  138. else
  139. {
  140. $res = $this->check_password($new_password);
  141. if(is_string($res))
  142. {
  143. return $res;
  144. }
  145. else
  146. {
  147. $this->db->update('users',array('password'=>sha1($new_password),'api_key'=>$api_key),array('id'=>$id));
  148.  
  149. if($this->db->affected_rows() > 0)
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return '-1';
  156. }
  157. }
  158. }
  159. }
  160.  
  161. function send_pta_push($parent_ids,$class_id,$app_name)
  162. {
  163. $tokens = $this->token_model->get_teachers_tokens($parent_ids);
  164. $gan_class_name = $this->class_model->get_class_gan_name($class_id);
  165. $class_name = $gan_class_name['class_name'];
  166.  
  167. $loc_args = array($class_name);
  168.  
  169. $acme = array("cls_id" => $class_id);
  170.  
  171. if($app_name == GB || $app_name == null)
  172. {
  173. $text = PUSH_PTA_HE;
  174. }
  175. else
  176. {
  177. $text = PUSH_PTA_EN;
  178. }
  179.  
  180. $text = str_replace('CLASS_NAME',$class_name,$text);
  181.  
  182. $users_ids = array();
  183.  
  184. foreach ($parent_ids as $p=>$p_id)
  185. {
  186. $users_ids[(string)$p_id] = array("cls_id" => $class_id);
  187. }
  188.  
  189. // file_put_contents("/opt/app/current/testFile.txt","daa". print_r($users_ids, true));
  190.  
  191. $this->message_model->send_push($tokens,$users_ids,$text,$class_id,PTA1_KEY,$loc_args,$acme,null,$app_name);
  192. }
  193.  
  194. function compare_old_password($mail,$old_pass)
  195. {
  196. $this->db->select('password',FALSE);
  197.  
  198. $this->db->from('users');
  199.  
  200. $this->db->where('users.mail', $mail);
  201.  
  202. $password = $this->db->get()->row()->password;
  203.  
  204. if(sha1($old_pass) !== $password)
  205. {
  206. return false;
  207. }
  208. else
  209. {
  210. return true;
  211. }
  212. }
  213.  
  214. function update_fail_attempts()
  215. {
  216. $ip = $this->get_ip_address();
  217.  
  218. $query = $this->db->get_where('users_login_attempts',array('ip'=>$ip));
  219. $count = $query->num_rows(); //counting result from query
  220.  
  221. if ($count === 0) // not exist
  222. {
  223. $res = $this->db->insert('users_login_attempts', array('ip'=>$ip,'attempts'=>1,'time'=>date("Y-m-d H:i:s")));
  224. return true;
  225. }
  226. else
  227. {
  228. $attempts = $query->row()->attempts;
  229.  
  230. $attempts ++;
  231.  
  232. $data['attempts'] = $attempts;
  233.  
  234. if($attempts >= BLOCK_ATTEMPTS)
  235. {
  236. $data['time'] = date("Y-m-d H:i:s");
  237. }
  238.  
  239. $res = $this->db->update('users_login_attempts',$data,array('ip'=>$ip));
  240.  
  241. if($attempts >= BLOCK_ATTEMPTS)
  242. {
  243. return false;
  244. }
  245. else
  246. {
  247. return true;
  248. }
  249. }
  250. }
  251.  
  252. function check_block_time()
  253. {
  254. $ip = $this->get_ip_address();
  255. $query = $this->db->get_where('users_login_attempts',array('ip'=>$ip));
  256.  
  257. if ($count === 0) // not exist
  258. {
  259. return true;
  260. }
  261. else
  262. {
  263. $attempts = $query->row()->attempts;
  264.  
  265. if($attempts >= BLOCK_ATTEMPTS)
  266. {
  267. // if block time didnt pass --> error message
  268. $attempt_time = $query->row()->time;
  269. $now = date("Y-m-d H:i:s");
  270.  
  271. $attempt_time_time = strtotime($attempt_time);
  272. $now_time = strtotime($now);
  273.  
  274. $diff = $now_time - $attempt_time_time;
  275.  
  276. if(intval($diff) < intval(BLOCK_TIME))
  277. {
  278. return false;
  279. }
  280. else
  281. {
  282. //else delete from table and return true
  283. $this->db->delete('users_login_attempts',array('ip'=>$ip));
  284. return true;
  285. }
  286. }
  287. else
  288. {
  289. return true;
  290. }
  291. }
  292. }
  293.  
  294. function get_kids_2014_no_2015()
  295. {
  296. $this->db->select('distinct kids.id',FALSE);
  297. $this->db->select('users.id as user_id',FALSE);
  298. $this->db->select('users.first_name as user_name',FALSE);
  299. $this->db->select('kids.name as kid_name',FALSE);
  300. $this->db->select('gans.name as gan_name',FALSE);
  301. $this->db->select('classes.name as class_name',FALSE);
  302. $this->db->select('users.mail',FALSE);
  303. $this->db->select('users.mail_key as mail_key',FALSE);
  304. $this->db->select('users.first_name',FALSE);
  305. $this->db->select('kids_classes_2014.year as class_year_2014',FALSE);
  306. $this->db->select('tokens.type',FALSE);
  307. // $this->db->select('kids_classes_2015.year as class_year_2015',FALSE);
  308.  
  309. $this->db->from('kids');
  310.  
  311. $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');
  312. $this->db->join('classes', 'kids_classes_2014.class_id = classes.id');
  313. $this->db->join('gans', 'classes.gan_id = gans.id');
  314. $this->db->join('users', 'users.id = kids.parent_id');
  315. $this->db->join('tokens', 'tokens.user_id = users.id');
  316. // $this->db->join('kids_classes as kids_classes_2015', 'kids.id = kids_classes_2015.kid_id and kids_classes_2015.year = 2015','left');
  317.  
  318. // $this->db->where('kids_classes_2014.class_id', '1839');
  319. $this->db->where('tokens.type', 'android');
  320. // $this->db->where('kids_classes_2015.class_id', '1839');
  321.  
  322. // $this->db->where('users.id', '3977');
  323.  
  324. $kids = $this->db->get()->result_array();
  325.  
  326. $kids_new = array();
  327.  
  328. foreach($kids as $kid)
  329. {
  330. $query = $this->db->get_where('kids_classes',array('kid_id' => $kid['id'],'year' => '2015'));
  331.  
  332. if($query->num_rows() == 0) // not in 2015 - it's good
  333. {
  334. $kids_new[] = $kid;
  335. }
  336. }
  337.  
  338. $kids_new_mail = array();
  339.  
  340. foreach($kids_new as $kid_new)
  341. {
  342. $kids_new_mail[$kid_new['mail']][] = $kid_new;
  343. }
  344.  
  345. $arr = array();
  346.  
  347. foreach($kids_new_mail as $kid_new_mail)
  348. {
  349. $new_arr = array();
  350. for ($i=0; $i < count($kid_new_mail); $i++)
  351. {
  352. $new_arr['email'] = $kid_new_mail[$i]['mail'];
  353. $new_arr['code'] = $kid_new_mail[$i]['mail_key'];
  354. $new_arr['name'] = $kid_new_mail[$i]['user_name'];
  355.  
  356. $new_arr['gan_name'.$i] = $kid_new_mail[$i]['gan_name'];
  357. $new_arr['class_name'.$i] = $kid_new_mail[$i]['class_name'];
  358. $new_arr['kid_name'.$i] = $kid_new_mail[$i]['kid_name'];
  359. }
  360.  
  361. if(count($kid_new_mail) < 3)
  362. {
  363. for ($i=count($kid_new_mail); $i < 3; $i++)
  364. {
  365. $new_arr['gan_name'.$i] = ' ';
  366. $new_arr['class_name'.$i] = ' ';
  367. $new_arr['kid_name'.$i] = ' ';
  368. }
  369. }
  370.  
  371. $arr[] = $new_arr;
  372.  
  373. }
  374.  
  375. // foreach($kids_new as $kid_new)
  376. // {
  377. // $this->db->where('id', $kid_new['user_id']);
  378.  
  379. // $mail_key = md5(uniqid(rand(), true));
  380.  
  381. // $this->db->update('users', array("mail_key" => $mail_key));
  382. // }
  383.  
  384. $count = count($kids_new);
  385.  
  386. // return array('count'=>$count,'kids'=>$kids_new);
  387. return $arr;
  388. }
  389.  
  390. function update_last_seen($id)
  391. {
  392. $date_str = date("Y-m-d H:i:s");
  393.  
  394. $this->db->select('last_seen',FALSE);
  395. $this->db->select('type',FALSE);
  396.  
  397. $query = $this->db->get_where('users',array('id' => $id));
  398.  
  399. $type = $query->row()->type;
  400.  
  401. if($type == "2" && $id == "12022")
  402. {
  403. $last_seen = $query->row()->last_seen;
  404.  
  405. $last_seen_date = strtotime($last_seen);
  406.  
  407. $date = strtotime($date_str);
  408.  
  409. // $diff = $last_seen_date->diff($date);
  410. if((intval($date) - intval($last_seen_date)) >= 600) // more then 10 sec
  411. {
  412. return false;
  413. }
  414. else
  415. {
  416. $this->update_last_seen_no_check($id,$date_str);
  417. return true;
  418. }
  419. }
  420. else
  421. {
  422. $this->update_last_seen_no_check($id,$date_str);
  423. return true;
  424. }
  425. }
  426.  
  427. function update_last_seen_no_check($id,$date_str)
  428. {
  429. $this->db->where('id', $id);
  430. $this->db->update('users', array("last_seen" => $date_str));
  431. }
  432.  
  433. function register_retention($mail,$token_id)
  434. {
  435. $query = $this->db->get_where('retention',array('mail' => $mail,'token_id' => $token_id));
  436.  
  437. if($query->num_rows() == 0)
  438. {
  439. $date = date("Y-m-d H:i:s");
  440. $this->db->insert('retention', array('mail' => $mail,'token_id' => $token_id,"active" => 1,'created' => $date));
  441. return $this->db->insert_id();
  442. }
  443. else
  444. {
  445. return false;
  446. }
  447. }
  448.  
  449. function delete_retention($mail)
  450. {
  451. $this->db->where('mail', $mail);
  452. $this->db->update('retention', array("active" => 0));
  453. return $this->db->affected_rows() > 0;
  454. }
  455.  
  456. function encrypt_password()
  457. {
  458. $this->db->from('users');
  459.  
  460. $query = $this->db->get();
  461.  
  462. foreach ($query->result() as $row)
  463. {
  464. $id = $row->id;
  465. $password = $row->password;
  466.  
  467. $new_password = sha1($password);
  468.  
  469. $this->db->where('id', $id);
  470.  
  471. $data = array(
  472. 'password' => $new_password
  473. );
  474.  
  475. $this->db->update('users', $data);
  476. }
  477.  
  478. }
  479.  
  480. function insert($data)
  481. {
  482. $this->db->insert('users', $data);
  483. }
  484.  
  485. function forgetpass($mail,$os=null,$key)
  486. {
  487. $this->db->select('password',FALSE);
  488. $query = $this->db->get_where('users',array('mail' => $mail));
  489.  
  490. if($query->num_rows() > 0)
  491. {
  492.  
  493. $res = $query->result_array();
  494. $pass = $query->row()->password;
  495.  
  496. $this->db->where('mail', $mail);
  497.  
  498. $data = array(
  499. 'password_key' => $key
  500. );
  501.  
  502. $this->db->update('users', $data);
  503.  
  504. return $pass;
  505. }
  506. else
  507. {
  508. if($os != null)
  509. {
  510. $pass = "emptypassword";
  511. return $pass;
  512. }
  513. else
  514. {
  515. return false;
  516. }
  517. }
  518.  
  519. }
  520.  
  521. function login($mail,$password)
  522. {
  523. $this->db->select('id',FALSE);
  524. $this->db->select('first_name',FALSE);
  525. $this->db->select('last_name',FALSE);
  526. $this->db->select('mobile',FALSE);
  527. $this->db->select('phone',FALSE);
  528. $this->db->select('address',FALSE);
  529. $this->db->select('cityName',FALSE);
  530. $this->db->select('type',FALSE);
  531. $this->db->select('active',FALSE);
  532. $this->db->select('activation',FALSE);
  533.  
  534. $query = $this->db->get_where('users',array('mail' => $mail, 'password' => sha1($password)));
  535.  
  536. if($query->num_rows() > 0)
  537. {
  538.  
  539. $res = $query->result_array();
  540.  
  541.  
  542. $type = $query->row()->type;
  543.  
  544. if($type == '1')
  545. {
  546. $res[0]['active'] = '0';
  547.  
  548. $id = $query->row()->id;
  549.  
  550. $this->db->select('COUNT(distinct kids_classes.kid_id) AS count',FALSE);
  551.  
  552. $this->db->from('users');
  553.  
  554. $this->db->join('kids', 'kids.parent_id = users.id ');
  555. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id ');
  556.  
  557. $this->db->where('users.id', $id);
  558. $this->db->where('kids_classes.active', 1);
  559.  
  560. $query2 = $this->db->get();
  561.  
  562. $count = $query2->row()->count;
  563.  
  564. if($count > 0)
  565. {
  566. $res[0]['active'] = '1';
  567. }
  568. return $res;
  569. }
  570. else
  571. {
  572. return $res;
  573. }
  574. }
  575. else
  576. {
  577. return '';
  578. }
  579.  
  580. }
  581.  
  582.  
  583. function post_login($query,$token_id,$gmt,$app_name,$data,$os_name,$app_version,$object_id)
  584. {
  585. $user = false;
  586.  
  587. if($query)
  588. {
  589. $user = $this->login_new( $query );
  590. //error_log("*** POST LOGIN2 user " . print_r($user, TRUE));
  591.  
  592. if($user)
  593. {
  594. //create token
  595. // $token_id = $this->post('token_id');
  596. // $gmt = $this->post('gmt');
  597. // $app_name = $this->post('app_name');
  598.  
  599.  
  600. $user_id_key = isset($user["parent_id"]) ? "parent_id" : "teacher_id";
  601. $user_id = isset($user["parent_id"]) ? $user["parent_id"] : $user["teacher_id"];
  602.  
  603. $user_data = array("app_name" => $app_name);
  604.  
  605. $this->updateuser($user_id,$user_data);
  606.  
  607. // $data = array(
  608. // 'device_vendor' => $this->post('device_vendor'),
  609. // 'device_model' => $this->post('device_model'),
  610. // 'os_name' => $this->post('os_name'),
  611. // 'os_version' => $this->post('os_version'),
  612. // 'scale' => $this->post('scale'),
  613. // 'app_version' => $this->post('app_version'),
  614. // 'user_id' => $user_id,
  615. // 'active' => '1',
  616. // 'gmt' => $this->post('gmt')
  617. // );
  618.  
  619. // $this->token_model->update_token($token_id,array("user_id" => $user_id, "active" => '1', "gmt" => $gmt));
  620.  
  621. if($object_id != "")
  622. {
  623. $this->token_model->update_parse_token_login((string)$object_id,(string)$user_id,(string)$token_id);
  624. }
  625.  
  626. $data['user_id'] = $user_id;
  627.  
  628. if($token_id == '')
  629. {
  630. $token_id = $this->token_model->create_token($data);
  631. $user['token_id'] = $token_id;
  632. }
  633. else
  634. {
  635. $update_res = $this->token_model->update_token($token_id,$data);
  636.  
  637. if(!$update_res)
  638. {
  639.  
  640. file_put_contents('/opt/app/current/log_update_token.txt', print_r(array("token_id"=>$token_id,"data"=>$data), true), FILE_APPEND);
  641. }
  642. }
  643.  
  644. $user[$user_id_key] = $this->util_model->get_encrypt_user_id($user_id);
  645.  
  646. $user['update'] = (string)$this->version_model->get_update($os_name,$app_version);
  647.  
  648. $user['user_id'] = $user_id;
  649.  
  650. // 200 being the HTTP response code
  651. }
  652. else
  653. {
  654. $user = false;
  655. // $this->response(array("errors"=>array("code"=>"9000","msg"=>"Wrong User Name or Password")), 400);
  656. }
  657. }
  658. else
  659. {
  660. $user = false;
  661. // $this->response(array("errors"=>array("code"=>"9000","msg"=>"Wrong User Name or Password")), 400);
  662. }
  663.  
  664. return $user;
  665. }
  666.  
  667. function user_exists($mail,$password,$migrate=false)
  668. {
  669. $double_check = 0;
  670. $password_to_check = sha1($password);
  671.  
  672. while ($double_check < 2)
  673. {
  674. $query = $this->db->get_where('users',array('mail' => $mail, 'password' => $password_to_check ));
  675.  
  676. if($query->num_rows() > 0)
  677. {
  678. return $query;
  679. }
  680. else
  681. {
  682. if(!$migrate)
  683. {
  684. return false;
  685. }
  686. else
  687. {
  688. $password_to_check = $password;
  689. $double_check++;
  690. }
  691. }
  692.  
  693. }
  694.  
  695. return false;
  696. }
  697.  
  698. function user_exists_migrate_android($user_id)
  699. {
  700. $query = $this->db->get_where('users',array('id' => $user_id ));
  701.  
  702. if($query->num_rows() > 0)
  703. {
  704. return $query;
  705. }
  706. else
  707. {
  708. return false;
  709. }
  710. }
  711.  
  712. function login_new($query)
  713. {
  714. // $query = $this->db->get_where('users',array('mail' => $mail, 'password' => sha1($password) ));
  715. // $query = $this->user_exists($mail,$password);
  716.  
  717. // if($query)
  718. // {
  719. // $res = $query->result_array();
  720.  
  721. // $activation = $query->row()->activation;
  722.  
  723. // if($activation != null)
  724. // {
  725. // return -1;
  726. // }
  727.  
  728. $api_key = $query->row()->api_key;
  729. $id = $query->row()->id;
  730.  
  731. if($api_key == null)
  732. {
  733. $api_key = md5(uniqid(rand(), true));
  734. $this->updateuser($id,array('api_key' => $api_key));
  735. }
  736.  
  737. $type = $query->row()->type;
  738.  
  739. if($type == '2')
  740. {
  741. $idStr = " as teacher_id";
  742. $fnStr = " as teacher_first_name";
  743. $lnStr = " as teacher_last_name";
  744. $mobileStr = " as teacher_mobile";
  745. $phoneStr = " as teacher_phone";
  746. $addressStr = " as teacher_address";
  747. $cityStr = " as teacher_city";
  748. $mailStr = " as teacher_mail";
  749. }
  750. else
  751. {
  752. $idStr = " as parent_id";
  753. $fnStr = " as parent_first_name";
  754. $lnStr = " as parent_last_name";
  755. $mobileStr = " as parent_mobile";
  756. $phoneStr = " as parent_phone";
  757. $addressStr = " as parent_address";
  758. $cityStr = " as parent_city";
  759. $mailStr = " as parent_mail";
  760. }
  761.  
  762. $this->db->select('id'.$idStr,FALSE);
  763. $this->db->select('mail'.$mailStr,FALSE);
  764. $this->db->select('first_name'.$fnStr,FALSE);
  765. $this->db->select('last_name'.$lnStr,FALSE);
  766. // $this->db->select('area_code',FALSE);
  767. $this->db->select('mobile_phone'.$mobileStr,FALSE);
  768. $this->db->select('home_phone'.$phoneStr,FALSE);
  769. $this->db->select('address'.$addressStr,FALSE);
  770. $this->db->select('cityName'.$cityStr,FALSE);
  771. $this->db->select('city',FALSE);
  772. $this->db->select('type',FALSE);
  773. $this->db->select('api_key',FALSE);
  774.  
  775. $this->db->from('users');
  776.  
  777. // $this->db->where(array('mail' => $mail, 'password' => sha1($password)));
  778. // $this->db->where('mail', $mail);
  779. // $this->db->where('password', sha1($password));
  780.  
  781. $this->db->where('id', $id);
  782.  
  783. $query = $this->db->get();
  784. $user = reset($query->result_array());
  785.  
  786. // if($user['type'] == 2)
  787. // {
  788. $user['current_year'] = CURRENT_YEAR;
  789. // }
  790.  
  791. // $user_ac = $user['area_code'];
  792. // if($user_ac > 0)
  793. // {
  794. // if($user['type'] == 2)
  795. // {
  796. // $phone_index = 'teacher_phone';
  797. // $mobile_index = 'teacher_mobile';
  798. // }
  799. // else
  800. // {
  801. // $phone_index = 'parent_phone';
  802. // $mobile_index = 'parent_mobile';
  803. // }
  804.  
  805. // // if($user['type'] == 2)
  806. // // {
  807. // // $phone = $user['teacher_phone'];
  808. // // $mobile = $user['teacher_mobile'];
  809. // // }
  810. // // else
  811. // // {
  812. // // $phone = $user['parent_phone'];
  813. // // $mobile = $user['parent_mobile'];
  814. // // }
  815.  
  816. // $phone = $user[$phone_index];
  817. // $mobile = $user[$mobile_index];
  818. // $res = $this->set_mobile_phone($user_ac,$mobile,$phone);
  819.  
  820. // $user[$mobile_index] = $res['mobile'];
  821. // $user[$phone_index] = $res['phone'];
  822. // }
  823.  
  824. // unset($user['area_code']);
  825.  
  826. //city from old users
  827. $city_id = $user['city'];
  828. $cityName = $user['parent_city'];
  829. if($user['type'] == 2)
  830. {
  831. $cityName = $user['teacher_city'];
  832. }
  833.  
  834. if($city_id > 0 && ($cityName == null || $cityName == "0"))
  835. {
  836. if($user['type'] == 2)
  837. {
  838. $user['teacher_city'] = $this->city_model->get_name($city_id);
  839. }
  840. else
  841. {
  842. $user['parent_city'] = $this->city_model->get_name($city_id);
  843. }
  844. }
  845. unset($user['city']);
  846.  
  847. return $user;
  848.  
  849. // }
  850. // else
  851. // {
  852. // return false;
  853. // }
  854. }
  855.  
  856. function fix_users_mobile_phone()
  857. {
  858. $this->db->from('users');
  859.  
  860. $this->db->where('users.id >=','10933');
  861.  
  862. $users = $this->db->get()->result_array();
  863.  
  864. foreach($users as $u=>$user)
  865. {
  866. $arr = $this->set_mobile_phone($user['area_code'],$user['mobile'],$user['phone']);
  867. // $users[$u]['mobile'] = $arr['mobile'];
  868. // $users[$u]['phone'] = $arr['phone'];
  869.  
  870. $this->db->where('id', $user['id']);
  871. $this->db->update('users', $arr);
  872. }
  873.  
  874. // $this->db->update_batch('users',$users,'id');
  875. }
  876.  
  877. function fix_users_mobile_gan()
  878. {
  879. $this->db->from('gans');
  880.  
  881. $this->db->where('id >=','10933');
  882.  
  883. $gans = $this->db->get()->result_array();
  884.  
  885. foreach($gans as $g=>$gan)
  886. {
  887. $arr = $this->set_mobile_phone($gan['area_code'],$gan['mobile'],$gan['phone'],false);
  888. // $users[$u]['mobile'] = $arr['mobile'];
  889. // $users[$u]['phone'] = $arr['phone'];
  890.  
  891. $this->db->where('id', $gan['id']);
  892. $this->db->update('gans', $arr);
  893. }
  894.  
  895. // $this->db->update_batch('users',$users,'id');
  896. }
  897.  
  898. function set_mobile_phone($area_code,$mobile,$phone_num,$users=true)
  899. {
  900. $this->db->from('phones');
  901. $phonesarr = $this->db->get()->result_array();
  902.  
  903. //if phone starts with 050,054...
  904. $phone_start_with_ac = null;
  905. foreach ($phonesarr as $phone)
  906. {
  907. if($this->startsWith($phone_num,$phone['name']))
  908. {
  909. $phone_start_with_ac = $phone['id'];
  910. }
  911. }
  912.  
  913. if($phone_start_with_ac >= 12)
  914. {
  915. $mobile = $phone_num;
  916. $phone_num = null;
  917. }
  918. // else if ($phone_start_with_ac < 12)
  919. // {
  920.  
  921. // }
  922. else if($phone_start_with_ac == null)
  923. {
  924. $phones = array();
  925.  
  926. foreach ($phonesarr as $phone)
  927. {
  928. $phones[$phone['id']] = $phone['name'];
  929. }
  930.  
  931. $ac_name = $phones[$area_code];
  932. if($area_code >= 12)
  933. {
  934. if(!$this->startsWith($mobile,"0"))
  935. {
  936. $mobile = $ac_name.$phone_num;
  937. $phone_num = null;
  938. }
  939. }
  940. else
  941. {
  942. if(!$this->startsWith($phone_num,"0"))
  943. {
  944. $phone_num = $ac_name.$phone_num;
  945. }
  946. else
  947. {
  948. $phone_ac = substr($phone_num, 0, 3);
  949. $phone_ac_id = $this->phone_model->get_id($phone_ac);
  950.  
  951. if($phone_ac_id >=12) //mobile
  952. {
  953. $mobile = $phone_num;
  954. $phone_num = null;
  955. }
  956. }
  957. }
  958. }
  959.  
  960. if($users)
  961. {
  962. return array("mobile_phone"=>$mobile,"home_phone"=>$phone_num);
  963. }
  964. else
  965. {
  966. $number = ($phone_num == null) ? $mobile : $phone_num;
  967. return array("gan_phone"=>$number);
  968. }
  969. }
  970.  
  971. function getKids($id)
  972. {
  973. $this->db->select('gans.id as gan_id',FALSE);
  974. $this->db->select('gans.name as gan_name',FALSE);
  975. $this->db->select('gans.pic as pic',FALSE);
  976. $this->db->select('gans.max as gan_max',FALSE);
  977. $this->db->select('gans.code as gan_code',FALSE);
  978. $this->db->select('classes.name as class_name',FALSE);
  979. $this->db->select('classes.id as class_id',FALSE);
  980. $this->db->select('kids.name as kid_name',FALSE);
  981. $this->db->select('kids.birth_date as birth_date',FALSE);
  982. $this->db->select('kids.id as kid_id',FALSE);
  983. $this->db->select('kids.pic as kid_pic',FALSE);
  984. $this->db->select('kids_classes.active as kid_active',FALSE);
  985. $this->db->select('users.first_name as first_name',FALSE);
  986.  
  987. $this->db->from('users');
  988.  
  989. $this->db->join('kids', 'kids.parent_id = users.id');
  990. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
  991. $this->db->join('classes', 'kids_classes.class_id = classes.id');
  992. $this->db->join('gans', 'kids_classes.gan_id = gans.id');
  993.  
  994. $this->db->where('users.id', $id);
  995.  
  996. // $this->db->group_by('kids.id');
  997.  
  998. $query = $this->db->get();
  999.  
  1000. return $query->result_array();
  1001. }
  1002.  
  1003.  
  1004. function get_user_kids_new($parent_id){
  1005.  
  1006.  
  1007. $q='SELECT B.kid_name,
  1008. B.kid_bd ,
  1009. B.kid_id ,
  1010. B.kid_pic ,
  1011. B.kid_gender ,
  1012. B.gan_id ,
  1013. B.gan_name ,
  1014. B.gan_max ,
  1015. B.gan_address ,
  1016. B.gan_phone ,
  1017. B.gan_city ,
  1018. B.gan_code ,
  1019. B.class_name ,
  1020. B.class_id ,
  1021. B.teacher_id ,
  1022. B.teacher_mail ,
  1023. B.teacher_first_name ,
  1024. B.teacher_last_name ,
  1025. B.teacher_mobile ,
  1026. B.teacher_phone ,
  1027. B.kid_active ,
  1028. B.class_year ,
  1029.  
  1030. C.sent_messages-C.read_messages as unread_messaages ,
  1031. B.vaad_type
  1032. FROM
  1033. (SELECT A.*,`gans`.`name` as gan_name,
  1034. gans.max as gan_max,
  1035. gans.address as gan_address,
  1036. gans.gan_phone,
  1037. gans.cityName as gan_city,
  1038. gans.code as gan_code,
  1039. classes.name as class_name,
  1040. users.id as teacher_id,
  1041. users.mail as teacher_mail,
  1042. users.first_name as teacher_first_name,
  1043. users.last_name as teacher_last_name,
  1044. users.mobile_phone as teacher_mobile,
  1045. users.home_phone as teacher_phone,
  1046.  
  1047. classes_vaad.vaad_type
  1048. FROM (
  1049. SELECT `kids`.`name` as kid_name,
  1050. kids.birth_date as kid_bd,
  1051. `kids`.`id` as kid_id,
  1052. `kids`.`pic` as kid_pic,
  1053. `kids`.`parent_id`,
  1054. `kids`.`active`,
  1055. `kids`.`gender` as kid_gender,
  1056. `kids_classes`.`gan_id`,
  1057. `kids_classes`.`class_id` ,
  1058. `kids_classes`.`year` ,
  1059. kids_classes.active as kid_active,
  1060. kids_classes.year as class_year
  1061. FROM `kids`
  1062. LEFT JOIN `kids_classes`
  1063. ON `kids`.`id`=`kids_classes`.`kid_id`
  1064. WHERE `kids`.`parent_id`= '.$parent_id.'
  1065. ) A
  1066. LEFT JOIN `gans` ON `gans`.`id`=A.gan_id
  1067. LEFT JOIN `classes` ON classes.id= A.class_id
  1068. LEFT JOIN users ON users.gan_id=A.gan_id
  1069. LEFT JOIN `classes_vaad` ON classes_vaad.user_id=A.parent_id AND A.class_id=classes_vaad.class_id
  1070. ) B
  1071. LEFT JOIN
  1072. (SELECT messages.class_id,
  1073. count(messages.id) as sent_messages,
  1074. count(read_messages.message_id) as read_messages
  1075. FROM messages
  1076. LEFT JOIN read_messages ON messages.id = read_messages.message_id
  1077. AND read_messages.user_id='.$parent_id.'
  1078. WHERE messages.year='.CURRENT_YEAR.'
  1079. AND messages.active=1
  1080. GROUP BY messages.class_id
  1081. ) C
  1082. ON B.class_id = C.class_id
  1083. ORDER BY B.class_year DESC, B.kid_active ASC
  1084. ';
  1085. $re = $this->db->query($q);
  1086. $kids = $re->result_array();
  1087.  
  1088. $q1 = 'SELECT album_seen_pictures.user_id,albums.class_id, count(pictures.id) as sent_pics,`album_seen_pictures`.seen_pics FROM albums
  1089. LEFT JOIN pictures ON albums.id=pictures.album_id AND albums.active=1 AND pictures.tmb=1 AND pictures.full=1
  1090. LEFT JOIN `album_seen_pictures` ON `albums`.`id` = `album_seen_pictures`.`album_id`
  1091. WHERE albums.year='.CURRENT_YEAR.' AND album_seen_pictures.user_id ='.$parent_id.'
  1092. GROUP BY class_id';
  1093. $re1 = $this->db->query($q1);
  1094. $media = $re1->result_array();
  1095. if(!empty($media)){
  1096. foreach($kids as $k=>$kid){
  1097. foreach($media as $m=>$me){
  1098. if($parent_id == $me['user_id'] AND $kid['class_id'] == $me['class_id'] ){
  1099. $kids[$k]['unseen_photos'] = abs((int)$me['sent_pics'] - (int)$me['seen_pics']);
  1100. break;
  1101. }
  1102. }
  1103.  
  1104. }
  1105. }
  1106.  
  1107. $new_kids_current_year = array();
  1108. $new_kids_last_year = array();
  1109.  
  1110. foreach ($kids as $k => $kid)
  1111. {
  1112. $kid['type'] = "1";
  1113. if($kid['class_year'] == CURRENT_YEAR)
  1114. {
  1115. $new_kids_current_year[] = $kid;
  1116. }
  1117. else
  1118. {
  1119. $new_kids_last_year[] = $kid;
  1120. }
  1121. }
  1122.  
  1123.  
  1124. foreach ($new_kids_current_year as $k => $kid)
  1125. {
  1126. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  1127. {
  1128. $v = $kid;
  1129. unset($new_kids_current_year[$k]);
  1130. $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
  1131. $new_kids_current_year[$last_key+1] = $v;
  1132. }
  1133. }
  1134.  
  1135. $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
  1136.  
  1137. $new_kids = array();
  1138.  
  1139. foreach ($new_kids_1 as $k => $kid)
  1140. {
  1141.  
  1142.  
  1143. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
  1144. {
  1145. $kid['current_year'] = CURRENT_YEAR;
  1146. $class_id = $kid['class_id'];
  1147.  
  1148. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  1149.  
  1150. $kid = array_merge($kid, $permissions);
  1151.  
  1152. $new_kids[$kid['kid_id']] = $kid;
  1153. }
  1154. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
  1155. {
  1156. $kid['current_year'] = CURRENT_YEAR;
  1157. $new_kids[$kid['kid_id']] = $kid;
  1158. }
  1159. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  1160. {
  1161. if(
  1162. isset($new_kids[$kid['kid_id']]['class_year']) &&
  1163. ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
  1164. isset($new_kids[$kid['kid_id']]['kid_active']) &&
  1165. ($new_kids[$kid['kid_id']]['kid_active']) != '0'
  1166. )
  1167. {
  1168.  
  1169. }
  1170. else
  1171. {
  1172. $kid_details = $this->set_kid_array_values($kid,false);
  1173. $new_kids[$kid['kid_id']] = $kid_details;
  1174. }
  1175. }
  1176. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
  1177. {
  1178. if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
  1179. {
  1180. $kid_details = $this->set_kid_array_values($kid,false);
  1181. $new_kids[$kid['kid_id']] = $kid_details;
  1182. }
  1183.  
  1184. if($kid['kid_active'] != '0')
  1185. {
  1186. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  1187. $kid = array_merge($kid, $permissions);
  1188.  
  1189. $history = $this->set_kid_array_values($kid,true);
  1190. $new_kids[$kid['kid_id']]['history'][] = $history;
  1191. }
  1192. }
  1193. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
  1194. {
  1195. $kid = $this->set_kid_array_values($kid);
  1196. $new_kids[$kid['kid_id']] = $kid;
  1197. }
  1198.  
  1199. }
  1200.  
  1201. return array_values($new_kids);
  1202. }
  1203.  
  1204. function get_user_kids($parent_id)
  1205. {
  1206. $this->db->select('users.type as type',FALSE);
  1207.  
  1208. $this->db->from('users');
  1209.  
  1210. $this->db->where('users.id', $parent_id);
  1211.  
  1212. $query = $this->db->get();
  1213.  
  1214. $type = $query->row()->type;
  1215.  
  1216. // $this->db->from('phones');
  1217. // $phonesarr = $this->db->get()->result_array();
  1218.  
  1219. // $phones = array();
  1220.  
  1221. // foreach ($phonesarr as $phone)
  1222. // {
  1223. // $phones[$phone['id']] = $phone['name'];
  1224. // }
  1225.  
  1226. // $this->db->select('kids.name as kid_name',FALSE);
  1227. // $this->db->select('kids.birth_date as kid_bd',FALSE);
  1228. // $this->db->select('kids.id as kid_id',FALSE);
  1229. // $this->db->select('kids.pic as kid_pic',FALSE);
  1230. // // $this->db->select('kids.active as kid_active',FALSE);
  1231. // $this->db->select('kids.gender as kid_gender',FALSE);
  1232. // // $this->db->select('kids.class_id as class_id',FALSE);
  1233.  
  1234. // $this->db->from('kids');
  1235.  
  1236. // $this->db->where('parent_id', $parent_id);
  1237.  
  1238. // $query = $this->db->get();
  1239.  
  1240. // $kids = $query->result_array();
  1241.  
  1242. // foreach ($kids as $k => $kid)
  1243. // {
  1244. $this->db->select('kids.name as kid_name',FALSE);
  1245. $this->db->select('kids.birth_date as kid_bd',FALSE);
  1246. $this->db->select('kids.id as kid_id',FALSE);
  1247. $this->db->select('kids.pic as kid_pic',FALSE);
  1248. // $this->db->select('kids.active as kid_active',FALSE);
  1249. $this->db->select('kids.gender as kid_gender',FALSE);
  1250.  
  1251. $this->db->select('gans.id as gan_id',FALSE);
  1252. $this->db->select('gans.name as gan_name',FALSE);
  1253. $this->db->select('gans.max as gan_max',FALSE);
  1254. $this->db->select('gans.address as gan_address',FALSE);
  1255. // $this->db->select('phones.name as gan_ac',FALSE);
  1256. // $this->db->select('gans.area_code as gan_ac',FALSE);
  1257. $this->db->select('gans.gan_phone as gan_phone',FALSE);
  1258. $this->db->select('gans.cityName as gan_city',FALSE);
  1259. $this->db->select('gans.city as gan_city_id',FALSE);
  1260. $this->db->select('gans.code as gan_code',FALSE);
  1261. $this->db->select('classes.name as class_name',FALSE);
  1262. $this->db->select('classes.id as class_id',FALSE);
  1263. $this->db->select('users.id as teacher_id',FALSE);
  1264. $this->db->select('users.mail as teacher_mail',FALSE);
  1265. $this->db->select('users.first_name as teacher_first_name',FALSE);
  1266. $this->db->select('users.last_name as teacher_last_name',FALSE);
  1267. $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
  1268. $this->db->select('users.home_phone as teacher_phone',FALSE);
  1269. // $this->db->select('users.area_code as teacher_ac',FALSE);
  1270. $this->db->select('kids_classes.active as kid_active',FALSE);
  1271. $this->db->select('kids_classes.year as class_year',FALSE);
  1272.  
  1273. $this->db->order_by("kids_classes.year", "desc");
  1274. $this->db->order_by("kids_classes.active", "asc");
  1275.  
  1276. $this->db->from('kids');
  1277.  
  1278. // $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  1279. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id','left');
  1280. $this->db->join('classes', 'kids_classes.class_id = classes.id','left');
  1281. $this->db->join('gans', 'kids_classes.gan_id = gans.id','left');
  1282. $this->db->join('users', 'users.gan_id = gans.id','left');
  1283. // $this->db->join('phones', 'gans.area_code = phones.id','left');
  1284.  
  1285. // $this->db->where('kids_classes.kid_id', $kid['kid_id']);
  1286. // $this->db->where('kids_classes.year', '2015');
  1287. $this->db->where('kids.parent_id', $parent_id);
  1288.  
  1289. $query = $this->db->get();
  1290.  
  1291. $kids = $query->result_array();
  1292.  
  1293. $new_kids_current_year = array();
  1294. $new_kids_last_year = array();
  1295.  
  1296. foreach ($kids as $k => $kid)
  1297. {
  1298. if($kid['class_year'] == CURRENT_YEAR)
  1299. {
  1300. $new_kids_current_year[] = $kid;
  1301. }
  1302. else
  1303. {
  1304. $new_kids_last_year[] = $kid;
  1305. }
  1306. }
  1307.  
  1308.  
  1309. foreach ($new_kids_current_year as $k => $kid)
  1310. {
  1311. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  1312. {
  1313. $v = $kid;
  1314. unset($new_kids_current_year[$k]);
  1315. $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
  1316. $new_kids_current_year[$last_key+1] = $v;
  1317. }
  1318. }
  1319.  
  1320. $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
  1321.  
  1322. $new_kids = array();
  1323.  
  1324. foreach ($new_kids_1 as $k => $kid)
  1325. {
  1326. // if(isset($kid['gan_ac']))
  1327. // {
  1328. // $kids[$k]['gan_phone'] = $phones[$kid['gan_ac']].$kid['gan_phone'];
  1329. // unset($kids[$k]['gan_ac']);
  1330. // }
  1331.  
  1332. // if($kid['teacher_ac'] > 0)
  1333. // {
  1334.  
  1335. // $res = $this->set_mobile_phone($kid['teacher_ac'],$kid['teacher_mobile'],$kid['teacher_phone']);
  1336.  
  1337. // $kids[$k]['teacher_mobile'] = $res['mobile'];
  1338. // $kids[$k]['teacher_phone'] = $res['phone'];
  1339. // }
  1340.  
  1341. // unset($kids[$k]['teacher_ac']);
  1342.  
  1343. //gan city - old users
  1344. //city from old users
  1345. $gan_city_id = $kid['gan_city_id'];
  1346. $gan_city = $kid['gan_city'];
  1347. if($gan_city_id > 0 && ($gan_city == null || $gan_city == "0"))
  1348. {
  1349. $new_kids_1[$k]['gan_city'] = $this->city_model->get_name($gan_city_id);
  1350. }
  1351. unset($kid['gan_city_id']);
  1352.  
  1353. $kid['type'] = $type;
  1354.  
  1355. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
  1356. {
  1357. $kid['current_year'] = CURRENT_YEAR;
  1358. $class_id = $kid['class_id'];
  1359. // $arr = $this->message_model->get_messages($class_id,CURRENT_YEAR,true);
  1360. // $messages = $arr['messages'];
  1361. $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
  1362. $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
  1363.  
  1364. // if(count($messages)>0)
  1365. // {
  1366. // $unread_messages = $this->message_model->get_count_unread_messgae($parent_id,$messages);
  1367. // }
  1368.  
  1369. // $photos = $this->album_model->get_class_total_pic($class_id);
  1370.  
  1371. // if($photos > 0)
  1372. // {
  1373. // $unseen_photos = $this->album_model->get_count_unseen_pics_for_class($parent_id,$class_id,$photos);
  1374. // }
  1375.  
  1376. // if($unread_messages > 0)
  1377. // {
  1378. $kid['unread_messages'] = (string)$unread_messages;
  1379. // }
  1380.  
  1381. // if($unseen_photos > 0)
  1382. // {
  1383. $kid['unseen_photos'] = (string)$unseen_photos;
  1384. // }
  1385.  
  1386. $kid['vaad_type'] = $this->get_parent_type($parent_id,$class_id,$type);
  1387.  
  1388. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  1389.  
  1390. $kid = array_merge($kid, $permissions);
  1391.  
  1392. $new_kids[$kid['kid_id']] = $kid;
  1393. }
  1394. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
  1395. {
  1396. $kid['current_year'] = CURRENT_YEAR;
  1397. $new_kids[$kid['kid_id']] = $kid;
  1398. }
  1399. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  1400. {
  1401. if(
  1402. isset($new_kids[$kid['kid_id']]['class_year']) &&
  1403. ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
  1404. isset($new_kids[$kid['kid_id']]['kid_active']) &&
  1405. ($new_kids[$kid['kid_id']]['kid_active']) != '0'
  1406. )
  1407. {
  1408.  
  1409. }
  1410. else
  1411. {
  1412. $kid_details = $this->set_kid_array_values($kid,false);
  1413. $new_kids[$kid['kid_id']] = $kid_details;
  1414. }
  1415. }
  1416. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
  1417. {
  1418. if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
  1419. {
  1420. $kid_details = $this->set_kid_array_values($kid,false);
  1421. $new_kids[$kid['kid_id']] = $kid_details;
  1422. }
  1423.  
  1424. if($kid['kid_active'] != '0')
  1425. {
  1426. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  1427. $kid = array_merge($kid, $permissions);
  1428.  
  1429. $history = $this->set_kid_array_values($kid,true);
  1430. $new_kids[$kid['kid_id']]['history'][] = $history;
  1431. }
  1432. }
  1433. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
  1434. {
  1435. $kid = $this->set_kid_array_values($kid);
  1436. $new_kids[$kid['kid_id']] = $kid;
  1437. }
  1438.  
  1439. }
  1440.  
  1441. return array_values($new_kids);
  1442.  
  1443. // $kids[$k]['gan'] = $gan;
  1444. // $gans = reset($gans);
  1445.  
  1446. // $current_gan = null;
  1447. // $history = null;
  1448.  
  1449. // foreach ($kids as $k=>$kid)
  1450. // {
  1451. // $kids[$k]['current_year'] = CURRENT_YEAR;
  1452. // if($kid['class_year'] == CURRENT_YEAR)
  1453. // {
  1454. // $current_gan = $kid;
  1455. // }
  1456. // else
  1457. // {
  1458. // if($kid['kid_active'] == "1")
  1459. // {
  1460. // // $history[] = $gan;//array("class_id" =>$gan['class_id'],"class_year" =>$gan['class_year']);
  1461. // $kids[$k]['history'] = $kids;
  1462. // }
  1463. // }
  1464. // }
  1465.  
  1466. //get unread messages for the cuurent gan
  1467. // if($current_gan['kid_active'] == "1")
  1468. // {
  1469. // $class_id = $current_gan['class_id'];
  1470. // // $arr = $this->message_model->get_messages($class_id,CURRENT_YEAR,true);
  1471. // // $messages = $arr['messages'];
  1472. // $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
  1473. // $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
  1474.  
  1475. // // if(count($messages)>0)
  1476. // // {
  1477. // // $unread_messages = $this->message_model->get_count_unread_messgae($parent_id,$messages);
  1478. // // }
  1479.  
  1480. // // $photos = $this->album_model->get_class_total_pic($class_id);
  1481.  
  1482. // // if($photos > 0)
  1483. // // {
  1484. // // $unseen_photos = $this->album_model->get_count_unseen_pics_for_class($parent_id,$class_id,$photos);
  1485. // // }
  1486.  
  1487. // // if($unread_messages > 0)
  1488. // // {
  1489. // $current_gan['unread_messages'] = (string)$unread_messages;
  1490. // // }
  1491.  
  1492. // // if($unseen_photos > 0)
  1493. // // {
  1494. // $current_gan['unseen_photos'] = (string)$unseen_photos;
  1495. // // }
  1496. // }
  1497.  
  1498. // $kids[$k] = array_merge((array)$kid, (array)$current_gan);
  1499. // $kids[$k]['current_year'] = CURRENT_YEAR;
  1500. // $kids[$k]['history'] = $history;
  1501.  
  1502. // }
  1503.  
  1504. // return $new_kids;
  1505. }
  1506.  
  1507. function get_parent_type($parent_id,$class_id,$type)
  1508. {
  1509. /*vaad*/
  1510. $this->db->select('classes_vaad.vaad_type as vaad_type',FALSE);
  1511.  
  1512. $this->db->from('classes_vaad');
  1513.  
  1514. $this->db->where('classes_vaad.user_id', $parent_id);
  1515. $this->db->where('classes_vaad.class_id', $class_id);
  1516. $this->db->where('classes_vaad.active', 1);
  1517.  
  1518. $query = $this->db->get();
  1519. $count = $query->num_rows();
  1520.  
  1521. if($count > 0)
  1522. {
  1523. return $query->row()->vaad_type;
  1524. }
  1525.  
  1526. return '0';
  1527. /*vaad*/
  1528. }
  1529.  
  1530. function set_kid_array_values($kid,$history=false)
  1531. {
  1532. if($history)
  1533. {
  1534. unset($kid['kid_name']);
  1535. unset($kid['kid_bd']);
  1536. // unset($kid['kid_id']);
  1537. unset($kid['kid_pic']);
  1538. unset($kid['kid_gender']);
  1539. }
  1540. else
  1541. {
  1542. unset($kid['gan_id']);
  1543. unset($kid['gan_name']);
  1544. unset($kid['gan_max']);
  1545. unset($kid['gan_address']);
  1546. unset($kid['gan_phone']);
  1547. unset($kid['gan_city']);
  1548. unset($kid['gan_code']);
  1549. unset($kid['class_name']);
  1550. unset($kid['class_id']);
  1551. unset($kid['teacher_id']);
  1552. unset($kid['teacher_mail']);
  1553. unset($kid['teacher_first_name']);
  1554. unset($kid['teacher_last_name']);
  1555. unset($kid['teacher_mobile']);
  1556. unset($kid['teacher_phone']);
  1557. unset($kid['kid_active']);
  1558. unset($kid['class_year']);
  1559.  
  1560. }
  1561. return $kid;
  1562. }
  1563.  
  1564. function getKids2014($id)
  1565. {
  1566. $this->db->select('gans.id as gan_id',FALSE);
  1567. $this->db->select('gans.name as gan_name',FALSE);
  1568. $this->db->select('gans.pic as pic',FALSE);
  1569. $this->db->select('gans.max as gan_max',FALSE);
  1570. $this->db->select('gans.code as gan_code',FALSE);
  1571. $this->db->select('classes.name as class_name',FALSE);
  1572. $this->db->select('classes.id as class_id',FALSE);
  1573. $this->db->select('kids.name as kid_name',FALSE);
  1574. $this->db->select('kids.birth_date as birth_date',FALSE);
  1575. $this->db->select('kids.id as kid_id',FALSE);
  1576. $this->db->select('kids.pic as kid_pic',FALSE);
  1577. $this->db->select('kids_classes.active as kid_active',FALSE);
  1578. $this->db->select('kids_classes.year as year',FALSE);
  1579. $this->db->select('users.first_name as first_name',FALSE);
  1580.  
  1581. $this->db->from('users');
  1582.  
  1583. $this->db->join('kids', 'kids.parent_id = users.id');
  1584. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
  1585. $this->db->join('classes', 'kids_classes.class_id = classes.id');
  1586. $this->db->join('gans', 'kids_classes.gan_id = gans.id');
  1587.  
  1588. $this->db->where('users.id', $id);
  1589. // $this->db->group_by('kids.id');
  1590.  
  1591. $kids = $this->db->get()->result_array();
  1592.  
  1593. $kids_new = array();
  1594. $res = array();
  1595.  
  1596. foreach ($kids as $kid)
  1597. {
  1598. $kids_new[$kid['kid_id']][] = $kid;
  1599. }
  1600.  
  1601. foreach ($kids_new as $kid_new)
  1602. {
  1603. $new_year = false;
  1604.  
  1605. foreach ($kid_new as $kid)
  1606. {
  1607. if($kid['year'] == "2015")
  1608. {
  1609. $new_year = true;
  1610. }
  1611. }
  1612.  
  1613. if(!$new_year)
  1614. {
  1615. $res[] = reset($kid_new);
  1616. }
  1617. // if(count($kid_new)==1)
  1618. // {
  1619. // $res[] = reset($kid_new);
  1620. // }
  1621. }
  1622.  
  1623. return $res;
  1624. }
  1625.  
  1626. function startsWith($haystack, $needle)
  1627. {
  1628. return $needle === "" || strpos($haystack, $needle) === 0;
  1629. }
  1630.  
  1631. function getReadGansDetailsFn($mail)
  1632. {
  1633. $this->db->select('users.first_name as first_name',FALSE);
  1634.  
  1635.  
  1636. $this->db->from('users');
  1637.  
  1638. $this->db->join('gans', 'gans.id = users.gan_id');
  1639.  
  1640. $this->db->where('users.mail', $mail);
  1641.  
  1642. $query = $this->db->get();
  1643.  
  1644. return $query->row()->first_name;
  1645. }
  1646.  
  1647. function getReadGansDetailsLn($mail)
  1648. {
  1649. $this->db->select('users.last_name as last_name',FALSE);
  1650.  
  1651.  
  1652. $this->db->from('users');
  1653.  
  1654. $this->db->join('gans', 'gans.id = users.gan_id');
  1655.  
  1656. $this->db->where('users.mail', $mail);
  1657.  
  1658. $query = $this->db->get();
  1659.  
  1660. return $query->row()->last_name;
  1661. }
  1662.  
  1663. function getReadGansDetailsCode($mail)
  1664. {
  1665.  
  1666. $this->db->select('gans.code as code',FALSE);
  1667.  
  1668.  
  1669.  
  1670. $this->db->from('users');
  1671.  
  1672. $this->db->join('gans', 'gans.id = users.gan_id');
  1673.  
  1674. $this->db->where('users.mail', $mail);
  1675.  
  1676. $query = $this->db->get();
  1677.  
  1678. return $query->row()->code;
  1679. }
  1680.  
  1681.  
  1682. //
  1683. function editkiddetails($id,$name,$class_id,$bd,$gan_id,$path)
  1684. {
  1685. $this->db->where('id', $id);
  1686.  
  1687. if( strcmp($path, "") != 0)
  1688. {
  1689. $data = array(
  1690. 'birth_date' => $bd,
  1691. 'name' => $name,
  1692. 'gan_id' => $gan_id,
  1693. 'class_id' => $class_id,
  1694. 'pic' => $id
  1695. );
  1696. }
  1697. else
  1698. {
  1699. $data = array(
  1700. 'birth_date' => $bd,
  1701. 'name' => $name,
  1702. 'gan_id' => $gan_id,
  1703. 'class_id' => $class_id
  1704. );
  1705. }
  1706.  
  1707. return $this->db->update('kids', $data);
  1708. }
  1709.  
  1710. function registerkid($id,$name,$class_id,$bd,$gan_id)
  1711. {
  1712. $data = array(
  1713. 'parent_id' => $id,
  1714. 'birth_date' => $bd,
  1715. 'name' => $name,
  1716. 'gan_id' => $gan_id,
  1717. 'class_id' => $class_id
  1718. );
  1719.  
  1720. $bool = $this->db->insert('kids', $data);
  1721. $kid_id = $this->db->insert_id();
  1722.  
  1723. $data_kid_class = array(
  1724. "class_id" => $class_id,
  1725. "gan_id" => $gan_id,
  1726. "kid_id" => $kid_id,
  1727. "year" => "2015",
  1728. "date" => date("Y-m-d")
  1729. );
  1730.  
  1731. $this->db->insert('kids_classes', $data_kid_class);
  1732.  
  1733. return $kid_id;
  1734. }
  1735.  
  1736. function updatekidpic($id,$name=null)
  1737. {
  1738. $this->db->where('id', $id);
  1739.  
  1740. $data = array();
  1741.  
  1742. if($name == null)
  1743. {
  1744. $data['pic'] = $id;
  1745. }
  1746. else
  1747. {
  1748. $data['pic'] = $name;
  1749. }
  1750.  
  1751. $this->db->update('kids', $data);
  1752. }
  1753.  
  1754. function kidconfirmed($id)
  1755. {
  1756. $this->db->select('active');
  1757. $this->db->where('id', $id);
  1758. $query = $this->db->get('kids');
  1759.  
  1760. return $query->row()->active;
  1761. }
  1762.  
  1763. function updateclass($kid_id,$class_id_param,$user_id=null)
  1764. {
  1765. // check if already kid_id,class_id,current_year exist
  1766. if($class_id_param != "")
  1767. {
  1768. $this->db->where('kid_id', $kid_id);
  1769. $this->db->where('class_id', $class_id_param);
  1770. $this->db->where('year', CURRENT_YEAR);
  1771. $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
  1772.  
  1773. $query = $this->db->get('kids_classes');
  1774.  
  1775. if($query->num_rows() > 0)
  1776. {
  1777. return false;
  1778. }
  1779. }
  1780.  
  1781. $this->db->select('class_id',FALSE);
  1782. $this->db->select('active',FALSE);
  1783.  
  1784. $this->db->where('kid_id', $kid_id);
  1785. // $this->db->where('class_id', $class);
  1786. $this->db->where('year', CURRENT_YEAR);
  1787. $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
  1788.  
  1789. $query = $this->db->get('kids_classes');
  1790.  
  1791. if($query->num_rows() > 0)
  1792. {
  1793. $class_id = $query->row()->class_id;
  1794. $active = $query->row()->active;
  1795.  
  1796. $this->db->where('kid_id', $kid_id);
  1797. $this->db->where('class_id', $class_id);
  1798. $this->db->where('active', $active);
  1799.  
  1800. $data_kid_class = array(
  1801. "active" => 0,
  1802. // "year" => CURRENT_YEAR,
  1803. "date" => date("Y-m-d H:i:s")
  1804. );
  1805.  
  1806. $res = $this->db->update('kids_classes', $data_kid_class); // deactivate older classes
  1807.  
  1808. $this->retention_model->delete_token_retention_after_active_kid($class_id, $user_id); // update old classes
  1809.  
  1810. if($class_id_param == "")
  1811. {
  1812. return $res;
  1813. }
  1814. }
  1815.  
  1816. // return false;
  1817. // if( strcmp($class_id_param, "") != 0)
  1818. // {
  1819. return $this->setclass($kid_id, $class_id_param, "1");
  1820. // }
  1821. // else
  1822. // {
  1823. // return $res;
  1824. // }
  1825.  
  1826. }
  1827.  
  1828. // function setclass($id,$class)
  1829. // {
  1830. // $this->db->where('kid_id', $id);
  1831. // $this->db->where('class_id', $class);
  1832. // $this->db->where('year', CURRENT_YEAR);
  1833.  
  1834. // $query = $this->db->get('kids_classes');
  1835.  
  1836. // if($query->num_rows() > 0)
  1837. // {
  1838. // $this->db->where('kid_id', $id);
  1839. // $this->db->where('class_id', $class);
  1840. // $this->db->where('year', CURRENT_YEAR);
  1841.  
  1842. // $data_kid_class = array(
  1843. // "active" => 2,
  1844. // "year" => CURRENT_YEAR,
  1845. // "date" => date("Y-m-d H:i:s")
  1846. // );
  1847.  
  1848. // $res = $this->db->update('kids_classes', $data_kid_class);
  1849.  
  1850. // return $res;
  1851. // }
  1852. // else
  1853. // {
  1854. // return $this->add_class($id, $class);
  1855. // }
  1856.  
  1857. // }
  1858.  
  1859. function set_class_batch_by_year($kids,$year)
  1860. {
  1861. $kids_insert = array();
  1862.  
  1863. foreach($kids as $class_id => $kids_arr)
  1864. {
  1865. foreach($kids_arr as $kid_id)
  1866. {
  1867. $kid = array("kid_id"=>$kid_id, "class_id"=>$class_id);
  1868. $kids_insert[] = $kid;
  1869. }
  1870. }
  1871.  
  1872. foreach($kids_insert as $kid)
  1873. {
  1874. $res = $this->setclass($kid['kid_id'],$kid['class_id'],'1',$year);
  1875. if($res)
  1876. {
  1877. //send push
  1878. }
  1879. }
  1880. }
  1881.  
  1882. function setclass($id, $class, $active, $year=CURRENT_YEAR)
  1883. {
  1884. // if($active == '1')
  1885. // {
  1886. // $query = $this->db->get_where('kids_classes',array('kid_id' => $id,
  1887. // 'class_id' => $class,
  1888. // 'active' => $active,
  1889. // 'year' => $year
  1890. // ));
  1891.  
  1892. // if($query->num_rows() > 0)
  1893. // {
  1894. // return false;
  1895. // }
  1896. // }
  1897.  
  1898. $this->db->where('kid_id', $id);
  1899. $this->db->where('class_id', $class);
  1900. $this->db->where('year', $year);
  1901. $this->db->where('(kids_classes.active = 1 or kids_classes.active = 2)');
  1902.  
  1903. $query = $this->db->get('kids_classes');
  1904.  
  1905. if($query->num_rows() > 0)
  1906. {
  1907. return false;
  1908. }
  1909.  
  1910. $this->db->select('gan_id');
  1911. $this->db->where('id', $class);
  1912. $query = $this->db->get('classes');
  1913.  
  1914. $gan_id = $query->row()->gan_id;
  1915.  
  1916. $data_kid_class = array(
  1917. "active" => $active,
  1918. "class_id" => $class,
  1919. "gan_id" => $gan_id,
  1920. "kid_id" => $id,
  1921. "year" => $year,
  1922. "date" => date("Y-m-d H:i:s")
  1923. );
  1924.  
  1925. $res = $this->db->insert('kids_classes', $data_kid_class);
  1926.  
  1927. //update kid
  1928. // $this->db->where('id', $id);
  1929.  
  1930. // $data = array(
  1931. // 'active' => 0,
  1932. // 'gan_id' => $gan_id,
  1933. // 'class_id' => $class
  1934. // );
  1935.  
  1936. // $this->db->update('kids', $data);
  1937.  
  1938. // if($res)
  1939. // {
  1940.  
  1941. // }
  1942.  
  1943. return $res;
  1944. }
  1945.  
  1946. function create_kid($id,$name,$gender,$bd,$path,$gan_code)
  1947. {
  1948. $date = date("Y-m-d H:i:s");
  1949.  
  1950. $query = $this->db->get_where('kids',array('name' => $name,
  1951. 'gender' => $gender,
  1952. // 'birth_date' => $bd,
  1953. 'parent_id' => $id
  1954. ));
  1955.  
  1956. if($query->num_rows() > 0)
  1957. {
  1958. $created = $query->row()->created;
  1959.  
  1960. $time_date = strtotime($date);
  1961. $time_created = strtotime($created);
  1962.  
  1963. if($time_date - $time_created <= 600)
  1964. {
  1965. return $query->row()->id;
  1966. }
  1967. }
  1968.  
  1969.  
  1970. $data = array(
  1971. 'parent_id' => $id,
  1972. 'birth_date' => $bd,
  1973. 'gender' => $gender,
  1974. // 'active' => $active,
  1975. 'created' => $date,
  1976. 'name' => $name
  1977. );
  1978.  
  1979. $this->db->insert('kids', $data);
  1980. $kid_id = $this->db->insert_id();
  1981.  
  1982. if( strcmp($path, "") != 0)
  1983. {
  1984. $this->db->where('id', $kid_id);
  1985.  
  1986. $data = array(
  1987. 'pic' => $kid_id
  1988. );
  1989.  
  1990. $this->db->update('kids', $data);
  1991. }
  1992.  
  1993. if ($kid_id)
  1994. {
  1995. return $kid_id;
  1996. }
  1997. else
  1998. {
  1999. return false;
  2000. }
  2001. }
  2002.  
  2003. function registerkiddetails($id,$name,$class_id,$bd,$gan_id,$path)
  2004. {
  2005.  
  2006. $data = array(
  2007. 'parent_id' => $id,
  2008. 'birth_date' => $bd,
  2009. 'name' => $name,
  2010. 'gan_id' => $gan_id,
  2011. 'class_id' => $class_id
  2012. );
  2013.  
  2014. $bool = $this->db->insert('kids', $data);
  2015. $kid_id = $this->db->insert_id();
  2016.  
  2017. $data_kid_class = array(
  2018. "active" => 2,
  2019. "class_id" => $class_id,
  2020. "gan_id" => $gan_id,
  2021. "kid_id" => $kid_id,
  2022. "year" => "2015",
  2023. "date" => date("Y-m-d")
  2024. );
  2025.  
  2026. $bool = $this->db->insert('kids_classes', $data_kid_class);
  2027.  
  2028. if( strcmp($path, "") != 0)
  2029. {
  2030. $this->db->where('id', $kid_id);
  2031.  
  2032. $data = array(
  2033. 'pic' => $kid_id
  2034. );
  2035.  
  2036. $this->db->update('kids', $data);
  2037. }
  2038. if ($bool)
  2039. {
  2040. return $kid_id;
  2041. }
  2042. }
  2043.  
  2044.  
  2045.  
  2046. function getParentsByGanenetId($id)
  2047. {
  2048. //get gan_id
  2049. $this->db->select('gan_id');
  2050. $this->db->where('id', $id);
  2051. $query = $this->db->get('users');
  2052. $gan_id = $query->row()->gan_id;
  2053.  
  2054. $this->db->select('distinct users.*',FALSE);
  2055.  
  2056. $this->db->from('users');
  2057. $this->db->join('kids', 'kids.parent_id = users.id ');
  2058.  
  2059. $this->db->where('kids.gan_id', $gan_id);
  2060.  
  2061. $query = $this->db->get();
  2062.  
  2063. return $query->result_array();
  2064. }
  2065.  
  2066. function getParentsByClassId($id)
  2067. {
  2068. $this->db->select('distinct users.*',FALSE);
  2069.  
  2070. $this->db->from('users');
  2071. $this->db->join('kids', 'kids.parent_id = users.id ');
  2072.  
  2073. $this->db->where('kids.class_id', $id);
  2074.  
  2075. $query = $this->db->get();
  2076.  
  2077. return $query->result_array();
  2078. }
  2079.  
  2080. function mailexistRetention($mail)
  2081. {
  2082. $this->db->where('mail',$mail);
  2083. $query = $this->db->get('retention');
  2084. if ($query->num_rows() > 0){
  2085. return true;
  2086. }
  2087. else{
  2088. return false;
  2089. }
  2090. }
  2091.  
  2092. function mailexistNew($mail)
  2093. {
  2094. $this->db->where('mail',$mail);
  2095. //$this->db->where('activation',NULL);
  2096.  
  2097. $query = $this->db->get('users');
  2098. if ($query->num_rows() > 0)
  2099. {
  2100. return true;
  2101. }
  2102. else
  2103. {
  2104. return false;
  2105. }
  2106.  
  2107. }
  2108.  
  2109.  
  2110. function mailexist($mail)
  2111. {
  2112. $this->db->select('mail',FALSE);
  2113.  
  2114. $this->db->from('users');
  2115.  
  2116. $this->db->where('users.mail', $mail);
  2117.  
  2118. $query = $this->db->get();
  2119.  
  2120. return $query->result_array();
  2121. /*$classes = array(
  2122. '4' => $mail ,
  2123. '2' => $mail
  2124.  
  2125. );
  2126. for ($i = 1; $i <= 2; $i++)
  2127. {
  2128. if (array_key_exists($i, $classes)) {
  2129. echo $i;
  2130.  
  2131. }
  2132. }*/
  2133. }
  2134.  
  2135. function getuserpersonaldetails($id)
  2136. {
  2137. $this->db->select('id',FALSE);
  2138. $this->db->select('mail',FALSE);
  2139. $this->db->select('password',FALSE);
  2140. $this->db->select('first_name',FALSE);
  2141. $this->db->select('last_name',FALSE);
  2142. $this->db->select('area_code',FALSE);
  2143. $this->db->select('phone',FALSE);
  2144. $this->db->select('address',FALSE);
  2145. $this->db->select('city',FALSE);
  2146. $this->db->select('country',FALSE);
  2147. $this->db->select('state',FALSE);
  2148. $this->db->select('cityName',FALSE);
  2149.  
  2150. $this->db->from('users');
  2151.  
  2152. $this->db->where('users.id', $id);
  2153.  
  2154. $query = $this->db->get();
  2155.  
  2156. return $query->result_array();
  2157. }
  2158.  
  2159. function edituserpersonaldetails($id,$mail,$password,$first_name,$last_name,$address,$area_code,$phone,$city,$country,$state,$cityName)
  2160. {
  2161. $this->db->where('id', $id);
  2162.  
  2163. $data = array(
  2164. 'mail' => $mail,
  2165. // 'password' => sha1($password),
  2166. 'first_name' => $first_name,
  2167. 'last_name' => $last_name,
  2168. 'address' => $address,
  2169. 'area_code' => $area_code,
  2170. 'phone' => $phone,
  2171. 'country' => $country,
  2172. 'state' => $state,
  2173. 'cityName' => $cityName,
  2174. 'city' => $city
  2175. );
  2176.  
  2177. return $this->db->update('users', $data);
  2178. }
  2179.  
  2180. function updateuser($id,$post_data)
  2181. // function updateuser($id,$first_name,$last_name,$address,$phone,$mobile,$city)
  2182. {
  2183.  
  2184. $data = $this->util_model->filldata($post_data);
  2185.  
  2186. // if($first_name != "")
  2187. // {
  2188. // $user['first_name'] = $first_name;
  2189. // }
  2190.  
  2191. // if($last_name != "")
  2192. // {
  2193. // $user['last_name'] = $last_name;
  2194. // }
  2195.  
  2196. // if($address != "")
  2197. // {
  2198. // $user['address'] = $address;
  2199. // }
  2200.  
  2201. // if($phone != "")
  2202. // {
  2203. // $user['phone'] = $phone;
  2204. // }
  2205.  
  2206. // if($mobile != "")
  2207. // {
  2208. // $user['mobile'] = $mobile;
  2209. // }
  2210.  
  2211. // if($city != "")
  2212. // {
  2213. // $user['cityName'] = $city;
  2214. // }
  2215.  
  2216. // $user['area_code'] = 0;
  2217.  
  2218. // $user = array(
  2219. // 'phone' => $phone,
  2220. // 'first_name' => $first_name,
  2221. // 'last_name' => $last_name,
  2222. // 'mobile' => $mobile,
  2223. // 'address' => $address,
  2224. // 'city' => $city
  2225. // );
  2226.  
  2227. $this->db->where('id', $id);
  2228. return $this->db->update('users', $data);
  2229. // return $this->db->update('users', $user);
  2230. }
  2231.  
  2232. function editkids($kids)
  2233. {
  2234. $count = count($kids);
  2235. for ($i = 1; $i <= $count; $i++)
  2236. {
  2237. $arr = $kids[$i];
  2238. $id = $arr["id"] ;
  2239.  
  2240. $this->db->where('id', $id);
  2241.  
  2242. $query = $this->db->update('kids', $arr);
  2243. }
  2244. }
  2245.  
  2246. function send_setclass_push($kid_id,$class_id,$app_name)
  2247. {
  2248. //send notification to teacher
  2249.  
  2250. $kid_name = $this->parent_model->get_kid_name($kid_id);
  2251. $class_name = $this->class_model->get_class_name($class_id);
  2252.  
  2253. // $text = $kid_name . ' ' . 'נרשמ/ה לכיתת ' . $class_name . ' ' . 'ומחכה לאישורך';
  2254.  
  2255. if($app_name == GB || $app_name == null)
  2256. {
  2257. $text = PUSH_SET_CLASS_HE;
  2258. }
  2259. else
  2260. {
  2261. $text = PUSH_SET_CLASS_EN;
  2262. }
  2263.  
  2264. $text = str_replace('KID_NAME',$kid_name,$text);
  2265. $text = str_replace('CLASS_NAME',$class_name,$text);
  2266.  
  2267. $arr = $this->get_teacher_by_class($class_id);
  2268.  
  2269. $res = reset($arr);
  2270.  
  2271. $user_id = $res['user_id'];
  2272. $gan_id = $res['gan_id'];
  2273.  
  2274. $badge = $this->parent_model->get_count_waiting_parents($gan_id);
  2275.  
  2276. //send push
  2277. $tokens = $this->token_model->get_user_tokens($user_id);
  2278.  
  2279. $loc_args = array($kid_name,$class_name);
  2280.  
  2281. $acme = array("cls_id" => $class_id);
  2282.  
  2283. $user_id_array = array();
  2284. $user_id_array[$user_id] = array("cls_id" => $class_id);
  2285.  
  2286. $this->message_model->send_push($tokens,$user_id_array,$text,$class_id,SET_CLASS_LOC_KEY,$loc_args,$acme,$badge,$app_name);
  2287. }
  2288.  
  2289. function update_seen_albums($user_id,$album_id,$num_seen_photos,$year)
  2290. {
  2291. //update only views count (seen_albums)
  2292. $this->db->where('user_id',$user_id);
  2293. $this->db->where('album_id',$album_id);
  2294. $query = $this->db->get('seen_albums');
  2295.  
  2296. if ($query->num_rows() == 0)
  2297. {
  2298. $data = array('user_id'=>$user_id,
  2299. 'album_id'=>$album_id,
  2300. 'year'=>$year,
  2301. 'views'=>1
  2302. );
  2303. $query = $this->db->insert('seen_albums', $data);
  2304.  
  2305. if (!$query)
  2306. {
  2307. return false;
  2308. }
  2309. }
  2310. else
  2311. {
  2312. $res = reset($query->result_array());
  2313. $views = $res['views'];
  2314. $views ++;
  2315.  
  2316. $this->db->where('user_id', $user_id);
  2317. $this->db->where('album_id', $album_id);
  2318.  
  2319. $data = array(
  2320. 'views' => $views
  2321. );
  2322.  
  2323. $query = $this->db->update('seen_albums', $data);
  2324. if (!$query)
  2325. {
  2326. return false;
  2327. }
  2328. }
  2329.  
  2330. if($num_seen_photos != "")
  2331. {
  2332. //update also num pic count (album_seen_pictures)
  2333. $this->db->where('user_id',$user_id);
  2334. $this->db->where('album_id',$album_id);
  2335. $query = $this->db->get('album_seen_pictures');
  2336.  
  2337. if ($query->num_rows() == 0)
  2338. {
  2339. $data = array('user_id'=>$user_id,
  2340. 'album_id'=>$album_id,
  2341. 'year'=>$year,
  2342. 'seen_pics'=>$num_seen_photos
  2343. );
  2344. $query = $this->db->insert('album_seen_pictures', $data);
  2345. if (!$query)
  2346. {
  2347. return false;
  2348. }
  2349. }
  2350. else
  2351. {
  2352. $this->db->where('user_id', $user_id);
  2353. $this->db->where('album_id', $album_id);
  2354.  
  2355. $data = array(
  2356. 'seen_pics' => $num_seen_photos
  2357. );
  2358.  
  2359. $query = $this->db->update('album_seen_pictures', $data);
  2360. if (!$query)
  2361. {
  2362. return false;
  2363. }
  2364. }
  2365. }
  2366.  
  2367. return true;
  2368. }
  2369.  
  2370. function createuser($data)
  2371. {
  2372. $query = $this->db->insert('users', $data);
  2373. $id = $this->db->insert_id();
  2374. return $id;
  2375. }
  2376.  
  2377. function check_password($pass)
  2378. {
  2379. $arr = str_split($pass);
  2380.  
  2381. if(count($arr) < 6)
  2382. {
  2383. return '9010';
  2384. }
  2385. else if($this->HasConsec($arr))
  2386. {
  2387. return "9011";
  2388. }
  2389. else if (!preg_match('/[A-Za-z]/', $pass) || !preg_match('/[0-9]/', $pass))
  2390. {
  2391. return "9012";
  2392. }
  2393. else
  2394. {
  2395. return true;
  2396. }
  2397. }
  2398.  
  2399. function HasConsec($array)
  2400. {
  2401. $consec = 0;
  2402. $res = false;
  2403. $cb = false;
  2404.  
  2405. foreach ($array as $im)
  2406. {
  2407. if ($cb !== false && $cb == $im)
  2408. {
  2409. $consec ++;
  2410. if($consec == 2)
  2411. {
  2412. return true;
  2413. }
  2414. }
  2415. else
  2416. {
  2417. if($consec == 1)
  2418. {
  2419. $consec = 0;
  2420. }
  2421. }
  2422.  
  2423. $cb = $im + 1;
  2424. }
  2425.  
  2426. return $res;
  2427. }
  2428.  
  2429.  
  2430. function register($user,$first_name,$last_name,$kids,$gan,$classes,$mail,$pass,$phone,$area_code,$address,$city,$country,$state,$cityName,$activation)
  2431. {
  2432. $bool = true;
  2433.  
  2434. $ip = $this->get_ip_address();
  2435.  
  2436. if($cityName == "")
  2437. {
  2438. $cityName = null;
  2439. }
  2440.  
  2441. $data = array(
  2442. 'first_name' => $first_name ,
  2443. 'last_name' => $last_name ,
  2444. 'mail' => $mail,
  2445. 'password' => sha1($pass),
  2446. 'phone' => $phone,
  2447. 'area_code' => $area_code,
  2448. 'address' => $address,
  2449. 'city' => $city,
  2450. 'country' => $country,
  2451. 'state' => $state,
  2452. 'cityName' => $cityName,
  2453. 'type' => $user,
  2454. 'activation' => $activation,
  2455. 'ip' => $ip,
  2456. 'date' => date("Y-m-d H:i:s")
  2457. );
  2458.  
  2459.  
  2460. if($user != 1) // ganenet
  2461. {
  2462. $code = rand(1000, 9999);
  2463.  
  2464. $flag_code = $this->gan_model->getCountGanCode($code);
  2465.  
  2466. while($flag_code)
  2467. {
  2468. $code = rand(1000, 9999);
  2469. $flag_code = $this->gan_model->getCountGanCode($code);
  2470. }
  2471.  
  2472. $gan['active'] = 1;
  2473. $gan['code'] = $code;
  2474. $gan['date'] = date("Y-m-d H:i:s");
  2475.  
  2476. if($gan['area_code'] == '0')
  2477. {
  2478. $gan['area_code'] = '1';
  2479. }
  2480.  
  2481. $query = $this->db->insert('gans', $gan);
  2482. $gan_id = $this->db->insert_id();
  2483. if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
  2484. {
  2485. $bool = false;
  2486. return $bool;
  2487. }
  2488. else
  2489. {
  2490. $this->db->where('id', $gan_id);
  2491.  
  2492. /*if( strcmp($gan["pic"], "default") == 0)
  2493. {
  2494. $pic = "default";
  2495. }
  2496. else
  2497. {
  2498. $pic = 'gan_'.$gan_id;
  2499. }*/
  2500.  
  2501. if (array_key_exists('pic', $gan))
  2502. {
  2503. $pic = 'gan_'.$gan_id;
  2504.  
  2505. $dataGan = array(
  2506. 'pic' => $pic
  2507. );
  2508.  
  2509. $this->db->update('gans', $dataGan);
  2510. }
  2511.  
  2512. // $this->db->order_by("id", "desc");
  2513. // $query = $this->db->get('gans', 1, 0);
  2514. // $id = $query->row()->id;
  2515. $data['gan_id'] = $gan_id;
  2516. $data['active'] = 1;
  2517.  
  2518. /*$count = count($classes);
  2519. for ($i = 1; $i <= $count; $i++)
  2520. {
  2521. if (array_key_exists(strval($i), $classes)) {
  2522.  
  2523. $arr = $classes[$i];
  2524. $arr["gan_id"] = $gan_id;
  2525.  
  2526. $query = $this->db->insert('classes', $arr);
  2527. if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
  2528. {
  2529. $bool = false;
  2530. }
  2531. }
  2532. }*/
  2533.  
  2534. foreach ($classes as $key => $value)
  2535. {
  2536. $arr = $value;
  2537. $arr["gan_id"] = $gan_id;
  2538.  
  2539. $query = $this->db->insert('classes', $arr);
  2540. if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
  2541. {
  2542. $bool = false;
  2543. }
  2544. }
  2545. }
  2546. }
  2547.  
  2548.  
  2549.  
  2550. $query = $this->db->insert('users', $data);
  2551.  
  2552. // if ($this->db->_error_message())
  2553. // {
  2554. // return false;
  2555. // }
  2556.  
  2557. $id = $this->db->insert_id();
  2558.  
  2559. if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
  2560. {
  2561. $bool = false;
  2562. return $bool;
  2563. }
  2564.  
  2565. $kid_ids = "";
  2566.  
  2567. if($user == 1) // parent
  2568. {
  2569. // $this->db->order_by("id", "desc");
  2570. // $query = $this->db->get('users', 1, 0);
  2571. // $id = $query->row()->id;
  2572.  
  2573. $count = count($kids);
  2574. for ($i = 1; $i <= $count; $i++)
  2575. {
  2576. $arr = $kids[$i];
  2577.  
  2578. $arr["parent_id"] = $id;
  2579.  
  2580. $query = $this->db->insert('kids', $arr);
  2581. $kid_id = $this->db->insert_id();
  2582.  
  2583. $data_kid_class = array(
  2584. "class_id" => $arr["class_id"],
  2585. "gan_id" => $arr["gan_id"],
  2586. "kid_id" => $kid_id,
  2587. "year" => "2015",
  2588. "date" => date("Y-m-d")
  2589. );
  2590.  
  2591. $this->db->insert('kids_classes', $data_kid_class);
  2592.  
  2593. $kid_ids .= $kid_id;
  2594. $kid_ids .= ",";
  2595.  
  2596. if ((strpos($query, "error") !== FALSE) || (strpos($query, "Error") !== FALSE))
  2597. {
  2598. $bool = false;
  2599. }
  2600. else
  2601. {
  2602. if (array_key_exists('pic', $arr))
  2603. {
  2604. $this->db->where('id', $kid_id);
  2605.  
  2606. $data = array(
  2607. 'pic' => $kid_id
  2608. );
  2609.  
  2610. $this->db->update('kids', $data);
  2611. }
  2612. }
  2613. }
  2614.  
  2615. $kid_ids = substr($kid_ids, 0, -1);
  2616. }
  2617.  
  2618.  
  2619. if($bool)
  2620. {
  2621. if($user != 1) // ganenet
  2622. {
  2623. return $id.'-'.$code;
  2624. }
  2625. else
  2626. {
  2627. return $kid_ids;
  2628. }
  2629. }
  2630.  
  2631. }
  2632.  
  2633. /**
  2634. * Get user ip address
  2635. */
  2636. function get_ip_address()
  2637. {
  2638. 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) {
  2639. if (array_key_exists($key, $_SERVER) === true) {
  2640. foreach (explode(',', $_SERVER[$key]) as $ip) {
  2641. if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
  2642. return $ip;
  2643. }
  2644. }
  2645. }
  2646. }
  2647. }
  2648.  
  2649. function getLastUserId($id)
  2650. {
  2651. // $this->db->order_by("id", "desc");
  2652. // $query = $this->db->get('users', 1, 0);
  2653. // return $query->row()->id;
  2654.  
  2655. $this->db->select('parent_id',FALSE);
  2656.  
  2657. $this->db->from('kids');
  2658.  
  2659. $this->db->where('kids.id', $id);
  2660.  
  2661. $query = $this->db->get();
  2662.  
  2663. return $query->row()->parent_id;
  2664.  
  2665. }
  2666.  
  2667. function getLastGanId()
  2668. {
  2669. $this->db->order_by("id", "desc");
  2670. $query = $this->db->get('gans', 1, 0);
  2671. return $query->row()->id;
  2672. }
  2673.  
  2674. function getLastRegisteredKidsIds($kids)
  2675. {
  2676. $str_ids = '';
  2677. $len = sizeof($kids);
  2678.  
  2679. // for ( $counter = 0; $counter < $len; $counter += 1)
  2680. // {
  2681. $this->db->order_by("id", "desc");
  2682. $query = $this->db->get('kids', $len, $counter);
  2683.  
  2684. foreach ($query->result() as $row)
  2685. {
  2686. $str_ids = $row->id . $str_ids;
  2687. $str_ids = ',' . $str_ids;
  2688.  
  2689. }
  2690.  
  2691. // }
  2692. $str_ids = substr( $str_ids, 1 );
  2693. return $str_ids;
  2694. }
  2695.  
  2696. function deactivate($mail)
  2697. {
  2698. $this->db->where('mail', $mail);
  2699. // $this->db->where('activation', $key);
  2700.  
  2701. $data = array(
  2702. 'activation' => '111'
  2703. );
  2704.  
  2705. $this->db->update('users', $data);
  2706. }
  2707.  
  2708. function activate($key,$mail)
  2709. {
  2710. $this->db->where('mail', $mail);
  2711. $this->db->where('activation', $key);
  2712.  
  2713. $data = array(
  2714. 'activation' => null
  2715. );
  2716.  
  2717. return $this->db->update('users', $data);
  2718. }
  2719.  
  2720. /*function activate($key,$mail)
  2721. {
  2722. $this->db->where('mail', $mail);
  2723. $this->db->where('activation', $key);
  2724.  
  2725. $data = array(
  2726. 'activation' => null
  2727. );
  2728.  
  2729. $this->db->update('users', $data);
  2730.  
  2731. $this->db->select('type');
  2732.  
  2733. $this->db->from('users');
  2734.  
  2735. $this->db->where('mail', $mail);
  2736.  
  2737. $query = $this->db->get();
  2738.  
  2739. return $query->row()->type;
  2740. }*/
  2741.  
  2742. function forget_key_exist($key)
  2743. {
  2744. $query = $this->db->get_where('users',array('password_key'=>$key));
  2745. $count = $query->num_rows(); //counting result from query
  2746.  
  2747. if ($count === 0)
  2748. {
  2749. return false;
  2750. }
  2751. return true;
  2752. }
  2753.  
  2754. function forget($key,$mail,$password)
  2755. {
  2756. $this->db->where('mail', $mail);
  2757. $this->db->where('password_key', $key);
  2758.  
  2759. $data = array(
  2760. 'password' => sha1($password),
  2761. 'password_key' => NULL
  2762.  
  2763. );
  2764.  
  2765. $this->db->update('users', $data);
  2766.  
  2767. return $this->db->affected_rows() > 0;
  2768. }
  2769.  
  2770. function delete_teacher($id)
  2771. {
  2772. $this->db->select('gan_id');
  2773. $this->db->where('id', $id);
  2774. $query = $this->db->get('users');
  2775. $gan_id = $query->row()->gan_id;
  2776.  
  2777. $this->db->where('gan_id', $gan_id);
  2778. $this->db->delete('classes');
  2779.  
  2780. $this->db->where('id', $gan_id);
  2781. $this->db->delete('gans');
  2782.  
  2783. $this->db->where('id', $id);
  2784. $this->db->delete('users');
  2785. }
  2786.  
  2787. function get_kid_class($kid_id)
  2788. {
  2789. $this->db->select('kids_classes.class_id as class_id');
  2790. $this->db->select('classes.name as class_name');
  2791. $this->db->select('users.first_name as teacher_first_name');
  2792. $this->db->select('users.last_name as teacher_last_name');
  2793. $this->db->select('gans.name as gan_name');
  2794.  
  2795. $this->db->from('kids_classes');
  2796.  
  2797. $this->db->join('classes', 'kids_classes.class_id = classes.id');
  2798. $this->db->join('users', 'users.gan_id = classes.gan_id');
  2799. $this->db->join('gans', 'kids_classes.gan_id = gans.id');
  2800.  
  2801. $this->db->where('kids_classes.kid_id', $kid_id);
  2802. $this->db->where('kids_classes.year', CURRENT_YEAR);
  2803.  
  2804. $query = $this->db->get();
  2805.  
  2806. return reset($query->result_array());
  2807. }
  2808.  
  2809. function get_users_in_class($class_id,$type)
  2810. {
  2811.  
  2812. if($type != TEACHER_TYPE)
  2813. {
  2814. $this->db->select('distinct(users.id) as user_id');
  2815. //$this->db->select('kids.id as kid_id');
  2816.  
  2817. $this->db->from('classes');
  2818.  
  2819. $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
  2820. $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  2821. $this->db->join('users', 'users.id = kids.parent_id');
  2822.  
  2823. $this->db->where('classes.id', $class_id);
  2824.  
  2825. $users = $this->db->get()->result_array();
  2826. }
  2827. else
  2828. {
  2829. $this->db->select('distinct(users.id) as user_id');
  2830.  
  2831. $this->db->from('classes');
  2832.  
  2833. $this->db->join('gans', 'gans.id = classes.gan_id');
  2834. $this->db->join('users', 'users.gan_id = gans.id');
  2835.  
  2836. $this->db->where('classes.id', $class_id);
  2837.  
  2838. $users = $this->db->get()->result_array();
  2839. }
  2840.  
  2841. // $users = array_merge($users, $teachers);
  2842.  
  2843. $res = array();
  2844.  
  2845. foreach ($users as $u=>$user)
  2846. {
  2847. $res[] = $user['user_id'];
  2848. }
  2849.  
  2850. return $res;
  2851. }
  2852.  
  2853. function get_users_in_class_test($class_id,$users_ids=null)
  2854. {
  2855. $this->db->select('distinct(users.id) as user_id');
  2856. $this->db->select('kids.id as kid_id');
  2857.  
  2858. $this->db->from('classes');
  2859.  
  2860. $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
  2861. $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  2862. $this->db->join('users', 'users.id = kids.parent_id');
  2863.  
  2864. $this->db->where('classes.id', $class_id);
  2865. if($users_ids != null)
  2866. {
  2867. $this->db->where_in('users.id', $users_ids);
  2868. }
  2869.  
  2870. $users = $this->db->get()->result_array();
  2871.  
  2872. $this->db->select('distinct(users.id) as user_id');
  2873.  
  2874. $this->db->from('classes');
  2875.  
  2876. $this->db->join('gans', 'gans.id = classes.gan_id');
  2877. $this->db->join('users', 'users.gan_id = gans.id');
  2878.  
  2879. $this->db->where('classes.id', $class_id);
  2880.  
  2881. $teachers = $this->db->get()->result_array();
  2882.  
  2883. $users = array_merge($users, $teachers);
  2884.  
  2885. $ids = array();
  2886. $kids_ids = array();
  2887.  
  2888. foreach ($users as $u=>$user)
  2889. {
  2890. if(!in_array($user['user_id'],$ids))
  2891. {
  2892. $ids[] = $user['user_id'];
  2893. $kids_ids[$user['user_id']] = $user['kid_id'];
  2894. }
  2895. }
  2896.  
  2897. // $res = array();
  2898.  
  2899. // $res['kids_ids'] = $kids_ids;
  2900. // $res['ids'] = $ids;
  2901.  
  2902.  
  2903. return $kids_ids;
  2904. }
  2905.  
  2906. function get_kids_parents($kids)
  2907. {
  2908. $this->db->select('distinct(users.id) as user_id',FALSE);
  2909. $this->db->select('kids.id as kid_id',FALSE);
  2910.  
  2911. $this->db->from('kids');
  2912.  
  2913. $this->db->join('users', 'kids.parent_id = users.id');
  2914.  
  2915. $this->db->where_in('kids.id', $kids);
  2916.  
  2917. $parents = $this->db->get()->result_array();
  2918.  
  2919. $res = array();
  2920.  
  2921. foreach ($parents as $p=>$parent)
  2922. {
  2923. $res[$parent['user_id']] = $parent['kid_id'];
  2924. }
  2925.  
  2926. return $res;
  2927. }
  2928.  
  2929. function get_kids_parents2($kids)
  2930. {
  2931. $this->db->select('kids.parent_id as user_id',FALSE);
  2932. $this->db->select('kids.id as kid_id',FALSE);
  2933.  
  2934. $this->db->from('kids');
  2935.  
  2936. $this->db->join('users', 'kids.parent_id = users.id');
  2937.  
  2938. $this->db->where_in('kids.id', $kids);
  2939.  
  2940. $parents = $this->db->get()->result_array();
  2941.  
  2942. $res = array();
  2943.  
  2944. error_log("*** DB *** " . print_r($parents, TRUE));
  2945.  
  2946. foreach ($parents as $p=>$parent)
  2947. {
  2948. $res[$parent['user_id']] = $parent['kid_id'];
  2949. }
  2950.  
  2951. return $res;
  2952. }
  2953.  
  2954. function get_kids_parents_mails($kids)
  2955. {
  2956. $this->db->select('distinct(users.id) as user_id',FALSE);
  2957. $this->db->select('users.mail as user_mail',FALSE);
  2958. $this->db->select('kids.name as kid_name',FALSE);
  2959. $this->db->select('kids.birth_date as kid_birth_date',FALSE);
  2960.  
  2961. $this->db->from('kids');
  2962.  
  2963. $this->db->join('users', 'kids.parent_id = users.id');
  2964.  
  2965. $this->db->where_in('kids.id', $kids);
  2966. $this->db->where('users.app_name', '1');
  2967.  
  2968. $parents = $this->db->get()->result_array();
  2969.  
  2970. $res = array();
  2971.  
  2972. foreach ($parents as $p=>$parent)
  2973. {
  2974. $res[$p] = $parent;
  2975. $year = date('Y');
  2976. $kid_bd = $parent['kid_birth_date'];
  2977. $kid_year = date('Y', strtotime($kid_bd));
  2978.  
  2979. $res[$p]['kid_age'] = intval($year) - intval($kid_year);
  2980. }
  2981.  
  2982. return $res;
  2983. }
  2984.  
  2985. function get_kid_parent($kid_id)
  2986. {
  2987. $this->db->select('users.id as id');
  2988. $this->db->select('users.first_name as first_name');
  2989. $this->db->select('users.type as type');
  2990. $this->db->select('users.mail as mail');
  2991. $this->db->select('kids.name as name');
  2992. $this->db->select('users.app_name as app_name');
  2993. $this->db->select('gans.name as gan_name');
  2994.  
  2995. $this->db->from('kids');
  2996.  
  2997. $this->db->join('users', 'users.id = kids.parent_id');
  2998. $this->db->join('kids_classes', 'kids_classes.kid_id = kids.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
  2999. $this->db->join('gans', 'gans.id = kids_classes.gan_id');
  3000.  
  3001. $this->db->where('kids.id', $kid_id);
  3002.  
  3003. $query = $this->db->get();
  3004.  
  3005. return reset($query->result_array());
  3006. }
  3007.  
  3008. function udpate_mail_key($id)
  3009. {
  3010. $key = md5(uniqid(rand(), true));
  3011.  
  3012. $data = array(
  3013. 'mail_key' => $key
  3014. );
  3015.  
  3016. $this->db->where('id', $id);
  3017. $this->db->update('users', $data);
  3018.  
  3019. }
  3020.  
  3021. function get_users_kids_2014()
  3022. {
  3023. // $this->db->select('gans.name as gan_name',FALSE);
  3024. // $this->db->select('classes.name as class_name',FALSE);
  3025. // $this->db->select('kids.name as kid_name',FALSE);
  3026. $this->db->select('users.first_name as first_name',FALSE);
  3027. $this->db->select('users.mail as mail',FALSE);
  3028. $this->db->select('users.id as id',FALSE);
  3029. $this->db->select('users.mail_key as mail_key',FALSE);
  3030.  
  3031. $this->db->from('users');
  3032.  
  3033. $this->db->join('kids', 'kids.parent_id = users.id');
  3034. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
  3035. $this->db->join('classes', 'kids_classes.class_id = classes.id');
  3036. $this->db->join('gans', 'kids_classes.gan_id = gans.id');
  3037.  
  3038. $this->db->where('kids_classes.active', 1);
  3039. $this->db->where('kids_classes.year', 2014);
  3040.  
  3041. $this->db->group_by('users.id');
  3042.  
  3043. $users = $this->db->get()->result_array();
  3044.  
  3045. foreach ($users as $u=>$user)
  3046. {
  3047. $this->db->select('gans.name as gan_name',FALSE);
  3048. $this->db->select('classes.name as class_name',FALSE);
  3049. $this->db->select('kids.name as kid_name',FALSE);
  3050.  
  3051. $this->db->from('users');
  3052.  
  3053. $this->db->join('kids', 'kids.parent_id = users.id');
  3054. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id');
  3055. $this->db->join('classes', 'kids_classes.class_id = classes.id');
  3056. $this->db->join('gans', 'kids_classes.gan_id = gans.id');
  3057.  
  3058. $this->db->where('users.id', $user['id']);
  3059. $this->db->where('kids_classes.active', 1);
  3060. $this->db->where('kids_classes.year', 2014);
  3061.  
  3062. $kids = $this->db->get()->result_array();
  3063.  
  3064. foreach ($kids as $k=>$kid)
  3065. {
  3066. foreach ($kid as $key=>$value)
  3067. {
  3068. $kid[$key] = str_replace('"', "", $kid[$key]);
  3069. $users[$u][$key.$k] = $kid[$key];
  3070. }
  3071.  
  3072. }
  3073.  
  3074. if(count($kids) == 1)
  3075. {
  3076. $users[$u]['gan_name1'] = " ";
  3077. $users[$u]['class_name1'] = " ";
  3078. $users[$u]['kid_name1'] = " ";
  3079.  
  3080. $users[$u]['gan_name2'] = " ";
  3081. $users[$u]['class_name2'] = " ";
  3082. $users[$u]['kid_name2'] = " ";
  3083. }
  3084. else if(count($kids) == 2)
  3085. {
  3086. $users[$u]['gan_name2'] = " ";
  3087. $users[$u]['class_name2'] = " ";
  3088. $users[$u]['kid_name2'] = " ";
  3089. }
  3090.  
  3091. }
  3092.  
  3093. $res = array();
  3094.  
  3095. foreach ($users as $u=>$user)
  3096. {
  3097. unset($user['id']);
  3098. $res[] = $user;
  3099. }
  3100.  
  3101. return $res;
  3102. }
  3103.  
  3104. function userkey($key)
  3105. {
  3106. $this->db->select('id');
  3107.  
  3108. $this->db->where('mail_key', $key);
  3109.  
  3110. $this->db->from('users');
  3111.  
  3112. $query = $this->db->get();
  3113.  
  3114. return $query->row()->id;
  3115. }
  3116.  
  3117. function get_teacher_by_gan($gan_id)
  3118. {
  3119. $this->db->select('id');
  3120.  
  3121. $this->db->where('gan_id', $gan_id);
  3122.  
  3123. $this->db->from('users');
  3124.  
  3125. $query = $this->db->get();
  3126.  
  3127. return $query->row()->id;
  3128. }
  3129.  
  3130. function get_teacher_by_class($class_id)
  3131. {
  3132. $this->db->select('users.id as user_id');
  3133. $this->db->select('gans.id as gan_id');
  3134.  
  3135. $this->db->where('classes.id', $class_id);
  3136.  
  3137. $this->db->from('users');
  3138.  
  3139. $this->db->join('gans', 'gans.id = users.gan_id');
  3140. $this->db->join('classes', 'gans.id = classes.gan_id');
  3141.  
  3142. return $this->db->get()->result_array();
  3143. }
  3144.  
  3145. function get_teacher_name_by_class($class_id)
  3146. {
  3147. $this->db->select('users.first_name as teacher_first_name');
  3148. $this->db->select('users.last_name as teacher_last_name');
  3149. $this->db->select('users.mail as teacher_mail');
  3150. $this->db->select('gans.name as gan_name');
  3151.  
  3152. $this->db->where('classes.id', $class_id);
  3153.  
  3154. $this->db->from('users');
  3155.  
  3156. $this->db->join('gans', 'gans.id = users.gan_id');
  3157. $this->db->join('classes', 'gans.id = classes.gan_id');
  3158.  
  3159. return reset($this->db->get()->result_array());
  3160. }
  3161.  
  3162. function get_parents_classes_messages($parents_ids)
  3163. {
  3164. $this->db->select('distinct(messages.id) as message_id');
  3165. $this->db->select('classes.id as class_id');
  3166. $this->db->select('users.id as user_id');
  3167.  
  3168. $this->db->from('messages');
  3169.  
  3170. $this->db->join('classes', 'messages.class_id = classes.id');
  3171. $this->db->join('kids_classes', 'kids_classes.class_id = classes.id');
  3172. $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  3173. $this->db->join('users', 'kids.parent_id = users.id');
  3174.  
  3175. $this->db->where_in('users.id', $parents_ids);
  3176. $this->db->where('kids_classes.active', '1');
  3177. $this->db->where('messages.year', CURRENT_YEAR);
  3178.  
  3179. // $this->db->limit(50);
  3180.  
  3181. $messages = $this->db->get()->result_array();
  3182.  
  3183. $classes = array();
  3184. $users = array();
  3185.  
  3186. foreach ($messages as $m => $message)
  3187. {
  3188. $users[$message['user_id']][] = $message;
  3189. }
  3190.  
  3191. $res = array();
  3192.  
  3193. foreach ($users as $u => $user)
  3194. {
  3195. $arr = array("user_id" => $u,"messages"=>$user);
  3196. $res[] = $arr;
  3197. }
  3198.  
  3199.  
  3200. return $res;
  3201. }
  3202.  
  3203. function get_parents_classes_pics($parents_ids)
  3204. {
  3205. $this->db->select('COUNT(distinct pictures.id) as count_pic');
  3206. $this->db->select('users.id as user_id');
  3207.  
  3208. $this->db->from('pictures');
  3209.  
  3210. $this->db->join('albums', 'albums.id = pictures.album_id');
  3211. $this->db->join('classes', 'albums.class_id = classes.id');
  3212. $this->db->join('kids_classes', 'kids_classes.class_id = classes.id');
  3213. $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  3214. $this->db->join('users', 'kids.parent_id = users.id');
  3215.  
  3216. $this->db->where_in('users.id', $parents_ids);
  3217. $this->db->where('kids_classes.active', '1');
  3218. $this->db->where('pictures.active', '1');
  3219. $this->db->where('albums.year', CURRENT_YEAR);
  3220.  
  3221. $this->db->group_by('users.id');
  3222.  
  3223. $pictures = $this->db->get()->result_array();
  3224.  
  3225. $users = array();
  3226.  
  3227. foreach ($pictures as $p => $picture)
  3228. {
  3229. $users[$picture['user_id']][] = $picture['count_pic'];
  3230. }
  3231.  
  3232.  
  3233. return $pictures;
  3234. }
  3235.  
  3236. function get_user_app($user_id)
  3237. {
  3238. $this->db->select('app_name');
  3239.  
  3240. $this->db->where('id', $user_id);
  3241.  
  3242. $this->db->from('users');
  3243.  
  3244. $query = $this->db->get();
  3245.  
  3246. return $query->row()->app_name;
  3247. }
  3248.  
  3249. function get_user_app_mail($mail)
  3250. {
  3251. $this->db->select('app_name');
  3252.  
  3253. $this->db->where('mail', $mail);
  3254.  
  3255. $this->db->from('users');
  3256.  
  3257. $query = $this->db->get();
  3258.  
  3259. return $query->row()->app_name;
  3260. }
  3261.  
  3262. function is_user_teacher($user_id)
  3263. {
  3264. $this->db->select('type');
  3265.  
  3266. $this->db->where('id', $user_id);
  3267.  
  3268. $this->db->from('users');
  3269.  
  3270. $query = $this->db->get();
  3271.  
  3272. return ($query->row()->type == "2");
  3273. }
  3274.  
  3275. function create_user_push($push_id,$token_id)
  3276. {
  3277. $res = $this->db->insert('users_push', array('push_id'=>$push_id,'token_id'=>$token_id,'date'=>date("Y-m-d H:i:s")));
  3278. }
  3279.  
  3280. function is_user_pta_in_class_or_teacher($user_id,$class_id)
  3281. {
  3282.  
  3283. // return 0 - parent, return 1 - teacher, return 2 - PTA
  3284.  
  3285. $this->db->select('type');
  3286.  
  3287. $this->db->where('id', $user_id);
  3288.  
  3289. $this->db->from('users');
  3290.  
  3291. $query = $this->db->get();
  3292.  
  3293. if($query->row()->type != "2") //parent
  3294. {
  3295. $this->db->select('active');
  3296.  
  3297. $this->db->where('user_id', $user_id);
  3298. $this->db->where('class_id', $class_id);
  3299.  
  3300. $this->db->from('classes_vaad');
  3301.  
  3302. $query = $this->db->get();
  3303.  
  3304. if($query->num_rows == 0)
  3305. {
  3306. return 0;
  3307. }
  3308.  
  3309. $active = $query->row()->active;
  3310.  
  3311. if($active == "1")
  3312. {
  3313. return 2;
  3314. }
  3315. else
  3316. {
  3317. return 0;
  3318. }
  3319. }
  3320. else
  3321. {
  3322. return 1;
  3323. }
  3324.  
  3325.  
  3326. }
  3327.  
  3328. function get_permissions_for_user($user_id)
  3329. {
  3330. $res = array();
  3331.  
  3332. $this->db->select('users.type');
  3333. $this->db->select('users.gan_id');
  3334.  
  3335. $this->db->from('users');
  3336.  
  3337. $this->db->where('users.id', $user_id);
  3338.  
  3339. $user = reset($this->db->get()->result_array());
  3340.  
  3341. if($user['type'] == '2')
  3342. {
  3343. $res = $this->get_permissions_for_gan($user['gan_id']);
  3344. }
  3345.  
  3346. return $res;
  3347. }
  3348.  
  3349.  
  3350. function get_permissions_for_gan($gan_id)
  3351. {
  3352. $this->db->from('gans_permissions');
  3353.  
  3354. $this->db->where('gans_permissions.active', '1');
  3355. $this->db->where('gans_permissions.gan_id', $gan_id);
  3356.  
  3357. $permissions = $this->db->get()->result_array();
  3358.  
  3359. $res = array();
  3360.  
  3361. foreach ($permissions as $permission)
  3362. {
  3363. if($permission['permission_type'] == "1")
  3364. {
  3365. $res['like_forbidden'] = true;
  3366. }
  3367. else if($permission['permission_type'] == "2")
  3368. {
  3369. $res['comment_forbidden'] = true;
  3370. }
  3371. }
  3372.  
  3373. return $res;
  3374. }
  3375.  
  3376. function get_users_mails_in_class($class_id)
  3377. {
  3378. $this->db->select('distinct(users.mail) as user_mail');
  3379. $this->db->select('users.first_name as first_name');
  3380.  
  3381. $this->db->from('classes');
  3382.  
  3383. $this->db->join('kids_classes', 'kids_classes.class_id = classes.id and kids_classes.active=1 and kids_classes.year='.CURRENT_YEAR);
  3384. $this->db->join('kids', 'kids.id = kids_classes.kid_id');
  3385. $this->db->join('users', 'users.id = kids.parent_id');
  3386.  
  3387. $this->db->where('classes.id', $class_id);
  3388.  
  3389. $mails = $this->db->get()->result_array();
  3390.  
  3391. return $mails;
  3392. }
  3393.  
  3394.  
  3395. function get_user_kids_2($parent_id)
  3396. {
  3397. $this->db->select('users.type as type',FALSE);
  3398.  
  3399. $this->db->from('users');
  3400.  
  3401. $this->db->where('users.id', $parent_id);
  3402.  
  3403. $query = $this->db->get();
  3404.  
  3405. $type = $query->row()->type;
  3406.  
  3407. $this->db->select('kids.name as kid_name',FALSE);
  3408. $this->db->select('kids.birth_date as kid_bd',FALSE);
  3409. $this->db->select('kids.id as kid_id',FALSE);
  3410. $this->db->select('kids.pic as kid_pic',FALSE);
  3411. $this->db->select('kids.gender as kid_gender',FALSE);
  3412. // $this->db->select('gans.id as gan_id',FALSE);
  3413. // $this->db->select('gans.name as gan_name',FALSE);
  3414. // $this->db->select('gans.max as gan_max',FALSE);
  3415. // $this->db->select('gans.address as gan_address',FALSE);
  3416. // $this->db->select('gans.gan_phone as gan_phone',FALSE);
  3417. // $this->db->select('gans.cityName as gan_city',FALSE);
  3418. // $this->db->select('gans.city as gan_city_id',FALSE);
  3419. // $this->db->select('gans.code as gan_code',FALSE);
  3420. // $this->db->select('classes.name as class_name',FALSE);
  3421. // $this->db->select('classes.id as class_id',FALSE);
  3422. // $this->db->select('users.id as teacher_id',FALSE);
  3423. // $this->db->select('users.mail as teacher_mail',FALSE);
  3424. // $this->db->select('users.first_name as teacher_first_name',FALSE);
  3425. // $this->db->select('users.last_name as teacher_last_name',FALSE);
  3426. // $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
  3427. // $this->db->select('users.home_phone as teacher_phone',FALSE);
  3428. $this->db->select('kids_classes.active as kid_active',FALSE);
  3429. $this->db->select('kids_classes.year as class_year',FALSE);
  3430. $this->db->select('kids_classes.class_id as class_id',FALSE);
  3431.  
  3432. $this->db->order_by("kids_classes.year", "desc");
  3433. $this->db->order_by("kids_classes.active", "asc");
  3434.  
  3435. $this->db->from('kids');
  3436.  
  3437. $this->db->join('kids_classes', 'kids.id = kids_classes.kid_id','left');
  3438. // $this->db->join('classes', 'kids_classes.class_id = classes.id','left');
  3439. // $this->db->join('gans', 'kids_classes.gan_id = gans.id','left');
  3440. // $this->db->join('users', 'users.gan_id = gans.id','left');
  3441.  
  3442. $this->db->where('kids.parent_id', $parent_id);
  3443.  
  3444. $query = $this->db->get();
  3445.  
  3446. $kids = $query->result_array();
  3447.  
  3448. if(count($kids) == 0)
  3449. {
  3450. return array();
  3451. }
  3452.  
  3453. $classes_ids = array();
  3454.  
  3455. foreach ($kids as $k => $kid)
  3456. {
  3457. if(!in_array($kid['class_id'], $classes_ids))
  3458. {
  3459. $classes_ids[] = $kid['class_id'];
  3460. }
  3461. }
  3462.  
  3463. /*************************************************************/
  3464.  
  3465. $this->db->select('gans.id as gan_id',FALSE);
  3466. $this->db->select('gans.name as gan_name',FALSE);
  3467. $this->db->select('gans.max as gan_max',FALSE);
  3468. $this->db->select('gans.address as gan_address',FALSE);
  3469. $this->db->select('gans.gan_phone as gan_phone',FALSE);
  3470. $this->db->select('gans.cityName as gan_city',FALSE);
  3471. $this->db->select('gans.city as gan_city_id',FALSE);
  3472. $this->db->select('gans.code as gan_code',FALSE);
  3473. $this->db->select('classes.name as class_name',FALSE);
  3474. $this->db->select('classes.id as class_id',FALSE);
  3475. $this->db->select('users.id as teacher_id',FALSE);
  3476. $this->db->select('users.mail as teacher_mail',FALSE);
  3477. $this->db->select('users.first_name as teacher_first_name',FALSE);
  3478. $this->db->select('users.last_name as teacher_last_name',FALSE);
  3479. $this->db->select('users.mobile_phone as teacher_mobile',FALSE);
  3480. $this->db->select('users.home_phone as teacher_phone',FALSE);
  3481.  
  3482. $this->db->from('classes');
  3483.  
  3484. $this->db->join('gans', 'classes.gan_id = gans.id');
  3485. $this->db->join('users', 'users.gan_id = gans.id');
  3486.  
  3487. $this->db->where_in('classes.id', $classes_ids);
  3488.  
  3489. $this->db->group_by('classes.id');
  3490.  
  3491. $query = $this->db->get();
  3492.  
  3493. $classes = $query->result_array();
  3494.  
  3495. foreach ($kids as $k => $kid)
  3496. {
  3497. foreach ($classes as $c => $class)
  3498. {
  3499. if($kid['class_id'] == $class['class_id'])
  3500. {
  3501. $kids[$k] = array_merge($kids[$k],$class);
  3502. }
  3503. }
  3504. }
  3505.  
  3506. /*************************************************************/
  3507.  
  3508. $new_kids_current_year = array();
  3509. $new_kids_last_year = array();
  3510.  
  3511. foreach ($kids as $k => $kid)
  3512. {
  3513. if($kid['class_year'] == CURRENT_YEAR)
  3514. {
  3515. $new_kids_current_year[] = $kid;
  3516. }
  3517. else
  3518. {
  3519. $new_kids_last_year[] = $kid;
  3520. }
  3521. }
  3522.  
  3523.  
  3524. foreach ($new_kids_current_year as $k => $kid)
  3525. {
  3526. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  3527. {
  3528. $v = $kid;
  3529. unset($new_kids_current_year[$k]);
  3530. $last_key = key( array_slice( $new_kids_current_year, -1, 1, TRUE ) );
  3531. $new_kids_current_year[$last_key+1] = $v;
  3532. }
  3533. }
  3534.  
  3535. $new_kids_1 = array_merge($new_kids_current_year, $new_kids_last_year);
  3536.  
  3537. $new_kids = array();
  3538.  
  3539. foreach ($new_kids_1 as $k => $kid)
  3540. {
  3541. $gan_city_id = $kid['gan_city_id'];
  3542. $gan_city = $kid['gan_city'];
  3543. if($gan_city_id > 0 && ($gan_city == null || $gan_city == "0"))
  3544. {
  3545. $new_kids_1[$k]['gan_city'] = $this->city_model->get_name($gan_city_id);
  3546. }
  3547. unset($kid['gan_city_id']);
  3548.  
  3549. $kid['type'] = $type;
  3550.  
  3551. if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '1')
  3552. {
  3553. $kid['current_year'] = CURRENT_YEAR;
  3554. $class_id = $kid['class_id'];
  3555. $unread_messages = $this->message_model->get_count_unread_messgaes($parent_id,$class_id);
  3556. $unseen_photos = $this->album_model->get_count_unseen_pics_for_user($parent_id,$class_id);
  3557.  
  3558. $kid['unread_messages'] = (string)$unread_messages;
  3559. $kid['unseen_photos'] = (string)$unseen_photos;
  3560.  
  3561. $kid['vaad_type'] = $this->get_parent_type($parent_id,$class_id,$type);
  3562.  
  3563. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  3564.  
  3565. $kid = array_merge($kid, $permissions);
  3566.  
  3567. $new_kids[$kid['kid_id']] = $kid;
  3568. }
  3569. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '2')
  3570. {
  3571. $kid['current_year'] = CURRENT_YEAR;
  3572. $new_kids[$kid['kid_id']] = $kid;
  3573. }
  3574. else if($kid['class_year'] == CURRENT_YEAR && $kid['kid_active'] == '0')
  3575. {
  3576. if(
  3577. isset($new_kids[$kid['kid_id']]['class_year']) &&
  3578. ($new_kids[$kid['kid_id']]['class_year']) == CURRENT_YEAR &&
  3579. isset($new_kids[$kid['kid_id']]['kid_active']) &&
  3580. ($new_kids[$kid['kid_id']]['kid_active']) != '0'
  3581. )
  3582. {
  3583.  
  3584. }
  3585. else
  3586. {
  3587. $kid_details = $this->set_kid_array_values($kid,false);
  3588. $new_kids[$kid['kid_id']] = $kid_details;
  3589. }
  3590. }
  3591. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] != null)
  3592. {
  3593. if(!array_key_exists ( $kid['kid_id'] , $new_kids ))
  3594. {
  3595. $kid_details = $this->set_kid_array_values($kid,false);
  3596. $new_kids[$kid['kid_id']] = $kid_details;
  3597. }
  3598.  
  3599. if($kid['kid_active'] != '0')
  3600. {
  3601. $permissions = $this->get_permissions_for_gan($kid['gan_id']);
  3602. $kid = array_merge($kid, $permissions);
  3603.  
  3604. $history = $this->set_kid_array_values($kid,true);
  3605. $new_kids[$kid['kid_id']]['history'][] = $history;
  3606. }
  3607. }
  3608. else if($kid['class_year'] < CURRENT_YEAR && $kid['class_id'] == null)
  3609. {
  3610. $kid = $this->set_kid_array_values($kid);
  3611. $new_kids[$kid['kid_id']] = $kid;
  3612. }
  3613.  
  3614. }
  3615.  
  3616. return array_values($new_kids);
  3617.  
  3618.  
  3619. }
  3620.  
  3621. function generateAuthToken($userid) {
  3622. $token = hash('sha256', $userid . rand() . time());
  3623. return $token;
  3624. }
  3625.  
  3626. function updateAuthToken($userid, $newToken) {
  3627. if(isset($userid) && isset($newToken)) {
  3628. $this->db->where('id', $userid);
  3629. $data = array(
  3630. 'auth_token' => $newToken
  3631. );
  3632. $this->db->update('users', $data);
  3633. }
  3634. }
  3635.  
  3636. function checkPermissions($authToken, $method_name, $method_full, $anonymous_functions, $teacher_functions, $unsigned_functions, $args) {
  3637.  
  3638. if(!isset($method_name) || $method_name == "") {
  3639. error_log("*** No method");
  3640. return false;
  3641. }
  3642.  
  3643. if(in_array($method_name, $anonymous_functions)) {
  3644. error_log("*** passing anonymous function: " . $method_name);
  3645. return true;
  3646. }
  3647.  
  3648. //$authToken = 'bla'; //'ivo123';
  3649.  
  3650. if(!isset($authToken) || $authToken == "") {
  3651. error_log("*** No auth token");
  3652. return false;
  3653. }
  3654.  
  3655. if(($authToken == '7caae84146a63264b33rfu562677062f9dfa3c70e3311eac4edd23d85ea542de' && $_SERVER['REMOTE_ADDR'] == '188.226.178.75') || ($method_name == 'getfavorite' && $_SERVER['REMOTE_ADDR'] == '188.226.178.75')) {
  3656. error_log("*** Allowing dashboard calls");
  3657. return true;
  3658. }
  3659.  
  3660. $this->db->select('id',FALSE);
  3661. $this->db->select('mail',FALSE);
  3662. $this->db->select('gan_id',FALSE);
  3663. $this->db->select('type',FALSE);
  3664. $this->db->select('active',FALSE);
  3665. $this->db->select('activation',FALSE);
  3666. $this->db->select('auth_token',FALSE);
  3667. $query = $this->db->get_where('users',array('auth_token' => $authToken));
  3668.  
  3669. if($query->num_rows() <= 0) {
  3670. error_log("*** No user for token: " . $authToken);
  3671. return false;
  3672. }
  3673.  
  3674. $user = reset($query->result_array());
  3675.  
  3676. //error_log(print_r($user, TRUE));
  3677.  
  3678. if($user["type"] != 2 && in_array($method_name, $teacher_functions)) {
  3679. error_log("*** User with auth token " . $authToken . " is NOT a teacher");
  3680. return false;
  3681. }
  3682.  
  3683. $query->free_result();
  3684.  
  3685. $classes = Array();
  3686. $kids = Array();
  3687.  
  3688. if($method_name != "getkindergarten" && isset($args["user_id"]) && $args["user_id"] != "") {
  3689. $user_id = $args["user_id"];
  3690.  
  3691. if(!in_array($method_name, $unsigned_functions)) {
  3692. $user_id = $this->util_model->get_decrypt_user_id($user_id);
  3693. }
  3694.  
  3695. if($user_id != $user["id"]) {
  3696. error_log("*** Wrong caller id");
  3697. return false;
  3698. }
  3699. }
  3700.  
  3701. // load classes
  3702. if($user["gan_id"] != null) {
  3703. $this->db->select('id', FALSE);
  3704. $query = $this->db->get_where('classes', Array('gan_id' => $user["gan_id"]));
  3705.  
  3706. if($query->num_rows() > 0) {
  3707. foreach ($query->result_array() as $row) {
  3708. $classes[] = $row["id"];
  3709. }
  3710. }
  3711.  
  3712. $query->free_result();
  3713.  
  3714. if(count($classes) > 0) {
  3715. $this->db->select('kid_id',FALSE);
  3716. $this->db->distinct();
  3717. $this->db->from('kids_classes');
  3718. $this->db->where_in('class_id', $classes);
  3719. $query = $this->db->get();
  3720.  
  3721. if($query->num_rows() > 0) {
  3722. foreach ($query->result_array() as $row) {
  3723. $kids[] = $row["kid_id"];
  3724. }
  3725. }
  3726.  
  3727. $query->free_result();
  3728. }
  3729.  
  3730. } else {
  3731. $this->db->select('id', FALSE);
  3732. $query = $this->db->get_where('kids', Array('parent_id' => $user["id"]));
  3733. if($query->num_rows() > 0) {
  3734. foreach ($query->result_array() as $row) {
  3735. $kids[] = $row["id"];
  3736. }
  3737. }
  3738. $query->free_result();
  3739.  
  3740. if(count($kids) > 0) {
  3741. $this->db->select('class_id',FALSE);
  3742. $this->db->distinct();
  3743. $this->db->from('kids_classes');
  3744. $this->db->where_in('kid_id', $kids);
  3745. $query = $this->db->get();
  3746. if($query->num_rows() > 0) {
  3747. foreach ($query->result_array() as $row) {
  3748. $classes[] = $row["class_id"];
  3749. }
  3750. }
  3751. $query->free_result();
  3752. }
  3753.  
  3754. }
  3755.  
  3756.  
  3757. //error_log(print_r($classes, TRUE));
  3758. //error_log(print_r($kids, TRUE));
  3759.  
  3760.  
  3761.  
  3762. if(isset($args["class_id"]) && $args["class_id"] != "" && $method_name != 'setclass' && !in_array($args["class_id"], $classes)) {
  3763. error_log("*** Wrong class");
  3764. return false;
  3765. }
  3766.  
  3767. if(isset($args["kid_id"]) && $args["kid_id"] != "" && !in_array($args["kid_id"], $kids)) {
  3768. error_log("*** Wrong kid");
  3769. return false;
  3770. }
  3771.  
  3772. if(isset($args["gan_id"]) && $args["gan_id"] != "" && $args["gan_id"] != $user["gan_id"]) {
  3773. error_log("*** Wrong gan id");
  3774. return false;
  3775. }
  3776.  
  3777. if(isset($args["parent_id"]) && $args["parent_id"] != "") {
  3778. $parent_id = $args["parent_id"];
  3779.  
  3780. if(!in_array($method_name, $unsigned_functions)) {
  3781. $parent_id = $this->util_model->get_decrypt_user_id($parent_id);
  3782. }
  3783.  
  3784. if($parent_id != $user["id"]) {
  3785. error_log("*** Wrong parent id");
  3786. return false;
  3787. }
  3788. }
  3789.  
  3790. if(isset($args["album_id"]) && $args["album_id"] != "") {
  3791.  
  3792. $this->db->select('class_id', FALSE);
  3793. $query = $this->db->get_where('albums',array('id' => $args["album_id"]));
  3794.  
  3795. if($query->num_rows() <= 0) {
  3796. error_log("*** No such album: " . $args["album_id"]);
  3797. return false;
  3798. }
  3799.  
  3800. $album = reset($query->result_array());
  3801.  
  3802. if(!in_array($album["class_id"], $classes)) {
  3803. error_log("*** No permissions for album: " . $args["album_id"]);
  3804. return false;
  3805. }
  3806.  
  3807. $query->free_result();
  3808.  
  3809. }
  3810.  
  3811. return true;
  3812. }
  3813. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement