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