From 469621fec60954a736f1e7fc2c3611a29298e5f2 Mon Sep 17 00:00:00 2001
From: Ron Lucke <lucke@elan-ev.de>
Date: Wed, 8 Feb 2023 13:45:21 +0000
Subject: [PATCH] fix #2100

Closes #2100

Merge request studip/studip!1358
---
 app/controllers/contents/courseware.php       | 16 +++++++++-----
 app/controllers/course/courseware.php         | 13 ++++++-----
 app/views/contents/courseware/courseware.php  | 22 ++++++++++---------
 app/views/course/courseware/courseware.php    | 22 ++++++++++---------
 .../Courseware/CoursewareInstancesHelper.php  |  4 +++-
 resources/vue/courseware-shelf-app.js         |  2 +-
 6 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/app/controllers/contents/courseware.php b/app/controllers/contents/courseware.php
index 099a16a9a7f..61b5115e794 100644
--- a/app/controllers/contents/courseware.php
+++ b/app/controllers/contents/courseware.php
@@ -22,6 +22,8 @@ class Contents_CoursewareController extends CoursewareController
         PageLayout::setTitle(_('Courseware'));
 
         $this->user = $GLOBALS['user'];
+        $this->licenses = $this->getLicenses();
+        $this->unitsNotFound = Unit::countBySql('range_id = ?', [$this->user->id]) === 0;
     }
 
     /**
@@ -38,8 +40,6 @@ class Contents_CoursewareController extends CoursewareController
         Navigation::activateItem('/contents/courseware/shelf');
         $this->user_id = $GLOBALS['user']->id;
         $this->setShelfSidebar();
-
-        $this->licenses = $this->getLicenses();
     }
 
     private function setShelfSidebar(): void
@@ -63,6 +63,14 @@ class Contents_CoursewareController extends CoursewareController
     {
         global $perm, $user;
 
+        Navigation::activateItem('/contents/courseware/courseware');
+        if ($this->unitsNotFound) {
+            PageLayout::postMessage(MessageBox::info(_('Es wurde kein Lernmaterial gefunden.')));
+            return;
+        }
+
+        $this->setCoursewareSidebar();
+
         $this->user_id = $user->id;
         /** @var array<mixed> $last */
         $last = UserConfig::get($this->user_id)->getValue('COURSEWARE_LAST_ELEMENT');
@@ -78,9 +86,6 @@ class Contents_CoursewareController extends CoursewareController
         $unit = Unit::find($unit_id);
         if (isset($unit)) {
             $this->setEntryElement('user', $unit, $last, $this->user_id);
-            Navigation::activateItem('/contents/courseware/courseware');
-            $this->licenses = $this->getLicenses();
-            $this->setCoursewareSidebar();
         }
     }
 
@@ -315,7 +320,6 @@ class Contents_CoursewareController extends CoursewareController
 
         $this->user_id = $struct->owner_id;
 
-        $this->licenses = $this->getLicenses();
 
         $this->oer_enabled = Config::get()->OERCAMPUS_ENABLED && $perm->have_perm(Config::get()->OER_PUBLIC_STATUS);
 
diff --git a/app/controllers/course/courseware.php b/app/controllers/course/courseware.php
index 89fd6e01ee3..133ebdc1750 100644
--- a/app/controllers/course/courseware.php
+++ b/app/controllers/course/courseware.php
@@ -30,18 +30,25 @@ class Course_CoursewareController extends CoursewareController
         $this->studip_module = checkObjectModule('CoursewareModule', true);
         object_set_visit_module($this->studip_module->getPluginId());
         $this->last_visitdate = object_get_visit(Context::getId(), $this->studip_module->getPluginId());
+        $this->licenses = $this->getLicenses();
+        $this->unitsNotFound = Unit::countBySql('range_id = ?', [Context::getId()]) === 0;
     }
 
     public function index_action(): void
     {
         Navigation::activateItem('course/courseware/shelf');
-        $this->licenses = $this->getLicenses();
         $this->setIndexSidebar();
     }
 
     public function courseware_action($unit_id = null):  void
     {
         global $perm, $user;
+        Navigation::activateItem('course/courseware/unit');
+        if ($this->unitsNotFound) {
+            PageLayout::postMessage(MessageBox::info(_('Es wurde kein Lernmaterial gefunden.')));
+            return;
+        }
+        $this->setCoursewareSidebar();
 
         $this->user_id = $user->id;
         /** @var array<mixed> $last */
@@ -58,10 +65,6 @@ class Course_CoursewareController extends CoursewareController
         $unit = Unit::find($unit_id);
         if (isset($unit)) {
             $this->setEntryElement('course', $unit, $last, Context::getId());
-
-            Navigation::activateItem('course/courseware/unit');
-            $this->licenses = $this->getLicenses();
-            $this->setCoursewareSidebar();
         }
     }
 
diff --git a/app/views/contents/courseware/courseware.php b/app/views/contents/courseware/courseware.php
index bba4f1cc7ed..ba4a48e5467 100644
--- a/app/views/contents/courseware/courseware.php
+++ b/app/views/contents/courseware/courseware.php
@@ -1,10 +1,12 @@
-<div
-    id="courseware-index-app"
-    entry-element-id="<?= htmlReady($entry_element_id) ?>"
-    entry-type="users"
-    entry-id="<?= htmlReady($user_id) ?>"
-    unit-id="<?= htmlReady($unit_id) ?>"
-    oer-enabled='<?= htmlReady(Config::get()->OERCAMPUS_ENABLED) ?>'
-    licenses='<?= htmlReady($licenses) ?>'
-    >
-</div>
+<? if (!$unitsNotFound): ?>
+    <div
+        id="courseware-index-app"
+        entry-element-id="<?= htmlReady($entry_element_id) ?>"
+        entry-type="users"
+        entry-id="<?= htmlReady($user_id) ?>"
+        unit-id="<?= htmlReady($unit_id) ?>"
+        oer-enabled='<?= htmlReady(Config::get()->OERCAMPUS_ENABLED) ?>'
+        licenses='<?= htmlReady($licenses) ?>'
+        >
+    </div>
+<? endif; ?>
diff --git a/app/views/course/courseware/courseware.php b/app/views/course/courseware/courseware.php
index 68503b0b029..5440c973f14 100644
--- a/app/views/course/courseware/courseware.php
+++ b/app/views/course/courseware/courseware.php
@@ -1,10 +1,12 @@
-<div
-    id="courseware-index-app"
-    entry-element-id="<?= htmlReady($entry_element_id) ?>"
-    entry-type="courses"
-    entry-id="<?= htmlReady(Context::getId()) ?>"
-    unit-id="<?= htmlReady($unit_id) ?>"
-    oer-enabled="<?= htmlReady(Config::get()->OERCAMPUS_ENABLED) ?>"
-    licenses='<?= htmlReady($licenses) ?>'
-    >
-</div>
\ No newline at end of file
+<? if (!$unitsNotFound): ?>
+    <div
+        id="courseware-index-app"
+        entry-element-id="<?= htmlReady($entry_element_id) ?>"
+        entry-type="courses"
+        entry-id="<?= htmlReady(Context::getId()) ?>"
+        unit-id="<?= htmlReady($unit_id) ?>"
+        oer-enabled="<?= htmlReady(Config::get()->OERCAMPUS_ENABLED) ?>"
+        licenses='<?= htmlReady($licenses) ?>'
+        >
+    </div>
+<? endif; ?>
diff --git a/lib/classes/JsonApi/Routes/Courseware/CoursewareInstancesHelper.php b/lib/classes/JsonApi/Routes/Courseware/CoursewareInstancesHelper.php
index a120f63e624..e203fcdb1aa 100644
--- a/lib/classes/JsonApi/Routes/Courseware/CoursewareInstancesHelper.php
+++ b/lib/classes/JsonApi/Routes/Courseware/CoursewareInstancesHelper.php
@@ -37,7 +37,9 @@ trait CoursewareInstancesHelper
             $chunks = explode('_', $rangeId);
             $courseId = $chunks[0];
             $unitId = $chunks[1] ?? null;
-
+            if ($unitId === '') {
+                throw new BadRequestException('Unit id must not be empty.');
+            }
             if ($unitId) {
                 $unit = Unit::findOneBySQL('range_id = ? AND id = ?', [$courseId, $unitId]);
             } else {
diff --git a/resources/vue/courseware-shelf-app.js b/resources/vue/courseware-shelf-app.js
index 2d31aef10df..f175e587a6c 100644
--- a/resources/vue/courseware-shelf-app.js
+++ b/resources/vue/courseware-shelf-app.js
@@ -90,4 +90,4 @@ const mountApp = async (STUDIP, createApp, element) => {
 
 };
 
-export default mountApp;
\ No newline at end of file
+export default mountApp;
-- 
GitLab