ILIAS  release_8 Revision v8.24
class.assMarkSchema.php
Go to the documentation of this file.
1<?php
2
19require_once './Modules/Test/classes/inc.AssessmentConstants.php';
20
32{
33 public array $mark_steps;
34
35 public function __construct()
36 {
37 $this->mark_steps = array();
38 }
39
55 public function createSimpleSchema(
56 string $txt_failed_short = "failed",
57 string $txt_failed_official = "failed",
58 float $percentage_failed = 0,
59 int $failed_passed = 0,
60 string $txt_passed_short = "passed",
61 string $txt_passed_official = "passed",
62 float $percentage_passed = 50,
63 int $passed_passed = 1
64 ) {
65 $this->flush();
66 $this->addMarkStep($txt_failed_short, $txt_failed_official, $percentage_failed, $failed_passed);
67 $this->addMarkStep($txt_passed_short, $txt_passed_official, $percentage_passed, $passed_passed);
68 }
69
81 public function addMarkStep(string $txt_short = "", string $txt_official = "", $percentage = 0, $passed = 0): void
82 {
83 require_once './Modules/Test/classes/class.assMark.php';
84 $mark = new ASS_Mark($txt_short, $txt_official, $percentage, $passed);
85 array_push($this->mark_steps, $mark);
86 }
87
88 public function saveToDb(int $test_id): void
89 {
90 global $DIC;
91 $lng = $DIC['lng'];
92 $ilDB = $DIC['ilDB'];
93
94 $oldmarks = array();
95 include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
97 $result = $ilDB->queryF(
98 "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
99 array('integer'),
100 array($test_id)
101 );
102 if ($result->numRows()) {
104 while ($row = $ilDB->fetchAssoc($result)) {
105 $oldmarks[$row["minimum_level"]] = $row;
106 }
107 }
108 }
109
110 if (!$test_id) {
111 return;
112 }
113 // Delete all entries
114 $ilDB->manipulateF(
115 "DELETE FROM tst_mark WHERE test_fi = %s",
116 array('integer'),
117 array($test_id)
118 );
119 if (count($this->mark_steps) == 0) {
120 return;
121 }
122
123 // Write new datasets
124 foreach ($this->mark_steps as $key => $value) {
125 $next_id = $ilDB->nextId('tst_mark');
126 $ilDB->manipulateF(
127 "INSERT INTO tst_mark (mark_id, test_fi, short_name, official_name, minimum_level, passed, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
128 array('integer','integer','text','text','float','text','integer'),
129 array(
130 $next_id,
131 $test_id,
132 substr($value->getShortName(), 0, 15),
133 substr($value->getOfficialName(), 0, 50),
134 $value->getMinimumLevel(),
135 $value->getPassed(),
136 time()
137 )
138 );
139 }
141 $result = $ilDB->queryF(
142 "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
143 array('integer'),
144 array($test_id)
145 );
146 $newmarks = array();
147 if ($result->numRows()) {
149 while ($row = $ilDB->fetchAssoc($result)) {
150 $newmarks[$row["minimum_level"]] = $row;
151 }
152 }
153 foreach ($oldmarks as $level => $row) {
154 if (array_key_exists($level, $newmarks)) {
155 $difffields = array();
156 foreach ($row as $key => $value) {
157 if (strcmp($value, $newmarks[$level][$key]) != 0) {
158 switch ($key) {
159 case "mark_id":
160 case "tstamp":
161 break;
162 default:
163 array_push($difffields, "$key: $value => " . $newmarks[$level][$key]);
164 break;
165 }
166 }
167 }
168 if (count($difffields)) {
169 $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_changed", ilObjAssessmentFolder::_getLogLanguage()) . ": " . join(", ", $difffields));
170 }
171 } else {
172 $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_removed", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
173 $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
174 $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
175 $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
176 $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
177 }
178 }
179 foreach ($newmarks as $level => $row) {
180 if (!array_key_exists($level, $oldmarks)) {
181 $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_added", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
182 $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
183 $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
184 $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
185 $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
186 }
187 }
188 }
189 }
190
191 public function loadFromDb(int $test_id): void
192 {
193 global $DIC;
194 $ilDB = $DIC['ilDB'];
195
196 if (!$test_id) {
197 return;
198 }
199 $result = $ilDB->queryF(
200 "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
201 array('integer'),
202 array($test_id)
203 );
204 if ($result->numRows() > 0) {
206 while ($data = $ilDB->fetchAssoc($result)) {
207 $this->addMarkStep(
208 $data["short_name"] ?? '',
209 $data["official_name"] ?? '',
210 (float) $data["minimum_level"],
211 (int) $data["passed"]
212 );
213 }
214 }
215 }
216
217 public function flush(): void
218 {
219 $this->mark_steps = array();
220 }
221
227 public function sort(): void
228 {
229 function level_sort($a, $b): int
230 {
231 if ($a->getMinimumLevel() == $b->getMinimumLevel()) {
232 $res = strcmp($a->getShortName(), $b->getShortName());
233 if ($res == 0) {
234 return strcmp($a->getOfficialName(), $b->getOfficialName());
235 } else {
236 return $res;
237 }
238 }
239 return ($a->getMinimumLevel() < $b->getMinimumLevel()) ? -1 : 1;
240 }
241 usort($this->mark_steps, 'level_sort');
242 }
243
251 public function deleteMarkStep($index = 0)
252 {
253 if ($index < 0) {
254 return;
255 }
256 if (count($this->mark_steps) < 1) {
257 return;
258 }
259 if ($index >= count($this->mark_steps)) {
260 return;
261 }
262 unset($this->mark_steps[$index]);
263 $this->mark_steps = array_values($this->mark_steps);
264 }
265
270 public function deleteMarkSteps(array $indexes): void
271 {
272 foreach ($indexes as $key => $index) {
273 if (!(($index < 0) or (count($this->mark_steps) < 1))) {
274 unset($this->mark_steps[$index]);
275 }
276 }
277 $this->mark_steps = array_values($this->mark_steps);
278 }
279
289 public function getMatchingMark($percentage)
290 {
291 for ($i = count($this->mark_steps) - 1; $i >= 0; $i--) {
292 $curMinLevel = $this->mark_steps[$i]->getMinimumLevel();
293 $reached = round($percentage, 2);
294 $level = round($curMinLevel, 2);
295 if ($reached >= $level) {
296 return $this->mark_steps[$i];
297 }
298 }
299 return false;
300 }
301
312 public static function _getMatchingMark($test_id, $percentage)
313 {
314 global $DIC;
315 $ilDB = $DIC['ilDB'];
316 $result = $ilDB->queryF(
317 "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level DESC",
318 array('integer'),
319 array($test_id)
320 );
321
323 while ($row = $ilDB->fetchAssoc($result)) {
324 if ($percentage >= $row["minimum_level"]) {
325 return $row;
326 }
327 }
328 return false;
329 }
330
340 public static function _getMatchingMarkFromObjId($a_obj_id, float $percentage)
341 {
342 global $DIC;
343 $ilDB = $DIC['ilDB'];
344 $result = $ilDB->queryF(
345 "SELECT tst_mark.* FROM tst_mark, tst_tests WHERE tst_mark.test_fi = tst_tests.test_id AND tst_tests.obj_fi = %s ORDER BY minimum_level DESC",
346 array('integer'),
347 array($a_obj_id)
348 );
349 while ($row = $ilDB->fetchAssoc($result)) {
350 if ($percentage >= $row["minimum_level"]) {
351 return $row;
352 }
353 }
354 return false;
355 }
356
367 public static function _getMatchingMarkFromActiveId($active_id, $percentage)
368 {
370 global $DIC;
371 $ilDB = $DIC['ilDB'];
372 $result = $ilDB->queryF(
373 "SELECT tst_mark.* FROM tst_active, tst_mark, tst_tests WHERE tst_mark.test_fi = tst_tests.test_id AND tst_tests.test_id = tst_active.test_fi AND tst_active.active_id = %s ORDER BY minimum_level DESC",
374 array('integer'),
375 array($active_id)
376 );
377
379 while ($row = $ilDB->fetchAssoc($result)) {
380 if ($percentage >= $row["minimum_level"]) {
381 return $row;
382 }
383 }
384 return false;
385 }
386
394 public function checkMarks()
395 {
396 $minimum_percentage = 100;
397 $passed = 0;
398 for ($i = 0; $i < count($this->mark_steps); $i++) {
399 if ($this->mark_steps[$i]->getMinimumLevel() < $minimum_percentage) {
400 $minimum_percentage = $this->mark_steps[$i]->getMinimumLevel();
401 }
402 if ($this->mark_steps[$i]->getPassed()) {
403 $passed++;
404 }
405 }
406
407 if ($minimum_percentage != 0) {
408 return "min_percentage_ne_0";
409 }
410
411 if ($passed == 0) {
412 return "no_passed_mark";
413 }
414 return true;
415 }
416
420 public function getMarkSteps(): array
421 {
422 return $this->mark_steps;
423 }
424
428 public function setMarkSteps(array $mark_steps): void
429 {
430 $this->mark_steps = $mark_steps;
431 }
432
436 public function logAction($test_id, string $logtext = ""): void
437 {
439 global $DIC;
440 $ilUser = $DIC['ilUser'];
441 include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
442 ilObjAssessmentFolder::_addLog($ilUser->getId(), ilObjTest::_getObjectIDFromTestID($test_id), $logtext, "", "", true);
443 }
444}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getMatchingMarkFromObjId($a_obj_id, float $percentage)
Returns the matching mark for a given percentage.
saveToDb(int $test_id)
loadFromDb(int $test_id)
addMarkStep(string $txt_short="", string $txt_official="", $percentage=0, $passed=0)
Adds a mark step to the mark schema.
sort()
Sorts the mark schema using the minimum level values.
createSimpleSchema(string $txt_failed_short="failed", string $txt_failed_official="failed", float $percentage_failed=0, int $failed_passed=0, string $txt_passed_short="passed", string $txt_passed_official="passed", float $percentage_passed=50, int $passed_passed=1)
Creates a simple mark schema for two mark steps: failed and passed.
deleteMarkStep($index=0)
Deletes the mark step with a given index.
setMarkSteps(array $mark_steps)
static _getMatchingMark($test_id, $percentage)
Returns the matching mark for a given percentage.
checkMarks()
Check the marks for consistency.
getMatchingMark($percentage)
Returns the matching mark for a given percentage.
deleteMarkSteps(array $indexes)
Deletes multiple mark steps using their index positions.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _addLog( $user_id, $object_id, $logtext, $question_id=0, $original_id=0, $test_only=false, $test_ref_id=0)
Add an assessment log entry.
static _getObjectIDFromTestID($test_id)
Returns the ILIAS test object id for a given test id.
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
$res
Definition: ltiservices.php:69
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
string $key
Consumer key/client ID value.
Definition: System.php:193
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$lng