1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<?php
namespace JsonApi\Routes\Courseware;
use Courseware\Container;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use JsonApi\Schemas\Courseware\Block as BlockSchema;
use JsonApi\Schemas\Courseware\BlockFeedback as BlockFeedbackSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Studip\Activity\Activity;
use Studip\Activity\CoursewareProvider;
/**
* Create feedback on a block.
*/
class BlockFeedbacksCreate extends JsonApiController
{
use ValidationTrait;
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(Request $request, Response $response, $args)
{
$json = $this->validate($request);
$block = $this->getBlockFromJson($json);
if (!Authority::canCreateBlockFeedback($user = $this->getUser($request), $block)) {
throw new AuthorizationFailedException();
}
$blockFeedback = $this->createBlockFeedback($user, $json, $block);
return $this->getCreatedResponse($blockFeedback);
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
protected function validateResourceDocument($json, $data)
{
if (!self::arrayHas($json, 'data')) {
return 'Missing `data` member at document“s top level.';
}
if (BlockFeedbackSchema::TYPE !== self::arrayGet($json, 'data.type')) {
return 'Wrong `type` member of document“s `data`.';
}
if (self::arrayHas($json, 'data.id')) {
return 'New document must not have an `id`.';
}
if (!self::arrayHas($json, 'data.attributes.feedback')) {
return 'Missing `feedback` attribute.';
}
if (!self::arrayHas($json, 'data.relationships.block')) {
return 'Missing `block` relationship.';
}
if (!$this->getBlockFromJson($json)) {
return 'Invalid `block` relationship.';
}
}
private function getBlockFromJson($json)
{
if (!$this->validateResourceObject($json, 'data.relationships.block', BlockSchema::TYPE)) {
return null;
}
$blockId = self::arrayGet($json, 'data.relationships.block.data.id');
return \Courseware\Block::find($blockId);
}
private function createBlockFeedback(\User $user, array $json, \Courseware\Block $block)
{
$blockFeedback = \Courseware\BlockFeedback::build([
'block_id' => $block->id,
'user_id' => $user->id,
'feedback' => self::arrayGet($json, 'data.attributes.feedback'),
]);
$blockFeedback->store();
return $blockFeedback;
}
}
|