1313use Chamilo \CoreBundle \DataTransformer \CourseToolDataTranformer ;
1414use Chamilo \CoreBundle \Entity \ResourceLink ;
1515use Chamilo \CoreBundle \Entity \User ;
16+ use Chamilo \CoreBundle \ServiceHelper \PluginServiceHelper ;
1617use Chamilo \CoreBundle \Settings \SettingsManager ;
18+ use Chamilo \CoreBundle \Tool \AbstractPlugin ;
1719use Chamilo \CoreBundle \Tool \ToolChain ;
1820use Chamilo \CoreBundle \Traits \CourseFromRequestTrait ;
1921use Chamilo \CourseBundle \Entity \CTool ;
@@ -37,6 +39,7 @@ public function __construct(
3739 private readonly Security $ security ,
3840 private readonly ToolChain $ toolChain ,
3941 protected RequestStack $ requestStack ,
42+ private readonly PluginServiceHelper $ pluginServiceHelper ,
4043 ) {
4144 $ this ->transformer = new CourseToolDataTranformer (
4245 $ this ->requestStack ,
@@ -51,23 +54,33 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5154 $ result = $ this ->provider ->provide ($ operation , $ uriVariables , $ context );
5255
5356 $ request = $ this ->requestStack ->getMainRequest ();
54-
5557 $ studentView = $ request ? $ request ->getSession ()->get ('studentview ' ) : 'studentview ' ;
5658
5759 /** @var User|null $user */
5860 $ user = $ this ->security ->getUser ();
5961
6062 $ isAllowToEdit = $ user && ($ user ->hasRole ('ROLE_ADMIN ' ) || $ user ->hasRole ('ROLE_CURRENT_COURSE_TEACHER ' ));
61- $ isAllowToEditBack = $ user && ($ user ->hasRole ('ROLE_ADMIN ' ) || $ user ->hasRole ('ROLE_CURRENT_COURSE_TEACHER ' ));
62- $ isAllowToSessionEdit = $ user && ($ user ->hasRole ('ROLE_ADMIN ' ) || $ user ->hasRole ('ROLE_CURRENT_COURSE_TEACHER ' ) || $ user ->hasRole ('ROLE_CURRENT_COURSE_SESSION_TEACHER ' ));
63+ $ isAllowToEditBack = $ isAllowToEdit ;
64+ $ isAllowToSessionEdit = $ user && (
65+ $ user ->hasRole ('ROLE_ADMIN ' ) ||
66+ $ user ->hasRole ('ROLE_CURRENT_COURSE_TEACHER ' ) ||
67+ $ user ->hasRole ('ROLE_CURRENT_COURSE_SESSION_TEACHER ' )
68+ );
6369
6470 $ allowVisibilityInSession = $ this ->settingsManager ->getSetting ('course.allow_edit_tool_visibility_in_session ' );
6571 $ session = $ this ->getSession ();
72+ $ course = $ this ->getCourse ();
73+
74+ [$ restrictToPositioning , $ allowedToolName ] = $ this ->shouldRestrictToPositioningOnly ($ user , $ course ->getId (), $ session ?->getId());
6675
6776 $ results = [];
6877
6978 /** @var CTool $cTool */
7079 foreach ($ result as $ cTool ) {
80+ if ($ restrictToPositioning && $ cTool ->getTool ()->getTitle () !== $ allowedToolName ) {
81+ continue ;
82+ }
83+
7184 $ toolModel = $ this ->toolChain ->getToolFromName (
7285 $ cTool ->getTool ()->getTitle ()
7386 );
@@ -107,4 +120,46 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
107120
108121 return $ results ;
109122 }
123+
124+ private function shouldRestrictToPositioningOnly (?User $ user , int $ courseId , ?int $ sessionId ): array
125+ {
126+ if (!$ user || !$ user ->hasRole ('ROLE_STUDENT ' )) {
127+ return [false , null ];
128+ }
129+
130+ $ tool = $ this ->toolChain ->getToolFromName ('positioning ' );
131+
132+ if (!$ tool instanceof AbstractPlugin) {
133+ return [false , null ];
134+ }
135+
136+ if (!$ this ->pluginServiceHelper ->isPluginEnabled ('positioning ' )) {
137+ return [false , null ];
138+ }
139+
140+ $ pluginInstance = $ this ->pluginServiceHelper ->loadLegacyPlugin ('Positioning ' );
141+
142+ if (!$ pluginInstance || 'true ' !== $ pluginInstance ->get ('block_course_if_initial_exercise_not_attempted ' )) {
143+ return [false , null ];
144+ }
145+
146+ $ initialData = $ pluginInstance ->getInitialExercise ($ courseId , $ sessionId );
147+
148+ if (!isset ($ initialData ['exercise_id ' ])) {
149+ return [false , null ];
150+ }
151+
152+ $ results = \Event::getExerciseResultsByUser (
153+ $ user ->getId (),
154+ (int ) $ initialData ['exercise_id ' ],
155+ $ courseId ,
156+ $ sessionId
157+ );
158+
159+ if (empty ($ results )) {
160+ return [true , 'positioning ' ];
161+ }
162+
163+ return [false , null ];
164+ }
110165}
0 commit comments