ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.assMarkSchema.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/Test/classes/inc.AssessmentConstants.php';
5 
17 {
19  public $mark_steps;
20 
28  public function __construct()
29  {
30  $this->mark_steps = array();
31  }
32 
48  public function createSimpleSchema(
49  $txt_failed_short = "failed",
50  $txt_failed_official = "failed",
51  $percentage_failed = 0,
52  $failed_passed = 0,
53  $txt_passed_short = "passed",
54  $txt_passed_official = "passed",
55  $percentage_passed = 50,
56  $passed_passed = 1
57  ) {
58  $this->flush();
59  $this->addMarkStep($txt_failed_short, $txt_failed_official, $percentage_failed, $failed_passed);
60  $this->addMarkStep($txt_passed_short, $txt_passed_official, $percentage_passed, $passed_passed);
61  }
62 
74  public function addMarkStep($txt_short = "", $txt_official = "", $percentage = 0, $passed = 0)
75  {
76  require_once './Modules/Test/classes/class.assMark.php';
77  $mark = new ASS_Mark($txt_short, $txt_official, $percentage, $passed);
78  array_push($this->mark_steps, $mark);
79  }
80 
86  public function saveToDb($test_id)
87  {
88  global $DIC;
89  $lng = $DIC['lng'];
90  $ilDB = $DIC['ilDB'];
91 
92  $oldmarks = array();
93  include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
95  $result = $ilDB->queryF(
96  "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
97  array('integer'),
98  array($test_id)
99  );
100  if ($result->numRows()) {
102  while ($row = $ilDB->fetchAssoc($result)) {
103  $oldmarks[$row["minimum_level"]] = $row;
104  }
105  }
106  }
107 
108  if (!$test_id) {
109  return;
110  }
111  // Delete all entries
112  $ilDB->manipulateF(
113  "DELETE FROM tst_mark WHERE test_fi = %s",
114  array('integer'),
115  array($test_id)
116  );
117  if (count($this->mark_steps) == 0) {
118  return;
119  }
120 
121  // Write new datasets
122  foreach ($this->mark_steps as $key => $value) {
123  $next_id = $ilDB->nextId('tst_mark');
124  $ilDB->manipulateF(
125  "INSERT INTO tst_mark (mark_id, test_fi, short_name, official_name, minimum_level, passed, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
126  array('integer','integer','text','text','float','text','integer'),
127  array(
128  $next_id,
129  $test_id,
130  $value->getShortName(),
131  $value->getOfficialName(),
132  $value->getMinimumLevel(),
133  $value->getPassed(),
134  time()
135  )
136  );
137  }
139  $result = $ilDB->queryF(
140  "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
141  array('integer'),
142  array($test_id)
143  );
144  $newmarks = array();
145  if ($result->numRows()) {
147  while ($row = $ilDB->fetchAssoc($result)) {
148  $newmarks[$row["minimum_level"]] = $row;
149  }
150  }
151  foreach ($oldmarks as $level => $row) {
152  if (array_key_exists($level, $newmarks)) {
153  $difffields = array();
154  foreach ($row as $key => $value) {
155  if (strcmp($value, $newmarks[$level][$key]) != 0) {
156  switch ($key) {
157  case "mark_id":
158  case "tstamp":
159  break;
160  default:
161  array_push($difffields, "$key: $value => " . $newmarks[$level][$key]);
162  break;
163  }
164  }
165  }
166  if (count($difffields)) {
167  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_changed", ilObjAssessmentFolder::_getLogLanguage()) . ": " . join(", ", $difffields));
168  }
169  } else {
170  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_removed", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
171  $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
172  $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
173  $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
174  $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
175  }
176  }
177  foreach ($newmarks as $level => $row) {
178  if (!array_key_exists($level, $oldmarks)) {
179  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_added", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
180  $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
181  $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
182  $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
183  $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
184  }
185  }
186  }
187  }
188 
194  public function loadFromDb($test_id)
195  {
196  global $DIC;
197  $ilDB = $DIC['ilDB'];
198 
199  if (!$test_id) {
200  return;
201  }
202  $result = $ilDB->queryF(
203  "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
204  array('integer'),
205  array($test_id)
206  );
207  if ($result->numRows() > 0) {
209  while ($data = $ilDB->fetchAssoc($result)) {
210  $this->addMarkStep($data["short_name"], $data["official_name"], $data["minimum_level"], $data["passed"]);
211  }
212  }
213  }
214 
220  public function flush()
221  {
222  $this->mark_steps = array();
223  }
224 
230  public function sort()
231  {
232  function level_sort($a, $b)
233  {
234  if ($a->getMinimumLevel() == $b->getMinimumLevel()) {
235  $res = strcmp($a->getShortName(), $b->getShortName());
236  if ($res == 0) {
237  return strcmp($a->getOfficialName(), $b->getOfficialName());
238  } else {
239  return $res;
240  }
241  }
242  return ($a->getMinimumLevel() < $b->getMinimumLevel()) ? -1 : 1;
243  }
244  usort($this->mark_steps, 'level_sort');
245  }
246 
254  public function deleteMarkStep($index = 0)
255  {
256  if ($index < 0) {
257  return;
258  }
259  if (count($this->mark_steps) < 1) {
260  return;
261  }
262  if ($index >= count($this->mark_steps)) {
263  return;
264  }
265  unset($this->mark_steps[$index]);
266  $this->mark_steps = array_values($this->mark_steps);
267  }
268 
276  public function deleteMarkSteps($indexes)
277  {
278  foreach ($indexes as $key => $index) {
279  if (!(($index < 0) or (count($this->mark_steps) < 1))) {
280  unset($this->mark_steps[$index]);
281  }
282  }
283  $this->mark_steps = array_values($this->mark_steps);
284  }
285 
295  public function getMatchingMark($percentage)
296  {
297  for ($i = count($this->mark_steps) - 1; $i >= 0; $i--) {
298  $curMinLevel = $this->mark_steps[$i]->getMinimumLevel();
299 
300  if ($percentage > $curMinLevel || (string) $percentage == (string) $curMinLevel) { // >= does NOT work since PHP is a fucking female float pig !!!!
301  return $this->mark_steps[$i];
302  }
303  }
304  return false;
305  }
306 
317  public static function _getMatchingMark($test_id, $percentage)
318  {
319  global $DIC;
320  $ilDB = $DIC['ilDB'];
321  $result = $ilDB->queryF(
322  "SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level DESC",
323  array('integer'),
324  array($test_id)
325  );
326 
328  while ($row = $ilDB->fetchAssoc($result)) {
329  if ($percentage >= $row["minimum_level"]) {
330  return $row;
331  }
332  }
333  return false;
334  }
335 
346  public static function _getMatchingMarkFromObjId($a_obj_id, $percentage)
347  {
348  global $DIC;
349  $ilDB = $DIC['ilDB'];
350  $result = $ilDB->queryF(
351  "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",
352  array('integer'),
353  array($a_obj_id)
354  );
355  while ($row = $ilDB->fetchAssoc($result)) {
356  if ($percentage >= $row["minimum_level"]) {
357  return $row;
358  }
359  }
360  return false;
361  }
362 
373  public static function _getMatchingMarkFromActiveId($active_id, $percentage)
374  {
376  global $DIC;
377  $ilDB = $DIC['ilDB'];
378  $result = $ilDB->queryF(
379  "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",
380  array('integer'),
381  array($active_id)
382  );
383 
385  while ($row = $ilDB->fetchAssoc($result)) {
386  if ($percentage >= $row["minimum_level"]) {
387  return $row;
388  }
389  }
390  return false;
391  }
392 
400  public function checkMarks()
401  {
402  $minimum_percentage = 100;
403  $passed = 0;
404  for ($i = 0; $i < count($this->mark_steps); $i++) {
405  if ($this->mark_steps[$i]->getMinimumLevel() < $minimum_percentage) {
406  $minimum_percentage = $this->mark_steps[$i]->getMinimumLevel();
407  }
408  if ($this->mark_steps[$i]->getPassed()) {
409  $passed++;
410  }
411  }
412 
413  if ($minimum_percentage != 0) {
414  return "min_percentage_ne_0";
415  }
416 
417  if ($passed == 0) {
418  return "no_passed_mark";
419  }
420  return true;
421  }
422 
426  public function getMarkSteps()
427  {
428  return $this->mark_steps;
429  }
430 
434  public function setMarkSteps($mark_steps)
435  {
436  $this->mark_steps = $mark_steps;
437  }
438 
447  public function logAction($test_id, $logtext = "")
448  {
450  global $DIC;
451  $ilUser = $DIC['ilUser'];
452  include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
453  ilObjAssessmentFolder::_addLog($ilUser->id, ilObjTest::_getObjectIDFromTestID($test_id), $logtext, "", "", true, $_GET["ref_id"]);
454  }
455 }
static _getObjectIDFromTestID($test_id)
Returns the ILIAS test object id for a given test id.
$result
global $DIC
Definition: saml.php:7
$_GET["client_id"]
sort()
Sorts the mark schema using the minimum level values.
flush()
Empties the mark schema and removes all mark steps.
A class defining marks for assessment test objects.
$index
Definition: metadata.php:60
addMarkStep($txt_short="", $txt_official="", $percentage=0, $passed=0)
Adds a mark step to the mark schema.
deleteMarkSteps($indexes)
Deletes multiple mark steps using their index positions.
static _getLogLanguage()
retrieve the log language for assessment logging
static _getMatchingMarkFromObjId($a_obj_id, $percentage)
Returns the matching mark for a given percentage.
static _enabledAssessmentLogging()
check wether assessment logging is enabled or not
saveToDb($test_id)
Saves an ASS_MarkSchema object to a database.
foreach($_POST as $key=> $value) $res
static _addLog($user_id, $object_id, $logtext, $question_id="", $original_id="", $test_only=false, $test_ref_id=null)
Add an assessment log entry.
$lng
createSimpleSchema( $txt_failed_short="failed", $txt_failed_official="failed", $percentage_failed=0, $failed_passed=0, $txt_passed_short="passed", $txt_passed_official="passed", $percentage_passed=50, $passed_passed=1)
Creates a simple mark schema for two mark steps: failed and passed.
$ilUser
Definition: imgupload.php:18
__construct()
ASS_MarkSchema constructor.
$row
checkMarks()
Check the marks for consistency.
global $ilDB
loadFromDb($test_id)
Loads an ASS_MarkSchema object from a database.
$i
Definition: disco.tpl.php:19
A class defining mark schemas for assessment test objects.
static _getMatchingMark($test_id, $percentage)
Returns the matching mark for a given percentage.
$key
Definition: croninfo.php:18
setMarkSteps($mark_steps)
deleteMarkStep($index=0)
Deletes the mark step with a given index.
getMatchingMark($percentage)
Returns the matching mark for a given percentage.
$data
Definition: bench.php:6