fix calculator

This commit is contained in:
aslan 2024-03-07 15:09:01 +03:00
parent 5d7863a655
commit d0abc35a07
2 changed files with 27 additions and 17 deletions

View File

@ -3,36 +3,45 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\Direction;
use App\Models\EntranceExamination;
use App\Models\Faculty;
use Illuminate\Http\Request;
use function _PHPStan_cc8d35ffb\React\Promise\map;
class CalculatorController extends Controller
{
public function findDirectionFromSubjects(Request $request)
{
$getsSubjects = $request->input();
$getJSON = $request->input()['predmets'];
$getsSubjects = json_decode($getJSON);
$countUserSubjects = count($getsSubjects);
// $entranceExamination = EntranceExamination::all()
// ->whereIn('subject_id', $result)
// ->where('subject_type_id','=',1)
// ->select('subject_id','direction_id')
// ->whereIn('subject_id', $result)
// ->groupBy('direction_id')
// ->toArray();
// dd($entranceExamination);
// $directions = Direction::all();
$findIds = EntranceExamination::all()
$filteredDirectionIds = EntranceExamination::all()
->select("direction_id", "subject_id")
->groupBy('direction_id')
->map(function ($direction) {
return $direction->map(fn($item) => $item['subject_id']);
})
->filter(fn ($direction) => count($direction) <= $countUserSubjects)
->filter(fn($direction) => count($direction) <= $countUserSubjects)
->keys();
$directions = Direction::whereIn('id', $findIds)->get();
return response()->json($directions);
$directions = Direction::whereIn('id', $filteredDirectionIds)->get();
$generateHtml = function ($acc, $direction) {
$department = Department::find($direction->department_id);
$faculty = Faculty::find($department->faculty_id);
return "{$acc} <tr class=\"\">
<td id=\"faculty\"> {$faculty->name} </td>
<td> {$direction->name} </td>
<td class=\"text-end\"> {$direction->period} </td>
</tr>";
};
$html = $directions->reduce($generateHtml, '');
return response()->json(['html' => $html]);
}
}

View File

@ -494,7 +494,7 @@
</div>
<script>window.onload = function() {
$(".calcul input").click(function(){
var selected = []; let predmets='';
let selected = []; let predmets='';
$('.calcul input:checked').each(function() {
selected.push($(this).val());
predmets += $(this).val()+',';
@ -502,9 +502,10 @@
console.log(selected);
$(".calcul_rez").html('<tr><td>обрабатываем</td></tr>');
$.ajax({ url: "json.php", dataType: 'json', cache:false,type: "POST",data: 'ajx=get_napr&format=html&predmets='+selected,
$.ajax({ url: "{{ route('calculator') }}", dataType: 'json', cache:false,type: "POST",data: 'ajx=get_napr&format=html&predmets='+JSON.stringify(selected),
success: function(data) {
$(".calcul_rez").html(data.html);
console.log(data);
}});
});
};