ILIAS  release_8 Revision v8.24
class.ilAdvancedSearch.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
34{
35 private string $mode = '';
36 protected array $options = [];
37
38
39
40 public function setMode(string $a_mode): void
41 {
42 $this->mode = $a_mode;
43 }
44 public function getMode(): string
45 {
46 return $this->mode;
47 }
48
49 public function setOptions(array &$options): void
50 {
51 $this->options = &$options;
52 }
53
54
55 public function performSearch(): ?ilSearchResult
56 {
57 switch ($this->getMode()) {
58 case 'requirement':
59 return $this->__searchRequirement();
60
61 case 'educational':
62 return $this->__searchEducational();
63
64 case 'typical_age_range':
65 return $this->__searchTypicalAgeRange();
66
67 case 'rights':
68 return $this->__searchRights();
69
70 case 'classification':
71 return $this->__searchClassification();
72
73 case 'taxon':
74 return $this->__searchTaxon();
75
76 case 'keyword':
77 return $this->__searchKeyword();
78
79 case 'format':
80 return $this->__searchFormat();
81
82 case 'lifecycle':
83 return $this->__searchLifecycle();
84
85 case 'contribute':
86 return $this->__searchContribute();
87
88 case 'entity':
89 return $this->__searchEntity();
90
91 case 'general':
92 return $this->__searchGeneral();
93
94 case 'keyword_all':
95 return $this->__searchKeyword(false);
96
97 case 'title_description':
98 return $this->__searchTitleDescription();
99
100 case 'title':
101 return $this->__searchTitle();
102
103 case 'description':
104 return $this->__searchDescription();
105
106 case 'language':
107 return $this->__searchLanguage();
108
109 default:
110 throw new InvalidArgumentException('ilMDSearch: no valid mode given');
111 }
112 }
113
115 {
116 $this->searchObjectProperties('title', 'description');
118 }
119
120 public function __searchTitle(): ilSearchResult
121 {
122 return $this->searchObjectProperties('title');
123 }
124
126 {
127 return $this->searchObjectProperties('description');
128 }
129
130 protected function searchObjectProperties(string ...$fields): ilSearchResult
131 {
132 $this->setFields($fields);
133
134 $and = ("AND type " . $this->__getInStatement($this->getFilter()));
135 $where = $this->__createObjectPropertiesWhereCondition(...$fields);
136 $locate = $this->__createLocateString();
137
138 $query = "SELECT obj_id,type " .
139 $locate .
140 "FROM object_data " .
141 $where . " " . $and . ' ' .
142 "ORDER BY obj_id DESC";
143
144 $res = $this->db->query($query);
145 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
146 $this->search_result->addEntry(
147 (int) $row->obj_id,
148 (string) $row->type,
149 $this->__prepareFound($row)
150 );
151 }
152
154 }
155
156 public function __searchGeneral(): ?ilSearchResult
157 {
158 global $DIC;
159
160 $ilDB = $DIC->database();
161
162 if (
163 !($this->options['lom_coverage'] ?? null) and
164 !($this->options['lom_structure'] ?? null)
165 ) {
166
167 return null;
168 }
169 $and = $locate = '';
170
171 if ($this->options['lom_coverage'] ?? null) {
172 $this->setFields(array('coverage'));
173 $and = $this->__createCoverageAndCondition();
174 $locate = $this->__createLocateString();
175 }
176 if ($this->options['lom_structure'] ?? null) {
177 $and .= ("AND general_structure = " . $ilDB->quote($this->options['lom_structure'], ilDBConstants::T_TEXT) . " ");
178 }
179
180 $query = "SELECT rbac_id,obj_type,obj_id " .
181 $locate . " " .
182 "FROM il_meta_general " .
183 "WHERE obj_type " . $this->__getInStatement($this->getFilter()) . " " .
184 $and;
185
186 $res = $this->db->query($query);
187 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
188 if ($this->options['lom_coverage'] ?? null) {
189 $found = $this->__prepareFound($row);
190 if (!in_array(0, $found)) {
191 $this->search_result->addEntry(
192 (int) $row->rbac_id,
193 (string) $row->obj_type,
194 $found,
195 (int) $row->obj_id
196 );
197 }
198 } else {
199 $this->search_result->addEntry(
200 (int) $row->rbac_id,
201 (string) $row->obj_type,
202 array(),
203 (int) $row->obj_id
204 );
205 }
206 }
207
209 }
210
212 {
213 if (!($this->options['lom_language'] ?? null)) {
214 return null;
215 }
216
217 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_language " .
218 "WHERE language = " . $this->db->quote($this->options['lom_language'], 'text') . " " .
219 "AND obj_type " . $this->__getInStatement($this->getFilter()) . ' ' .
220 "AND parent_type = 'meta_general'";
221
222 $res = $this->db->query($query);
223 #var_dump("<pre>",$query,"<pre>");
224 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
225 $this->search_result->addEntry(
226 (int) $row->rbac_id,
227 (string) $row->obj_type,
228 array(),
229 (int) $row->obj_id
230 );
231 }
233 }
234
236 {
237 if (!($this->options['lom_role'] ?? null)) {
238 return null;
239 }
240
241 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_contribute " .
242 "WHERE role = " . $this->db->quote($this->options['lom_role'], 'text') . " " .
243 "AND obj_type " . $this->__getInStatement($this->getFilter());
244
245 $res = $this->db->query($query);
246 #var_dump("<pre>",$query,"<pre>");
247 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
248 $this->search_result->addEntry(
249 (int) $row->rbac_id,
250 (string) $row->obj_type,
251 array(),
252 (int) $row->obj_id
253 );
254 }
256 }
257
258 public function __searchEntity(): ?ilSearchResult
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(ilDBConstants::FETCHMODE_OBJECT)) {
273 $found = $this->__prepareFound($row);
274 if (!in_array(0, $found)) {
275 $this->search_result->addEntry(
276 (int) $row->rbac_id,
277 (string) $row->obj_type,
278 $found,
279 (int) $row->obj_id
280 );
281 }
282 }
283
285 }
286
287
288
290 {
291 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_requirement ";
292
293 if (!strlen($where = $this->__createRequirementWhere())) {
294 return null;
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(ilDBConstants::FETCHMODE_OBJECT)) {
301 $this->search_result->addEntry(
302 (int) $row->rbac_id,
303 (string) $row->obj_type,
304 array(),
305 (int) $row->obj_id
306 );
307 }
309 }
310
312 {
313 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_educational ";
314
315 if (!strlen($where = $this->__createEducationalWhere())) {
316 return null;
317 }
318 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
319 $query = $query . $where . $and;
320 $res = $this->db->query($query);
321 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
322 $this->search_result->addEntry(
323 (int) $row->rbac_id,
324 (string) $row->obj_type,
325 array(),
326 (int) $row->obj_id
327 );
328 }
330 }
331
333 {
334 if (
335 !($this->options['typ_age_1'] ?? null) or
336 !($this->options['typ_age_2'] ?? null)
337 ) {
338 return null;
339 }
340
341 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_typical_age_range " .
342 "WHERE typical_age_range_min >= '" . (int) $this->options['typ_age_1'] . "' " .
343 "AND typical_age_range_max <= '" . (int) $this->options['typ_age_2'] . "'";
344
345
346 $res = $this->db->query($query);
347 #var_dump("<pre>",$query,"<pre>");
348 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
349 $this->search_result->addEntry(
350 (int) $row->rbac_id,
351 (string) $row->obj_type,
352 array(),
353 (int) $row->obj_id
354 );
355 }
357 }
358
359 public function __searchRights(): ?ilSearchResult
360 {
361 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_rights ";
362
363 if (!strlen($where = $this->__createRightsWhere())) {
364 return null;
365 }
366 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
367 $query = $query . $where . $and;
368 $res = $this->db->query($query);
369 #var_dump("<pre>",$query,"<pre>");
370 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
371 $this->search_result->addEntry(
372 (int) $row->rbac_id,
373 (string) $row->obj_type,
374 array(),
375 (int) $row->obj_id
376 );
377 }
379 }
380
382 {
383 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_classification ";
384
385 if (!strlen($where = $this->__createClassificationWhere())) {
386 return null;
387 }
388 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
389 $query = $query . $where . $and;
390 $res = $this->db->query($query);
391 #var_dump("<pre>",$query,"<pre>");
392 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
393 $this->search_result->addEntry(
394 (int) $row->rbac_id,
395 (string) $row->obj_type,
396 array(),
397 (int) $row->obj_id
398 );
399 }
401 }
402
403 public function __searchTaxon(): ?ilSearchResult
404 {
405 $this->setFields(array('taxon'));
406
407 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
408 $where = $this->__createTaxonWhereCondition();
409 $locate = $this->__createLocateString();
410
411 $query = "SELECT rbac_id,obj_id,obj_type " .
412 $locate .
413 "FROM il_meta_taxon " .
414 $where . " " . $and . ' ';
415
416 $res = $this->db->query($query);
417 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
418 $found = $this->__prepareFound($row);
419 if (!in_array(0, $found)) {
420 $this->search_result->addEntry(
421 (int) $row->rbac_id,
422 (string) $row->obj_type,
423 $found,
424 (int) $row->obj_id
425 );
426 }
427 }
428
430 }
431
432 public function __searchKeyword(bool $a_in_classification = false): ilSearchResult
433 {
434 $this->setFields(array('keyword'));
435
436 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
437 if ($a_in_classification) {
438 $and .= " AND parent_type = 'meta_classification' ";
439 }
440 $where = $this->__createKeywordWhereCondition();
441 $locate = $this->__createLocateString();
442
443 $query = "SELECT rbac_id,obj_id,obj_type " .
444 $locate .
445 "FROM il_meta_keyword " .
446 $where . " " . $and . ' ';
447
448 $res = $this->db->query($query);
449 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
450 $found = $this->__prepareFound($row);
451 if (!in_array(0, $found) or !$a_in_classification) {
452 $this->search_result->addEntry(
453 (int) $row->rbac_id,
454 (string) $row->obj_type,
455 $found,
456 (int) $row->obj_id
457 );
458 }
459 }
460
462 }
464 {
465 $this->setFields(array('meta_version'));
466
467 $locate = '';
468 if ($this->options['lom_version'] ?? null) {
469 $where = $this->__createLifecycleWhereCondition();
470 $locate = $this->__createLocateString();
471 } else {
472 $where = "WHERE 1 = 1 ";
473 }
474 $and = ("AND obj_type " . $this->__getInStatement($this->getFilter()));
475
476 if ($this->options['lom_status'] ?? null) {
477 $and .= (" AND lifecycle_status = " . $this->db->quote($this->options['lom_status'], 'text') . "");
478 }
479
480 $query = "SELECT rbac_id,obj_id,obj_type " .
481 $locate .
482 "FROM il_meta_lifecycle " .
483 $where . " " . $and . ' ';
484
485 $res = $this->db->query($query);
486 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
487 $found = $this->__prepareFound($row);
488 if (!in_array(0, $found)) {
489 $this->search_result->addEntry(
490 (int) $row->rbac_id,
491 (string) $row->obj_type,
492 $found,
493 (int) $row->obj_id
494 );
495 }
496 }
497
499 }
500
501 public function __searchFormat(): ?ilSearchResult
502 {
503 if (!($this->options['lom_format'] ?? null)) {
504 return null;
505 }
506
507 $query = "SELECT rbac_id,obj_id,obj_type FROM il_meta_format " .
508 "WHERE format LIKE(" . $this->db->quote($this->options['lom_format'], ilDBConstants::T_TEXT) . ") " .
509 "AND obj_type " . $this->__getInStatement($this->getFilter());
510
511 $res = $this->db->query($query);
512 #var_dump("<pre>",$query,"<pre>");
513 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
514 $this->search_result->addEntry(
515 (int) $row->rbac_id,
516 (string) $row->obj_type,
517 array(),
518 (int) $row->obj_id
519 );
520 }
522 }
523
524
525 public function __createRightsWhere(): string
526 {
527 $counter = 0;
528 $where = 'WHERE ';
529
530
531 if ($this->options['lom_costs'] ?? null) {
532 $and = $counter++ ? 'AND ' : ' ';
533 $where .= ($and . "costs = " . $this->db->quote($this->options['lom_costs'], 'text') . " ");
534 }
535 if ($this->options['lom_copyright'] ?? null) {
536 $and = $counter++ ? 'AND ' : ' ';
537 $where .= ($and . "cpr_and_or = " . $this->db->quote($this->options['lom_copyright'], 'text') . " ");
538 }
539 return $counter ? $where : '';
540 }
541 public function __createClassificationWhere(): string
542 {
543 $counter = 0;
544 $where = 'WHERE ';
545
546
547 if ($this->options['lom_purpose'] ?? null) {
548 $and = $counter++ ? 'AND ' : ' ';
549 $where .= ($and . "purpose = " . $this->db->quote($this->options['lom_purpose'], ilDBConstants::T_TEXT) . " ");
550 }
551 return $counter ? $where : '';
552 }
553 public function __createEducationalWhere(): string
554 {
555 $counter = 0;
556 $where = 'WHERE ';
557
558
559 if ($this->options['lom_interactivity'] ?? null) {
560 $and = $counter++ ? 'AND ' : ' ';
561 $where .= ($and . "interactivity_type = " . $this->db->quote($this->options['lom_interactivity'], 'text') . " ");
562 }
563 if ($this->options['lom_resource'] ?? null) {
564 $and = $counter++ ? 'AND ' : ' ';
565 $where .= ($and . "learning_resource_type = " . $this->db->quote($this->options['lom_resource'], 'text') . " ");
566 }
567 if ($this->options['lom_user_role'] ?? null) {
568 $and = $counter++ ? 'AND ' : ' ';
569 $where .= ($and . "intended_end_user_role = " . $this->db->quote($this->options['lom_user_role'], 'text') . " ");
570 }
571 if ($this->options['lom_context'] ?? null) {
572 $and = $counter++ ? 'AND ' : ' ';
573 $where .= ($and . "context = " . $this->db->quote($this->options['lom_context'], 'text') . " ");
574 }
575 if (
576 ($this->options['lom_level_start'] ?? null) or
577 ($this->options['lom_level_end'] ?? null)) {
578 $and = $counter++ ? 'AND ' : ' ';
579
580 $fields = $this->__getDifference(
581 $this->options['lom_level_start'],
582 $this->options['lom_level_end'],
583 array('VeryLow','Low','Medium','High','VeryHigh')
584 );
585
586 $where .= ($and . "interactivity_level " . $this->__getInStatement($fields));
587 }
588 if (
589 ($this->options['lom_density_start'] ?? null) or
590 ($this->options['lom_density_end'] ?? null)
591 ) {
592 $and = $counter++ ? 'AND ' : ' ';
593
594 $fields = $this->__getDifference(
595 $this->options['lom_density_start'],
596 $this->options['lom_density_end'],
597 array('VeryLow','Low','Medium','High','VeryHigh')
598 );
599
600 $where .= ($and . "semantic_density " . $this->__getInStatement($fields));
601 }
602 if (
603 ($this->options['lom_difficulty_start'] ?? null) or
604 ($this->options['lom_difficulty_end'] ?? null)
605 ) {
606 $and = $counter++ ? 'AND ' : ' ';
607
608 $fields = $this->__getDifference(
609 $this->options['lom_difficulty_start'],
610 $this->options['lom_difficulty_end'],
611 array('VeryEasy','Easy','Medium','Difficult','VeryDifficult')
612 );
613
614 $where .= ($and . "difficulty " . $this->__getInStatement($fields));
615 }
616
617 return $counter ? $where : '';
618 }
619 public function __createRequirementWhere(): string
620 {
621 $counter = 0;
622 $where = 'WHERE ';
623
624
625 if ($this->options['lom_operating_system'] ?? null) {
626 $and = $counter++ ? 'AND ' : ' ';
627 $where .= ($and . "operating_system_name = " . $this->db->quote($this->options['lom_operating_system'], ilDBConstants::T_TEXT) . " ");
628 }
629 if ($this->options['lom_browser'] ?? null) {
630 $and = $counter++ ? 'AND ' : ' ';
631 $where .= ($and . "browser_name = " . $this->db->quote($this->options['lom_browser'], ilDBConstants::T_TEXT) . " ");
632 }
633 return $counter ? $where : '';
634 }
635
639 public function __getDifference(int $a_val1, int $a_val2, array $options): array
640 {
641 $a_val2 = $a_val2 ?: count($options);
642 // Call again if a > b
643 if ($a_val1 > $a_val2) {
644 return $this->__getDifference($a_val2, $a_val1, $options);
645 }
646
647 $counter = 0;
648 $fields = [];
649 foreach ($options as $option) {
650 if ($a_val1 > ++$counter) {
651 continue;
652 }
653 if ($a_val2 < $counter) {
654 break;
655 }
656 $fields[] = $option;
657 }
658 return $fields;
659 }
660
661 public function __getInStatement(array $a_fields): string
662 {
663 if (!$a_fields) {
664 return '';
665 }
666 $in = " IN ('";
667 $in .= implode("','", $a_fields);
668 $in .= "') ";
669
670 return $in;
671 }
672}
setFields(array $a_fields)
ilSearchResult $search_result
__searchKeyword(bool $a_in_classification=false)
searchObjectProperties(string ... $fields)
__getInStatement(array $a_fields)
__getDifference(int $a_val1, int $a_val2, array $options)
setOptions(array &$options)
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query