ILIAS  release_4-3 Revision
 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();
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  $this->setFields(array('title','description'));
150 
151  $and = ("AND type ".$this->__getInStatement($this->getFilter()));
152  $where = $this->__createTitleDescriptionWhereCondition();
153  $locate = $this->__createLocateString();
154 
155  $query = "SELECT obj_id,type ".
156  $locate.
157  "FROM object_data ".
158  $where." ".$and.' '.
159  "ORDER BY obj_id DESC";
160 
161  $res = $this->db->query($query);
162  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
163  {
164  $this->search_result->addEntry($row->obj_id,$row->type,$this->__prepareFound($row));
165  }
166 
167  return $this->search_result;
168  }
169 
170  function &__searchGeneral()
171  {
172  global $ilDB;
173 
174  if(!$this->options['lom_coverage'] and !$this->options['lom_structure'])
175  {
176  return false;
177  }
178  if($this->options['lom_coverage'])
179  {
180  $this->setFields(array('coverage'));
181  $and = $this->__createCoverageAndCondition();
182  $locate = $this->__createLocateString();
183  }
184  if($this->options['lom_structure'])
185  {
186  $and .= ("AND general_structure = ".$ilDB->quote($this->options['lom_structure'] ,'text')." ");
187  }
188 
189  $query = "SELECT rbac_id,obj_type,obj_id ".
190  $locate." ".
191  "FROM il_meta_general ".
192  "WHERE obj_type ".$this->__getInStatement($this->getFilter())." ".
193  $and;
194 
195  $res = $this->db->query($query);
196  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
197  {
198  if($this->options['lom_coverage'])
199  {
200  $found = $this->__prepareFound($row);
201  if(!in_array(0,$found))
202  {
203  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
204  }
205  }
206  else
207  {
208  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
209  }
210  }
211 
212  return $this->search_result;
213  }
214 
215  function &__searchLanguage()
216  {
217  global $ilDB;
218 
219  if(!$this->options['lom_language'])
220  {
221  return false;
222  }
223 
224  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_language ".
225  "WHERE language = ".$ilDB->quote($this->options['lom_language'] ,'text')." ".
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  global $ilDB;
241 
242  if(!$this->options['lom_role'])
243  {
244  return false;
245  }
246 
247  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_contribute ".
248  "WHERE role = ".$ilDB->quote($this->options['lom_role'] ,'text')." ".
249  "AND obj_type ".$this->__getInStatement($this->getFilter());
250 
251  $res = $this->db->query($query);
252  #var_dump("<pre>",$query,"<pre>");
253  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
254  {
255  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
256  }
257  return $this->search_result;
258  }
259 
260  function &__searchEntity()
261  {
262  $this->setFields(array('entity'));
263 
264  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
265  $where = $this->__createEntityWhereCondition();
266  $locate = $this->__createLocateString();
267 
268  $query = "SELECT rbac_id,obj_id,obj_type ".
269  $locate.
270  "FROM il_meta_entity ".
271  $where." ".$and.' ';
272 
273  $res = $this->db->query($query);
274  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
275  {
276  $found = $this->__prepareFound($row);
277  if(!in_array(0,$found))
278  {
279  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
280  }
281  }
282 
283  return $this->search_result;
284  }
285 
286 
287 
289  {
290  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_requirement ";
291 
292  if(!strlen($where = $this->__createRequirementWhere()))
293  {
294  return false;
295  }
296  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
297  $query = $query.$where.$and;
298  $res = $this->db->query($query);
299  #var_dump("<pre>",$query,"<pre>");
300  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
301  {
302  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
303  }
304  return $this->search_result;
305  }
306 
308  {
309  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_educational ";
310 
311  if(!strlen($where = $this->__createEducationalWhere()))
312  {
313  return false;
314  }
315  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
316  $query = $query.$where.$and;
317  $res = $this->db->query($query);
318  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
319  {
320  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
321  }
322  return $this->search_result;
323  }
324 
326  {
327  if(!$this->options['typ_age_1'] or !$this->options['typ_age_2'])
328  {
329  return false;
330  }
331 
332  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_typical_age_range ".
333  "WHERE typical_age_range_min >= '".(int) $this->options['typ_age_1']."' ".
334  "AND typical_age_range_max <= '".(int) $this->options['typ_age_2']."'";
335 
336 
337  $res = $this->db->query($query);
338  #var_dump("<pre>",$query,"<pre>");
339  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
340  {
341  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
342  }
343  return $this->search_result;
344  }
345 
346  function &__searchRights()
347  {
348  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_rights ";
349 
350  if(!strlen($where = $this->__createRightsWhere()))
351  {
352  return false;
353  }
354  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
355  $query = $query.$where.$and;
356  $res = $this->db->query($query);
357  #var_dump("<pre>",$query,"<pre>");
358  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
359  {
360  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
361  }
362  return $this->search_result;
363  }
364 
366  {
367  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_classification ";
368 
369  if(!strlen($where = $this->__createClassificationWhere()))
370  {
371  return false;
372  }
373  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
374  $query = $query.$where.$and;
375  $res = $this->db->query($query);
376  #var_dump("<pre>",$query,"<pre>");
377  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
378  {
379  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
380  }
381  return $this->search_result;
382  }
383 
384  function &__searchTaxon()
385  {
386  $this->setFields(array('taxon'));
387 
388  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
389  $where = $this->__createTaxonWhereCondition();
390  $locate = $this->__createLocateString();
391 
392  $query = "SELECT rbac_id,obj_id,obj_type ".
393  $locate.
394  "FROM il_meta_taxon ".
395  $where." ".$and.' ';
396 
397  $res = $this->db->query($query);
398  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
399  {
400  $found = $this->__prepareFound($row);
401  if(!in_array(0,$found))
402  {
403  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
404  }
405  }
406 
407  return $this->search_result;
408  }
409 
410  function &__searchKeyword($a_in_classification = false)
411  {
412  $this->setFields(array('keyword'));
413 
414  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
415  if($a_in_classification)
416  {
417  $and .= " AND parent_type = 'meta_classification' ";
418  }
419  $where = $this->__createKeywordWhereCondition();
420  $locate = $this->__createLocateString();
421 
422  $query = "SELECT rbac_id,obj_id,obj_type ".
423  $locate.
424  "FROM il_meta_keyword ".
425  $where." ".$and.' ';
426 
427  $res = $this->db->query($query);
428  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
429  {
430  $found = $this->__prepareFound($row);
431  if(!in_array(0,$found) or !$a_in_classification)
432  {
433  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
434  }
435  }
436 
437  return $this->search_result;
438  }
439  function &__searchLifecycle()
440  {
441  global $ilDB;
442 
443  $this->setFields(array('meta_version'));
444 
445  if($this->options['lom_version'])
446  {
447  $where = $this->__createLifecycleWhereCondition();
448  $locate = $this->__createLocateString();
449  }
450  else
451  {
452  $where = "WHERE 1 = 1 ";
453  }
454  $and = ("AND obj_type ".$this->__getInStatement($this->getFilter()));
455 
456  if($this->options['lom_status'])
457  {
458  $and .= (" AND lifecycle_status = ".$ilDB->quote($this->options['lom_status'] ,'text') ."");
459  }
460 
461  $query = "SELECT rbac_id,obj_id,obj_type ".
462  $locate.
463  "FROM il_meta_lifecycle ".
464  $where." ".$and.' ';
465 
466  $res = $this->db->query($query);
467  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
468  {
469  $found = $this->__prepareFound($row);
470  if(!in_array(0,$found))
471  {
472  $this->search_result->addEntry($row->rbac_id,$row->obj_type,$found,$row->obj_id);
473  }
474  }
475 
476  return $this->search_result;
477  }
478 
479  function &__searchFormat()
480  {
481  global $ilDB;
482 
483  if(!$this->options['lom_format'])
484  {
485  return false;
486  }
487 
488  $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_format ".
489  "WHERE format LIKE(".$ilDB->quote($this->options['lom_format']).") ".
490  "AND obj_type ".$this->__getInStatement($this->getFilter());
491 
492  $res = $this->db->query($query);
493  #var_dump("<pre>",$query,"<pre>");
494  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
495  {
496  $this->search_result->addEntry($row->rbac_id,$row->obj_type,array(),$row->obj_id);
497  }
498  return $this->search_result;
499  }
500 
501 
503  {
504  global $ilDB;
505 
506  $counter = 0;
507  $where = 'WHERE ';
508 
509 
510  if($this->options['lom_costs'])
511  {
512  $and = $counter++ ? 'AND ' : ' ';
513  $where .= ($and."costs = ".$ilDB->quote($this->options['lom_costs'] ,'text')." ");
514  }
515  if($this->options['lom_copyright'])
516  {
517  $and = $counter++ ? 'AND ' : ' ';
518  $where .= ($and."cpr_and_or = ".$ilDB->quote($this->options['lom_copyright'] ,'text')." ");
519  }
520  return $counter ? $where : '';
521  }
523  {
524  global $ilDB;
525 
526  $counter = 0;
527  $where = 'WHERE ';
528 
529 
530  if($this->options['lom_purpose'])
531  {
532  $and = $counter++ ? 'AND ' : ' ';
533  $where .= ($and."purpose = ".$ilDB->quote($this->options['lom_purpose'])." ");
534  }
535  return $counter ? $where : '';
536  }
538  {
539  global $ilDB;
540 
541  $counter = 0;
542  $where = 'WHERE ';
543 
544 
545  if($this->options['lom_interactivity'])
546  {
547  $and = $counter++ ? 'AND ' : ' ';
548  $where .= ($and."interactivity_type = ".$ilDB->quote($this->options['lom_interactivity'] ,'text')." ");
549  }
550  if($this->options['lom_resource'])
551  {
552  $and = $counter++ ? 'AND ' : ' ';
553  $where .= ($and."learning_resource_type = ".$ilDB->quote($this->options['lom_resource'] ,'text')." ");
554  }
555  if($this->options['lom_user_role'])
556  {
557  $and = $counter++ ? 'AND ' : ' ';
558  $where .= ($and."intended_end_user_role = ".$ilDB->quote($this->options['lom_user_role'] ,'text')." ");
559  }
560  if($this->options['lom_context'])
561  {
562  $and = $counter++ ? 'AND ' : ' ';
563  $where .= ($and."context = ".$ilDB->quote($this->options['lom_context'] ,'text')." ");
564  }
565  if($this->options['lom_level_start'] or $this->options['lom_level_end'])
566  {
567  $and = $counter++ ? 'AND ' : ' ';
568 
569  $fields = $this->__getDifference($this->options['lom_level_start'],$this->options['lom_level_end'],
570  array('VeryLow','Low','Medium','High','VeryHigh'));
571 
572  $where .= ($and."interactivity_level ".$this->__getInStatement($fields));
573  }
574  if($this->options['lom_density_start'] or $this->options['lom_density_end'])
575  {
576  $and = $counter++ ? 'AND ' : ' ';
577 
578  $fields = $this->__getDifference($this->options['lom_density_start'],$this->options['lom_density_end'],
579  array('VeryLow','Low','Medium','High','VeryHigh'));
580 
581  $where .= ($and."semantic_density ".$this->__getInStatement($fields));
582  }
583  if($this->options['lom_difficulty_start'] or $this->options['lom_difficulty_end'])
584  {
585  $and = $counter++ ? 'AND ' : ' ';
586 
587  $fields = $this->__getDifference($this->options['lom_difficulty_start'],$this->options['lom_difficulty_end'],
588  array('VeryEasy','Easy','Medium','Difficult','VeryDifficult'));
589 
590  $where .= ($and."difficulty ".$this->__getInStatement($fields));
591  }
592 
593  return $counter ? $where : '';
594  }
596  {
597  global $ilDB;
598 
599  $counter = 0;
600  $where = 'WHERE ';
601 
602 
603  if($this->options['lom_operating_system'])
604  {
605  $and = $counter++ ? 'AND ' : ' ';
606  $where .= ($and."operating_system_name = ".$ilDB->quote($this->options['lom_operating_system'])." ");
607  }
608  if($this->options['lom_browser'])
609  {
610  $and = $counter++ ? 'AND ' : ' ';
611  $where .= ($and."browser_name = ".$ilDB->quote($this->options['lom_browser'])." ");
612  }
613  return $counter ? $where : '';
614  }
615 
616  function __getDifference($a_val1,$a_val2,$options)
617  {
618  $a_val2 = $a_val2 ? $a_val2 : count($options);
619  // Call again if a > b
620  if($a_val1 > $a_val2)
621  {
622  return $this->__getDifference($a_val2,$a_val1,$options);
623  }
624 
625  $counter = 0;
626  foreach($options as $option)
627  {
628  if($a_val1 > ++$counter)
629  {
630  continue;
631  }
632  if($a_val2 < $counter)
633  {
634  break;
635  }
636  $fields[] = $option;
637  }
638  return $fields ? $fields : array();
639  }
640 
641  function __getInStatement($a_fields)
642  {
643  if(!$a_fields)
644  {
645  return '';
646  }
647  $in = " IN ('";
648  $in .= implode("','",$a_fields);
649  $in .= "') ";
650 
651  return $in;
652  }
653 
654 }
655 ?>