• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

assessment/classes/class.ilObjTestAccess.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 include_once "./classes/class.ilObjectAccess.php";
00025 
00038 class ilObjTestAccess extends ilObjectAccess
00039 {
00055         function _checkAccess($a_cmd, $a_permission, $a_ref_id, $a_obj_id, $a_user_id = "")
00056         {
00057                 global $ilUser, $lng, $rbacsystem, $ilAccess;
00058                 if ($a_user_id == "")
00059                 {
00060                         $a_user_id = $ilUser->getId();
00061                 }
00062 
00063                 switch ($a_permission)
00064                 {
00065                         case "visible":
00066                                 if (!ilObjTestAccess::_lookupCreationComplete($a_obj_id) &&
00067                                         (!$rbacsystem->checkAccess('write', $a_ref_id)))
00068                                 {
00069                                         $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("tst_warning_test_not_complete"));
00070                                         return false;
00071                                 }
00072                                 break;
00073                 }
00074                 switch ($a_cmd)
00075                 {
00076                         case "eval_a":
00077                         case "eval_stat":
00078                                 if (!ilObjTestAccess::_lookupCreationComplete($a_obj_id))
00079                                 {
00080                                         $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("tst_warning_test_not_complete"));
00081                                         return false;
00082                                 }
00083                                 break;
00084 
00085                 }
00086 
00087                 return true;
00088         }
00089 
00095         function _checkCondition($a_obj_id, $a_operator, $a_value)
00096         {
00097                 global $ilias;
00098                 switch($a_operator)
00099                 {
00100                         case 'passed':
00101                                 $result = ilObjTestAccess::_getTestResult($ilias->account->getId(), $a_obj_id);
00102                                 if ($result["passed"] == 1)
00103                                 {
00104                                         return true;
00105                                 }
00106                                 else
00107                                 {
00108                                         return false;
00109                                 }
00110                                 break;
00111 
00112                         case 'finished':
00113                                 return ilObjTestAccess::_hasFinished($ilias->account->getId(),$a_obj_id);
00114 
00115                         case 'not_finished':
00116                                 return !ilObjTestAccess::_hasFinished($ilias->account->getId(),$a_obj_id);
00117 
00118                         default:
00119                                 return true;
00120                 }
00121                 return true;
00122         }
00123 
00136         function _getCommands()
00137         {
00138                 $commands = array
00139                 (
00140                         array("permission" => "read", "cmd" => "run", "lang_var" => "tst_run",
00141                                 "default" => true),
00142                         array("permission" => "write", "cmd" => "", "lang_var" => "edit"),
00143                         array("permission" => "write", "cmd" => "eval_a", "lang_var" => "tst_anon_eval"),
00144                         array("permission" => "write", "cmd" => "eval_stat", "lang_var" => "tst_statistical_evaluation")
00145                 );
00146                 
00147                 return $commands;
00148         }
00149 
00150         //
00151         // object specific access related methods
00152         //
00153 
00157         function _lookupCreationComplete($a_obj_id)
00158         {
00159                 global $ilDB;
00160 
00161                 $q = sprintf("SELECT * FROM tst_tests WHERE obj_fi=%s",
00162                         $ilDB->quote($a_obj_id)
00163                 );
00164                 $result = $ilDB->query($q);
00165                 if ($result->numRows() == 1)
00166                 {
00167                         $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
00168                 }
00169 
00170                 if (!$row->complete)
00171                 {
00172                         return false;
00173                 }
00174 
00175                 return true;
00176         }
00177 
00187         function _hasFinished($a_user_id,$a_obj_id)
00188         {
00189                 global $ilDB;
00190 
00191                 $query = sprintf("SELECT active_id FROM tst_active WHERE user_fi = %s AND test_fi = %s AND tries > '0'",
00192                         $ilDB->quote($a_user_id . ""),
00193                         $ilDB->quote(ilObjTestAccess::_getTestIDFromObjectID($a_obj_id) . "")
00194                 );
00195                 $res = $ilDB->query($query);
00196 
00197                 return $res->numRows() ? true : false;
00198         }
00199 
00209         function _getTestIDFromObjectID($object_id)
00210         {
00211                 global $ilDB;
00212                 $test_id = FALSE;
00213                 $query = sprintf("SELECT test_id FROM tst_tests WHERE obj_fi = %s",
00214                         $ilDB->quote($object_id . "")
00215                 );
00216                 $result = $ilDB->query($query);
00217                 if ($result->numRows())
00218                 {
00219                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00220                         $test_id = $row["test_id"];
00221                 }
00222                 return $test_id;
00223         }
00224         
00234         function &_getTestResult($user_id, $test_obj_id) 
00235         {
00236                 global $ilDB;
00237                 
00238                 $test_result = array();
00239                 $query = sprintf("SELECT tst_mark.*, tst_tests.* FROM tst_mark, tst_tests WHERE tst_mark.test_fi = tst_tests.test_id AND tst_tests.obj_fi = %s ORDER BY tst_mark.minimum_level",
00240                         $ilDB->quote($test_obj_id . "")
00241                 );
00242                 $result = $ilDB->query($query);
00243                 if ($result->numRows())
00244                 {
00245                         $test_result["marks"] = array();
00246                         $min_passed_percentage = 100;
00247                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00248                         {
00249                                 if (($row["passed"] == 1) && ($row["minimum_level"] < $min_passed_percentage))
00250                                 {
00251                                         $min_passed_percentage = $row["minimum_level"];
00252                                 }
00253                                 array_push($test_result["marks"], $row);
00254                         }
00255                         // count points
00256                         $query = sprintf("SELECT qpl_questions.*, tst_test_result.points AS reached_points FROM qpl_questions, tst_test_result WHERE qpl_questions.question_id = tst_test_result.question_fi AND tst_test_result.test_fi = %s AND tst_test_result.user_fi = %s",
00257                                 $ilDB->quote(ilObjTestAccess::_getTestIDFromObjectID($test_obj_id) . ""),
00258                                 $ilDB->quote($user_id . "")
00259                         );
00260                         $result = $ilDB->query($query);
00261                         $max_points = 0;
00262                         $reached_points = 0;
00263                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00264                         {
00265                                 $max_points += $row["points"];
00266                                 $reached_points += $row["reached_points"];
00267                         }
00268                         $test_result["max_points"] = $max_points;
00269                         $test_result["reached_points"] = $reached_points;
00270                         // calculate the percentage of the reached points
00271                         $solved = 0;
00272                         if ($max_points > 0)
00273                         {
00274                                 $solved = ($reached_points / $max_points) * 100.0;
00275                         }
00276                         // get the mark for the reached points
00277                         $mark_percentage = 0;
00278                         $mark_value = null;
00279                         foreach ($test_result["marks"] as $key => $value)
00280                         {
00281                                 if (($value["minimum_level"] <= $solved) && ($mark_percentage < $value["minimum_level"]))
00282                                 {
00283                                         $mark_percentage = $value["minimum_level"];
00284                                         $mark_value = $value;
00285                                 }
00286                         }
00287                         $test_result["mark"] = $mark_value;
00288                         // get the passed state
00289                         $test_result["passed"] = $test_result["mark"][passed];
00290                 }
00291                 return $test_result;
00292         }
00293 
00302         function _isComplete($a_obj_id)
00303         {
00304                 global $ilDB;
00305                 
00306                 $test_id = ilObjTestAccess::_getTestIDFromObjectID($a_obj_id);
00307                 $query = sprintf("SELECT tst_mark.*, tst_tests.* FROM tst_tests, tst_mark WHERE tst_mark.test_fi = tst_tests.test_id AND tst_tests.test_id = %s",
00308                         $ilDB->quote($test_id . "")
00309                 );
00310                 $result = $ilDB->query($query);
00311                 $found = $result->numRows();
00312                 if ($found)
00313                 {
00314                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00315                         // check for at least: title, author and minimum of 1 mark step
00316                         if ((strlen($row["title"])) &&
00317                                 (strlen($row["author"])) &&
00318                                 ($found))
00319                         {
00320                                 // check also for minmum of 1 question
00321                                 if (ilObjTestAccess::_getQuestionCount($test_id) > 0)
00322                                 {
00323                                         return true;
00324                                 }
00325                                 else
00326                                 {
00327                                         return false;
00328                                 }
00329                         }
00330                         else
00331                         {
00332                                 return false;
00333                         }
00334                 }
00335                 else
00336                 {
00337                         return false;
00338                 }
00339                 $test = new ilObjTest($obj_id, false);
00340                 $test->loadFromDb();
00341                 if (($test->getTitle()) and ($test->author) and (count($test->mark_schema->mark_steps)) and (count($test->questions)))
00342                 {
00343                         return true;
00344                 } 
00345                         else 
00346                 {
00347                         return false;
00348                 }
00349         }
00350         
00359         function _getQuestionCount($a_test_id)
00360         {
00361                 global $ilDB;
00362 
00363                 $num = 0;
00364                 
00365                 $query = sprintf("SELECT * FROM tst_tests WHERE test_id = %s",
00366                         $ilDB->quote($a_test_id)
00367                 );
00368                 $result = $ilDB->query($query);
00369                 if ($result->numRows != 1) return 0;
00370                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00371                 
00372                 if ($row["random_test"] == 1)
00373                 {
00374                         if ($row["random_question_count"] > 0)
00375                         {
00376                                 $num = $row["random_question_count"];
00377                         }
00378                                 else
00379                         {
00380                                 $query = sprintf("SELECT * FROM tst_test_random WHERE test_fi = %s ORDER BY test_random_id",
00381                                         $ilDB->quote($a_test_id . "")
00382                                 );
00383                                 $result = $ilDB->query($query);
00384                                 if ($result->numRows())
00385                                 {
00386                                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00387                                         {
00388                                                 $num += $row["num_of_q"];
00389                                         }
00390                                 }
00391                         }
00392                 }
00393                 else
00394                 {
00395                         $query = sprintf("SELECT question_fi FROM tst_test_question WHERE test_fi = %s",
00396                                 $ilDB->quote($a_test_id . "")
00397                         );
00398                         $result = $ilDB->query($query);
00399                         $num = $result->numRows();
00400                 }
00401                 return $num;
00402         }
00403         
00412         function _lookupOnlineTestAccess($a_test_id, $a_user_id)
00413         {
00414                 global $ilDB, $lng;
00415                 
00416                 $test_result = array();
00417                 $query = sprintf("SELECT tst_tests.* FROM tst_tests WHERE tst_tests.obj_fi = %s",
00418                         $ilDB->quote($a_test_id . "")
00419                 );
00420                 $result = $ilDB->query($query);
00421                 if ($result->numRows())
00422                 {
00423                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00424                         if ($row["test_type_fi"] == 4)
00425                         {
00426                                 $query = sprintf("SELECT * FROM tst_invited_user WHERE test_fi = %s AND user_fi = %s",
00427                                         $ilDB->quote($row["test_id"] . ""),
00428                                         $ilDB->quote($a_user_id . "")
00429                                 );
00430                                 $result = $ilDB->query($query);
00431                                 if ($result->numRows())
00432                                 {
00433                                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00434                                         if (strcmp($row["clientip"],"")!=0 && strcmp($row["clientip"],$_SERVER["REMOTE_ADDR"])!=0)
00435                                         {
00436                                                 return $lng->txt("tst_user_wrong_clientip");
00437                                         }
00438                                         else
00439                                         {
00440                                                 return true;
00441                                         }
00442                                 }
00443                                 else
00444                                 {
00445                                         return $lng->txt("tst_user_not_invited");
00446                                 }
00447                         }
00448                         else
00449                         {
00450                                 return true;
00451                         }
00452                 }
00453                 else
00454                 {
00455                         return true;
00456                 }
00457         }
00458 
00459 }
00460 
00461 ?>

Generated on Fri Dec 13 2013 10:18:26 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1