Skip to content

Commit 871e6a8

Browse files
committed
Improving exercise links and generating migration.
1 parent f493455 commit 871e6a8

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

app/model/entity/ExerciseFileLink.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ class ExerciseFileLink
2828

2929
/**
3030
* The key (fixed ID) used to identify the file in exercise specification (for simple replacement).
31-
* @ORM\Column(type="string")
31+
* @ORM\Column(type="string", length=16)
3232
*/
3333
protected $key;
3434

35+
/**
36+
* New name under which the file is downloaded (null means the original name).
37+
* @ORM\Column(type="string", nullable=true)
38+
*/
39+
protected $saveName;
40+
3541
/**
3642
* Minimal required user role to access the file (null means even non-logged-in users).
3743
* @ORM\Column(type="string", nullable=true)
@@ -56,36 +62,53 @@ class ExerciseFileLink
5662

5763
/**
5864
* Link constructor
59-
* @param string $key
65+
* @param string $key used to identify the file in exercise specification (for simple replacement)
6066
* @param ExerciseFile $exerciseFile
61-
* @param string|null $requiredRole
6267
* @param Exercise|null $exercise
6368
* @param Assignment|null $assignment
69+
* @param string|null $requiredRole minimal required user role to access the file (null = non-logged-in users)
70+
* @param string|null $saveName new name under which the file is downloaded (null means the original name)
6471
*/
6572
private function __construct(
6673
string $key,
6774
ExerciseFile $exerciseFile,
68-
?string $requiredRole = null,
6975
?Exercise $exercise = null,
7076
?Assignment $assignment = null,
77+
?string $requiredRole = null,
78+
?string $saveName = null
7179
) {
7280
$this->key = $key;
7381
$this->requiredRole = $requiredRole;
82+
$this->saveName = $saveName;
7483
$this->exerciseFile = $exerciseFile;
7584
$this->exercise = $exercise;
7685
$this->assignment = $assignment;
7786
$this->createdAt = new DateTime();
7887
}
7988

89+
/**
90+
* Create a link for exercise
91+
* @param string $key
92+
* @param ExerciseFile $exerciseFile
93+
* @param Exercise $exercise
94+
* @param string|null $requiredRole
95+
* @param string|null $saveName
96+
*/
8097
public static function createForExercise(
8198
string $key,
8299
ExerciseFile $exerciseFile,
83-
?string $requiredRole,
84-
Exercise $exercise
100+
Exercise $exercise,
101+
?string $requiredRole = null,
102+
?string $saveName = null
85103
): self {
86-
return new self($key, $exerciseFile, $requiredRole, $exercise, null);
104+
return new self($key, $exerciseFile, $exercise, null, $requiredRole, $saveName);
87105
}
88106

107+
/**
108+
* Create a link for assignment by copying an existing link
109+
* @param ExerciseFileLink $link to be copied when assignment is being created
110+
* @param Assignment $assignment the assignment for which the link is being created
111+
*/
89112
public static function copyForAssignment(
90113
ExerciseFileLink $link,
91114
Assignment $assignment
@@ -95,7 +118,7 @@ public static function copyForAssignment(
95118
'Can only copy links associated with an exercise of selected assignment.'
96119
);
97120
}
98-
return new self($link->key, $link->exerciseFile, $link->requiredRole, null, $assignment);
121+
return new self($link->key, $link->exerciseFile, null, $assignment, $link->requiredRole, $link->saveName);
99122
}
100123

101124
/*
@@ -107,11 +130,31 @@ public function getKey(): string
107130
return $this->key;
108131
}
109132

133+
public function setKey(string $key): void
134+
{
135+
$this->key = $key;
136+
}
137+
110138
public function getRequiredRole(): ?string
111139
{
112140
return $this->requiredRole;
113141
}
114142

143+
public function setRequiredRole(?string $requiredRole): void
144+
{
145+
$this->requiredRole = $requiredRole;
146+
}
147+
148+
public function getSaveName(): ?string
149+
{
150+
return $this->saveName;
151+
}
152+
153+
public function setSaveName(?string $saveName): void
154+
{
155+
$this->saveName = $saveName;
156+
}
157+
115158
public function getExerciseFile(): ExerciseFile
116159
{
117160
return $this->exerciseFile;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20251114174621 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('CREATE TABLE exercise_file_link (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', exercise_file_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', exercise_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', assignment_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', `key` VARCHAR(16) NOT NULL, save_name VARCHAR(255) DEFAULT NULL, required_role VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, INDEX IDX_1187F77549DE8E29 (exercise_file_id), INDEX IDX_1187F775E934951A (exercise_id), INDEX IDX_1187F775D19302F8 (assignment_id), UNIQUE INDEX UNIQ_1187F7758A90ABA9E934951A (`key`, exercise_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
24+
$this->addSql('ALTER TABLE exercise_file_link ADD CONSTRAINT FK_1187F77549DE8E29 FOREIGN KEY (exercise_file_id) REFERENCES `uploaded_file` (id)');
25+
$this->addSql('ALTER TABLE exercise_file_link ADD CONSTRAINT FK_1187F775E934951A FOREIGN KEY (exercise_id) REFERENCES exercise (id)');
26+
$this->addSql('ALTER TABLE exercise_file_link ADD CONSTRAINT FK_1187F775D19302F8 FOREIGN KEY (assignment_id) REFERENCES assignment (id)');
27+
$this->addSql('ALTER TABLE uploaded_file DROP is_public');
28+
}
29+
30+
public function down(Schema $schema): void
31+
{
32+
// this down() migration is auto-generated, please modify it to your needs
33+
$this->addSql('ALTER TABLE exercise_file_link DROP FOREIGN KEY FK_1187F77549DE8E29');
34+
$this->addSql('ALTER TABLE exercise_file_link DROP FOREIGN KEY FK_1187F775E934951A');
35+
$this->addSql('ALTER TABLE exercise_file_link DROP FOREIGN KEY FK_1187F775D19302F8');
36+
$this->addSql('DROP TABLE exercise_file_link');
37+
$this->addSql('ALTER TABLE `uploaded_file` ADD is_public TINYINT(1) NOT NULL');
38+
}
39+
}

0 commit comments

Comments
 (0)