ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Mark.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Refinery\Factory as Refinery;
26
35class Mark
36{
37 public function __construct(
38 private string $short_name = "",
39 private string $official_name = "",
40 private float $minimum_level = 0.0,
41 private bool $passed = false
42 ) {
43 }
44
45 public function getShortName(): string
46 {
47 return $this->short_name;
48 }
49
50 public function withShortName(string $short_name): self
51 {
52 $clone = clone $this;
53 $clone->short_name = $short_name;
54 return $clone;
55 }
56
57 public function getOfficialName(): string
58 {
59 return $this->official_name;
60 }
61
62 public function withOfficialName(string $official_name): self
63 {
64 $clone = clone $this;
65 $clone->official_name = $official_name;
66 return $clone;
67 }
68
69 public function getMinimumLevel(): float
70 {
71 return $this->minimum_level;
72 }
73
74 public function withMinimumLevel(float $minimum_level): self
75 {
76 if (($minimum_level >= 0.0) && ($minimum_level <= 100.0)) {
77 $clone = clone $this;
78 $clone->minimum_level = $minimum_level;
79 return $clone;
80 } else {
81 throw new \Exception('Markstep: minimum level must be between 0 and 100');
82 }
83 }
84
85 public function getPassed(): bool
86 {
87 return $this->passed;
88 }
89
90 public function withPassed(bool $passed): self
91 {
92 $clone = clone $this;
93 $clone->passed = $passed;
94 return $clone;
95 }
96
100 public function toForm(
102 FieldFactory $f,
104 MarkSchema $mark_schema
105 ): Group {
106 $percent_trafo = $refinery->kindlyTo()->float();
107 $percent_constraint = $refinery->custom()->constraint(
108 static function (float $v): bool {
109 if ($v > 100.0 || $v < 0.0) {
110 return false;
111 }
112 return true;
113 },
114 $lng->txt('tst_mark_minimum_level_invalid')
115 );
116 $mark_trafo = $refinery->custom()->transformation(
117 static function (array $vs): Mark {
118 return new self(
119 $vs['name'],
120 $vs['official_name'],
121 $vs['minimum_level'],
122 $vs['passed']
123 );
124 }
125 );
126 $missing_passed_check = $refinery->custom()->constraint(
127 static function (Mark $v) use ($mark_schema) {
128 if ($v->getPassed() === true) {
129 return true;
130 }
131 $mark_steps = $mark_schema->getMarkSteps();
132 $mark_steps[] = $v;
133 $local_schema = $mark_schema->withMarkSteps($mark_steps);
134 if ($local_schema->checkForMissingPassed()) {
135 return false;
136 }
137 return true;
138 },
139 $lng->txt('no_passed_mark')
140 );
141 $missing_zero_check = $refinery->custom()->constraint(
142 static function (Mark $v) use ($mark_schema) {
143 if ($v->getMinimumLevel() > 0.0) {
144 return true;
145 }
146 $mark_steps = $mark_schema->getMarkSteps();
147 $mark_steps[] = $v;
148 $local_schema = $mark_schema->withMarkSteps($mark_steps);
149 if ($local_schema->checkForMissingZeroPercentage()) {
150 return false;
151 }
152 return true;
153 },
154 $lng->txt('no_passed_mark')
155 );
156 return $f->group([
157 'name' => $f->text($lng->txt('tst_mark_short_form'))
158 ->withValue($this->getShortName())
159 ->withRequired(true),
160 'official_name' => $f->text($lng->txt('tst_mark_official_form'))
161 ->withValue($this->getOfficialName())
162 ->withRequired(true),
163 'minimum_level' => $f->text($lng->txt('tst_mark_minimum_level'))
164 ->withAdditionalTransformation($percent_trafo)
165 ->withAdditionalTransformation($percent_constraint)
166 ->withValue((string) $this->getMinimumLevel())
167 ->withRequired(true),
168 'passed' => $f->checkbox($lng->txt('tst_mark_passed'))
169 ->withValue($this->getPassed())
170 ])->withAdditionalTransformation($mark_trafo)
171 ->withAdditionalTransformation($missing_passed_check)
172 ->withAdditionalTransformation($missing_zero_check);
173 }
174
175 public function toStorage(): array
176 {
177 return [
178 'short_name' => ['text', mb_substr($this->getShortName(), 0, 15)],
179 'official_name' => ['text', mb_substr($this->getOfficialName(), 0, 50)],
180 'minimum_level' => ['float', $this->getMinimumLevel()],
181 'passed' => ['text', (int) $this->getPassed()],
182 'tstamp' => ['integer', time()]
183 ];
184 }
185}
Builds data types.
Definition: Factory.php:36
A class defining mark schemas for assessment test objects.
Definition: MarkSchema.php:36
A class defining marks for assessment test objects.
Definition: Mark.php:36
__construct(private string $short_name="", private string $official_name="", private float $minimum_level=0.0, private bool $passed=false)
Definition: Mark.php:37
withOfficialName(string $official_name)
Definition: Mark.php:62
withPassed(bool $passed)
Definition: Mark.php:90
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, MarkSchema $mark_schema)
Definition: Mark.php:100
withShortName(string $short_name)
Definition: Mark.php:50
withMinimumLevel(float $minimum_level)
Definition: Mark.php:74
language handling
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