53 lines
1.8 KiB
PHP
53 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\UpdateStatisfationStatRequest;
|
|
use App\Models\FaqQuestions;
|
|
use App\Models\FaqThemes;
|
|
|
|
class FaqController extends Controller
|
|
{
|
|
/**
|
|
* OrderBy utility
|
|
* Working SQL query
|
|
*
|
|
* select * from `faq_themes`
|
|
* inner join `faq_questions` on `faq_themes`.`id` = `faq_questions`.`faq_themes_id`
|
|
* inner JOIN (
|
|
* SELECT (1 - faq_questions.negative_vote / faq_questions.positive_vote) AS `faq_questions_utility`
|
|
* FROM `faq_questions`
|
|
* ) b on `faq_themes`.`id` = `faq_questions`.`faq_themes_id`
|
|
* order by `faq_questions_utility` desc;
|
|
*
|
|
* @return void
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('front.faq.index', [
|
|
'themes' => FaqThemes::with(['questions' => function ($q) {
|
|
return $q->select('faq_questions.*')
|
|
->selectRaw(
|
|
'(1 - faq_questions.negative_vote / faq_questions.positive_vote) AS `faq_questions_utility`'
|
|
)->orderBy('faq_questions_utility', 'desc');
|
|
}])->orderBy('order')->get()
|
|
]);
|
|
}
|
|
|
|
public function updateSatisfactionStat(UpdateStatisfationStatRequest $request, FaqQuestions $question)
|
|
{
|
|
$opinion = (bool)$request->opinion;
|
|
$sessionKey = sprintf('faq.questions.%d', $question->id);
|
|
if (!session()->has($sessionKey)) {
|
|
session()->put($sessionKey, true);
|
|
session()->save();
|
|
if ($opinion) {
|
|
$question->positive_vote = intval(($question->positive_vote ?? 0) + 1);
|
|
} else {
|
|
$question->negative_vote = intval(($question->negative_vote ?? 0) + 1);
|
|
}
|
|
$question->saveOrFail();
|
|
}
|
|
return response()->json();
|
|
}
|
|
}
|