aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/classes/JsonApi/Routes/Courseware/UserProgressesOfUnitsShow.php7
-rw-r--r--lib/classes/JsonApi/Schemas/Courseware/Unit.php4
-rw-r--r--lib/models/Courseware/Unit.php40
3 files changed, 44 insertions, 7 deletions
diff --git a/lib/classes/JsonApi/Routes/Courseware/UserProgressesOfUnitsShow.php b/lib/classes/JsonApi/Routes/Courseware/UserProgressesOfUnitsShow.php
index 7b900e7..26bd261 100644
--- a/lib/classes/JsonApi/Routes/Courseware/UserProgressesOfUnitsShow.php
+++ b/lib/classes/JsonApi/Routes/Courseware/UserProgressesOfUnitsShow.php
@@ -36,6 +36,11 @@ class UserProgressesOfUnitsShow extends NonJsonApiController
if (!$GLOBALS['perm']->have_studip_perm('autor', $root->range_id) || !$unit->canRead($user)) {
throw new AuthorizationFailedException();
}
+ $response = $response->withHeader('Content-Type', 'application/json');
+ if (!$root) {
+ $response->getBody()->write((string) json_encode([]));
+ return $response;
+ }
$instance = new Instance($root);
$isTeacher = $GLOBALS['perm']->have_studip_perm('tutor', $root->range_id);
@@ -46,9 +51,7 @@ class UserProgressesOfUnitsShow extends NonJsonApiController
$progresses = $this->prepareProgressData($elements, $progress);
- $response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write((string) json_encode($progresses));
-
return $response;
}
diff --git a/lib/classes/JsonApi/Schemas/Courseware/Unit.php b/lib/classes/JsonApi/Schemas/Courseware/Unit.php
index a1c554e..7a0bf4e 100644
--- a/lib/classes/JsonApi/Schemas/Courseware/Unit.php
+++ b/lib/classes/JsonApi/Schemas/Courseware/Unit.php
@@ -53,7 +53,7 @@ class Unit extends SchemaProvider
]
: [self::RELATIONSHIP_DATA => null];
- $relationships[self::REL_STRUCTURAL_ELEMENT] = $resource['structural_element_id']
+ $relationships[self::REL_STRUCTURAL_ELEMENT] = $resource->structural_element
? [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($resource->structural_element),
@@ -76,4 +76,4 @@ class Unit extends SchemaProvider
return $relationships;
}
-} \ No newline at end of file
+}
diff --git a/lib/models/Courseware/Unit.php b/lib/models/Courseware/Unit.php
index 647f9f9..736fa92 100644
--- a/lib/models/Courseware/Unit.php
+++ b/lib/models/Courseware/Unit.php
@@ -74,12 +74,46 @@ class Unit extends \SimpleORMap implements \PrivacyObject
public function canRead(\User $user): bool
{
- return $this->structural_element->canRead($user);
+ if ($this->structural_element) {
+ return $this->structural_element->canRead($user);
+ } else {
+ if ($GLOBALS['perm']->have_perm('root', $user->id)) {
+ return true;
+ }
+ switch ($this->range_type) {
+ case 'user':
+ if ($this->range_id === $user->id) {
+ return true;
+ }
+ case 'course':
+ return $GLOBALS['perm']->have_studip_perm('user', $this->range_id, $user->id);
+ default:
+ throw new \InvalidArgumentException('Unknown range type.');
+ }
+ }
}
public function canEdit(\User $user): bool
{
- return $this->structural_element->canEdit($user);;
+ if ($this->structural_element) {
+ return $this->structural_element->canEdit($user);
+ } else {
+ if ($GLOBALS['perm']->have_perm('root', $user->id)) {
+ return true;
+ }
+
+ switch ($this->range_type) {
+ case 'user':
+ if ($this->range_id === $user->id) {
+ return true;
+ }
+ case 'course':
+ return $GLOBALS['perm']->have_studip_perm('tutor', $this->range_id, $user->id);
+
+ default:
+ throw new \InvalidArgumentException('Unknown range type.');
+ }
+ }
}
public function copy(\User $user, string $rangeId, string $rangeType, array $modified = null): Unit
@@ -125,6 +159,6 @@ class Unit extends \SimpleORMap implements \PrivacyObject
if ($units) {
$storage->addTabularData(_('Courseware Lernmaterialien'), 'cw_units', $units);
}
-
+
}
}