/fuel/app/modules/user/classes/model/user.php
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
- <?php
- /**
- * Code-Grounds is an interactive programming tutorial on C/C++, Facebook application.
- * 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.
- *
- * @package CodeGrounds
- * @version 1.2
- * @author Team Untitled
- * @copyright 2013 - 2014 Team Untitled
- */
-
- namespace User;
-
- /**
- * This manages all query request on user module
- *
- * @package user
- * @extends Model
- */
- class Model_User extends \Model
- {
-
- /**
- * Get user count on the database
- *
- * @access public
- * @return Integer [# of accounts registered]
- */
- public static function get_user_count()
- {
- $result = 0;
-
- try {
- $result = \DB::count_records("user");
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Get the user's name
- *
- * @access public
- * @param String $uid [Encrypted User ID]
- * @return String [Username]
- */
- public static function get_username($uid = null)
- {
- $result = "";
-
- try {
- if ( is_null($uid) ) {
- $uid = \Session::get("uid");
- }
-
- $query = \DB::select("username")
- ->from("user")
- ->where("id", '=', $uid)
- ->execute()
- ->as_array();
-
- if ( ! empty($query) ) {
- $result = $query[0]["username"];
- }
- } catch(\Database_Exception $e) {
- \Helper::alert("alert", $e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Create new user account
- *
- * @access public
- * @param String $fbID [Encrypted FB uid]
- * @return NULL/Integer $result [Return"s the new user id if successfully created, else return NULL]
- */
- public static function create_user($fbID, $username)
- {
- $result = null;
-
- try {
- list($result,) = \DB::insert("user")
- ->columns(array(
- "fb_uid",
- "username",
- "date_created"
- ))
- ->values(array(
- $fbID,
- $username,
- date("Y-m-d H:i:s")
- ))
- ->execute();
-
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Retrieve the user's group ID
- *
- * @access public
- * @param String $uid [Encrypted User ID]
- * @return Array [Group ID & Name ]
- */
- public static function get_user_group($uid = null)
- {
- $result = null;
-
- try {
- if ( is_null($uid) ) {
- $uid = \Session::get("uid");
- }
-
- $query = \DB::select("group")
- ->from("user")
- ->where("id", '=', $uid)
- ->execute()
- ->as_array();
-
- if ( ! empty($query) ) {
- $result = $query[0]["group"];
- }
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Retrieve user information on the database
- *
- * @access public
- * @param String $uid [Encrypted user ID]
- * @return Array [User Information]
- */
- public static function get_user_stats($uid = null)
- {
- $result = null;
-
- try {
- if ( is_null($uid) ) {
- $uid = \Session::get("uid");
- }
- //
- // Retrieve last code date
- //
- $query = \DB::select("last_coded", "current_exp")
- ->from("user")
- ->where("id", '=', $uid)
- ->execute()
- ->as_array();
-
- $result["last_coded"] = $query[0]["last_coded"];
- $result["current_exp"] = $query[0]["current_exp"];
- //
- // Get the number of tracks completed
- //
- $query = \DB::select("id")
- ->from("track_progress")
- ->where("is_accomplished", '=', 1)
- ->and_where("user_id", '=', $uid)
- ->execute()
- ->as_array();
-
- $result["track_count"] = count($query);
- //
- // Get user's achievement(s)
- //
- $query = \DB::select(array("achievement.id", "id"),
- array("achievement.title", "title"),
- array("achievement.image_path", "image_path"))
- ->from("achievement")
- ->join("user_achievement", "LEFT")
- ->on("user_achievement.achievement_id", '=', "achievement.id")
- ->where("user_achievement.account_id", '=', $uid)
- ->limit(7)
- ->execute()
- ->as_array();
-
- $result["achievements"] = $query;
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Retrieve the User's ID by their Facebook UID
- *
- * @access public
- * @param String $id [Encrypted Facebook UID]
- * @return String $result [User's ID]
- */
- public static function get_uid_with_fb_id($fbID)
- {
- $result = null;
-
- try {
- $query = \DB::select("id")
- ->from("user")
- ->where("fb_uid",'=', $fbID)
- ->execute()
- ->as_array();
-
- if ( ! empty($query) ) {
- $result = $query[0]["id"];
- }
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /**
- * Get the list of users on the database
- *
- * @access public
- * @return Array [User List with ID & FB UID]
- */
- public static function get_user_list()
- {
- $result = array();
-
- try {
- $result = \DB::select("id", "fb_uid")
- ->from("user")
- ->execute()
- ->as_array();
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- /***/
- public static function get_top_users()
- {
- $result = array();
-
- try {
- $result = \DB::select()
- ->from("user")
- ->order_by("current_exp", "desc")
- ->limit(10)
- ->where("last_coded", '!=', '')
- ->execute()
- ->as_array();
- } catch(\Database_Exception $e) {
- \Helper::error($e->getMessage());
- }
-
- return $result;
- }
-
- }