Skip to content

Commit

Permalink
[Core] Assign quiz, question and category to teacher (author) #84
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentBouquet committed Jul 28, 2021
1 parent fce238e commit c1c02e0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
7 changes: 7 additions & 0 deletions public/js/bootstrap.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions public/js/jquery-3.3.1.slim.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions public/js/popper.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Controller/QuizController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function podium(Request $request, Quiz $quiz, EntityManagerInterface $em)
$this->denyAccessUnlessGranted('ROLE_TEACHER', null, 'Access not allowed');

if ($quiz->getActive()) {
// $quiz->setActive(false, $em); //TODO
// $quiz->setActiveInSession(false, $em); //TODO
$em->persist($quiz);
$em->flush();
}
Expand Down Expand Up @@ -183,7 +183,7 @@ public function podium(Request $request, Quiz $quiz, EntityManagerInterface $em)
public function activate(Request $request, Quiz $quiz, EntityManagerInterface $em): Response
{
$activate = ($request->query->get('active') == 1);
$quiz->setActive($activate, $em);
$quiz->setActiveInSession($activate, $em);
$em->persist($quiz);
$em->flush();

Expand All @@ -203,7 +203,7 @@ public function monitor(Request $request, Quiz $quiz, EntityManagerInterface $em
$this->denyAccessUnlessGranted('ROLE_TEACHER', null, 'Access not allowed');

if (!$quiz->getActive()) {
$quiz->setActive(true, $em);
$quiz->setActiveInSession(true, $em);
$em->persist($quiz);
$em->flush();
}
Expand Down
23 changes: 21 additions & 2 deletions src/Entity/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public function __construct()
$this->setUpdatedAt(new \DateTime());
$this->setShowResultQuestion(false);
$this->setShowResultQuiz(false);
$this->setNumberOfQuestions(10);
$this->setNumberOfQuestions(5);
$this->setDefaultQuestionMaxDuration(180);
$this->categories = new ArrayCollection();
$this->workouts = new ArrayCollection();
$this->setAllowAnonymousWorkout(false);
Expand Down Expand Up @@ -177,7 +178,25 @@ public function getActive(): ?bool
return $this->active;
}

public function setActive(bool $active, EntityManager $em): self
public function setActive(bool $active): self
{
$now = new DateTime();

if ($active) {
if (!$this->getActive()) {
$this->actived_at = $now;
}
} else {
$this->actived_at = null;
$session = $this->getLastSession();
}

$this->active = $active;

return $this;
}

public function setActiveInSession(bool $active, EntityManager $em): self
{
$now = new DateTime();

Expand Down
10 changes: 7 additions & 3 deletions src/Form/QuestionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class QuestionType extends AbstractType
{
private $translator;
private $param;
private $tokenStorage;

public function __construct(TranslatorInterface $translator, ParameterBagInterface $param)
public function __construct(TranslatorInterface $translator, ParameterBagInterface $param, TokenStorageInterface $tokenStorage)
{
$this->translator = $translator;
$this->param = $param;
$this->tokenStorage = $tokenStorage;
}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand All @@ -47,12 +50,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
case 'teacher':
$builder->add('text');
$builder->add('max_duration', IntegerType::class, array(
'required' => false,
'label' => 'Question max duration (seconds)',
));
$builder->add('categories', EntityType::class, array(
'class' => Category::class,
'query_builder' => function (CategoryRepository $er) {
return $er->createQueryBuilder('c')->andWhere('c.language = :language')->setParameter('language', $this->param->get('locale'))->orderBy('c.shortname', 'ASC');
'query_builder' => function (CategoryRepository $repository) {
return $repository->createQueryBuilder('c')->andWhere('c.created_by = :created_by')->setParameter('created_by', $this->tokenStorage->getToken()->getUser())->andWhere('c.language = :language')->setParameter('language', $this->param->get('locale'))->orderBy('c.shortname', 'ASC');
},
'choice_label' => 'longname',
'multiple' => true
Expand Down
8 changes: 6 additions & 2 deletions src/Form/QuizType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class QuizType extends AbstractType
{
private $translator;
private $param;
private $tokenStorage;

public function __construct(TranslatorInterface $translator, ParameterBagInterface $param)
public function __construct(TranslatorInterface $translator, ParameterBagInterface $param, TokenStorageInterface $tokenStorage)
{
$this->translator = $translator;
$this->param = $param;
$this->tokenStorage = $tokenStorage;

}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand All @@ -32,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add('categories', EntityType::class, array(
'class' => Category::class,
'query_builder' => function (CategoryRepository $er) {
return $er->createQueryBuilder('c')->andWhere('c.language = :language')->setParameter('language', $this->param->get('locale'))->orderBy('c.shortname', 'ASC');
return $er->createQueryBuilder('c')->andWhere('c.created_by = :created_by')->setParameter('created_by', $this->tokenStorage->getToken()->getUser())->andWhere('c.language = :language')->setParameter('language', $this->param->get('locale'))->orderBy('c.shortname', 'ASC');
},
'choice_label' => 'longname',
'multiple' => true
Expand Down
6 changes: 4 additions & 2 deletions src/Repository/QuizRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public function find($id, $lockMode = null, $lockVersion = null, $isTeacher = fa
$builder->andWhere('q.language = :language');
$builder->setParameter('language', $this->language);

$builder->andWhere('q.created_by = :created_by');
$builder->setParameter('created_by', $this->tokenStorage->getToken()->getUser());
// if (!$isAdmin) {
// $builder->andWhere('q.created_by = :created_by');
// $builder->setParameter('created_by', $this->tokenStorage->getToken()->getUser());
// }

$builder->orderBy('q.title', 'ASC');
return $builder->getQuery()->getOneOrNullResult();
Expand Down

0 comments on commit c1c02e0

Please sign in to comment.