Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ public function __toString()
foreach ($this->events as $event) {
for ($d = 0; $d <= $event[2] - 1; $d++) {
if (date('y-m-d', strtotime($this->active_year . '-' . $this->active_month . '-' . $i . ' -' . $d . ' day')) === date('y-m-d', strtotime($event[1]))) {
$html .= '<a href="/bob/show/' . $event[4] . '"><div class="event' . $event[3] . '">';
if ($event[4]!==null)
$html .= '<a href="/bob/show/' . $event[4] . '">';
$html .= '<div class="event' . $event[3] . '">';
$html .= $event[0];
$html .= '</div></a>';
$html .= '</div>';
if ($event[4]!==null)
$html .= '</a>';
}
}
}
Expand Down
66 changes: 38 additions & 28 deletions app/Http/Controllers/ControlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,39 +710,49 @@ public function destroy(int $id)

public function history()
{
// Not API and auditee
abort_if(
Auth::User()->role === 4 || Auth::User()->role === 5,
Response::HTTP_FORBIDDEN,
'403 Forbidden'
);
// Abort if user role is 4 or 5
abort_if(in_array(Auth::user()->role, [4, 5]), Response::HTTP_FORBIDDEN, '403 Forbidden');

// Get all controls
$controls = DB::table('controls')
->select('id', 'score', 'observations', 'realisation_date', 'plan_date')
->get();

// Fetch measures for all controls in one query
$controlMeasures = DB::table('control_measure')
->select(['control_id', 'clause'])
->leftjoin('measures', 'measures.id', '=', 'measure_id')
->whereIn('control_id', $controls->pluck('id'))
// Fetch controls and their measures in a single query
$controlsData = DB::table('controls')
->select(
'controls.id',
'controls.score',
'controls.observations',
'controls.realisation_date',
'controls.plan_date',
'controls.periodicity',
'measures.clause')
->leftJoin('control_measure', 'controls.id', '=', 'control_measure.control_id')
->join('measures', 'control_measure.measure_id', '=', 'measures.id')
->get();

// Group measures by control_id
$measuresByControlId = $controlMeasures->groupBy('control_id');

// map clauses
$controls = $controlsData->groupBy('id')->map(function ($controlGroup) {
$control = $controlGroup->first();
$control->measures = $controlGroup->pluck('clause')->filter()->values();
return $control;
})->values();

// Repeat controls based on periodicity
$expandedControls = collect();
foreach ($controls as $control) {
$control->measures = $measuresByControlId
->get($control->id, collect())
->map(function ($controlMeasure) {
return $controlMeasure->clause;
});
$expandedControls->push($control);

if (($control->realisation_date==null)&&($control->periodicity>0))
for ($i = 1; $i <= 12 / $control->periodicity; $i++) {
$repeatedControl = clone $control;
$repeatedControl->id = null;
$repeatedControl->score = null;
$repeatedControl->observations = null;
$repeatedControl->realisation_date = null;
$repeatedControl->plan_date = Carbon::parse($control->plan_date)->addMonthsNoOverflow($i * $control->periodicity);
$expandedControls->push($repeatedControl);
}
}

// return
return view('controls.history')->with('controls', $controls);
// Return view with controls
return view('controls.history')
->with('controls', $expandedControls);
}

/*
Expand Down Expand Up @@ -1265,7 +1275,7 @@ public function make(Request $request)
} else {
$next_date =
$control->next_date === null
? \Carbon\Carbon::createFromFormat('Y-m-d', $control->plan_date)
? Carbon::createFromFormat('Y-m-d', $control->plan_date)
->addMonthsNoOverflow($control->periodicity)
->format('Y-m-d')
: $control->next_date->format('Y-m-d');
Expand Down