• 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 include_once "./assessment/classes/inc.AssessmentConstants.php";
00026 
00039 class ilObjTestAccess extends ilObjectAccess
00040 {
00056         function _checkAccess($a_cmd, $a_permission, $a_ref_id, $a_obj_id, $a_user_id = "")
00057         {
00058                 global $ilUser, $lng, $rbacsystem, $ilAccess;
00059                 if ($a_user_id == "")
00060                 {
00061                         $a_user_id = $ilUser->getId();
00062                 }
00063 
00064                 switch ($a_permission)
00065                 {
00066                         case "read":
00067                         case "visible":
00068                                 if (!ilObjTestAccess::_lookupCreationComplete($a_obj_id) &&
00069                                         (!$rbacsystem->checkAccess('write', $a_ref_id)))
00070                                 {
00071                                         $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("tst_warning_test_not_complete"));
00072                                         return false;
00073                                 }
00074                                 break;
00075                 }
00076                 switch ($a_cmd)
00077                 {
00078                         case "eval_a":
00079                         case "eval_stat":
00080                                 if (!ilObjTestAccess::_lookupCreationComplete($a_obj_id))
00081                                 {
00082                                         $ilAccess->addInfoItem(IL_NO_OBJECT_ACCESS, $lng->txt("tst_warning_test_not_complete"));
00083                                         return false;
00084                                 }
00085                                 break;
00086 
00087                 }
00088 
00089                 return true;
00090         }
00091 
00097         function _checkCondition($a_obj_id, $a_operator, $a_value)
00098         {
00099                 global $ilias;
00100                 switch($a_operator)
00101                 {
00102                         case 'passed':
00103                                 $result = ilObjTestAccess::_getTestResult($ilias->account->getId(), $a_obj_id);
00104                                 if ($result["passed"] == 1)
00105                                 {
00106                                         return true;
00107                                 }
00108                                 else
00109                                 {
00110                                         return false;
00111                                 }
00112                                 break;
00113 
00114                         case 'finished':
00115                                 return ilObjTestAccess::_hasFinished($ilias->account->getId(),$a_obj_id);
00116 
00117                         case 'not_finished':
00118                                 return !ilObjTestAccess::_hasFinished($ilias->account->getId(),$a_obj_id);
00119 
00120                         default:
00121                                 return true;
00122                 }
00123                 return true;
00124         }
00125 
00138         function _getCommands()
00139         {
00140                 $commands = array
00141                 (
00142                         array("permission" => "read", "cmd" => "infoScreen", "lang_var" => "tst_run",
00143                                 "default" => true),
00144                         array("permission" => "write", "cmd" => "", "lang_var" => "edit"),
00145                         array("permission" => "write", "cmd" => "eval_stat", "lang_var" => "tst_statistical_evaluation")
00146                 );
00147                 
00148                 return $commands;
00149         }
00150 
00151         //
00152         // object specific access related methods
00153         //
00154 
00158         function _lookupCreationComplete($a_obj_id)
00159         {
00160                 global $ilDB;
00161 
00162                 $q = sprintf("SELECT * FROM tst_tests WHERE obj_fi=%s",
00163                         $ilDB->quote($a_obj_id)
00164                 );
00165                 $result = $ilDB->query($q);
00166                 if ($result->numRows() == 1)
00167                 {
00168                         $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
00169                 }
00170 
00171                 if (!$row->complete)
00172                 {
00173                         return false;
00174                 }
00175 
00176                 return true;
00177         }
00178 
00188         function _hasFinished($a_user_id,$a_obj_id)
00189         {
00190                 global $ilDB;
00191 
00192                 $query = sprintf("SELECT active_id FROM tst_active WHERE user_fi = %s AND test_fi = %s AND tries > '0'",
00193                         $ilDB->quote($a_user_id . ""),
00194                         $ilDB->quote(ilObjTestAccess::_getTestIDFromObjectID($a_obj_id) . "")
00195                 );
00196                 $res = $ilDB->query($query);
00197 
00198                 return $res->numRows() ? true : false;
00199         }
00200 
00210         function _getTestIDFromObjectID($object_id)
00211         {
00212                 global $ilDB;
00213                 $test_id = FALSE;
00214                 $query = sprintf("SELECT test_id FROM tst_tests WHERE obj_fi = %s",
00215                         $ilDB->quote($object_id . "")
00216                 );
00217                 $result = $ilDB->query($query);
00218                 if ($result->numRows())
00219                 {
00220                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00221                         $test_id = $row["test_id"];
00222                 }
00223                 return $test_id;
00224         }
00225         
00235         function &_getTestResult($user_id, $test_obj_id, $pass = NULL) 
00236         {
00237                 global $ilDB;
00238                 
00239                 $test_result = array();
00240                 $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",
00241                         $ilDB->quote($test_obj_id . "")
00242                 );
00243                 $result = $ilDB->query($query);
00244                 if ($result->numRows())
00245                 {
00246                         $test_result["marks"] = array();
00247                         $min_passed_percentage = 100;
00248                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00249                         {
00250                                 if (($row["passed"] == 1) && ($row["minimum_level"] < $min_passed_percentage))
00251                                 {
00252                                         $min_passed_percentage = $row["minimum_level"];
00253                                 }
00254                                 array_push($test_result["marks"], $row);
00255                         }
00256                         // count points
00257 //                      $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",
00258 //                              $ilDB->quote(ilObjTestAccess::_getTestIDFromObjectID($test_obj_id) . ""),
00259 //                              $ilDB->quote($user_id . "")
00260 //                      );
00261                         $test_id = ilObjTestAccess::_getTestIDFromObjectID($test_obj_id);
00262                         if (is_null($pass))
00263                         {
00264                                 $query = sprintf("SELECT qpl_questions.question_id, qpl_questions.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 AND tst_test_result.pass = %s",
00265                                         $ilDB->quote($test_id . ""),
00266                                         $ilDB->quote($user_id . ""),
00267                                         $ilDB->quote("0")
00268                                 );
00269                         }
00270                         else
00271                         {
00272                                 $query = sprintf("SELECT qpl_questions.question_id, qpl_questions.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 AND tst_test_result.pass = %s",
00273                                         $ilDB->quote($test_id . ""),
00274                                         $ilDB->quote($user_id . ""),
00275                                         $ilDB->quote($pass . "")
00276                                 );
00277                         }
00278                         $result = $ilDB->query($query);
00279                         $max_points = 0;
00280                         $reached_points = 0;
00281                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00282                         {
00283                                 include_once "./assessment/classes/class.assQuestion.php";
00284                                 $preached = ASS_Question::_getReachedPoints($user_id, $test_id, $row["question_id"], $pass);
00285                                 $max_points += $row["points"];
00286                                 $reached_points += $preached;
00287                         }
00288                         $test_result["max_points"] = $max_points;
00289                         $test_result["reached_points"] = $reached_points;
00290                         // calculate the percentage of the reached points
00291                         $solved = 0;
00292                         if ($max_points > 0)
00293                         {
00294                                 $solved = ($reached_points / $max_points) * 100.0;
00295                         }
00296                         // get the mark for the reached points
00297                         $mark_percentage = 0;
00298                         $mark_value = null;
00299                         foreach ($test_result["marks"] as $key => $value)
00300                         {
00301                                 if (($value["minimum_level"] <= $solved) && ($mark_percentage < $value["minimum_level"]))
00302                                 {
00303                                         $mark_percentage = $value["minimum_level"];
00304                                         $mark_value = $value;
00305                                 }
00306                         }
00307                         $test_result["mark"] = $mark_value;
00308                         // get the passed state
00309                         $test_result["passed"] = $test_result["mark"]["passed"];
00310                 }
00311                 return $test_result;
00312         }
00313 
00322         function _isComplete($a_obj_id)
00323         {
00324                 global $ilDB;
00325                 
00326                 $test_id = ilObjTestAccess::_getTestIDFromObjectID($a_obj_id);
00327                 $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",
00328                         $ilDB->quote($test_id . "")
00329                 );
00330                 $result = $ilDB->query($query);
00331                 $found = $result->numRows();
00332                 if ($found)
00333                 {
00334                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00335                         // check for at least: title, author and minimum of 1 mark step
00336                         if ((strlen($row["title"])) &&
00337                                 (strlen($row["author"])) &&
00338                                 ($found))
00339                         {
00340                                 // check also for minmum of 1 question
00341                                 if (ilObjTestAccess::_getQuestionCount($test_id) > 0)
00342                                 {
00343                                         return true;
00344                                 }
00345                                 else
00346                                 {
00347                                         return false;
00348                                 }
00349                         }
00350                         else
00351                         {
00352                                 return false;
00353                         }
00354                 }
00355                 else
00356                 {
00357                         return false;
00358                 }
00359                 $test = new ilObjTest($obj_id, false);
00360                 $test->loadFromDb();
00361                 if (($test->getTitle()) and ($test->author) and (count($test->mark_schema->mark_steps)) and (count($test->questions)))
00362                 {
00363                         return true;
00364                 } 
00365                         else 
00366                 {
00367                         return false;
00368                 }
00369         }
00370         
00379         function _getQuestionCount($a_test_id)
00380         {
00381                 global $ilDB;
00382 
00383                 $num = 0;
00384                 
00385                 $query = sprintf("SELECT * FROM tst_tests WHERE test_id = %s",
00386                         $ilDB->quote($a_test_id)
00387                 );
00388                 $result = $ilDB->query($query);
00389                 if ($result->numRows != 1) return 0;
00390                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00391                 
00392                 if ($row["random_test"] == 1)
00393                 {
00394                         if ($row["random_question_count"] > 0)
00395                         {
00396                                 $num = $row["random_question_count"];
00397                         }
00398                                 else
00399                         {
00400                                 $query = sprintf("SELECT * FROM tst_test_random WHERE test_fi = %s ORDER BY test_random_id",
00401                                         $ilDB->quote($a_test_id . "")
00402                                 );
00403                                 $result = $ilDB->query($query);
00404                                 if ($result->numRows())
00405                                 {
00406                                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00407                                         {
00408                                                 $num += $row["num_of_q"];
00409                                         }
00410                                 }
00411                         }
00412                 }
00413                 else
00414                 {
00415                         $query = sprintf("SELECT question_fi FROM tst_test_question WHERE test_fi = %s",
00416                                 $ilDB->quote($a_test_id . "")
00417                         );
00418                         $result = $ilDB->query($query);
00419                         $num = $result->numRows();
00420                 }
00421                 return $num;
00422         }
00423         
00432         function _lookupOnlineTestAccess($a_test_id, $a_user_id)
00433         {
00434                 global $ilDB, $lng;
00435                 
00436                 $test_result = array();
00437                 $query = sprintf("SELECT tst_tests.* FROM tst_tests WHERE tst_tests.obj_fi = %s",
00438                         $ilDB->quote($a_test_id . "")
00439                 );
00440                 $result = $ilDB->query($query);
00441                 if ($result->numRows())
00442                 {
00443                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00444                         if ($row["test_type_fi"] == 4)
00445                         {
00446                                 $query = sprintf("SELECT * FROM tst_invited_user WHERE test_fi = %s AND user_fi = %s",
00447                                         $ilDB->quote($row["test_id"] . ""),
00448                                         $ilDB->quote($a_user_id . "")
00449                                 );
00450                                 $result = $ilDB->query($query);
00451                                 if ($result->numRows())
00452                                 {
00453                                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00454                                         if (strcmp($row["clientip"],"")!=0 && strcmp($row["clientip"],$_SERVER["REMOTE_ADDR"])!=0)
00455                                         {
00456                                                 return $lng->txt("tst_user_wrong_clientip");
00457                                         }
00458                                         else
00459                                         {
00460                                                 return true;
00461                                         }
00462                                 }
00463                                 else
00464                                 {
00465                                         return $lng->txt("tst_user_not_invited");
00466                                 }
00467                         }
00468                         else
00469                         {
00470                                 return true;
00471                         }
00472                 }
00473                 else
00474                 {
00475                         return true;
00476                 }
00477         }
00478 
00495         function &_getPassedUsers($a_obj_id)
00496         {
00497                 include_once 'assessment/classes/class.ilObjTest.php';
00498 
00499                 global $ilDB;
00500                 
00501                 $passed_users = array();
00502                 $query = sprintf("SELECT tst_active.* FROM tst_active, tst_tests ".
00503                                                  "WHERE tst_tests.obj_fi = %s AND tst_active.tries > 0 ".
00504                                                  "AND tst_active.test_fi = tst_tests.test_id",
00505                         $ilDB->quote($a_obj_id . "")
00506                 );
00507                 $result = $ilDB->query($query);
00508                 if ($result->numRows())
00509                 {
00510                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00511                         {
00512                                 $user_id = $row["user_fi"];
00513                                 $test_id = $row["test_fi"];
00514                                 $pass = ilObjTest::_getResultPass($user_id, $test_id);
00515                                 $testres =& ilObjTestAccess::_getTestResult($user_id, $a_obj_id, $pass);
00516 
00517                                 array_push($passed_users, 
00518                                                    array(
00519                                                            "user_id" => $user_id,
00520                                                            "max_points" => $testres["max_points"],
00521                                                            "reached_points" => $testres["reached_points"],
00522                                                            "mark_short" => $testres["mark"]["short_name"],
00523                                                            "mark_official" => $testres["mark"]["official_name"],
00524                                                            "passed" => (bool) $testres['passed']
00525                                                            )
00526                                         );
00527                         }
00528                 }
00529                 return $passed_users;
00530         }
00531 }
00532 
00533 ?>

Generated on Fri Dec 13 2013 11:57:53 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1