public function ILTScoreForm::buildForm in Opigno Instructor-led Trainings 3.x
Same name and namespace in other branches
- 8 src/Form/ILTScoreForm.php \Drupal\opigno_ilt\Form\ILTScoreForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ ILTScoreForm.php, line 59
Class
- ILTScoreForm
- Provides a form for scoring a opigno_ilt entity.
Namespace
Drupal\opigno_ilt\FormCode
public function buildForm(array $form, FormStateInterface $form_state, ILTInterface $opigno_ilt = NULL) {
$this->opigno_ilt = $opigno_ilt;
$training = $opigno_ilt
->getTraining();
if (!isset($training)) {
$message = $this
->t('The Instructor-Led Training should has related training to save the presences.');
$this
->messenger()
->addError($message);
return $form;
}
$form['submit_scores'] = [
'#type' => 'table',
'#title' => $this
->t('Participants for @training', [
'@training' => $training
->label(),
]),
'#header' => [
$this
->t('Name'),
$this
->t('Attendance'),
$this
->t('Score'),
],
];
$scores =& $form['submit_scores'];
// Load the Instructor-Led Training members
// or the related training members, if there is no member restriction.
$users = $opigno_ilt
->getMembers();
if (empty($users)) {
$members = $training
->getMembers();
$users = array_map(function ($member) {
/** @var \Drupal\group\GroupMembership $member */
return $member
->getUser();
}, $members);
}
uasort($users, function ($user1, $user2) {
/** @var \Drupal\user\Entity\User $user1 */
/** @var \Drupal\user\Entity\User $user2 */
return strcasecmp($user1
->getDisplayName(), $user2
->getDisplayName());
});
// Load the existing Instructor-Led Training results.
/** @var \Drupal\opigno_ilt\ILTResultInterface[] $results */
$results = $this->entityTypeManager
->getStorage('opigno_ilt_result')
->loadByProperties([
'opigno_ilt' => $opigno_ilt
->id(),
]);
// Reindex results by the user ID.
$results_by_user = [];
array_walk($results, function ($result) use (&$results_by_user) {
/** @var \Drupal\opigno_ilt\ILTResultInterface $result */
$results_by_user[$result
->getUserId()] = $result;
});
foreach ($users as $user) {
$id = $user
->id();
if (isset($results_by_user[$id])) {
// If result for this Instructor-Led Training
// and user is exists, use it.
/** @var \Drupal\opigno_ilt\ILTResultInterface $result */
$result = $results_by_user[$id];
$attendance = $result
->getStatus();
$score = $result
->getScore();
}
else {
// Else get the default values.
$attendance = 1;
$score = 100;
}
$scores[$id]['name'] = $user
->toLink()
->toRenderable();
$scores[$id]['attendance'] = [
'#type' => 'select',
'#options' => [
0 => $this
->t('Absent'),
1 => $this
->t('Attended'),
],
'#default_value' => $attendance,
];
$scores[$id]['score'] = [
'#type' => 'number',
'#min' => 0,
'#max' => 100,
'#step' => 1,
'#default_value' => $score,
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save attendances'),
];
return $form;
}