aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php')
-rw-r--r--lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php b/lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php
new file mode 100644
index 0000000..42bee8a
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Courseware/PeerReview/ProcessesIndex.php
@@ -0,0 +1,108 @@
+<?php
+
+namespace JsonApi\Routes\Courseware\PeerReview;
+
+use Course;
+use Courseware\PeerReviewProcess;
+use JsonApi\Errors\AuthorizationFailedException;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\JsonApiController;
+use JsonApi\Routes\Courses\Authority as CoursesAuthority;
+use JsonApi\Routes\Courseware\Authority;
+use JsonApi\Schemas\Courseware\PeerReviewProcess as ProcessSchema;
+use Psr\Http\Message\ResponseInterface as Response;
+use Psr\Http\Message\ServerRequestInterface as Request;
+use User;
+
+/**
+ * Displays all visible PeerReviewProcesses.
+ *
+ * @SuppressWarnings(PHPMD.LongVariable)
+ * @SuppressWarnings(PHPMD.StaticAccess)
+ */
+class ProcessesIndex extends JsonApiController
+{
+ protected $allowedFilteringParameters = ['cid'];
+
+ protected $allowedIncludePaths = [
+ ProcessSchema::REL_COURSE,
+ ProcessSchema::REL_OWNER,
+ ProcessSchema::REL_TASK_GROUP,
+ ];
+
+ protected $allowedPagingParameters = ['offset', 'limit'];
+
+ /**
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ *
+ * @param array $args
+ *
+ * @return Response
+ */
+ public function __invoke(Request $request, Response $response, $args)
+ {
+ $user = $this->getUser($request);
+ $filtering = $this->getQueryParameters()->getFilteringParameters() ?: [];
+
+ $this->validateFilters($filtering);
+ $this->authorize($user, $filtering);
+
+ $resources = empty($filtering) ? $this->findAllProcesses($user) : $this->filterProcesses($user, $filtering);
+
+ return $this->getPaginatedContentResponse(
+ array_slice($resources, ...$this->getOffsetAndLimit()),
+ count($resources)
+ );
+ }
+
+ /**
+ * @throws BadRequestException
+ */
+ private function validateFilters(array $filtering): void
+ {
+ if (isset($filtering['cid']) && !Course::exists($filtering['cid'])) {
+ throw new BadRequestException('Could not find a course matching this `filter[cid]`.');
+ }
+ }
+
+ /**
+ * @throws AuthorizationFailedException
+ */
+ private function authorize(User $user, array $filtering): void
+ {
+ if (!Authority::canIndexPeerReviewProcesses($user)) {
+ throw new AuthorizationFailedException();
+ }
+
+ if (isset($filtering['cid'])) {
+ if (
+ !CoursesAuthority::canShowCourse(
+ $user,
+ Course::find($filtering['cid']),
+ CoursesAuthority::SCOPE_EXTENDED
+ )
+ ) {
+ throw new AuthorizationFailedException();
+ }
+ }
+ }
+
+ private function findAllProcesses(User $user): iterable
+ {
+ return PeerReviewProcess::findByUser($user);
+ }
+
+ private function filterProcesses(User $user, array $filtering): iterable
+ {
+ if (isset($filtering['cid'])) {
+ /** @var ?\Course $course */
+ $course = \Course::find($filtering['cid']);
+
+ return array_filter(PeerReviewProcess::findByCourse($course), function ($process) use ($user) {
+ return Authority::canShowPeerReviewProcess($user, $process);
+ });
+ }
+
+ return [];
+ }
+}