ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMarkSchema.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
36 {
45 
53  function ASS_MarkSchema()
54  {
55  $this->mark_steps = array();
56  }
57 
76  $txt_failed_short = "failed",
77  $txt_failed_official = "failed",
78  $percentage_failed = 0,
79  $failed_passed = 0,
80  $txt_passed_short = "passed",
81  $txt_passed_official = "passed",
82  $percentage_passed = 50,
83  $passed_passed = 1
84  )
85  {
86  $this->flush();
87  $this->addMarkStep($txt_failed_short, $txt_failed_official, $percentage_failed, $failed_passed);
88  $this->addMarkStep($txt_passed_short, $txt_passed_official, $percentage_passed, $passed_passed);
89  }
90 
104  function addMarkStep(
105  $txt_short = "",
106  $txt_official = "",
107  $percentage = 0,
108  $passed = 0
109  )
110  {
111  include_once "./Modules/Test/classes/class.assMark.php";
112  $mark = new ASS_Mark($txt_short, $txt_official, $percentage, $passed);
113  array_push($this->mark_steps, $mark);
114  }
115 
124  function saveToDb($test_id)
125  {
126  global $lng;
127  global $ilDB;
128 
129  $oldmarks = array();
130  include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
132  {
133  $query = sprintf("SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
134  $ilDB->quote($test_id)
135  );
136  $result = $ilDB->query($query);
137  if ($result->numRows())
138  {
139  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
140  {
141  $oldmarks[$row["minimum_level"]] = $row;
142  }
143  }
144  }
145 
146  if (!$test_id) return;
147  // Delete all entries
148  $query = sprintf("DELETE FROM tst_mark WHERE test_fi = %s",
149  $ilDB->quote($test_id)
150  );
151  $result = $ilDB->query($query);
152  if (count($this->mark_steps) == 0) return;
153 
154  // Write new datasets
155  foreach ($this->mark_steps as $key => $value)
156  {
157  $query = sprintf("INSERT INTO tst_mark (mark_id, test_fi, short_name, official_name, minimum_level, passed, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
158  $ilDB->quote($test_id),
159  $ilDB->quote($value->getShortName()),
160  $ilDB->quote($value->getOfficialName()),
161  $ilDB->quote($value->getMinimumLevel()),
162  $ilDB->quote(sprintf("%d", $value->getPassed()))
163  );
164  $result = $ilDB->query($query);
165  if (PEAR::isError($result))
166  {
167  global $ilias;
168  $ilias->raiseError($result->getMessage());
169  }
170  }
172  {
173  $query = sprintf("SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
174  $ilDB->quote($test_id)
175  );
176  $result = $ilDB->query($query);
177  $newmarks = array();
178  if ($result->numRows())
179  {
180  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
181  {
182  $newmarks[$row["minimum_level"]] = $row;
183  }
184  }
185  foreach ($oldmarks as $level => $row)
186  {
187  if (array_key_exists($level, $newmarks))
188  {
189  $difffields = array();
190  foreach ($row as $key => $value)
191  {
192  if (strcmp($value, $newmarks[$level][$key]) != 0)
193  {
194  switch ($key)
195  {
196  case "mark_id":
197  case "TIMESTAMP":
198  break;
199  default:
200  array_push($difffields, "$key: $value => " .$newmarks[$level][$key]);
201  break;
202  }
203  }
204  }
205  if (count($difffields))
206  {
207  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_changed", ilObjAssessmentFolder::_getLogLanguage()) . ": " . join($difffields, ", "));
208  }
209  }
210  else
211  {
212  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_removed", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
213  $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
214  $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
215  $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
216  $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
217  }
218  }
219  foreach ($newmarks as $level => $row)
220  {
221  if (!array_key_exists($level, $oldmarks))
222  {
223  $this->logAction($test_id, $lng->txtlng("assessment", "log_mark_added", ilObjAssessmentFolder::_getLogLanguage()) . ": " .
224  $lng->txtlng("assessment", "tst_mark_minimum_level", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["minimum_level"] . ", " .
225  $lng->txtlng("assessment", "tst_mark_short_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["short_name"] . ", " .
226  $lng->txtlng("assessment", "tst_mark_official_form", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["official_name"] . ", " .
227  $lng->txtlng("assessment", "tst_mark_passed", ilObjAssessmentFolder::_getLogLanguage()) . " = " . $row["passed"]);
228  }
229  }
230  }
231  }
232 
241  function loadFromDb($test_id)
242  {
243  global $ilDB;
244 
245  if (!$test_id) return;
246  $query = sprintf("SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level",
247  $ilDB->quote($test_id)
248  );
249 
250  $result = $ilDB->query($query);
251  if ($result->numRows() > 0)
252  {
253  while ($data = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
254  {
255  $this->addMarkStep($data->short_name, $data->official_name, $data->minimum_level, $data->passed);
256  }
257  }
258  }
259 
268  function flush()
269  {
270  $this->mark_steps = array();
271  }
272 
281  function sort() {
282  function level_sort($a, $b) {
283  if ($a->getMinimumLevel() == $b->getMinimumLevel()) {
284  $res = strcmp($a->getShortName(), $b->getShortName());
285  if ($res == 0) {
286  return strcmp($a->getOfficialName(), $b->getOfficialName());
287  } else {
288  return $res;
289  }
290  }
291  return ($a->getMinimumLevel() < $b->getMinimumLevel()) ? -1 : 1;
292  }
293 
294  usort($this->mark_steps, 'level_sort');
295  }
296 
306  function deleteMarkStep($index = 0) {
307  if ($index < 0) return;
308  if (count($this->mark_steps) < 1) return;
309  if ($index >= count($this->mark_steps)) return;
310  unset($this->mark_steps[$index]);
311  $this->mark_steps = array_values($this->mark_steps);
312  }
313 
323  function deleteMarkSteps($indexes) {
324  foreach ($indexes as $key => $index) {
325  if (!(($index < 0) or (count($this->mark_steps) < 1))) {
326  unset($this->mark_steps[$index]);
327  }
328  }
329  $this->mark_steps = array_values($this->mark_steps);
330  }
331 
342  function getMatchingMark($percentage) {
343  for ($i = count($this->mark_steps) - 1; $i >= 0; $i--) {
344  if ($percentage >= $this->mark_steps[$i]->getMinimumLevel()) {
345  return $this->mark_steps[$i];
346  }
347  }
348  return false;
349  }
350 
362  function _getMatchingMark($test_id, $percentage)
363  {
364  global $ilDB;
365  $query = sprintf("SELECT * FROM tst_mark WHERE test_fi = %s ORDER BY minimum_level DESC",
366  $ilDB->quote($test_id . "")
367  );
368  $result = $ilDB->query($query);
369  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
370  {
371  if ($percentage >= $row["minimum_level"])
372  {
373  return $row;
374  }
375  }
376  return FALSE;
377  }
378 
390  function _getMatchingMarkFromObjId($a_obj_id, $percentage)
391  {
392  global $ilDB;
393  $query = sprintf("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",
394  $ilDB->quote($a_obj_id . "")
395  );
396  $result = $ilDB->query($query);
397  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
398  {
399  if ($percentage >= $row["minimum_level"])
400  {
401  return $row;
402  }
403  }
404  return FALSE;
405  }
406 
416  function checkMarks()
417  {
418  $minimum_percentage = 100;
419  $passed = 0;
420  for ($i = 0; $i < count($this->mark_steps); $i++) {
421  if ($this->mark_steps[$i]->getMinimumLevel() < $minimum_percentage)
422  {
423  $minimum_percentage = $this->mark_steps[$i]->getMinimumLevel();
424  }
425  if ($this->mark_steps[$i]->getPassed())
426  {
427  $passed++;
428  }
429  }
430  if ($minimum_percentage != 0)
431  {
432  return "min_percentage_ne_0";
433  }
434  if ($passed == 0)
435  {
436  return "no_passed_mark";
437  }
438  return true;
439  }
440 
450  function logAction($test_id, $logtext = "")
451  {
452  global $ilUser;
453  include_once "./Modules/Test/classes/class.ilObjAssessmentFolder.php";
454  ilObjAssessmentFolder::_addLog($ilUser->id, ilObjTest::_getObjectIDFromTestID($test_id), $logtext, "", "", TRUE, $_GET["ref_id"]);
455  }
456 }
457 
458 ?>