ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
MarkSchema.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
36class MarkSchema implements Exportable
37{
41 private array $mark_steps;
44
45 public function __construct(
46 private int $test_id
47 ) {
48 $this->mark_steps = [];
49 }
50
51 public function withTestId(int $test_id): self
52 {
53 $clone = clone $this;
54 $clone->test_id = $test_id;
55 return $clone;
56 }
57
58 public function getTestId(): int
59 {
60 return $this->test_id;
61 }
62
63 public function hasSinglePassedMark(): bool
64 {
65 return $this->nr_of_passed_marks === 1;
66 }
67
68 public function hasSingleZeroPercentageMark(): bool
69 {
70 return $this->nr_of_zero_percentage_marks === 1;
71 }
72
73 public function getMatchingMark(
74 float $percentage
75 ): ?Mark {
76 $reached = round($percentage, 2);
77 foreach (array_reverse($this->mark_steps) as $step) {
78 $level = round($step->getMinimumLevel(), 2);
79 if ($reached >= $level) {
80 return $step;
81 }
82 }
83 return null;
84 }
85
86 public function checkForMissingZeroPercentage(): bool
87 {
88 return $this->nr_of_zero_percentage_marks === 0;
89 }
90
91 public function checkForMissingPassed(): bool
92 {
93 return $this->nr_of_passed_marks === 0;
94 }
95
96 public function checkForFailedAfterPassed(): bool
97 {
98 $has_to_be_passed = false;
99 foreach ($this->mark_steps as $step) {
100 if ($has_to_be_passed && !$step->getPassed()) {
101 return true;
102 }
103 if ($step->getPassed() === true) {
104 $has_to_be_passed = true;
105 }
106 }
107 return false;
108 }
109
113 public function getMarkSteps(): array
114 {
115 return $this->mark_steps;
116 }
117
121 public function withMarkSteps(array $mark_steps): self
122 {
123 $clone = clone $this;
124 $clone->mark_steps = $this->sort($mark_steps);
125 [$clone->nr_of_passed_marks, $clone->nr_of_zero_percentage_marks] = array_reduce(
126 $mark_steps,
127 function (array $c, Mark $v): array {
128 if ($v->getPassed()) {
129 $c[0]++;
130 }
131 if ($v->getMinimumLevel() === 0.0) {
132 $c[1]++;
133 }
134 return $c;
135 },
136 [0, 0]
137 );
138 return $clone;
139 }
140
141 private function sort(array $mark_steps): array
142 {
143 usort(
144 $mark_steps,
145 function ($a, $b): int {
146 if ($a->getMinimumLevel() === $b->getMinimumLevel()) {
147 $res = strcmp($a->getShortName(), $b->getShortName());
148 if ($res === 0) {
149 return strcmp($a->getOfficialName(), $b->getOfficialName());
150 } else {
151 return $res;
152 }
153 }
154 return ($a->getMinimumLevel() < $b->getMinimumLevel()) ? -1 : 1;
155 }
156 );
157 return $mark_steps;
158 }
159
160
161 public function toLog(AdditionalInformationGenerator $additional_info): array
162 {
163 $log_array = [];
164 foreach ($this->getMarkSteps() as $mark) {
165 $log_array[$mark->getShortName()] = [
166 AdditionalInformationGenerator::KEY_MARK_SHORT_NAME => $mark->getShortName(),
167 AdditionalInformationGenerator::KEY_MARK_OFFICIAL_NAME => $mark->getOfficialName(),
168 AdditionalInformationGenerator::KEY_MARK_MINIMUM_LEVEL => $mark->getMinimumLevel(),
169 AdditionalInformationGenerator::KEY_MARK_IS_PASSING => $additional_info
170 ->getTrueFalseTagForBool($mark->getPassed())
171 ];
172 }
173 return $log_array;
174 }
175
176 public function toExport(): array
177 {
178 return ['mark_steps' => array_map(static fn(Mark $mark): array => $mark->toExport(), $this->mark_steps)];
179 }
180
181 public static function fromExport(array $data): static
182 {
183 return (new self($data['test_id'] ?? -1))
184 ->withMarkSteps(array_map(static fn(array $mark): Mark => Mark::fromExport($mark), $data['mark_steps']));
185 }
186}
A class defining mark schemas for assessment test objects.
Definition: MarkSchema.php:37
__construct(private int $test_id)
Definition: MarkSchema.php:45
toLog(AdditionalInformationGenerator $additional_info)
Definition: MarkSchema.php:161
getMatchingMark(float $percentage)
Definition: MarkSchema.php:73
toExport()
Transform the object into a simple, associative array.
Definition: MarkSchema.php:176
static fromExport(array $data)
Creates an instance of the object from an array.
Definition: MarkSchema.php:181
A class defining marks for assessment test objects.
Definition: Mark.php:37
toExport()
Transform the object into a simple, associative array.
Definition: Mark.php:187
$c
Definition: deliver.php:25
This interface allows an object to define its own transformation into a language-neutral,...
Definition: Exportable.php:40
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'))