class ILTScoreForm in Opigno Instructor-led Trainings 8
Same name and namespace in other branches
- 3.x src/Form/ILTScoreForm.php \Drupal\opigno_ilt\Form\ILTScoreForm
Provides a form for scoring a opigno_ilt entity.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\opigno_ilt\Form\ILTScoreForm
Expanded class hierarchy of ILTScoreForm
1 string reference to 'ILTScoreForm'
File
- src/
Form/ ILTScoreForm.php, line 15
Namespace
Drupal\opigno_ilt\FormView source
class ILTScoreForm extends FormBase {
/**
* The Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Opigno ILT.
*
* @var \Drupal\opigno_ilt\ILTInterface
*/
protected $opigno_ilt;
/**
* Creates a ILTScoreForm object.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'opigno_ilt_score_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this->opigno_ilt;
$scores = $form_state
->getValue('submit_scores');
foreach ($scores as $user_id => $values) {
$status = $values['attendance'];
$score = $values['score'];
// Try load existing result.
/** @var \Drupal\opigno_ilt\ILTResultInterface[] $results */
$results = $this->entityTypeManager
->getStorage('opigno_ilt_result')
->loadByProperties([
'opigno_ilt' => $entity
->id(),
'user_id' => $user_id,
]);
$result = current($results);
if ($result === FALSE) {
// Create new result.
$result = ILTResult::create();
$result
->setILT($entity);
$result
->setUserId($user_id);
}
// Update values.
$result
->setStatus($status);
$result
->setScore($score);
$result
->save();
// Update user achievements.
$gid = $entity
->getTrainingId();
if (isset($gid)) {
$step = opigno_learning_path_get_ilt_step($gid, $user_id, $entity);
opigno_learning_path_save_step_achievements($gid, $user_id, $step, 0);
opigno_learning_path_save_achievements($gid, $user_id, TRUE);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
ILTScoreForm:: |
protected | property | The Entity Type Manager. | |
ILTScoreForm:: |
protected | property | Opigno ILT. | |
ILTScoreForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ILTScoreForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ILTScoreForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ILTScoreForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ILTScoreForm:: |
public | function | Creates a ILTScoreForm object. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |