Prechádzať zdrojové kódy

Redirect from course base to course session to avoid not allow pages

…see BT#12083
jmontoyaa 8 rokov pred
rodič
commit
8b9b914d4b
2 zmenil súbory, kde vykonal 50 pridanie a 2 odobranie
  1. 22 0
      main/inc/lib/sessionmanager.lib.php
  2. 28 2
      main/inc/local.inc.php

+ 22 - 0
main/inc/lib/sessionmanager.lib.php

@@ -8064,4 +8064,26 @@ class SessionManager
 
         return $htmlRes;
     }
+
+    /**
+     * @param int $userId
+     * @param int $courseId
+     *
+     * @return array
+     */
+    public static function searchCourseInSessionsFromUser($userId, $courseId)
+    {
+        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
+        $userId = (int) $userId;
+        $courseId = (int) $courseId;
+        if (empty($userId) || empty($courseId)) {
+            return [];
+        }
+
+        $sql = "SELECT * FROM $table 
+                WHERE c_id = $courseId AND user_id = $userId";
+        $result = Database::query($sql);
+
+        return Database::store_result($result, 'ASSOC');
+    }
 }

+ 28 - 2
main/inc/local.inc.php

@@ -1312,9 +1312,35 @@ if ((isset($uidReset) && $uidReset) || (isset($cidReset) && $cidReset)) {
                 }
             }
 
-            //If I'm the admin platform i'm a teacher of the course
+            // If I'm the admin platform i'm a teacher of the course
             if ($is_platformAdmin) {
-                $is_courseAdmin     = true;
+                $is_courseAdmin = true;
+            }
+        } else {
+            // User has not access to the course
+            // This will check if the course was added in one of his sessions
+            // Then it will be redirected to that course-session
+            if ($is_courseMember == false) {
+                // Search session
+                $courseSession = SessionManager::searchCourseInSessionsFromUser(
+                    $user_id,
+                    $_course['real_id']
+                );
+
+                if (!empty($courseSession) && isset($courseSession[0])) {
+                    $courseSessionItem = $courseSession[0];
+                    if (isset($courseSessionItem['session_id'])) {
+                        $customSessionId = $courseSessionItem['session_id'];
+                        $url = $_course['course_public_url'].'?id_session='.$customSessionId;
+
+                        Session::erase('_real_cid');
+                        Session::erase('_cid');
+                        Session::erase('_course');
+
+                        header('Location: '.$url);
+                        exit;
+                    }
+                }
             }
         }
     } else { // keys missing => not anymore in the course - user relation