Skip to content

Commit

Permalink
Add set-flag endpoint for assignment solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloop committed Dec 25, 2019
1 parent 6d906dc commit 4a859d7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
70 changes: 68 additions & 2 deletions app/V1Module/presenters/AssignmentSolutionsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,72 @@ public function actionSetBonusPoints(string $id) {
$this->sendSuccessResponse("OK");
}

public function checkSetFlag(string $id) {
$solution = $this->assignmentSolutions->findOrThrow($id);
if (!$this->assignmentSolutionAcl->canSetFlag($solution)) {
throw new ForbiddenRequestException("You cannot change flags for this submission");
}
}

/**
* Set flag of the assignment solution.
* @POST
* @param string $id identifier of the solution
* @param string $flag name of the flag which should to be changed
* @Param(type="post", name="value", required=true, validation=boolean, description="True or false which should be set to given flag name")
* @throws NotFoundException
* @throws \Nette\Application\AbortException
* @throws \Exception
*/
public function actionSetFlag(string $id, string $flag) {
$req = $this->getRequest();
$solution = $this->assignmentSolutions->findOrThrow($id);
if ($solution->getAssignment() === null) {
throw new NotFoundException("Assignment for solution '$id' was deleted");
}

if ($solution->getSolution()->getAuthor() === null) {
throw new NotFoundException("Author of solution '$id' was deleted");
}

// map of boolean flag names with the information about uniqueness
$knownBoolFlags = [
"accepted" => true,
"reviewed" => false
];

if (!array_key_exists($flag, $knownBoolFlags)) {
throw new BadRequestException("Trying to set unknown boolean flag '$flag' to the solution");
}

// handle given flag
$unique = $knownBoolFlags[$flag];
if ($req->getPost("value") !== null) {
$value = filter_var($req->getPost("value"), FILTER_VALIDATE_BOOLEAN);
$solution->setFlag($flag, $value);

// handle unique flags
if ($unique && $value) {
// flag has to be set to false for all other submissions
$assignmentSubmissions = $this->assignmentSolutions->findSolutions($solution->getAssignment(), $solution->getSolution()->getAuthor());
foreach ($assignmentSubmissions as $assignmentSubmission) {
$assignmentSubmission->setFlag($flag, false);
}
}
}

// finally flush all changed to the database
$this->assignmentSolutions->flush();

// forward to student statistics of group
$groupOfSubmission = $solution->getAssignment()->getGroup();
if ($groupOfSubmission === null) {
throw new NotFoundException("Group for assignment '$id' was not found");
}

$this->forward('Groups:studentsStats', $groupOfSubmission->getId(), $solution->getSolution()->getAuthor()->getId());
}

public function checkSetAccepted(string $id) {
$solution = $this->assignmentSolutions->findOrThrow($id);
if (!$this->assignmentSolutionAcl->canSetAccepted($solution)) {
Expand All @@ -274,7 +340,7 @@ public function checkSetAccepted(string $id) {
/**
* Set solution of student as accepted, this solution will be then presented as the best one.
* @POST
* @param string $id identifier of the submission
* @param string $id identifier of the solution
* @throws \Nette\Application\AbortException
* @throws NotFoundException
*/
Expand Down Expand Up @@ -317,7 +383,7 @@ public function checkUnsetAccepted(string $id) {
/**
* Set solution of student as unaccepted if it was.
* @DELETE
* @param string $id identifier of the submission
* @param string $id identifier of the solution
* @throws \Nette\Application\AbortException
* @throws NotFoundException
*/
Expand Down
2 changes: 2 additions & 0 deletions app/V1Module/router/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ private static function createAssignmentSolutionsRoutes(string $prefix): RouteLi
$router[] = new DeleteRoute("$prefix/<id>", "AssignmentSolutions:deleteSolution");
$router[] = new PostRoute("$prefix/<id>/bonus-points", "AssignmentSolutions:setBonusPoints");
$router[] = new GetRoute("$prefix/<id>/evaluations", "AssignmentSolutions:evaluations");
$router[] = new PostRoute("$prefix/<id>/set-flag/<flag>", "AssignmentSolutions:setFlag");
$router[] = new PostRoute("$prefix/<id>/set-accepted", "AssignmentSolutions:setAccepted");
$router[] = new PostRoute("$prefix/<id>/set-accepted", "AssignmentSolutions:setAccepted");
$router[] = new DeleteRoute("$prefix/<id>/unset-accepted", "AssignmentSolutions:unsetAccepted");
$router[] = new PostRoute("$prefix/<id>/resubmit", "Submit:resubmit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function canViewDetail(AssignmentSolution $assignmentSolution): bool;
function canDelete(AssignmentSolution $assignmentSolution): bool;
function canSetBonusPoints(AssignmentSolution $assignmentSolution): bool;
function canSetAccepted(AssignmentSolution $assignmentSolution): bool;
function canSetFlag(AssignmentSolution $assignmentSolution): bool;
function canViewResubmissions(AssignmentSolution $assignmentSolution): bool;

function canViewEvaluation(AssignmentSolution $assignmentSolution): bool;
Expand Down
3 changes: 2 additions & 1 deletion app/config/permissions.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ roles:

- name: superadmin

permissions:
permissions:
- allow: true
role: scope-master

Expand Down Expand Up @@ -393,6 +393,7 @@ permissions:
- delete
- setBonusPoints
- setAccepted
- setFlag
- downloadResultArchive
conditions:
- assignmentSolution.isSupervisor
Expand Down
1 change: 1 addition & 0 deletions app/model/entity/AssignmentSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class AssignmentSolution
{
use MagicAccessors;
use FlagAccessor;

const JOB_TYPE = "student";

Expand Down
1 change: 1 addition & 0 deletions app/model/entity/base/FlagAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Model\Entity;

use Exception;
use ReflectionClass;

trait FlagAccessor {
Expand Down

0 comments on commit 4a859d7

Please sign in to comment.