ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilRepositorySearchGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35 include_once 'Services/Search/classes/class.ilSearchResult.php';
36 include_once 'Services/Search/classes/class.ilSearchSettings.php';
37 include_once './Services/User/classes/class.ilUserAccountSettings.php';
38 include_once 'Services/Search/classes/class.ilQueryParser.php';
39 
40 
42 {
43  private $search_results = array();
44 
45  protected $add_options = array();
46 
47  var $search_type = 'usr';
48 
54  {
55  global $ilCtrl,$tpl,$lng;
56 
57  $this->ctrl =& $ilCtrl;
58  $this->tpl =& $tpl;
59  $this->lng =& $lng;
60  $this->lng->loadLanguageModule('search');
61  $this->lng->loadLanguageModule('crs');
62 
63  $this->__setSearchType();
64  $this->__loadQueries();
65 
66  $this->result_obj = new ilSearchResult();
67  $this->result_obj->setMaxHits(1000000);
68  $this->settings =& new ilSearchSettings();
69 
70  }
71 
72 
77  function setString($a_str)
78  {
79  $_SESSION['search']['string'] = $this->string = $a_str;
80  }
81  function getString()
82  {
83  return $this->string;
84  }
85 
90  function &executeCommand()
91  {
92  global $rbacsystem;
93 
94  $next_class = $this->ctrl->getNextClass($this);
95  $cmd = $this->ctrl->getCmd();
96 
97  switch($next_class)
98  {
99  default:
100  if(!$cmd)
101  {
102  $cmd = "showSearch";
103  }
104  $this->$cmd();
105  break;
106  }
107  return true;
108  }
109 
110  function __clearSession()
111  {
112 
113  unset($_SESSION['rep_search']);
114  unset($_SESSION['append_results']);
115  unset($_SESSION['rep_query']);
116  unset($_SESSION['rep_search_type']);
117  }
118 
119  function cancel()
120  {
121  $this->ctrl->returnToParent($this);
122  }
123 
124  function start()
125  {
126  // delete all session info
127  $this->__clearSession();
128  $this->showSearch();
129 
130  return true;
131  }
132 
133 
134  public function addUser()
135  {
136  // call callback if that function does give a return value => show error message
137  $class =& $this->callback['class'];
138  $method = $this->callback['method'];
139 
140  // listener redirects if everything is ok.
141  $class->$method($_POST['user']);
142  $this->showSearchResults();
143  }
144 
145  function setCallback(&$class,$method,$a_add_options = array())
146  {
147  $this->callback = array('class' => $class,'method' => $method);
148  $this->add_options = $a_add_options ? $a_add_options : array();
149  }
150 
151  public function showSearch()
152  {
153  $this->initFormSearch();
154  $this->tpl->setContent($this->form->getHTML());
155  }
156 
157  public function initFormSearch()
158  {
159  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
160 
161  $this->form = new ilPropertyFormGUI();
162  $this->form->setFormAction($this->ctrl->getFormAction($this,'search'));
163  $this->form->setTitle($this->lng->txt('add_members_header'));
164  $this->form->addCommandButton('performSearch', $this->lng->txt('search'));
165  $this->form->addCommandButton('cancel', $this->lng->txt('cancel'));
166 
167 
168  $kind = new ilRadioGroupInputGUI($this->lng->txt('search_type'),'search_for');
169  $kind->setValue($this->search_type);
170  $this->form->addItem($kind);
171 
172  // Users
173  $users = new ilRadioOption($this->lng->txt('search_for_users'),'usr');
174 
175  // UDF
176  include_once 'Services/Search/classes/class.ilUserSearchOptions.php';
178  {
179  switch($info['type'])
180  {
182  case FIELD_TYPE_SELECT:
183 
184  $sel = new ilSelectInputGUI($info['lang'],"rep_query[usr][".$info['db']."]");
185  $sel->setOptions($info['values']);
186  $users->addSubItem($sel);
187  break;
188 
189  case FIELD_TYPE_UDF_TEXT:
190  case FIELD_TYPE_TEXT:
191 
192  $txt = new ilTextInputGUI($info['lang'],"rep_query[usr][".$info['db']."]");
193  $txt->setSize(30);
194  $txt->setMaxLength(120);
195  $users->addSubItem($txt);
196  break;
197  }
198  }
199  $kind->addOption($users);
200 
201  // Role
202  $roles = new ilRadioOption($this->lng->txt('search_for_role_members'),'role');
203  $role = new ilTextInputGUI($this->lng->txt('search_role_title'),'rep_query[role][title]');
204  $role->setSize(30);
205  $role->setMaxLength(120);
206  $roles->addSubItem($role);
207  $kind->addOption($roles);
208 
209  // Course
210  $groups = new ilRadioOption($this->lng->txt('search_for_crs_members'),'crs');
211  $group = new ilTextInputGUI($this->lng->txt('search_crs_title'),'rep_query[crs][title]');
212  $group->setSize(30);
213  $group->setMaxLength(120);
214  $groups->addSubItem($group);
215  $kind->addOption($groups);
216 
217  // Group
218  $groups = new ilRadioOption($this->lng->txt('search_for_grp_members'),'grp');
219  $group = new ilTextInputGUI($this->lng->txt('search_grp_title'),'rep_query[grp][title]');
220  $group->setSize(30);
221  $group->setMaxLength(120);
222  $groups->addSubItem($group);
223  $kind->addOption($groups);
224 
225 
226  }
227 
228 
229  function show()
230  {
231  $this->showSearchResults();
232  }
233 
234  function appendSearch()
235  {
236  $_SESSION['search_append'] = true;
237  $this->performSearch();
238  }
239 
244  function performSearch()
245  {
246  $found_query = false;
247  foreach((array) $_POST['rep_query'][$_POST['search_for']] as $field => $value)
248  {
249  if(trim(ilUtil::stripSlashes($value)))
250  {
251  $found_query = true;
252  break;
253  }
254  }
255  if(!$found_query)
256  {
257  ilUtil::sendFailure($this->lng->txt('msg_no_search_string'));
258  $this->start();
259  return false;
260  }
261 
262  // unset search_append if called directly
263  if($_POST['cmd']['performSearch'])
264  {
265  unset($_SESSION['search_append']);
266  }
267 
268  switch($this->search_type)
269  {
270  case 'usr':
271  $this->__performUserSearch();
272  break;
273 
274  case 'grp':
275  $this->__performGroupSearch();
276  break;
277 
278  case 'crs':
279  $this->__performCourseSearch();
280  break;
281 
282  case 'role':
283  $this->__performRoleSearch();
284  break;
285 
286  default:
287  echo 'not defined';
288  }
289  $this->result_obj->addObserver($this, 'userFilter');
290  $this->result_obj->filter(ROOT_FOLDER_ID,QP_COMBINATION_OR);
291 
292  // User access filter
293  if($this->search_type == 'usr')
294  {
295  include_once './Services/User/classes/class.ilUserFilter.php';
296  $this->search_results = array_intersect(
297  $this->result_obj->getResultIds(),
298  ilUserFilter::getInstance()->filter($this->result_obj->getResultIds())
299  );
300  }
301  else
302  {
303  $this->search_results = array();
304  foreach((array) $this->result_obj->getResults() as $res)
305  {
306  $this->search_results[] = $res['obj_id'];
307  }
308  }
309 
310  if(!count($this->search_results))
311  {
312  ilUtil::sendFailure($this->lng->txt('search_no_match'));
313  $this->showSearch();
314  return true;
315  }
316  $this->__updateResults();
317  if($this->result_obj->isLimitReached())
318  {
319  $message = sprintf($this->lng->txt('search_limit_reached'),$this->settings->getMaxHits());
320  ilUtil::sendInfo($message);
321  return true;
322  }
323  // show results
324  $this->show();
325  return true;
326  }
327 
329  {
330  include_once 'Services/Search/classes/class.ilUserSearchOptions.php';
331  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
332 
334  {
335  $name = $info['db'];
336  $query_string = $_SESSION['rep_query']['usr'][$name];
337 
338  // continue if no query string is given
339  if(!$query_string)
340  {
341  continue;
342  }
343 
344  if(!is_object($query_parser = $this->__parseQueryString($query_string)))
345  {
346  ilUtil::sendInfo($query_parser);
347  return false;
348  }
349  switch($info['type'])
350  {
352  // Do a phrase query for select fields
353  $query_parser = $this->__parseQueryString('"'.$query_string.'"');
354 
355  case FIELD_TYPE_UDF_TEXT:
357  $udf_search->setFields(array($name));
358  $result_obj = $udf_search->performSearch();
359 
360  // Store entries
361  $this->__storeEntries($result_obj);
362  break;
363 
364  case FIELD_TYPE_SELECT:
365  // Do a phrase query for select fields
366  $query_parser = $this->__parseQueryString('"'.$query_string.'"');
367 
368  case FIELD_TYPE_TEXT:
369  $user_search =& ilObjectSearchFactory::_getUserSearchInstance($query_parser);
370  $user_search->setFields(array($name));
371  $result_obj = $user_search->performSearch();
372 
373  // store entries
374  $this->__storeEntries($result_obj);
375  break;
376  }
377  }
378  }
379 
385  {
386  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
387 
388  $query_string = $_SESSION['rep_query']['grp']['title'];
389  if(!is_object($query_parser = $this->__parseQueryString($query_string)))
390  {
391  ilUtil::sendInfo($query_parser,true);
392  return false;
393  }
394 
395  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
396  $object_search = new ilLikeObjectSearch($query_parser);
397  $object_search->setFilter(array('grp'));
398  $this->__storeEntries($object_search->performSearch());
399 
400  return true;
401  }
402 
407  protected function __performCourseSearch()
408  {
409  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
410 
411  $query_string = $_SESSION['rep_query']['crs']['title'];
412  if(!is_object($query_parser = $this->__parseQueryString($query_string)))
413  {
414  ilUtil::sendInfo($query_parser,true);
415  return false;
416  }
417 
418  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
419  $object_search = new ilLikeObjectSearch($query_parser);
420  $object_search->setFilter(array('crs'));
421  $this->__storeEntries($object_search->performSearch());
422 
423  return true;
424  }
425 
431  {
432  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
433 
434  $query_string = $_SESSION['rep_query']['role']['title'];
435  if(!is_object($query_parser = $this->__parseQueryString($query_string)))
436  {
437  ilUtil::sendInfo($query_parser,true);
438  return false;
439  }
440 
441  // Perform like search
442  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
443  $object_search = new ilLikeObjectSearch($query_parser);
444  $object_search->setFilter(array('role'));
445  $this->__storeEntries($object_search->performSearch());
446 
447  return true;
448  }
449 
455  function &__parseQueryString($a_string,$a_combination_or = true)
456  {
457  $query_parser = new ilQueryParser(ilUtil::stripSlashes($a_string));
458  $query_parser->setCombination($a_combination_or ? QP_COMBINATION_OR : QP_COMBINATION_AND);
459  $query_parser->setMinWordLength(1,true);
460  $query_parser->parse();
461 
462  if(!$query_parser->validate())
463  {
464  return $query_parser->getMessage();
465  }
466  return $query_parser;
467  }
468 
469  // Private
470  function __loadQueries()
471  {
472  if(is_array($_POST['rep_query']))
473  {
474  $_SESSION['rep_query'] = $_POST['rep_query'];
475  }
476  }
477 
478 
479  function __setSearchType()
480  {
481  // Update search type. Default to user search
482  if($_POST['search_for'])
483  {
484  #echo 1;
485  $_SESSION['rep_search_type'] = $_POST['search_for'];
486  }
487  if(!$_POST['search_for'] and !$_SESSION['rep_search_type'])
488  {
489  #echo 2;
490  $_SESSION['rep_search_type'] = 'usr';
491  }
492 
493  $this->search_type = $_SESSION['rep_search_type'];
494  #echo $this->search_type;
495 
496  return true;
497  }
498 
499 
500  function __updateResults()
501  {
502  if(!$_SESSION['search_append'])
503  {
504  $_SESSION['rep_search'] = array();
505  }
506  foreach($this->search_results as $result)
507  {
508  $_SESSION['rep_search'][$this->search_type][] = $result;
509  }
510  if(!$_SESSION['rep_search'][$this->search_type])
511  {
512  $_SESSION['rep_search'][$this->search_type] = array();
513  }
514  else
515  {
516  // remove duplicate entries
517  $_SESSION['rep_search'][$this->search_type] = array_unique($_SESSION['rep_search'][$this->search_type]);
518  }
519  return true;
520  }
521 
522  function __appendToStoredResults($a_usr_ids)
523  {
524  if(!$_SESSION['search_append'])
525  {
526  return $_SESSION['rep_search']['usr'] = $a_usr_ids;
527  }
528  foreach($a_usr_ids as $usr_id)
529  {
530  $_SESSION['rep_search']['usr'][] = $usr_id;
531  }
532  return $_SESSION['rep_search']['usr'] ? array_unique($_SESSION['rep_search']['usr']) : array();
533  }
534 
535  function __storeEntries(&$new_res)
536  {
537  if($this->stored == false)
538  {
539  $this->result_obj->mergeEntries($new_res);
540  $this->stored = true;
541  return true;
542  }
543  else
544  {
545  $this->result_obj->intersectEntries($new_res);
546  return true;
547  }
548  }
549 
554  protected function addNewSearchButton()
555  {
556  include_once './Services/UIComponent/Toolbar/classes/class.ilToolbarGUI.php';
557  $toolbar = new ilToolbarGUI();
558  $toolbar->addButton(
559  $this->lng->txt('search_new'),
560  $this->ctrl->getLinkTarget($this,'showSearch')
561  );
562  $this->tpl->setVariable('ACTION_BUTTONS',$toolbar->getHTML());
563  }
564 
565  public function showSearchResults()
566  {
567  $counter = 0;
568  $f_result = array();
569 
570  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.rep_search_result.html','Services/Search');
571  $this->addNewSearchButton();
572 
573  switch($this->search_type)
574  {
575  case "usr":
576  $this->showSearchUserTable($_SESSION['rep_search']['usr'],'showSearchResults');
577  break;
578 
579  case 'grp':
580  $this->showSearchGroupTable($_SESSION['rep_search']['grp']);
581  break;
582 
583  case 'crs':
584  $this->showSearchCourseTable($_SESSION['rep_search']['crs']);
585  break;
586 
587  case 'role':
588  $this->showSearchRoleTable($_SESSION['rep_search']['role']);
589  break;
590  }
591  }
592 
598  protected function showSearchUserTable($a_usr_ids,$a_parent_cmd)
599  {
600  include_once './Services/Search/classes/class.ilRepositoryUserResultTableGUI.php';
601 
602  $table = new ilRepositoryUserResultTableGUI($this,$a_parent_cmd);
603  $table->initMultiCommands($this->add_options);
604  $table->parseUserIds($a_usr_ids);
605 
606  $this->tpl->setVariable('RES_TABLE',$table->getHTML());
607  }
608 
614  protected function showSearchRoleTable($a_obj_ids)
615  {
616  include_once './Services/Search/classes/class.ilRepositoryObjectResultTableGUI.php';
617 
618  $table = new ilRepositoryObjectResultTableGUI($this,'showSearchResults');
619  $table->parseObjectIds($a_obj_ids);
620 
621  $this->tpl->setVariable('RES_TABLE',$table->getHTML());
622  }
623 
629  protected function showSearchGroupTable($a_obj_ids)
630  {
631  include_once './Services/Search/classes/class.ilRepositoryObjectResultTableGUI.php';
632 
633  $table = new ilRepositoryObjectResultTableGUI($this,'showSearchResults');
634  $table->parseObjectIds($a_obj_ids);
635 
636  $this->tpl->setVariable('RES_TABLE',$table->getHTML());
637  }
638 
644  protected function showSearchCourseTable($a_obj_ids)
645  {
646  include_once './Services/Search/classes/class.ilRepositoryObjectResultTableGUI.php';
647 
648  $table = new ilRepositoryObjectResultTableGUI($this,'showSearchResults');
649  $table->parseObjectIds($a_obj_ids);
650 
651  $this->tpl->setVariable('RES_TABLE',$table->getHTML());
652  }
653 
658  protected function listUsers()
659  {
660  // get parameter is used e.g. in exercises to provide
661  // "add members of course" link
662  if ($_GET["list_obj"] != "" && !is_array($_POST['obj']))
663  {
664  $_POST['obj'][0] = $_GET["list_obj"];
665  }
666  if(!is_array($_POST['obj']) or !$_POST['obj'])
667  {
668  ilUtil::sendFailure($this->lng->txt('select_one'));
669  $this->showSearchResults();
670  return false;
671  }
672 
673  $_SESSION['rep_search']['objs'] = $_POST['obj'];
674 
675  // Get all members
676  $members = array();
677  foreach($_POST['obj'] as $obj_id)
678  {
679  $type = ilObject::_lookupType($obj_id);
680  switch($type)
681  {
682  case 'crs':
683  include_once './Modules/Course/classes/class.ilCourseParticipants.php';
685  $members = array_merge($members, $part->getParticipants());
686  break;
687 
688  case 'grp':
689  include_once './Modules/Group/classes/class.ilGroupParticipants.php';
691  $members = array_merge($members, $part->getParticipants());
692  break;
693 
694  case 'role':
695  global $rbacreview;
696 
697  $members = array_merge($members, $rbacreview->assignedUsers($obj_id));
698  break;
699  }
700  }
701  $members = array_unique((array) $members);
702  $this->__appendToStoredResults($members);
703 
704  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.rep_search_result.html','Services/Search');
705 
706  $this->addNewSearchButton();
707  $this->showSearchUserTable($_SESSION['rep_search']['usr'],'storedUserList');
708  return true;
709  }
710 
715  protected function storedUserList()
716  {
717  $_POST['obj'] = $_SESSION['rep_search']['objs'];
718  $this->listUsers();
719  return true;
720  }
721 
728  public function userFilter($a_ref_id,$a_data)
729  {
730  if($a_data['type'] == 'usr')
731  {
732  if($a_data['obj_id'] == ANONYMOUS_USER_ID)
733  {
734  return false;
735  }
736  }
737 
738  return true;
739  }
740 }
741 ?>