ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MarkSchema.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
36{
40 private array $mark_steps;
43
44 public function __construct(
45 private int $test_id
46 ) {
47 $this->mark_steps = [];
48 }
49
50 public function withTestId(int $test_id): self
51 {
52 $clone = clone $this;
53 $clone->test_id = $test_id;
54 return $clone;
55 }
56
57 public function getTestId(): int
58 {
59 return $this->test_id;
60 }
61
62 public function hasSinglePassedMark(): bool
63 {
64 return $this->nr_of_passed_marks === 1;
65 }
66
67 public function hasSingleZeroPercentageMark(): bool
68 {
69 return $this->nr_of_zero_percentage_marks === 1;
70 }
71
87 public function createSimpleSchema(
88 string $txt_failed_short = 'failed',
89 string $txt_failed_official = 'failed',
90 float $percentage_failed = 0,
91 bool $failed_passed = false,
92 string $txt_passed_short = 'passed',
93 string $txt_passed_official = 'passed',
94 float $percentage_passed = 50,
95 bool $passed_passed = true
96 ): self {
97 return $this->withMarkSteps([
98 new Mark($txt_failed_short, $txt_failed_official, $percentage_failed, $failed_passed),
99 new Mark($txt_passed_short, $txt_passed_official, $percentage_passed, $passed_passed)
100 ]);
101 }
102
103 public function getMatchingMark(
104 float $percentage
105 ): ?Mark {
106 $reached = round($percentage, 2);
107 foreach (array_reverse($this->mark_steps) as $step) {
108 $level = round($step->getMinimumLevel(), 2);
109 if ($reached >= $level) {
110 return $step;
111 }
112 }
113 return null;
114 }
115
116 public function checkForMissingZeroPercentage(): bool
117 {
118 return $this->nr_of_zero_percentage_marks === 0;
119 }
120
121 public function checkForMissingPassed(): bool
122 {
123 return $this->nr_of_passed_marks === 0;
124 }
125
126 public function checkForFailedAfterPassed(): bool
127 {
128 $has_to_be_passed = false;
129 foreach ($this->mark_steps as $step) {
130 if ($has_to_be_passed && !$step->getPassed()) {
131 return true;
132 }
133 if ($step->getPassed() === true) {
134 $has_to_be_passed = true;
135 }
136 }
137 return false;
138 }
139
143 public function getMarkSteps(): array
144 {
145 return $this->mark_steps;
146 }
147
151 public function withMarkSteps(array $mark_steps): self
152 {
153 $clone = clone $this;
154 $clone->mark_steps = $this->sort($mark_steps);
155 [$clone->nr_of_passed_marks, $clone->nr_of_zero_percentage_marks] = array_reduce(
156 $mark_steps,
157 function (array $c, Mark $v): array {
158 if ($v->getPassed()) {
159 $c[0]++;
160 }
161 if ($v->getMinimumLevel() === 0.0) {
162 $c[1]++;
163 }
164 return $c;
165 },
166 [0, 0]
167 );
168 return $clone;
169 }
170
171 private function sort(array $mark_steps): array
172 {
173 usort(
174 $mark_steps,
175 function ($a, $b): int {
176 if ($a->getMinimumLevel() === $b->getMinimumLevel()) {
177 $res = strcmp($a->getShortName(), $b->getShortName());
178 if ($res === 0) {
179 return strcmp($a->getOfficialName(), $b->getOfficialName());
180 } else {
181 return $res;
182 }
183 }
184 return ($a->getMinimumLevel() < $b->getMinimumLevel()) ? -1 : 1;
185 }
186 );
187 return $mark_steps;
188 }
189
190
191 public function toLog(AdditionalInformationGenerator $additional_info): array
192 {
193 $log_array = [];
194 foreach ($this->getMarkSteps() as $mark) {
195 $log_array[$mark->getShortName()] = [
196 AdditionalInformationGenerator::KEY_MARK_SHORT_NAME => $mark->getShortName(),
197 AdditionalInformationGenerator::KEY_MARK_OFFICIAL_NAME => $mark->getOfficialName(),
198 AdditionalInformationGenerator::KEY_MARK_MINIMUM_LEVEL => $mark->getMinimumLevel(),
199 AdditionalInformationGenerator::KEY_MARK_IS_PASSING => $additional_info
200 ->getTrueFalseTagForBool($mark->getPassed())
201 ];
202 }
203 return $log_array;
204 }
205}
A class defining mark schemas for assessment test objects.
Definition: MarkSchema.php:36
__construct(private int $test_id)
Definition: MarkSchema.php:44
toLog(AdditionalInformationGenerator $additional_info)
Definition: MarkSchema.php:191
getMatchingMark(float $percentage)
Definition: MarkSchema.php:103
createSimpleSchema(string $txt_failed_short='failed', string $txt_failed_official='failed', float $percentage_failed=0, bool $failed_passed=false, string $txt_passed_short='passed', string $txt_passed_official='passed', float $percentage_passed=50, bool $passed_passed=true)
Creates a simple mark schema for two mark steps: failed and passed.
Definition: MarkSchema.php:87
A class defining marks for assessment test objects.
Definition: Mark.php:36
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
if(!file_exists('../ilias.ini.php'))