Last active
December 11, 2015 00:59
-
-
Save Sigmus/4520113 to your computer and use it in GitHub Desktop.
Refactored Permission class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Permission { | |
public static function ask() | |
{ | |
return static::action('Question'); | |
} | |
public static function answer() | |
{ | |
return static::action('Answer'); | |
} | |
public static function vote() | |
{ | |
return static::action('Vote'); | |
} | |
public static function denounce() | |
{ | |
return static::action('Denounce'); | |
} | |
/** | |
* Check if the user has permission to perform an action today. | |
* | |
* @param string $model | |
* @param string $field | |
* @return boolean | |
*/ | |
private static function action($model, $field = '') | |
{ | |
$limit_per_day = static::query_action_limit(Session::get('ULVL'), $model, $field); | |
$user_did_today = static::query_user_actions_today(Session::get('UID'), $model)->count(); | |
return $limit_per_day - $user_did_today > 0; | |
} | |
/** | |
* | |
* Query the $model and get the limit for one day. If $field isn't | |
* provided, assume a lower-cased-pluralized version of $model. | |
* | |
* @param int $level | |
* @param string $model | |
* @param string $field | |
* @return int | |
*/ | |
private static function query_action_limit($level, $model, $field = '') | |
{ | |
$field = (empty($field)) ? strtolower(Str::plural($model)) : $field; | |
return Permissionslevel::where_level($level)->first()->$field; | |
} | |
/** | |
* Get the actions performed by the user today | |
* | |
* @param int $user_id | |
* @param string $model | |
* @return Laravel\Query | |
*/ | |
private static function query_user_actions_today($user_id, $model) | |
{ | |
return $model::where_user_id($user_id) | |
->where('created_at', '>', date('Y-m-d') . ' 00:00:00') | |
->where('created_at', '<', date('Y-m-d') . ' 23:59:59'); | |
} | |
private static function choose_best_answer() | |
{ | |
//list($count_limit_day,$count_did_today) = self::query('Vote','votes'); | |
//$count_did_today = Answer::where('choosed', '=', '1')->where('created_at', '>', date('Y-m-d') . ' 00:00:00')->where('created_at', '<', date('Y-m-d') . ' 23:59:59')->question_id; | |
//$count_did_todayA = $count_did_today-> | |
$count_limit_day = Permissionslevel::where('level', '=', Session::get('ULVL'))->first()->best_answers; | |
if($count_limit_day - $count_did_today > 0) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment