|
13 | 13 | use PhpSchool\PhpWorkshop\ExerciseRenderer; |
14 | 14 | use PhpSchool\PhpWorkshop\ExerciseRepository; |
15 | 15 | use PhpSchool\PhpWorkshop\MenuItem\ResetProgress; |
| 16 | +use PhpSchool\PhpWorkshop\UserState; |
16 | 17 | use PhpSchool\PhpWorkshop\UserStateSerializer; |
17 | 18 | use PhpSchool\PhpWorkshop\WorkshopType; |
18 | 19 |
|
@@ -48,14 +49,11 @@ public function __invoke(ContainerInterface $c) |
48 | 49 | ->addStaticItem('---------') |
49 | 50 | ->addItems( |
50 | 51 | array_map(function (ExerciseInterface $exercise) use ($exerciseRenderer, $userState, $workshopType) { |
51 | | - $isCurrent = $exercise->getName() === $userState->getCurrentExercise(); |
52 | | - $isComplete = in_array($exercise->getName(), $userState->getCompletedExercises()); |
53 | | - |
54 | 52 | return [ |
55 | 53 | $exercise->getName(), |
56 | 54 | $exerciseRenderer, |
57 | 55 | $userState->completedExercise($exercise->getName()), |
58 | | - $workshopType == WorkshopType::TUTORIAL() && !$isCurrent && !$isComplete |
| 56 | + $this->isExerciseDisabled($exercise, $userState, $workshopType) |
59 | 57 | ]; |
60 | 58 | }, $exerciseRepository->findAll()) |
61 | 59 | ) |
@@ -99,4 +97,28 @@ public function __invoke(ContainerInterface $c) |
99 | 97 |
|
100 | 98 | return $builder->build(); |
101 | 99 | } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param ExerciseInterface $exercise |
| 103 | + * @param UserState $userState |
| 104 | + * @param WorkshopType $type |
| 105 | + * @return bool |
| 106 | + */ |
| 107 | + private function isExerciseDisabled(ExerciseInterface $exercise, UserState $userState, WorkshopType $type) |
| 108 | + { |
| 109 | + static $previous = null; |
| 110 | + |
| 111 | + if (null === $previous || !$type->isTutorialMode()) { |
| 112 | + $previous = $exercise; |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + if (in_array($previous->getName(), $userState->getCompletedExercises())) { |
| 117 | + $previous = $exercise; |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + $previous = $exercise; |
| 122 | + return true; |
| 123 | + } |
102 | 124 | } |
0 commit comments