ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedSearch.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.ilAbstractSearch.php';
36 
38 {
39  var $mode = '';
40 
41  /*
42  * instance of query parser
43  */
44  var $query_parser = null;
45 
46  var $db = null;
47 
52  function ilAdvancedSearch(&$qp_obj)
53  {
54  parent::ilAbstractSearch($qp_obj);
55  }
56 
63  function setMode($a_mode)
64  {
65  $this->mode = $a_mode;
66  }
67  function getMode()
68  {
69  return $this->mode;
70  }
71 
72  function setOptions(&$options)
73  {
74  $this->options =& $options;
75  }
76 
77  function &performSearch()
78  {
79  switch($this->getMode())
80  {
81  case 'requirement':
82  return $this->__searchRequirement();
83  break;
84 
85  case 'educational':
86  return $this->__searchEducational();
87  break;
88 
89  case 'typical_age_range':
90  return $this->__searchTypicalAgeRange();
91  break;
92 
93  case 'rights':
94  return $this->__searchRights();
95  break;
96 
97  case 'classification':
98  return $this->__searchClassification();
99  break;
100 
101  case 'taxon':
102  return $this->__searchTaxon();
103  break;
104 
105  case 'keyword':
106  return $this->__searchKeyword(true);
107  break;
108 
109  case 'format':
110  return $this->__searchFormat();
111  break;
112 
113  case 'lifecycle':
114  return $this->__searchLifecycle();
115  break;
116 
117  case 'contribute':
118  return $this->__searchContribute();
119  break;
120 
121  case 'entity':
122  return $this->__searchEntity();
123  break;
124 
125  case 'general':
126  return $this->__searchGeneral();
127  break;
128 
129  case 'keyword_all':
130  return $this->__searchKeyword(false);
131  break;
132 
133  case 'title_description':
134  return $this->__searchTitleDescription();
135  break;
136 
137  case 'language':
138  return $this->__searchLanguage();
139  break;
140 
141  default:
142  echo "ilMDSearch::performSearch() no mode given";
143  return false;
144  }
145  }
146 
148  {
149  if(!$this->options['title'])
150  {
151  return false;
152  }
153  $this->setFields(array('title','description'));
154 
155  $and = ("AND type ".$this->__getInStatement($this->getFilter()));
156  $where = $this->__createTitleDescriptionWhereCondition();
157  $locate = $this->__createLocateString();
158 
159  $query = "SELECT obj_id,type ".
160  $locate.
161  "FROM object_data ".
162  $where." ".$and.' '.
163  "ORDER BY obj_id DESC";
164 
165  $res = $this->db->query($query);
166  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
167  {
168  $this->search_result->addEntry($row->obj_id,$row->type,$this->__prepareFound($row));
169  }
170 
171  return $this->search_result;
172  }
173 
174  function &__searchGeneral()
175  {
176  if(!$this->options['coverage'] and !$this->options['structure'])
177  {
178  return false;
179  }
180  if($this->options['coverage'])
181  {
182  $this->setFields(array('coverage'));
183  $and = $this->__createCoverageAndCondition();
184  $locate = $this->__createLocateString();
185  }
186  if($this->options['structure'])
187  {
188  $and .= ("AND general_structure = '".ilUtil::prepareDBString($this->options['structure'])."' ");
189  }
190 
191  $query = "SELECT rbac_id,obj_type,obj_id ".
192  $locate." ".
193  "FROM il_meta_general ".
194  "WHERE obj_type ".$this->__getInStatement($this->getFilter())." ".
195  $and;
196 
197  $res = $this->db->query($query);
198  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
199  {
200  if($this->options['coverage'])
201  {
202  $found = $this->__prepareFound($row);
203  if(!in_array(0,$found))
204  {
205  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
206  }
207  }
208  else
209  {
210  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
211  }
212  }
213 
214  return $this->search_result;
215  }
216 
217  function &__searchLanguage()
218  {
219  if(!$this->options['language'])
220  {
221  return false;
222  }
223 
224  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_language ".
225  "WHERE language = '".ilUtil::prepareDBString($this->options['language'])."' ".
226  "AND obj_type ".$this->__getInStatement($this->getFilter()).' '.
227  "AND parent_type = 'meta_general'";
228 
229  $res = $this->db->query($query);
230  #var_dump("<pre>",$query,"<pre>");
231  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
232  {
233  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
234  }
235  return $this->search_result;
236  }
237 
238  function &__searchContribute()
239  {
240  if(!$this->options['role'])
241  {
242  return false;
243  }
244 
245  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_contribute ".
246  "WHERE role = '".ilUtil::prepareDBString($this->options['role'])."' ".
247  "AND obj_type ".$this->__getInStatement($this->getFilter());
248 
249  $res = $this->db->query($query);
250  #var_dump("<pre>",$query,"<pre>");
251  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
252  {
253  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
254  }
255  return $this->search_result;
256  }
257 
258  function &__searchEntity()
259  {
260  $this->setFields(array('entity'));
261 
262  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
263  $where = $this->__createEntityWhereCondition();
264  $locate = $this->__createLocateString();
265 
266  $query = "SELECT rbac_id,obj_id,obj_type ".
267  $locate.
268  "FROM il_meta_entity ".
269  $where." ".$and.' ';
270 
271  $res = $this->db->query($query);
272  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
273  {
274  $found = $this->__prepareFound($row);
275  if(!in_array(0,$found))
276  {
277  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
278  }
279  }
280 
281  return $this->search_result;
282  }
283 
284 
285 
287  {
288  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_requirement ";
289 
290  if(!strlen($where = $this->__createRequirementWhere()))
291  {
292  return false;
293  }
294  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
295  $query = $query.$where.$and;
296  $res = $this->db->query($query);
297  #var_dump("<pre>",$query,"<pre>");
298  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
299  {
300  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
301  }
302  return $this->search_result;
303  }
304 
306  {
307  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_educational ";
308 
309  if(!strlen($where = $this->__createEducationalWhere()))
310  {
311  return false;
312  }
313  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
314  $query = $query.$where.$and;
315  $res = $this->db->query($query);
316  #var_dump("<pre>",$query,"<pre>");
317  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
318  {
319  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
320  }
321  return $this->search_result;
322  }
323 
325  {
326  if(!$this->options['typ_age_1'] or !$this->options['typ_age_2'])
327  {
328  return false;
329  }
330 
331  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_typical_age_range ".
332  "WHERE typical_age_range_min >= '".(int) $this->options['typ_age_1']."' ".
333  "AND typical_age_range_max <= '".(int) $this->options['typ_age_2']."'";
334 
335 
336  $res = $this->db->query($query);
337  #var_dump("<pre>",$query,"<pre>");
338  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
339  {
340  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
341  }
342  return $this->search_result;
343  }
344 
345  function &__searchRights()
346  {
347  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_rights ";
348 
349  if(!strlen($where = $this->__createRightsWhere()))
350  {
351  return false;
352  }
353  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
354  $query = $query.$where.$and;
355  $res = $this->db->query($query);
356  #var_dump("<pre>",$query,"<pre>");
357  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
358  {
359  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
360  }
361  return $this->search_result;
362  }
363 
365  {
366  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_classification ";
367 
368  if(!strlen($where = $this->__createClassificationWhere()))
369  {
370  return false;
371  }
372  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
373  $query = $query.$where.$and;
374  $res = $this->db->query($query);
375  #var_dump("<pre>",$query,"<pre>");
376  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
377  {
378  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
379  }
380  return $this->search_result;
381  }
382 
383  function &__searchTaxon()
384  {
385  $this->setFields(array('taxon'));
386 
387  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
388  $where = $this->__createTaxonWhereCondition();
389  $locate = $this->__createLocateString();
390 
391  $query = "SELECT rbac_id,obj_id,obj_type ".
392  $locate.
393  "FROM il_meta_taxon ".
394  $where." ".$and.' ';
395 
396  $res = $this->db->query($query);
397  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
398  {
399  $found = $this->__prepareFound($row);
400  if(!in_array(0,$found))
401  {
402  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
403  }
404  }
405 
406  return $this->search_result;
407  }
408 
409  function &__searchKeyword($a_in_classification = false)
410  {
411  $this->setFields(array('keyword'));
412 
413  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
414  if($a_in_classification)
415  {
416  $and .= " AND parent_type = 'meta_classification' ";
417  }
418  $where = $this->__createKeywordWhereCondition();
419  $locate = $this->__createLocateString();
420 
421  $query = "SELECT rbac_id,obj_id,obj_type ".
422  $locate.
423  "FROM il_meta_keyword ".
424  $where." ".$and.' ';
425 
426  $res = $this->db->query($query);
427  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
428  {
429  $found = $this->__prepareFound($row);
430  if(!in_array(0,$found) or !$a_in_classification)
431  {
432  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
433  }
434  }
435 
436  return $this->search_result;
437  }
438  function &__searchLifecycle()
439  {
440  global $ilDB;
441 
442  $this->setFields(array('meta_version'));
443 
444  if($this->options['version'])
445  {
446  $where = $this->__createLifecycleWhereCondition();
447  $locate = $this->__createLocateString();
448  }
449  else
450  {
451  $where = "WHERE 1 ";
452  }
453  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
454 
455  if($this->options['status'])
456  {
457  $and .= (" AND lifecycle_status = ".$ilDB->quote($this->options['status'])." ");
458  }
459 
460  $query = "SELECT rbac_id,obj_id,obj_type ".
461  $locate.
462  "FROM il_meta_lifecycle ".
463  $where." ".$and.' ';
464 
465  $res = $this->db->query($query);
466  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
467  {
468  $found = $this->__prepareFound($row);
469  if(!in_array(0,$found))
470  {
471  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
472  }
473  }
474 
475  return $this->search_result;
476  }
477 
478  function &__searchFormat()
479  {
480  if(!$this->options['format'])
481  {
482  return false;
483  }
484 
485  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_format ".
486  "WHERE format LIKE('".ilUtil::prepareDBString($this->options['format'])."') ".
487  "AND obj_type ".$this->__getInStatement($this->getFilter());
488 
489  $res = $this->db->query($query);
490  #var_dump("<pre>",$query,"<pre>");
491  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
492  {
493  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
494  }
495  return $this->search_result;
496  }
497 
498 
500  {
501  $counter = 0;
502  $where = 'WHERE ';
503 
504 
505  if($this->options['costs'])
506  {
507  $and = $counter++ ? 'AND ' : ' ';
508  $where .= ($and."costs = '".ilUtil::prepareDBString($this->options['costs'])."' ");
509  }
510  if($this->options['copyright'])
511  {
512  $and = $counter++ ? 'AND ' : ' ';
513  $where .= ($and."copyright_and_other_restrictions = '".ilUtil::prepareDBString($this->options['copyright'])."' ");
514  }
515  return $counter ? $where : '';
516  }
518  {
519  $counter = 0;
520  $where = 'WHERE ';
521 
522 
523  if($this->options['purpose'])
524  {
525  $and = $counter++ ? 'AND ' : ' ';
526  $where .= ($and."purpose = '".ilUtil::prepareDBString($this->options['purpose'])."' ");
527  }
528  return $counter ? $where : '';
529  }
531  {
532  $counter = 0;
533  $where = 'WHERE ';
534 
535 
536  if($this->options['int_type'])
537  {
538  $and = $counter++ ? 'AND ' : ' ';
539  $where .= ($and."interactivity_type = '".ilUtil::prepareDBString($this->options['int_type'])."' ");
540  }
541  if($this->options['lea_type'])
542  {
543  $and = $counter++ ? 'AND ' : ' ';
544  $where .= ($and."learning_resource_type = '".ilUtil::prepareDBString($this->options['lea_type'])."' ");
545  }
546  if($this->options['int_role'])
547  {
548  $and = $counter++ ? 'AND ' : ' ';
549  $where .= ($and."intended_end_user_role = '".ilUtil::prepareDBString($this->options['int_role'])."' ");
550  }
551  if($this->options['con'])
552  {
553  $and = $counter++ ? 'AND ' : ' ';
554  $where .= ($and."context = '".ilUtil::prepareDBString($this->options['con'])."' ");
555  }
556  if($this->options['int_level_1'] or $this->options['int_level_2'])
557  {
558  $and = $counter++ ? 'AND ' : ' ';
559 
560  $fields = $this->__getDifference($this->options['int_level_1'],$this->options['int_level_2'],
561  array('VeryLow','Low','Medium','High','VeryHigh'));
562 
563  $where .= ($and."interactivity_level ".$this->__getInStatement($fields));
564  }
565  if($this->options['sem_1'] or $this->options['sem_2'])
566  {
567  $and = $counter++ ? 'AND ' : ' ';
568 
569  $fields = $this->__getDifference($this->options['sem_1'],$this->options['sem_2'],
570  array('VeryLow','Low','Medium','High','VeryHigh'));
571 
572  $where .= ($and."semantic_density ".$this->__getInStatement($fields));
573  }
574  if($this->options['dif_1'] or $this->options['dif_2'])
575  {
576  $and = $counter++ ? 'AND ' : ' ';
577 
578  $fields = $this->__getDifference($this->options['dif_1'],$this->options['dif_2'],
579  array('VeryEasy','Easy','Medium','Difficult','VeryDifficult'));
580 
581  $where .= ($and."difficulty ".$this->__getInStatement($fields));
582  }
583 
584  return $counter ? $where : '';
585  }
587  {
588  $counter = 0;
589  $where = 'WHERE ';
590 
591 
592  if($this->options['os'])
593  {
594  $and = $counter++ ? 'AND ' : ' ';
595  $where .= ($and."operating_system_name = '".ilUtil::prepareDBString($this->options['os'])."' ");
596  }
597  if($this->options['browser'])
598  {
599  $and = $counter++ ? 'AND ' : ' ';
600  $where .= ($and."browser_name = '".ilUtil::prepareDBString($this->options['browser'])."' ");
601  }
602  return $counter ? $where : '';
603  }
604 
605  function __getDifference($a_val1,$a_val2,$options)
606  {
607  $a_val2 = $a_val2 ? $a_val2 : count($options);
608  // Call again if a > b
609  if($a_val1 > $a_val2)
610  {
611  return $this->__getDifference($a_val2,$a_val1,$options);
612  }
613 
614  $counter = 0;
615  foreach($options as $option)
616  {
617  if($a_val1 > ++$counter)
618  {
619  continue;
620  }
621  if($a_val2 < $counter)
622  {
623  break;
624  }
625  $fields[] = $option;
626  }
627  return $fields ? $fields : array();
628  }
629 
630  function __getInStatement($a_fields)
631  {
632  if(!$a_fields)
633  {
634  return '';
635  }
636  $in = " IN ('";
637  $in .= implode("','",$a_fields);
638  $in .= "') ";
639 
640  return $in;
641  }
642 
643 }
644 ?>