ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMDKeyword.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 
24 
31 include_once 'class.ilMDBase.php';
32 
33 class ilMDKeyword extends ilMDBase
34 {
35  function ilMDKeyword($a_rbac_id = 0,$a_obj_id = 0,$a_obj_type = '')
36  {
37  parent::ilMDBase($a_rbac_id,
38  $a_obj_id,
39  $a_obj_type);
40  }
41 
42  // SET/GET
43  function setKeyword($a_keyword)
44  {
45  $this->keyword = $a_keyword;
46  }
47  function getKeyword()
48  {
49  return $this->keyword;
50  }
51  function setKeywordLanguage(&$lng_obj)
52  {
53  if(is_object($lng_obj))
54  {
55  $this->keyword_language = $lng_obj;
56  }
57  }
58  function &getKeywordLanguage()
59  {
60  return is_object($this->keyword_language) ? $this->keyword_language : false;
61  }
63  {
64  return is_object($this->keyword_language) ? $this->keyword_language->getLanguageCode() : false;
65  }
66 
67  function save()
68  {
69  global $ilDB;
70 
71  $fields = $this->__getFields();
72  $fields['meta_keyword_id'] = array('integer',$next_id = $ilDB->nextId('il_meta_keyword'));
73 
74  if($this->db->insert('il_meta_keyword',$fields))
75  {
76  $this->setMetaId($next_id);
77  return $this->getMetaId();
78  }
79  return false;
80  }
81 
82  function update()
83  {
84  global $ilDB;
85 
86  if($this->getMetaId())
87  {
88  if($this->db->update('il_meta_keyword',
89  $this->__getFields(),
90  array("meta_keyword_id" => array('integer',$this->getMetaId()))))
91  {
92  return true;
93  }
94  }
95  return false;
96  }
97 
98  function delete()
99  {
100  global $ilDB;
101 
102  if($this->getMetaId())
103  {
104  $query = "DELETE FROM il_meta_keyword ".
105  "WHERE meta_keyword_id = ".$ilDB->quote($this->getMetaId() ,'integer');
106  $res = $ilDB->manipulate($query);
107 
108  return true;
109  }
110  return false;
111  }
112 
113 
114  function __getFields()
115  {
116  return array('rbac_id' => array('integer',$this->getRBACId()),
117  'obj_id' => array('integer', $this->getObjId()),
118  'obj_type' => array('text', $this->getObjType()),
119  'parent_type' => array('text', $this->getParentType()),
120  'parent_id' => array('integer', $this->getParentId()),
121  'keyword' => array('text', $this->getKeyword()),
122  'keyword_language' => array('text', $this->getKeywordLanguageCode()));
123  }
124 
125  function read()
126  {
127  global $ilDB;
128 
129  include_once 'Services/MetaData/classes/class.ilMDLanguageItem.php';
130 
131  if($this->getMetaId())
132  {
133  $query = "SELECT * FROM il_meta_keyword ".
134  "WHERE meta_keyword_id = ".$ilDB->quote($this->getMetaId() ,'integer');
135 
136  $res = $this->db->query($query);
137  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
138  {
139  $this->setRBACId($row->rbac_id);
140  $this->setObjId($row->obj_id);
141  $this->setObjType($row->obj_type);
142  $this->setParentId($row->parent_id);
143  $this->setParentType($row->parent_type);
144  $this->setKeyword($row->keyword);
145  $this->setKeywordLanguage( new ilMDLanguageItem($row->keyword_language));
146  }
147  }
148  return true;
149  }
150 
151  /*
152  * XML Export of all meta data
153  * @param object (xml writer) see class.ilMD2XML.php
154  *
155  */
156  function toXML(&$writer)
157  {
158  $writer->xmlElement('Keyword',array('Language' => $this->getKeywordLanguageCode() ?
159  $this->getKeywordLanguageCode() :
160  'en'),
161  $this->getKeyword());
162  }
163 
164 
165  // STATIC
166  function _getIds($a_rbac_id,$a_obj_id,$a_parent_id,$a_parent_type)
167  {
168  global $ilDB;
169 
170  $query = "SELECT meta_keyword_id FROM il_meta_keyword ".
171  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id ,'integer')." ".
172  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
173  "AND parent_id = ".$ilDB->quote($a_parent_id ,'integer')." ".
174  "AND parent_type = ".$ilDB->quote($a_parent_type ,'text')." ".
175  "ORDER BY meta_keyword_id ";
176 
177  $res = $ilDB->query($query);
178  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
179  {
180  $ids[] = $row->meta_keyword_id;
181  }
182  return $ids ? $ids : array();
183  }
184 
195  public static function _getKeywordsByLanguage($a_rbac_id,$a_obj_id,$a_type)
196  {
197  global $ilDB,$ilObjDataCache;
198 
199  $query = "SELECT keyword,keyword_language ".
200  "FROM il_meta_keyword ".
201  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id ,'integer')." ".
202  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
203  "AND obj_type = ".$ilDB->quote($a_type ,'text')." ";
204  $res = $ilDB->query($query);
205  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
206  {
207  if($row->keyword)
208  {
209  $keywords[$row->keyword_language][] = $row->keyword;
210  }
211  }
212  return $keywords ? $keywords : array();
213  }
224  public static function _getKeywordsByLanguageAsString($a_rbac_id,$a_obj_id,$a_type)
225  {
226  foreach(ilMDKeyword::_getKeywordsByLanguage($a_rbac_id,$a_obj_id,$a_type) as $lng_code => $keywords)
227  {
228  $key_string[$lng_code] = implode(",",$keywords);
229  }
230  return $key_string ? $key_string : array();
231  }
232 
239  public static function _searchKeywords($a_query,$a_type, $a_rbac_id = 0)
240  {
241  global $ilDB;
242 
243 
244  $qs = 'AND ';
245  $counter = 0;
246  foreach((array) explode(' ',$a_query) as $part)
247  {
248  if($counter++)
249  {
250  $qs .= 'OR ';
251  }
252  $qs .= ($ilDB->like('keyword','text',$part).' ');
253  }
254 
255  if($a_rbac_id)
256  {
257  $query = "SELECT * FROM il_meta_keyword ".
258  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id,'integer').' '.
259  'AND obj_type = '.$ilDB->quote($a_type,'text').' '.
260  $qs;
261  }
262  else
263  {
264  $query = "SELECT * FROM il_meta_keyword ".
265  'WHERE obj_type = '.$ilDB->quote($a_type,'text').' '.
266  $qs;
267  }
268  $res = $ilDB->query($query);
269  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
270  {
271  $obj_ids[] = $row->obj_id;
272  }
273  return (array) $obj_ids;
274  }
275 
283  public static function lookupKeywords($a_rbac_id, $a_obj_id)
284  {
285  global $ilDB;
286 
287  $query = "SELECT * FROM il_meta_keyword ".
288  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id,'integer').' '.
289  "AND obj_id = ".$ilDB->quote($a_obj_id,'integer').' ';
290  $res = $ilDB->query($query);
291  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
292  {
293  if(strlen($row->keyword))
294  $kws[] = $row->keyword;
295  }
296  return (array) $kws;
297  }
298 }
299 ?>