DeleteAchievementsForm.php in Opigno Learning path 3.x
File
src/Form/DeleteAchievementsForm.php
View source
<?php
namespace Drupal\opigno_learning_path\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\group\Entity\Group;
use Drupal\opigno_module\Entity\OpignoModule;
use Drupal\opigno_module\Entity\UserModuleStatus;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeleteAchievementsForm extends ConfirmFormBase {
protected $group;
protected $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function getFormId() {
return 'opigno_learning_path_delete_achievements';
}
public function buildForm(array $form, FormStateInterface $form_state, $group = NULL) {
$form = parent::buildForm($form, $form_state);
if (isset($group)) {
$this->group = $group;
$form['group'] = [
'#type' => 'hidden',
'#value' => $group
->id(),
];
}
$form['actions']['submit']['#name'] = 'submit';
$form['actions']['cancel']['#type'] = 'submit';
$form['actions']['cancel']['#name'] = 'cancel';
$form['actions']['cancel']['#value'] = $this
->getCancelText();
$is_ajax = $this
->getRequest()
->isXmlHttpRequest();
if ($is_ajax) {
$form['actions']['cancel']['#ajax'] = [
'callback' => [
$this,
'closeModal',
],
'event' => 'click',
];
}
return $form;
}
public function getQuestion() {
return '';
}
public function getDescription() {
return $this
->t('The contents of that training has changed. If you start again that training, your previous achievements for it will be deleted. Do you want to continue?');
}
public function getCancelUrl() {
if (isset($this->group)) {
return $this->group
->toUrl();
}
else {
return Url::fromRoute('<front>');
}
}
public function getConfirmText() {
return $this
->t('Yes');
}
public function getCancelText() {
return $this
->t('No');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
if (isset($element['#name'])) {
if ($element['#name'] === 'submit') {
$uid = $this
->currentUser()
->id();
$gid = $form_state
->getValue('group');
if (isset($gid)) {
$this->database
->delete('opigno_learning_path_step_achievements')
->condition('uid', $uid)
->condition('gid', $gid)
->execute();
$this->database
->delete('opigno_learning_path_achievements')
->condition('uid', $uid)
->condition('gid', $gid)
->execute();
$group = Group::load($gid);
if (isset($group)) {
$modules = $group
->getContentEntities('opigno_module_group');
$this
->deleteAnswers((int) $group
->id(), $uid, $modules);
$module = reset($modules);
if (!empty($module)) {
$attempt = UserModuleStatus::create([]);
$attempt
->setModule($module);
$attempt
->setFinished(0);
$attempt
->save();
}
}
$form_state
->setRedirect('opigno_learning_path.steps.start', [
'group' => $gid,
]);
}
}
elseif ($element['#name'] === 'cancel') {
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}
}
private function deleteAnswers(int $group_id, int $uid, array $modules) {
foreach ($modules as $module) {
if (!$module instanceof OpignoModule) {
continue;
}
$user_module_statuses = \Drupal::entityTypeManager()
->getStorage('user_module_status')
->loadByProperties([
'learning_path' => $group_id,
'user_id' => $uid,
'module' => $module
->id(),
]);
foreach ($user_module_statuses as $status) {
if (!$status instanceof UserModuleStatus) {
continue;
}
$answers = $status
->getAnswers();
foreach ($answers as $answer) {
$answer
->delete();
}
}
}
}
public function closeModal(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
}