Viewing file: RatingController.php (5.21 KB) -rwxrwxr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\AgentRating;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
class RatingController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
// $data = AgentRating::select(
// 'user_id',
// DB::raw('COUNT(*) AS total_rows'),
// DB::raw('COUNT(CASE WHEN rating > 0 THEN 1 END) AS rated_rows'),
// DB::raw('ROUND(AVG(CASE WHEN rating > 0 THEN rating END), 1) AS average_rating'),
// DB::raw('ROUND((COUNT(CASE WHEN rating > 0 THEN 1 END) / COUNT(*)) * 100, 1) AS rating_ratio')
// )
// ->with('agent_Data')
// ->groupBy('user_id')
// ->get();
if (Auth::user()->is_agent == 1) {
$data = AgentRating::with('agent_Data')->where('user_id', Auth::user()->id)->orderBy('id','desc')->limit(5000)->get();
}else {
$data = AgentRating::with('agent_Data')->orderBy('id','desc')->limit(5000)->get();
}
$agent=User::select('id','name')->where('is_agent',1)->get();
return view('rating.index',['ratings'=>$data,'agents'=>$agent]);
}
function filter(Request $req) {
$query= AgentRating::query();
$query->when($req->input('agent') !== 'all', function ($q) use ($req) {
$q->where('user_id', $req->input('agent'));
});
if ($req->input('date_range')) {
$date_range=explode('to',$req->input('date_range'));
$from_date=date_create($date_range[0]);
$from_date= date_format($from_date,"Y-m-d");
$to_date=date_create($date_range[1]);
$to_date= date_format($to_date,"Y-m-d");
$to_date = date(date('Y-m-d', strtotime($to_date. ' + 1 days')));
$query->where('created_at','>',$from_date);
$query->where('created_at','<',$to_date);
}
$data=$query->with('agent_Data')->limit(2000)->get();
$agent=User::select('id','name')->where('is_agent',1)->get();
return view("rating.index",['ratings'=>$data,'agents'=>$agent ,
'agent'=>$req->input('agent'),"date_range"=>$req->input('date_range')
]);
}
function details($user_id) {
$userStats = AgentRating::select(
DB::raw('COUNT(*) AS total_reviews'),
DB::raw('ROUND(AVG(CASE WHEN rating > 0 THEN rating END), 1) AS average_rating'),
DB::raw('COUNT(CASE WHEN rating = 5 THEN 1 END) AS total_5_star'),
DB::raw('COUNT(CASE WHEN rating = 4 THEN 1 END) AS total_4_star'),
DB::raw('COUNT(CASE WHEN rating = 3 THEN 1 END) AS total_3_star'),
DB::raw('COUNT(CASE WHEN rating = 2 THEN 1 END) AS total_2_star'),
DB::raw('COUNT(CASE WHEN rating = 1 THEN 1 END) AS total_1_star'),
DB::raw('ROUND((COUNT(CASE WHEN rating = 5 THEN 1 END) / COUNT(*)) * 100, 1) AS percentage_5_star'),
DB::raw('ROUND((COUNT(CASE WHEN rating = 4 THEN 1 END) / COUNT(*)) * 100, 1) AS percentage_4_star'),
DB::raw('ROUND((COUNT(CASE WHEN rating = 3 THEN 1 END) / COUNT(*)) * 100, 1) AS percentage_3_star'),
DB::raw('ROUND((COUNT(CASE WHEN rating = 2 THEN 1 END) / COUNT(*)) * 100, 1) AS percentage_2_star'),
DB::raw('ROUND((COUNT(CASE WHEN rating = 1 THEN 1 END) / COUNT(*)) * 100, 1) AS percentage_1_star')
)
->where('user_id', $user_id)
->where('rating','>',0)
->first();
$agent=User::where('id',$user_id)->first();
$data = AgentRating::with('service_Data')->where('user_id',$user_id)->orderBy('id', 'desc')->limit(500)->get();
return view('rating.details',['ratings'=>$data,'userStats'=>$userStats,'agent'=>$agent]);
}
function token_no() {
if (Auth::user()->is_agent == 0) {
abort(403, 'Unauthorized');
}
$data = AgentRating::where('user_id', Auth::user()->id)->whereDate('created_at', Carbon::today())->orderBy('id','desc')->limit(5000)->get();
return view('agent.token_no',['tokens'=>$data]);
}
function token_no_submit(Request $req) {
if (Auth::user()->is_agent == 0) {
abort(403, 'Unauthorized');
}
$validator = Validator::make($req->all(), [
'token_number' => 'required|string|max:255'
], [
'token_number.required' => 'Token Number can`t be empty',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors()->all())->withInput();
} else {
AgentRating::create([
'user_id' => Auth::user()->id,
'trans_id' => $req->token_number
]);
// Process the token number
return redirect()->back()->with('message', "Token Number submitted successfully");
}
}
}
|