ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSearchAutoComplete.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
25 {
26  public static function getLuceneList(string $a_str): string
27  {
28  $qp = new ilLuceneQueryParser('title:' . $a_str . '*');
29  $qp->parse();
30 
31  $searcher = ilLuceneSearcher::getInstance($qp);
32  $searcher->setType(ilLuceneSearcher::TYPE_STANDARD);
33  $searcher->search();
34 
35  $res = $searcher->getResult()->getCandidates();
36 
37  $max_entries = ilSearchSettings::getInstance()->getAutoCompleteLength() ?
38  ilSearchSettings::getInstance()->getAutoCompleteLength() :
39  10;
40 
41 
42  $list = array();
43  $num_entries = 0;
44  foreach ($res as $res_obj_id) {
45  if (self::checkObjectPermission($res_obj_id)) {
46  $list[] = ilObject::_lookupTitle($res_obj_id);
47  $num_entries++;
48  }
49  if ($num_entries >= $max_entries) {
50  break;
51  }
52  }
53 
54  $i = 0;
55  $result = array();
56  foreach ($list as $entry) {
57  $result[$i] = new stdClass();
58  $result[$i]->value = '"' . $entry . '"';
59  $i++;
60  }
61 
62  return json_encode($result, JSON_THROW_ON_ERROR);
63  }
64 
65 
66 
67  public static function getList(string $a_str): string
68  {
69  global $DIC;
70 
71  $ilDB = $DIC->database();
72 
73  if (ilSearchSettings::getInstance()->enabledLucene()) {
74  return self::getLuceneList($a_str);
75  }
76 
77 
78  $a_str = str_replace('"', "", $a_str);
79 
81 
82  $object_types = array('cat','dbk','crs','fold','frm','grp','lm','sahs','glo','mep','htlm','exc','file','qpl','tst','svy','spl',
83  'chat', 'webr','mcst','sess','pg','st','gdf','wiki', 'copa');
84 
85  $set = $ilDB->query("SELECT title, obj_id FROM object_data WHERE "
86  . $ilDB->like('title', 'text', $a_str . "%") . " AND "
87  . $ilDB->in('type', $object_types, false, 'text') . " ORDER BY title");
88  $max = ($settings->getAutoCompleteLength() > 0)
89  ? $settings->getAutoCompleteLength()
90  : 10;
91 
92  $cnt = 0;
93  $list = array();
94  $checked = array();
95  $lim = "";
96  while (($rec = $ilDB->fetchAssoc($set)) && $cnt < $max) {
97  if (strpos($rec["title"], " ") > 0 || strpos($rec["title"], "-") > 0) {
98  $rec["title"] = '"' . $rec["title"] . '"';
99  }
100  if (!in_array($rec["title"], $list) && !in_array($rec["obj_id"], $checked)) {
101  if (ilSearchAutoComplete::checkObjectPermission((int) $rec["obj_id"])) {
102  $list[] = $lim . $rec["title"];
103  $cnt++;
104  }
105  $checked[] = $rec["obj_id"];
106  }
107  }
108 
109  $set = $ilDB->query("SELECT rbac_id,obj_id,obj_type, keyword FROM il_meta_keyword WHERE "
110  . $ilDB->like('keyword', 'text', $a_str . "%") . " AND "
111  . $ilDB->in('obj_type', $object_types, false, 'text') . " ORDER BY keyword");
112  while (($rec = $ilDB->fetchAssoc($set)) && $cnt < $max) {
113  if (strpos($rec["keyword"], " ") > 0) {
114  $rec["keyword"] = '"' . $rec["keyword"] . '"';
115  }
116  if (!in_array($rec["keyword"], $list) && !in_array($rec["rbac_id"], $checked)) {
117  if (ilSearchAutoComplete::checkObjectPermission((int) $rec["rbac_id"])) {
118  $list[] = $lim . $rec["keyword"];
119  $cnt++;
120  }
121  }
122  $checked[] = $rec["rbac_id"];
123  }
124 
125  $i = 0;
126  $result = array();
127  foreach ($list as $l) {
128  $result[$i] = new stdClass();
129  $result[$i]->value = $l;
130  $i++;
131  }
132 
133  return json_encode($result, JSON_THROW_ON_ERROR);
134  }
135 
136  public static function checkObjectPermission(int $a_obj_id): bool
137  {
138  global $DIC;
139 
140  $ilAccess = $DIC->access();
141 
142  $refs = ilObject::_getAllReferences($a_obj_id);
143  foreach ($refs as $ref) {
144  if ($ilAccess->checkAccess("read", "", $ref)) {
145  return true;
146  }
147  }
148  return false;
149  }
150 }
$res
Definition: ltiservices.php:69
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
static getLuceneList(string $a_str)
static getList(string $a_str)
static _getAllReferences(int $id)
get all reference ids for object ID
static getInstance(ilLuceneQueryParser $qp)
Get singleton instance.
global $DIC
Definition: feed.php:28
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static checkObjectPermission(int $a_obj_id)
$i
Definition: metadata.php:41