ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Mark.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Refinery\Factory as Refinery;
27
36class Mark implements Exportable
37{
38 public function __construct(
39 private string $short_name = "",
40 private string $official_name = "",
41 private float $minimum_level = 0.0,
42 private bool $passed = false
43 ) {
44 }
45
46 public function getShortName(): string
47 {
48 return $this->short_name;
49 }
50
51 public function withShortName(string $short_name): self
52 {
53 $clone = clone $this;
54 $clone->short_name = $short_name;
55 return $clone;
56 }
57
58 public function getOfficialName(): string
59 {
60 return $this->official_name;
61 }
62
63 public function withOfficialName(string $official_name): self
64 {
65 $clone = clone $this;
66 $clone->official_name = $official_name;
67 return $clone;
68 }
69
70 public function getMinimumLevel(): float
71 {
72 return $this->minimum_level;
73 }
74
75 public function withMinimumLevel(float $minimum_level): self
76 {
77 if (($minimum_level >= 0.0) && ($minimum_level <= 100.0)) {
78 $clone = clone $this;
79 $clone->minimum_level = $minimum_level;
80 return $clone;
81 } else {
82 throw new \Exception('Markstep: minimum level must be between 0 and 100');
83 }
84 }
85
86 public function getPassed(): bool
87 {
88 return $this->passed;
89 }
90
91 public function withPassed(bool $passed): self
92 {
93 $clone = clone $this;
94 $clone->passed = $passed;
95 return $clone;
96 }
97
101 public function toForm(
103 FieldFactory $f,
105 MarkSchema $mark_schema
106 ): Group {
107 $percent_trafo = $refinery->kindlyTo()->float();
108 $percent_constraint = $refinery->custom()->constraint(
109 static function (float $v): bool {
110 if ($v > 100.0 || $v < 0.0) {
111 return false;
112 }
113 return true;
114 },
115 $lng->txt('tst_mark_minimum_level_invalid')
116 );
117 $mark_trafo = $refinery->custom()->transformation(
118 static function (array $vs): Mark {
119 return new self(
120 $vs['name'],
121 $vs['official_name'],
122 $vs['minimum_level'],
123 $vs['passed']
124 );
125 }
126 );
127 $missing_passed_check = $refinery->custom()->constraint(
128 static function (Mark $v) use ($mark_schema) {
129 if ($v->getPassed() === true) {
130 return true;
131 }
132 $mark_steps = $mark_schema->getMarkSteps();
133 $mark_steps[] = $v;
134 $local_schema = $mark_schema->withMarkSteps($mark_steps);
135 if ($local_schema->checkForMissingPassed()) {
136 return false;
137 }
138 return true;
139 },
140 $lng->txt('no_passed_mark')
141 );
142 $missing_zero_check = $refinery->custom()->constraint(
143 static function (Mark $v) use ($mark_schema) {
144 if ($v->getMinimumLevel() > 0.0) {
145 return true;
146 }
147 $mark_steps = $mark_schema->getMarkSteps();
148 $mark_steps[] = $v;
149 $local_schema = $mark_schema->withMarkSteps($mark_steps);
150 if ($local_schema->checkForMissingZeroPercentage()) {
151 return false;
152 }
153 return true;
154 },
155 $lng->txt('no_passed_mark')
156 );
157 return $f->group([
158 'name' => $f->text($lng->txt('tst_mark_short_form'))
159 ->withValue($this->getShortName())
160 ->withRequired(true),
161 'official_name' => $f->text($lng->txt('tst_mark_official_form'))
162 ->withValue($this->getOfficialName())
163 ->withRequired(true),
164 'minimum_level' => $f->text($lng->txt('tst_mark_minimum_level'))
165 ->withAdditionalTransformation($percent_trafo)
166 ->withAdditionalTransformation($percent_constraint)
167 ->withValue((string) $this->getMinimumLevel())
168 ->withRequired(true),
169 'passed' => $f->checkbox($lng->txt('tst_mark_passed'))
170 ->withValue($this->getPassed())
171 ])->withAdditionalTransformation($mark_trafo)
172 ->withAdditionalTransformation($missing_passed_check)
173 ->withAdditionalTransformation($missing_zero_check);
174 }
175
176 public function toStorage(): array
177 {
178 return [
179 'short_name' => ['text', mb_substr($this->getShortName(), 0, 15)],
180 'official_name' => ['text', mb_substr($this->getOfficialName(), 0, 50)],
181 'minimum_level' => ['float', $this->getMinimumLevel()],
182 'passed' => ['text', (int) $this->getPassed()],
183 'tstamp' => ['integer', time()]
184 ];
185 }
186
187 public function toExport(): array
188 {
189 return [
190 'short_name' => $this->getShortName(),
191 'official_name' => $this->getOfficialName(),
192 'minimum_level' => $this->getMinimumLevel(),
193 'passed' => $this->getPassed()
194 ];
195 }
196
197 public static function fromExport(array $data): static
198 {
199 return new self(
200 (string) $data['short_name'],
201 (string) $data['official_name'],
202 (float) $data['minimum_level'],
203 (bool) $data['passed']
204 );
205 }
206}
Builds data types.
Definition: Factory.php:36
A class defining mark schemas for assessment test objects.
Definition: MarkSchema.php:37
A class defining marks for assessment test objects.
Definition: Mark.php:37
__construct(private string $short_name="", private string $official_name="", private float $minimum_level=0.0, private bool $passed=false)
Definition: Mark.php:38
withOfficialName(string $official_name)
Definition: Mark.php:63
toExport()
Transform the object into a simple, associative array.
Definition: Mark.php:187
static fromExport(array $data)
Creates an instance of the object from an array.
Definition: Mark.php:197
withPassed(bool $passed)
Definition: Mark.php:91
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, MarkSchema $mark_schema)
Definition: Mark.php:101
withShortName(string $short_name)
Definition: Mark.php:51
withMinimumLevel(float $minimum_level)
Definition: Mark.php:75
language handling
This interface allows an object to define its own transformation into a language-neutral,...
Definition: Exportable.php:40
This is what a factory for input fields looks like.
Definition: Factory.php:31
Describes the monoid operation of grouping form inputs.
Definition: Group.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
global $lng
Definition: privfeed.php:31