ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilUserAutoComplete.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 {
12  const SEARCH_TYPE_LIKE = 1;
13 
17  const SEARCH_TYPE_EQUALS = 2;
18 
23 
28 
32  private $searchable_check = false;
33 
37  private $user_access_check = true;
38 
42  private $possible_fields = array();
43 
47  private $search_type;
48 
52  private $privacy_mode;
53 
57  private $user;
58 
62  public function __construct()
63  {
64 
65 
66  $this->setSearchType(self::SEARCH_TYPE_LIKE);
67  $this->setPrivacyMode(self::PRIVACY_MODE_IGNORE_USER_SETTING);
68  }
69 
73  public function setSearchType($search_type)
74  {
75  $this->search_type = $search_type;
76  }
77 
81  public function getSearchType()
82  {
83  return $this->search_type;
84  }
85 
89  public function setPrivacyMode($privacy_mode)
90  {
91  $this->privacy_mode = $privacy_mode;
92  }
93 
97  public function getPrivacyMode()
98  {
99  return $this->privacy_mode;
100  }
101 
105  public function setUser($user)
106  {
107  $this->user = $user;
108  }
109 
113  public function getUser()
114  {
115  return $this->user;
116  }
117 
122  public function enableFieldSearchableCheck($a_status)
123  {
124  $this->searchable_check = $a_status;
125  }
126 
132  {
134  }
135 
141  public function enableUserAccessCheck($a_status)
142  {
143  $this->user_access_check = $a_status;
144  }
145 
150  public function isUserAccessCheckEnabled()
151  {
153  }
154 
159  public function setSearchFields($a_fields)
160  {
161  $this->possible_fields = $a_fields;
162  }
163 
168  public function getSearchFields()
169  {
170  return $this->possible_fields;
171  }
172 
177  protected function getFields()
178  {
179  if(!$this->isFieldSearchableCheckEnabled())
180  {
181  return $this->getSearchFields();
182  }
183  $available_fields = array();
184  foreach($this->getSearchFields() as $field)
185  {
186  include_once 'Services/Search/classes/class.ilUserSearchOptions.php';
188  {
189  $available_fields[] = $field;
190  }
191  }
192  return $available_fields;
193  }
194 
200  public function getList($a_str)
201  {
206  global $ilDB, $ilLog;
207 
208  include_once './Services/JSON/classes/class.ilJsonUtil.php';
209  $result = new stdClass();
210  $result->response = new stdClass();
211  $result->response->results = array();
212  if (strlen($a_str) < 3)
213  {
214  return ilJsonUtil::encode($result);
215  }
216 
217  $select_part = $this->getSelectPart();
218  $where_part = $this->getWherePart($a_str);
219  $order_by_part = $this->getOrderByPart();
220  $query = implode(" ", array(
221  'SELECT ' . $select_part,
222  'FROM ' . $this->getFromPart(),
223  $where_part ? 'WHERE ' . $where_part : '',
224  $order_by_part ? 'ORDER BY ' . $order_by_part : ''
225  ));
226 
227  $ilLog->write(__METHOD__ . ': Query: ' . $query);
228 
229  $res = $ilDB->query($query);
230 
231  include_once './Services/Search/classes/class.ilSearchSettings.php';
232  $max = ilSearchSettings::getInstance()->getAutoCompleteLength();
233  $cnt = 0;
234  while(($rec = $ilDB->fetchAssoc($res)) && $cnt < $max)
235  {
236  $result->response->results[$cnt] = new stdClass();
237  $result->response->results[$cnt]->login = (string) $rec["login"];
238  $result->response->results[$cnt]->firstname = (string) $rec["firstname"];
239  $result->response->results[$cnt]->lastname = (string) $rec["lastname"];
240 
241  if(self::PRIVACY_MODE_RESPECT_USER_SETTING != $this->getPrivacyMode() || 'y' == $rec['email_value'])
242  {
243  $result->response->results[$cnt]->email = (string) $rec["email"];
244  }
245 
246  $cnt++;
247  }
248 
249  return ilJsonUtil::encode($result);
250  }
251 
255  protected function getSelectPart()
256  {
257  $fields = array(
258  'login',
259  'firstname',
260  'lastname',
261  'email'
262  );
263 
264  if(self::PRIVACY_MODE_RESPECT_USER_SETTING == $this->getPrivacyMode())
265  {
266  $fields[] = 'profpref.value profile_value';
267  $fields[] = 'pubemail.value email_value';
268  }
269 
270  return implode(', ', $fields);
271  }
272 
276  protected function getFromPart()
277  {
281  global $ilDB;
282 
283  $joins = array();
284 
285  if(self::PRIVACY_MODE_RESPECT_USER_SETTING == $this->getPrivacyMode())
286  {
287  $joins[] = 'LEFT JOIN usr_pref profpref
288  ON profpref.usr_id = usr_data.usr_id
289  AND profpref.keyword = ' . $ilDB->quote('public_profile', 'text');
290 
291  $joins[] = 'LEFT JOIN usr_pref pubemail
292  ON pubemail.usr_id = usr_data.usr_id
293  AND pubemail.keyword = ' . $ilDB->quote('public_email', 'text');
294  }
295 
296  if($joins)
297  {
298  return 'usr_data ' . implode(' ', $joins);
299  }
300  else
301  {
302  return 'usr_data ';
303  }
304  }
305 
310  protected function getWherePart($search_query)
311  {
316  global $ilDB, $ilSetting;
317 
318  $outer_conditions = array();
319 
320  // In 'anonymous' context with respected user privacy, only users with globally published profiles should be found.
321  if(self::PRIVACY_MODE_RESPECT_USER_SETTING == $this->getPrivacyMode() &&
322  $this->getUser() instanceof ilObjUser &&
323  $this->getUser()->isAnonymous()
324  )
325  {
326  if(!$ilSetting->get('enable_global_profiles', 0))
327  {
328  // If 'Enable User Content Publishing' is not set in the administration, no user should be found for 'anonymous' context.
329  return '1 = 2';
330  }
331  else
332  {
333  // Otherwise respect the profile activation setting of every user (as a global (outer) condition in the where clause).
334  $outer_conditions[] = 'profpref.value = ' . $ilDB->quote('g', 'text');
335  }
336  }
337 
338  $outer_conditions[] = 'usr_data.usr_id != ' . $ilDB->quote(ANONYMOUS_USER_ID, 'integer');
339 
340  $field_conditions = array();
341  foreach($this->getFields() as $field)
342  {
343  $field_condition = $this->getQueryConditionByFieldAndValue($field, $search_query);
344 
345  if('email' == $field && self::PRIVACY_MODE_RESPECT_USER_SETTING == $this->getPrivacyMode())
346  {
347  // If privacy should be respected, the profile setting of every user concerning the email address has to be
348  // respected (in every user context, no matter if the user is 'logged in' or 'anonymous').
349  $email_query = array();
350  $email_query[] = $field_condition;
351  $email_query[] = 'pubemail.value = ' . $ilDB->quote('y', 'text');
352  $field_conditions[] = '(' . implode(' AND ', $email_query) . ')';
353  }
354  else
355  {
356  $field_conditions[] = $field_condition;
357  }
358  }
359 
360  // If the current user context ist 'logged in' and privacy should be respected, all fields >>>except the login<<<
361  // should only be searchable if the users' profile is published (y oder g)
362  // In 'anonymous' context we do not need this additional conditions,
363  // because we checked the privacy setting in the condition above: profile = 'g'
364  if(self::PRIVACY_MODE_RESPECT_USER_SETTING == $this->getPrivacyMode() &&
365  $this->getUser() instanceof ilObjUser && !$this->getUser()->isAnonymous()
366  )
367  {
368  $fields = implode(' OR ', $field_conditions);
369 
370  $field_conditions[] = '(' . implode(' AND ', array(
371  $fields,
372  $ilDB->in('profpref.value', array('y', 'g'), false, 'text')
373  )) . ')';
374  }
375 
376  // The login field must be searchable regardless (for 'logged in' users) of any privacy settings.
377  // We handled the general condition for 'anonymous' context above: profile = 'g'
378  $field_conditions[] = $this->getQueryConditionByFieldAndValue('login', $search_query);
379 
380  include_once 'Services/User/classes/class.ilUserAccountSettings.php';
381  if(ilUserAccountSettings::getInstance()->isUserAccessRestricted())
382  {
383  include_once './Services/User/classes/class.ilUserFilter.php';
384  $outer_conditions[] = $ilDB->in('time_limit_owner', ilUserFilter::getInstance()->getFolderIds(), false, 'integer');
385  }
386 
387  if($field_conditions)
388  {
389  $outer_conditions[] = '(' . implode(' OR ', $field_conditions) . ')';
390  }
391 
392  return implode(' AND ', $outer_conditions);
393  }
394 
398  protected function getOrderByPart()
399  {
400  return 'login ASC';
401  }
402 
408  protected function getQueryConditionByFieldAndValue($field, $a_str)
409  {
413  global $ilDB;
414 
415  if(self::SEARCH_TYPE_LIKE == $this->getSearchType())
416  {
417  return $ilDB->like($field, 'text', $a_str . '%');
418  }
419  else
420  {
421  return $ilDB->like($field, 'text', $a_str);
422  }
423  }
424 }