1<?php 2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4/** 5 * Factory for test session 6 * @author Björn Heyser <bheyser@databay.de> 7 * @version $Id$ 8 * @package Modules/Test 9 */ 10class ilTestSessionFactory 11{ 12 /** 13 * singleton instances of test sessions 14 * 15 * @var array[ilTestSession|ilTestSessionDynamicQuestionSet] 16 */ 17 private $testSession = array(); 18 19 /** 20 * object instance of current test 21 * @var ilObjTest 22 */ 23 private $testOBJ = null; 24 25 /** 26 * constructor 27 * @param ilObjTest $testOBJ 28 */ 29 public function __construct(ilObjTest $testOBJ) 30 { 31 $this->testOBJ = $testOBJ; 32 } 33 34 /** 35 * temporarily bugfix for resetting the state of this singleton 36 * smeyer 37 * --> BH: not required anymore 38 */ 39 public function reset() 40 { 41 $this->testSession = array(); 42 } 43 44 45 46 47 /** 48 * Creates and returns an instance of a test sequence 49 * that corresponds to the current test mode 50 * 51 * @param integer $activeId 52 * @return ilTestSession|ilTestSessionDynamicQuestionSet 53 */ 54 public function getSession($activeId = null) 55 { 56 if ($activeId === null || $this->testSession[$activeId] === null) { 57 $testSession = $this->getNewTestSessionObject(); 58 59 $testSession->setRefId($this->testOBJ->getRefId()); 60 $testSession->setTestId($this->testOBJ->getTestId()); 61 62 if ($activeId) { 63 $testSession->loadFromDb($activeId); 64 $this->testSession[$activeId] = $testSession; 65 } else { 66 global $DIC; 67 $ilUser = $DIC['ilUser']; 68 69 $testSession->loadTestSession( 70 $this->testOBJ->getTestId(), 71 $ilUser->getId(), 72 $testSession->getAccessCodeFromSession() 73 ); 74 75 return $testSession; 76 } 77 } 78 79 return $this->testSession[$activeId]; 80 } 81 82 /** 83 * @todo: Björn, we also need to handle the anonymous user here 84 * @param integer $userId 85 * @return ilTestSession|ilTestSessionDynamicQuestionSet 86 */ 87 public function getSessionByUserId($userId) 88 { 89 if (!isset($this->testSession[$this->buildCacheKey($userId)])) { 90 $testSession = $this->getNewTestSessionObject(); 91 92 $testSession->setRefId($this->testOBJ->getRefId()); 93 $testSession->setTestId($this->testOBJ->getTestId()); 94 95 $testSession->loadTestSession($this->testOBJ->getTestId(), $userId); 96 97 $this->testSession[$this->buildCacheKey($userId)] = $testSession; 98 } 99 100 return $this->testSession[$this->buildCacheKey($userId)]; 101 } 102 103 /** 104 * @return ilTestSession|ilTestSessionDynamicQuestionSet 105 */ 106 private function getNewTestSessionObject() 107 { 108 switch ($this->testOBJ->getQuestionSetType()) { 109 case ilObjTest::QUESTION_SET_TYPE_FIXED: 110 case ilObjTest::QUESTION_SET_TYPE_RANDOM: 111 112 require_once 'Modules/Test/classes/class.ilTestSession.php'; 113 $testSession = new ilTestSession(); 114 break; 115 116 case ilObjTest::QUESTION_SET_TYPE_DYNAMIC: 117 118 require_once 'Modules/Test/classes/class.ilTestSessionDynamicQuestionSet.php'; 119 $testSession = new ilTestSessionDynamicQuestionSet(); 120 break; 121 } 122 123 return $testSession; 124 } 125 126 /** 127 * @param $userId 128 * @return string 129 */ 130 private function buildCacheKey($userId) 131 { 132 return "{$this->testOBJ->getTestId()}::{$userId}"; 133 } 134} 135