PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/app/modules/user/classes/model/user.php

https://bitbucket.org/trujka/codegrounds
PHP | 267 lines | 159 code | 36 blank | 72 comment | 6 complexity | 0e71639e4f7861bd694a6fc09e23a1f5 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Code-Grounds is an interactive programming tutorial on C/C++, Facebook application.
  4. * It is a game-like coding application wherein people who have no/less knowledge with C/C++ programming can learn interactively and in a fun way.
  5. *
  6. * @package CodeGrounds
  7. * @version 1.2
  8. * @author Team Untitled
  9. * @copyright 2013 - 2014 Team Untitled
  10. */
  11. namespace User;
  12. /**
  13. * This manages all query request on user module
  14. *
  15. * @package user
  16. * @extends Model
  17. */
  18. class Model_User extends \Model
  19. {
  20. /**
  21. * Get user count on the database
  22. *
  23. * @access public
  24. * @return Integer [# of accounts registered]
  25. */
  26. public static function get_user_count()
  27. {
  28. $result = 0;
  29. try {
  30. $result = \DB::count_records("user");
  31. } catch(\Database_Exception $e) {
  32. \Helper::error($e->getMessage());
  33. }
  34. return $result;
  35. }
  36. /**
  37. * Get the user's name
  38. *
  39. * @access public
  40. * @param String $uid [Encrypted User ID]
  41. * @return String [Username]
  42. */
  43. public static function get_username($uid = null)
  44. {
  45. $result = "";
  46. try {
  47. if ( is_null($uid) ) {
  48. $uid = \Session::get("uid");
  49. }
  50. $query = \DB::select("username")
  51. ->from("user")
  52. ->where("id", '=', $uid)
  53. ->execute()
  54. ->as_array();
  55. if ( ! empty($query) ) {
  56. $result = $query[0]["username"];
  57. }
  58. } catch(\Database_Exception $e) {
  59. \Helper::alert("alert", $e->getMessage());
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Create new user account
  65. *
  66. * @access public
  67. * @param String $fbID [Encrypted FB uid]
  68. * @return NULL/Integer $result [Return"s the new user id if successfully created, else return NULL]
  69. */
  70. public static function create_user($fbID, $username)
  71. {
  72. $result = null;
  73. try {
  74. list($result,) = \DB::insert("user")
  75. ->columns(array(
  76. "fb_uid",
  77. "username",
  78. "date_created"
  79. ))
  80. ->values(array(
  81. $fbID,
  82. $username,
  83. date("Y-m-d H:i:s")
  84. ))
  85. ->execute();
  86. } catch(\Database_Exception $e) {
  87. \Helper::error($e->getMessage());
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Retrieve the user's group ID
  93. *
  94. * @access public
  95. * @param String $uid [Encrypted User ID]
  96. * @return Array [Group ID & Name ]
  97. */
  98. public static function get_user_group($uid = null)
  99. {
  100. $result = null;
  101. try {
  102. if ( is_null($uid) ) {
  103. $uid = \Session::get("uid");
  104. }
  105. $query = \DB::select("group")
  106. ->from("user")
  107. ->where("id", '=', $uid)
  108. ->execute()
  109. ->as_array();
  110. if ( ! empty($query) ) {
  111. $result = $query[0]["group"];
  112. }
  113. } catch(\Database_Exception $e) {
  114. \Helper::error($e->getMessage());
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Retrieve user information on the database
  120. *
  121. * @access public
  122. * @param String $uid [Encrypted user ID]
  123. * @return Array [User Information]
  124. */
  125. public static function get_user_stats($uid = null)
  126. {
  127. $result = null;
  128. try {
  129. if ( is_null($uid) ) {
  130. $uid = \Session::get("uid");
  131. }
  132. //
  133. // Retrieve last code date
  134. //
  135. $query = \DB::select("last_coded", "current_exp")
  136. ->from("user")
  137. ->where("id", '=', $uid)
  138. ->execute()
  139. ->as_array();
  140. $result["last_coded"] = $query[0]["last_coded"];
  141. $result["current_exp"] = $query[0]["current_exp"];
  142. //
  143. // Get the number of tracks completed
  144. //
  145. $query = \DB::select("id")
  146. ->from("track_progress")
  147. ->where("is_accomplished", '=', 1)
  148. ->and_where("user_id", '=', $uid)
  149. ->execute()
  150. ->as_array();
  151. $result["track_count"] = count($query);
  152. //
  153. // Get user's achievement(s)
  154. //
  155. $query = \DB::select(array("achievement.id", "id"),
  156. array("achievement.title", "title"),
  157. array("achievement.image_path", "image_path"))
  158. ->from("achievement")
  159. ->join("user_achievement", "LEFT")
  160. ->on("user_achievement.achievement_id", '=', "achievement.id")
  161. ->where("user_achievement.account_id", '=', $uid)
  162. ->limit(7)
  163. ->execute()
  164. ->as_array();
  165. $result["achievements"] = $query;
  166. } catch(\Database_Exception $e) {
  167. \Helper::error($e->getMessage());
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Retrieve the User's ID by their Facebook UID
  173. *
  174. * @access public
  175. * @param String $id [Encrypted Facebook UID]
  176. * @return String $result [User's ID]
  177. */
  178. public static function get_uid_with_fb_id($fbID)
  179. {
  180. $result = null;
  181. try {
  182. $query = \DB::select("id")
  183. ->from("user")
  184. ->where("fb_uid",'=', $fbID)
  185. ->execute()
  186. ->as_array();
  187. if ( ! empty($query) ) {
  188. $result = $query[0]["id"];
  189. }
  190. } catch(\Database_Exception $e) {
  191. \Helper::error($e->getMessage());
  192. }
  193. return $result;
  194. }
  195. /**
  196. * Get the list of users on the database
  197. *
  198. * @access public
  199. * @return Array [User List with ID & FB UID]
  200. */
  201. public static function get_user_list()
  202. {
  203. $result = array();
  204. try {
  205. $result = \DB::select("id", "fb_uid")
  206. ->from("user")
  207. ->execute()
  208. ->as_array();
  209. } catch(\Database_Exception $e) {
  210. \Helper::error($e->getMessage());
  211. }
  212. return $result;
  213. }
  214. /***/
  215. public static function get_top_users()
  216. {
  217. $result = array();
  218. try {
  219. $result = \DB::select()
  220. ->from("user")
  221. ->order_by("current_exp", "desc")
  222. ->limit(10)
  223. ->where("last_coded", '!=', '')
  224. ->execute()
  225. ->as_array();
  226. } catch(\Database_Exception $e) {
  227. \Helper::error($e->getMessage());
  228. }
  229. return $result;
  230. }
  231. }