ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestParticipantData.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 
12 {
16  protected $db;
17 
21  protected $lng;
22 
26  private $activeIds;
27 
31  private $userIds;
32 
36  private $anonymousIds;
37 
41  private $byActiveId;
42 
46  private $byUserId;
47 
51  private $byAnonymousId;
52 
53  public function __construct(ilDB $db, ilLanguage $lng)
54  {
55  $this->db = $db;
56  $this->lng = $lng;
57 
58  $this->activeIds = array();
59  $this->userIds = array();
60  $this->anonymousIds = array();
61 
62  $this->byActiveId = array();
63  $this->byUserId = array();
64  $this->byAnonymousId = array();
65  }
66 
67  public function load($testId)
68  {
69  $this->byActiveId = array();
70  $this->byUserId = array();
71 
72  $query = "
73  SELECT ta.active_id,
74  ta.user_fi user_id,
75  ta.anonymous_id,
76  ud.firstname,
77  ud.lastname
78  FROM tst_active ta
79  LEFT JOIN usr_data ud
80  ON ud.usr_id = ta.user_fi
81  WHERE test_fi = %s
82  AND {$this->getConditionalExpression()}
83  ";
84 
85  $res = $this->db->queryF($query, array('integer'), array($testId));
86 
87  while( $row = $this->db->fetchAssoc($res) )
88  {
89  $this->byActiveId[ $row['active_id'] ] = $row;
90 
91  if( $row['user_id'] == ANONYMOUS_USER_ID )
92  {
93  $this->byAnonymousId[ $row['anonymous_id'] ] = $row;
94  }
95  else
96  {
97  $this->byUserId[ $row['user_id'] ] = $row;
98  }
99  }
100 
101  $this->setActiveIds(array_keys($this->byActiveId));
102  $this->setUserIds(array_keys($this->byUserId));
103  $this->setAnonymousIds(array_keys($this->byAnonymousId));
104  }
105 
106  public function getConditionalExpression()
107  {
108  $conditions = array();
109 
110  if( count($this->getActiveIds()) )
111  {
112  $conditions[] = $this->db->in('active_id', $this->getActiveIds(), false, 'integer');
113  }
114 
115  if( count($this->getUserIds()) )
116  {
117  $conditions[] = $this->db->in('user_fi', $this->getUserIds(), false, 'integer');
118  }
119 
120  if( count($this->getAnonymousIds()) )
121  {
122  $conditions[] = $this->db->in('anonymous_id', $this->getAnonymousIds(), false, 'integer');
123  }
124 
125  if( count($conditions) )
126  {
127  return '('.implode(' OR ', $conditions).')';
128  }
129 
130  return '1 = 1';
131  }
132 
133  public function setActiveIds($activeIds)
134  {
135  $this->activeIds = $activeIds;
136  }
137 
138  public function getActiveIds()
139  {
140  return $this->activeIds;
141  }
142 
143  public function setUserIds($userIds)
144  {
145  $this->userIds = $userIds;
146  }
147 
148  public function getUserIds()
149  {
150  return $this->userIds;
151  }
152 
154  {
155  $this->anonymousIds = $anonymousIds;
156  }
157 
158  public function getAnonymousIds()
159  {
160  return $this->anonymousIds;
161  }
162 
163  public function getUserIdByActiveId($activeId)
164  {
165  return $this->byActiveId[$activeId]['user_id'];
166  }
167 
168  public function getActiveIdByUserId($userId)
169  {
170  return $this->byUserId[$userId]['active_id'];
171  }
172 
173  public function getConcatedFullnameByActiveId($activeId)
174  {
175  return "{$this->byActiveId[$activeId]['firstname']} {$this->byActiveId[$activeId]['lastname']}";
176  }
177 
178  public function getFormatedFullnameByActiveId($activeId)
179  {
180  return $this->buildFormatedFullname($this->byActiveId[$activeId]);
181  }
182 
183  public function getOptionArray()
184  {
185  $options = array();
186 
187  foreach($this->byActiveId as $activeId => $usrData)
188  {
189  $options[$activeId] = $this->buildFormatedFullname($usrData);
190  }
191 
192  asort($options);
193 
194  return $options;
195  }
196 
197  private function buildFormatedFullname($usrData)
198  {
199  return sprintf(
200  $this->lng->txt('tst_participant_fullname_pattern'), $usrData['firstname'], $usrData['lastname']
201  );
202  }
203 
204  public function getAnonymousActiveIds()
205  {
206  $anonymousActiveIds = array();
207 
208  foreach($this->byActiveId as $activeId => $active)
209  {
210  if($active['user_id'] == ANONYMOUS_USER_ID)
211  {
212  $anonymousActiveIds[] = $activeId;
213  }
214  }
215 
216  return $anonymousActiveIds;
217  }
218 }