Skip to content

Commit f693d6d

Browse files
committed
improve calendar
1 parent 6dd4746 commit f693d6d

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

app/Calendar.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public function __toString()
9292
foreach ($this->events as $event) {
9393
for ($d = 0; $d <= $event[2] - 1; $d++) {
9494
if (date('y-m-d', strtotime($this->active_year . '-' . $this->active_month . '-' . $i . ' -' . $d . ' day')) === date('y-m-d', strtotime($event[1]))) {
95-
$html .= '<a href="/bob/show/' . $event[4] . '"><div class="event' . $event[3] . '">';
95+
if ($event[4]!==null)
96+
$html .= '<a href="/bob/show/' . $event[4] . '">';
97+
$html .= '<div class="event' . $event[3] . '">';
9698
$html .= $event[0];
97-
$html .= '</div></a>';
99+
$html .= '</div>';
100+
if ($event[4]!==null)
101+
$html .= '</a>';
98102
}
99103
}
100104
}

app/Http/Controllers/ControlController.php

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -710,39 +710,49 @@ public function destroy(int $id)
710710

711711
public function history()
712712
{
713-
// Not API and auditee
714-
abort_if(
715-
Auth::User()->role === 4 || Auth::User()->role === 5,
716-
Response::HTTP_FORBIDDEN,
717-
'403 Forbidden'
718-
);
713+
// Abort if user role is 4 or 5
714+
abort_if(in_array(Auth::user()->role, [4, 5]), Response::HTTP_FORBIDDEN, '403 Forbidden');
719715

720-
// Get all controls
721-
$controls = DB::table('controls')
722-
->select('id', 'score', 'observations', 'realisation_date', 'plan_date')
723-
->get();
724-
725-
// Fetch measures for all controls in one query
726-
$controlMeasures = DB::table('control_measure')
727-
->select(['control_id', 'clause'])
728-
->leftjoin('measures', 'measures.id', '=', 'measure_id')
729-
->whereIn('control_id', $controls->pluck('id'))
716+
// Fetch controls and their measures in a single query
717+
$controlsData = DB::table('controls')
718+
->select(
719+
'controls.id',
720+
'controls.score',
721+
'controls.observations',
722+
'controls.realisation_date',
723+
'controls.plan_date',
724+
'controls.periodicity',
725+
'measures.clause')
726+
->leftJoin('control_measure', 'controls.id', '=', 'control_measure.control_id')
727+
->join('measures', 'control_measure.measure_id', '=', 'measures.id')
730728
->get();
731729

732730
// Group measures by control_id
733-
$measuresByControlId = $controlMeasures->groupBy('control_id');
734-
735-
// map clauses
731+
$controls = $controlsData->groupBy('id')->map(function ($controlGroup) {
732+
$control = $controlGroup->first();
733+
$control->measures = $controlGroup->pluck('clause')->filter()->values();
734+
return $control;
735+
})->values();
736+
737+
// Repeat controls based on periodicity
738+
$expandedControls = collect();
736739
foreach ($controls as $control) {
737-
$control->measures = $measuresByControlId
738-
->get($control->id, collect())
739-
->map(function ($controlMeasure) {
740-
return $controlMeasure->clause;
741-
});
740+
$expandedControls->push($control);
741+
742+
if (($control->realisation_date==null)&&($control->periodicity>0))
743+
for ($i = 1; $i <= 12 / $control->periodicity; $i++) {
744+
$repeatedControl = clone $control;
745+
$repeatedControl->id = null;
746+
$repeatedControl->score = null;
747+
$repeatedControl->observations = null;
748+
$repeatedControl->realisation_date = null;
749+
$repeatedControl->plan_date = Carbon::parse($control->plan_date)->addMonthsNoOverflow($i * $control->periodicity);
750+
$expandedControls->push($repeatedControl);
751+
}
742752
}
743-
744-
// return
745-
return view('controls.history')->with('controls', $controls);
753+
// Return view with controls
754+
return view('controls.history')
755+
->with('controls', $expandedControls);
746756
}
747757

748758
/*
@@ -1265,7 +1275,7 @@ public function make(Request $request)
12651275
} else {
12661276
$next_date =
12671277
$control->next_date === null
1268-
? \Carbon\Carbon::createFromFormat('Y-m-d', $control->plan_date)
1278+
? Carbon::createFromFormat('Y-m-d', $control->plan_date)
12691279
->addMonthsNoOverflow($control->periodicity)
12701280
->format('Y-m-d')
12711281
: $control->next_date->format('Y-m-d');

0 commit comments

Comments
 (0)