Skip to content

Commit

Permalink
Allowing the exam flag of a group to be set when the group is created.
Browse files Browse the repository at this point in the history
  • Loading branch information
krulis-martin committed May 24, 2024
1 parent 26cc8f8 commit 2e8120d
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/V1Module/presenters/GroupsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function actionDefault(
* description="Should the group be visible to all student?")
* @Param(type="post", name="isOrganizational", validation="bool", required=false,
* description="Whether the group is organizational (no assignments nor students).")
* @Param(type="post", name="isExam", validation="bool", required=false,
* description="Whether the group is an exam group.")
* @Param(type="post", name="localizedTexts", validation="array", required=false,
* description="Localized names and descriptions")
* @Param(type="post", name="hasThreshold", validation="bool",
Expand Down Expand Up @@ -246,9 +248,14 @@ public function actionAddGroup()
$detaining = filter_var($req->getPost("detaining"), FILTER_VALIDATE_BOOLEAN);
$isPublic = filter_var($req->getPost("isPublic"), FILTER_VALIDATE_BOOLEAN);
$isOrganizational = filter_var($req->getPost("isOrganizational"), FILTER_VALIDATE_BOOLEAN);
$isExam = filter_var($req->getPost("isExam"), FILTER_VALIDATE_BOOLEAN);
$hasThreshold = filter_var($req->getPost("hasThreshold"), FILTER_VALIDATE_BOOLEAN);
$noAdmin = filter_var($req->getPost("noAdmin"), FILTER_VALIDATE_BOOLEAN);

if ($isOrganizational && $isExam) {
throw new InvalidArgumentException("A group cannot be both organizational and exam.");
}

$group = new Group(
$externalId,
$instance,
Expand All @@ -257,7 +264,8 @@ public function actionAddGroup()
$publicStats,
$isPublic,
$isOrganizational,
$detaining
$detaining,
$isExam,
);
if ($hasThreshold) {
$threshold = $req->getPost("threshold") !== null
Expand Down
8 changes: 7 additions & 1 deletion app/model/entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct(
bool $publicStats = false,
bool $isPublic = false,
bool $isOrganizational = false,
bool $isDetaining = false
bool $isDetaining = false,
bool $isExam = false,
) {
$this->externalId = $externalId;
$this->memberships = new ArrayCollection();
Expand Down Expand Up @@ -63,7 +64,12 @@ public function __construct(
$parentGroup->addChildGroup($this);
}

if ($isOrganizational && $isExam) {
throw new LogicException("A group cannot be both organizational and exam group.");
}

$this->isOrganizational = $isOrganizational;
$this->isExam = $isExam;
$this->isDetaining = $isDetaining;

$instance->addGroup($this);
Expand Down
118 changes: 118 additions & 0 deletions tests/Presenters/GroupsPresenter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ class TestGroupsPresenter extends Tester\TestCase
'isPublic' => true,
'hasThreshold' => false,
'isOrganizational' => false,
'isExam' => false,
'detaining' => true,
]
);
Expand All @@ -439,11 +440,128 @@ class TestGroupsPresenter extends Tester\TestCase
Assert::equal($instance->getRootGroup()->getId(), $payload["parentGroupId"]);
Assert::equal(true, $payload["privateData"]["publicStats"]);
Assert::equal(true, $payload["privateData"]["detaining"]);
Assert::equal(false, $payload["organizational"]);
Assert::equal(false, $payload["exam"]);
Assert::equal(true, $payload["public"]);
Assert::count(1, $payload['primaryAdminsIds']);
Assert::equal($admin->getId(), $payload["primaryAdminsIds"][0]);
}

public function testAddOrganizationalGroup()
{
$token = PresenterTestHelper::login($this->container, $this->adminLogin);
$admin = $this->container->getByType(Users::class)->getByEmail($this->adminLogin);

/** @var Instance $instance */
$instance = $this->presenter->instances->findAll()[0];
$allGroupsCount = count($this->presenter->groups->findAll());

$request = new Nette\Application\Request(
'V1:Groups',
'POST',
['action' => 'addGroup'],
[
'localizedTexts' => [
[
'locale' => 'en',
'name' => 'new name',
'description' => 'some neaty description'
]
],
'instanceId' => $instance->getId(),
'externalId' => 'external identification of exercise',
'parentGroupId' => null,
'publicStats' => true,
'isPublic' => false,
'hasThreshold' => false,
'isOrganizational' => true,
]
);
$response = $this->presenter->run($request);
Assert::type(Nette\Application\Responses\JsonResponse::class, $response);

$result = $response->getPayload();
/** @var Group $payload */
$payload = $result['payload'];

Assert::count(1, $payload["localizedTexts"]);
$localizedGroup = current($payload["localizedTexts"]);
Assert::notSame(null, $localizedGroup);

Assert::equal(200, $result['code']);
Assert::count($allGroupsCount + 1, $this->presenter->groups->findAll());
Assert::equal('new name', $localizedGroup->getName());
Assert::equal('some neaty description', $localizedGroup->getDescription());
Assert::equal($instance->getId(), $payload["privateData"]["instanceId"]);
Assert::equal('external identification of exercise', $payload["externalId"]);
Assert::equal($instance->getRootGroup()->getId(), $payload["parentGroupId"]);
Assert::equal(true, $payload["privateData"]["publicStats"]);
Assert::equal(true, $payload["organizational"]);
Assert::equal(false, $payload["exam"]);
Assert::equal(false, $payload["privateData"]["detaining"]);
Assert::equal(false, $payload["public"]);
Assert::count(1, $payload['primaryAdminsIds']);
Assert::equal($admin->getId(), $payload["primaryAdminsIds"][0]);
}

public function testAddExamGroup()
{
$token = PresenterTestHelper::login($this->container, $this->adminLogin);
$admin = $this->container->getByType(Users::class)->getByEmail($this->adminLogin);

/** @var Instance $instance */
$instance = $this->presenter->instances->findAll()[0];
$allGroupsCount = count($this->presenter->groups->findAll());

$request = new Nette\Application\Request(
'V1:Groups',
'POST',
['action' => 'addGroup'],
[
'localizedTexts' => [
[
'locale' => 'en',
'name' => 'new name',
'description' => 'some neaty description'
]
],
'instanceId' => $instance->getId(),
'externalId' => 'external identification of exercise',
'parentGroupId' => null,
'publicStats' => true,
'isPublic' => false,
'hasThreshold' => false,
'isExam' => true,
'detaining' => true,
]
);
$response = $this->presenter->run($request);
Assert::type(Nette\Application\Responses\JsonResponse::class, $response);

$result = $response->getPayload();
/** @var Group $payload */
$payload = $result['payload'];

Assert::count(1, $payload["localizedTexts"]);
$localizedGroup = current($payload["localizedTexts"]);
Assert::notSame(null, $localizedGroup);

Assert::equal(200, $result['code']);
Assert::count($allGroupsCount + 1, $this->presenter->groups->findAll());
Assert::equal('new name', $localizedGroup->getName());
Assert::equal('some neaty description', $localizedGroup->getDescription());
Assert::equal($instance->getId(), $payload["privateData"]["instanceId"]);
Assert::equal('external identification of exercise', $payload["externalId"]);
Assert::equal($instance->getRootGroup()->getId(), $payload["parentGroupId"]);
Assert::equal(true, $payload["privateData"]["publicStats"]);
Assert::equal(false, $payload["organizational"]);
Assert::equal(true, $payload["exam"]);
Assert::equal(true, $payload["privateData"]["detaining"]);
Assert::equal(false, $payload["public"]);
Assert::count(1, $payload['primaryAdminsIds']);
Assert::equal($admin->getId(), $payload["primaryAdminsIds"][0]);
}

public function testAddGroupNoAdmin()
{
$token = PresenterTestHelper::login($this->container, $this->adminLogin);
Expand Down

0 comments on commit 2e8120d

Please sign in to comment.