From 587540b5e4a32ce84e838cbdb8e9b50f9f87051e Mon Sep 17 00:00:00 2001
From: Malte Neugebauer <malte.neugebauer@hs-bochum.de>
Date: Tue, 22 Nov 2022 16:30:35 +0100
Subject: [PATCH] Function tutorialFocusElement optimized for WHS-Moodle.

---
 script/alquiz.js | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/script/alquiz.js b/script/alquiz.js
index d8fef2e..967bc9c 100644
--- a/script/alquiz.js
+++ b/script/alquiz.js
@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */
 
+
 ///START AL QUIZ SCRIPT///
 class QuestionGroup {
     constructor(id, description, Questions, nextGroup) {
@@ -955,16 +956,35 @@ function tutorialFocusElement(elem) {
     let userFocusBottom = document.createElement("div");
     userFocusBottom.classList.add("user-focus", "hide-bottom");
     userFocusBottom.onclick = removeTutorialFocus;
+
+    let offset = 0;
+    let pageHeight = 0;
+    let bodyNode = null;
+    let pageElementWHS = document.getElementById("page");
+    if(!pageElementWHS) {
+        //probably not in moodle of university of applied sciences Gelsenkirchen, Germany
+        offset = window.pageYOffset;
+        pageHeight = document.documentElement.scrollHeight;
+        bodyNode = document.body;
+    }
+    else {
+        //probably in moodle of university of applied sciences Gelsenkirchen, Germany
+        let navbar = document.querySelector("nav");
+        offset = pageElementWHS.scrollTop - (!navbar ? 0 : navbar.getBoundingClientRect().height);
+        pageHeight = pageElementWHS.scrollHeight;
+        console.log("we are in WHS-Moodle, offset: "+offset+", pageHeight: "+pageHeight);
+        bodyNode = pageElementWHS;
+    }
     
     let position = elem.getBoundingClientRect(elem);
-    userFocusTop.style.height=Math.ceil(position.top+window.pageYOffset-20)+"px";
+    userFocusTop.style.height=Math.ceil(position.top+offset-20)+"px";
 
-    let pageHeight = document.documentElement.scrollHeight;
-    userFocusBottom.style.top = Math.ceil(position.bottom+window.pageYOffset+20)+"px";
-    userFocusBottom.style.height=Math.ceil(pageHeight-(position.bottom+window.pageYOffset+20))+"px";
+    userFocusBottom.style.top = Math.ceil(position.bottom+offset+20)+"px";
+    userFocusBottom.style.height=Math.ceil(pageHeight-(position.bottom+offset+20))+"px";
+    console.log(userFocusTop.style.height,userFocusBottom.style.top,userFocusBottom.style.height);
 
-    document.body.appendChild(userFocusTop);
-    document.body.appendChild(userFocusBottom);
+    bodyNode.appendChild(userFocusTop);
+    bodyNode.appendChild(userFocusBottom);
 
     if(supportsSmoothScrolling() == true) {
         elem.scrollIntoView({behavior:"smooth", block:"center", inline:"center"});
-- 
GitLab