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