PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/models/user_model.old.php

https://github.com/mkhairul/Presta
PHP | 307 lines | 276 code | 25 blank | 6 comment | 14 complexity | b33006722db4e3d022410d724871f6a4 MD5 | raw file
  1. <?php
  2. class User_model extends Model {
  3. function User_model()
  4. {
  5. parent::Model();
  6. $this->table_name = 'user';
  7. if(!$this->db->table_exists($this->table_name))
  8. {
  9. $CI =& get_instance();
  10. $CI->load->dbforge();
  11. $CI->dbforge->add_key('id', TRUE);
  12. $fields = array(
  13. 'id' => array(
  14. 'type' => 'INT',
  15. 'constraint' => 11,
  16. 'unsigned' => TRUE,
  17. 'auto_increment' => TRUE
  18. ),
  19. 'department_id' => array(
  20. 'type' => 'INT',
  21. 'constraint' => 11,
  22. 'unsigned' => TRUE,
  23. 'default' => '0'
  24. ),
  25. 'position_id' => array(
  26. 'type' => 'INT',
  27. 'constraint' => 11,
  28. 'unsigned' => TRUE,
  29. 'default' => '0'
  30. ),
  31. 'type' => array(
  32. 'type' => 'VARCHAR',
  33. 'constraint' => '100'
  34. ),
  35. 'group_id' => array(
  36. 'type' => 'INT',
  37. 'constraint' => 11,
  38. 'unsigned' => TRUE,
  39. 'default' => '0'
  40. ),
  41. 'fullname' => array(
  42. 'type' => 'VARCHAR',
  43. 'constraint' => '255',
  44. 'null' => FALSE,
  45. ),
  46. 'employee_id' => array(
  47. 'type' => 'VARCHAR',
  48. 'constraint' => '255',
  49. 'null' => FALSE,
  50. ),
  51. 'group_id' => array(
  52. 'type' => 'INT',
  53. 'constraint' => 11,
  54. 'unsigned' => TRUE,
  55. 'default' => '0'
  56. ),
  57. 'reports_to' => array(
  58. 'type' =>'INT',
  59. 'constraint' => 11,
  60. 'null' => FALSE,
  61. ),
  62. 'username' => array(
  63. 'type' => 'VARCHAR',
  64. 'constraint' => '255',
  65. 'null' => FALSE,
  66. ),
  67. 'password' => array(
  68. 'type' => 'VARCHAR',
  69. 'constraint' => '150',
  70. 'null' => FALSE,
  71. ),
  72. 'email' => array(
  73. 'type' => 'VARCHAR',
  74. 'constraint' => '150',
  75. 'null' => FALSE,
  76. ),
  77. 'activated' => array(
  78. 'type' => 'TINYINT',
  79. 'constraint' => '1',
  80. 'default' => 1,
  81. 'null' => FALSE,
  82. ),
  83. 'timecreated' => array(
  84. 'type' => 'INT',
  85. 'constraint' => 11,
  86. 'null' => FALSE,
  87. )
  88. );
  89. $CI->dbforge->add_field($fields);
  90. if ($CI->dbforge->create_table($this->table_name))
  91. {
  92. log_message('debug', "User Table Created.. creating default account..");
  93. $CI->load->model('auth_model', 'auth');
  94. $password = $CI->auth->create_password('nameless');
  95. $data = array(
  96. 'group_id' => 1,
  97. 'type' => 'corporate',
  98. 'fullname' => 'Administrator',
  99. 'username' => 'admin',
  100. 'password' => $password,
  101. 'email' => 'admin@myprojectlog.com',
  102. 'timecreated' => strtotime('now')
  103. );
  104. $this->db->insert($this->table_name, $data);
  105. log_message('debug', "Admin account created");
  106. }
  107. }
  108. }
  109. function delete($id)
  110. {
  111. $this->db->where('id', $id);
  112. $this->db->delete('user');
  113. return TRUE;
  114. }
  115. function insert($data)
  116. {
  117. $this->db->set($data);
  118. $this->db->insert($this->table_name);
  119. }
  120. function update($id, $data)
  121. {
  122. $this->db->where('id', $id);
  123. $this->db->set($data);
  124. $this->db->update($this->table_name);
  125. }
  126. function get_department_id($user_id)
  127. {
  128. $this->db->where('id', $user_id);
  129. $query = $this->db->get($this->table_name);
  130. if($query->num_rows() > 0)
  131. {
  132. $result = $query->row();
  133. return $result->department_id;
  134. }
  135. else
  136. {
  137. return FALSE;
  138. }
  139. }
  140. function get_details($user_id)
  141. {
  142. $this->db->where('id', $user_id);
  143. $query = $this->db->get($this->table_name);
  144. return ($query->num_rows() > 0) ? $query->row():FALSE;
  145. }
  146. function get_groupid($user_id)
  147. {
  148. $this->db->where('id', $user_id);
  149. $query = $this->db->get($this->table_name);
  150. if($query->num_rows() > 0)
  151. {
  152. $result = $query->row();
  153. return $result->group_id;
  154. }
  155. else
  156. {
  157. return FALSE;
  158. }
  159. }
  160. function get_name($user_id)
  161. {
  162. $this->db->where('id', $user_id);
  163. $query = $this->db->get($this->table_name);
  164. if($query->num_rows() > 0)
  165. {
  166. $result = $query->row();
  167. return $result->fullname;
  168. }
  169. else
  170. {
  171. return FALSE;
  172. }
  173. }
  174. function get_list()
  175. {
  176. $query = $this->db->get($this->table_name);
  177. if($query->num_rows() > 0)
  178. {
  179. return $query;
  180. }
  181. else
  182. {
  183. return FALSE;
  184. }
  185. }
  186. function get_supervisor_list()
  187. {
  188. $CI =& get_instance();
  189. $CI->load->model('group_model', 'group');
  190. $group_id = $CI->group->get_id('supervisor');
  191. $this->db->where('group_id', $group_id);
  192. $query = $this->db->get($this->table_name);
  193. if($query->num_rows() > 0)
  194. {
  195. return $query;
  196. }
  197. else
  198. {
  199. return FALSE;
  200. }
  201. }
  202. function get_supervisor_name($dept_id)
  203. {
  204. $CI =& get_instance();
  205. $CI->load->model('group_model', 'group');
  206. $group_id = $CI->group->get_id('supervisor');
  207. $this->db->where('group_id', $group_id);
  208. $this->db->where('department_id', $dept_id);
  209. $query = $this->db->get($this->table_name);
  210. if($query->num_rows() > 0)
  211. {
  212. $result = $query->row();
  213. return $result->fullname;
  214. }
  215. else
  216. {
  217. return FALSE;
  218. }
  219. }
  220. function get_type($user_id)
  221. {
  222. $this->db->where('id', $user_id);
  223. $query = $this->db->get('user');
  224. if($query->num_rows() > 0)
  225. {
  226. $result = $query->row();
  227. return $result->type;
  228. }
  229. else
  230. {
  231. return FALSE;
  232. }
  233. }
  234. /*--------------------------------------------------------------------------
  235. Get the user's group, get the user's group's name. If the user is a supervisor,
  236. use the user's dept_id to retrieve the KPI id.
  237. If the user doesn't have any KPI ID, initialize KPI ID. The USER MUST HAVE KPI ID!
  238. -------------------------------------------------------------------------*/
  239. function get_kpi_id($user_id)
  240. {
  241. $this->db->where('id', $user_id);
  242. $query = $this->db->get($this->table_name);
  243. if($query->num_rows() > 0)
  244. {
  245. $user_details = $query->row();
  246. $CI =& get_instance();
  247. $CI->load->model('group_model', 'group');
  248. $group_name = $CI->group->get_name($user_details->group_id);
  249. $CI->load->model('kpi_model', 'kpi');
  250. if(strtolower($group_name) == 'supervisor')
  251. {
  252. $kpi_id = $CI->kpi->get_id_by_deptid($user_details->department_id);
  253. }
  254. else
  255. {
  256. $kpi_id = $CI->kpi->get_id_by_uid($user_details->id);
  257. }
  258. if(!$kpi_id)
  259. {
  260. $CI =& get_instance();
  261. $CI->load->model('kpi_model', 'kpi');
  262. $kpi_id = $CI->kpi->init_kpi_user($user_id);
  263. }
  264. return $kpi_id;
  265. }
  266. else
  267. {
  268. return FALSE;
  269. }
  270. }
  271. function total($all=0)
  272. {
  273. if(!$all)
  274. {
  275. $this->db->where('username !=', 'admin');
  276. }
  277. $query = $this->db->get($this->table_name);
  278. return $query->num_rows();
  279. }
  280. }
  281. ?>