@@ -102,31 +102,19 @@ public function __invoke(Request $request, Response $response, $args)
102102 } else {
103103 // Handle video roles
104104
105- // get all videos the user has permissions on
106- foreach (VideosUserPerms::findByUser_id ($ user_id ) as $ vperm ) {
107- if (!$ vperm ->video ->episode ) continue ;
108-
109- if ($ vperm ->perm == 'owner ' || $ vperm ->perm == 'write ' ) {
110- if ($ episode_id_role_access ) {
111- $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ;
112- $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_WRITE ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_WRITE ' ;
113- } else {
114- $ roles [$ vperm ->video ->episode . '_write ' ] = $ vperm ->video ->episode . '_write ' ;
115- }
116- } else {
117- if ($ episode_id_role_access ) {
118- $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ;
119- } else {
120- $ roles [$ vperm ->video ->episode . '_read ' ] = $ vperm ->video ->episode . '_read ' ;
121- }
122- }
123- }
124-
125- // get all videos in courseware blocks in courses and add them to the permission list as well
105+ // get all videos of courseware blocks in courses and add them to the permission list as well, ignore if user has permission through a course role
126106 $ stmt_courseware = \DBManager::get ()->prepare ("SELECT episode FROM oc_video_cw_blocks
127107 LEFT JOIN oc_video USING (token)
128108 LEFT JOIN seminar_user USING (seminar_id)
129- WHERE seminar_user.user_id = :user_id " );#
109+ WHERE seminar_user.user_id = :user_id
110+ AND NOT EXISTS (
111+ SELECT 1
112+ FROM oc_playlist_video
113+ JOIN oc_playlist_seminar USING (playlist_id)
114+ WHERE video_id = oc_video.id
115+ AND oc_video_cw_blocks.seminar_id = oc_playlist_seminar.seminar_id
116+ )
117+ " );
130118
131119 $ stmt_courseware ->execute ([':user_id ' => $ user_id ]);
132120
@@ -180,6 +168,37 @@ public function __invoke(Request $request, Response $response, $args)
180168 $ roles [$ course_id . '_Learner ' ] = $ course_id . '_Learner ' ;
181169 }
182170
171+ // Get all videos permissions of the user except those for videos to which the user has already write
172+ // permissions through course memberships and course playlists, in order to reduce the number of roles.
173+ $ vperms = VideosUserPerms::findBySQL ("
174+ user_id = :user_id
175+ AND NOT EXISTS (
176+ SELECT 1
177+ FROM oc_playlist_video
178+ JOIN oc_playlist_seminar USING (playlist_id)
179+ WHERE video_id = oc_video_user_perms.video_id AND seminar_id IN ( :course_ids )
180+ )
181+ " , [':user_id ' => $ user_id , ':course_ids ' => $ courses_write ]);
182+
183+ foreach ($ vperms as $ vperm ) {
184+ if (!$ vperm ->video ->episode ) continue ;
185+
186+ if ($ vperm ->perm == 'owner ' || $ vperm ->perm == 'write ' ) {
187+ if ($ episode_id_role_access ) {
188+ $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ;
189+ $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_WRITE ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_WRITE ' ;
190+ } else {
191+ $ roles [$ vperm ->video ->episode . '_write ' ] = $ vperm ->video ->episode . '_write ' ;
192+ }
193+ } else {
194+ if ($ episode_id_role_access ) {
195+ $ roles ['ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ] = 'ROLE_EPISODE_ ' . $ vperm ->video ->episode . '_READ ' ;
196+ } else {
197+ $ roles [$ vperm ->video ->episode . '_read ' ] = $ vperm ->video ->episode . '_read ' ;
198+ }
199+ }
200+ }
201+
183202 // Handle playlist roles
184203
185204 if (PlaylistMigration::isConverted ()) {
0 commit comments