ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDValues.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
15 {
16  private static $cached_values = array();
17 
27  public static function _getValuesByObjId($a_obj_id)
28  {
29  global $ilDB;
30 
31  if(isset(self::$cached_values[$a_obj_id]))
32  {
33  return self::$cached_values[$a_obj_id];
34  }
35  $query = "SELECT field_id,value FROM adv_md_values ".
36  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer');
37  $res = $ilDB->query($query);
38 
39  self::$cached_values[$a_obj_id] = array();
40  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
41  {
42  self::$cached_values[$a_obj_id][$row->field_id] = $row->value;
43  }
44  return self::$cached_values[$a_obj_id];
45  }
46 
56  public static function _cloneValues($a_source_id,$a_target_id)
57  {
58  global $ilLog;
59 
60  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
61  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValue.php');
62 
63 
65  {
66  $ilLog->write(__METHOD__.': No advanced meta data found.');
67  return true;
68  }
69 
70  $ilLog->write(__METHOD__.': Start cloning advanced meta data.');
71 
72  foreach(self::_getValuesByObjId($a_source_id) as $field_id => $value)
73  {
74  if(!in_array($field_id,$defs))
75  {
76  continue;
77  }
78  $new_value = new ilAdvancedMDValue($field_id,$a_target_id);
79  $new_value->setValue($value);
80  $new_value->save();
81 
82  }
83  return true;
84  }
85 
94  public static function _appendXMLByObjId(ilXmlWriter $xml_writer,$a_obj_id)
95  {
96  global $ilDB;
97 
98  $type = ilObject::_lookupType($a_obj_id);
99 
100  // Get active field_definitions
101  $query = "SELECT field_id FROM adv_md_record amr ".
102  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id ".
103  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
104  "WHERE active = 1 ".
105  "AND obj_type = ".$ilDB->quote($type ,'text')." ";
106 
107  $xml_writer->xmlStartTag('AdvancedMetaData');
108 
109  $res = $ilDB->query($query);
110  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
111  {
112  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValue.php');
113  $value = ilAdvancedMDValue::_getInstance($a_obj_id,$row->field_id);
114  $value->appendXML($xml_writer);
115  }
116  $xml_writer->xmlEndTag('AdvancedMetaData');
117  }
118 
127  public static function _preloadValuesByObjIds($obj_ids)
128  {
129  global $ilDB;
130 
131  $query = "SELECT obj_id,field_id,value FROM adv_md_values ".
132  "WHERE ".$ilDB->in('obj_id',$obj_ids,false,'integer');
133 
134  $res = $ilDB->query($query);
135  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
136  {
137  self::$cached_values[$row->obj_id][$row->field_id] = $row->value;
138  }
139  return true;
140  }
141 
151  public static function _deleteByFieldId($a_field_id)
152  {
153  global $ilDB;
154 
155  $query = "DELETE FROM adv_md_values ".
156  "WHERE field_id = ".$ilDB->quote($a_field_id ,'integer')." ";
157  $res = $ilDB->manipulate($query);
158  }
159 
168  public static function _deleteByObjId($a_obj_id)
169  {
170  global $ilDB;
171 
172  $query = "DELETE FROM adv_md_values ".
173  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
174  $res = $ilDB->manipulate($query);
175  }
176 
186  public static function _getValuesByObjIdAndSubtype($a_obj_id, $a_subtype)
187  {
188  global $ilDB;
189 
190  $result = array();
191  $set = $ilDB->query($q = "SELECT field_id, value, sub_type, sub_id FROM adv_md_values ".
192  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
193  "AND sub_type = ".$ilDB->quote($a_subtype,'text')
194  );
195  while ($rec = $ilDB->fetchAssoc($set))
196  {
197  $result[] = $rec;
198  }
199 
200  return $result;
201  }
202 
209  static public function queryForRecords($a_obj_id, $a_subtype, $a_records, $a_obj_id_key, $a_obj_subid_key, $a_amet_filter = "")
210  {
211  $result = $val = array();
212  if (!is_array($a_obj_id))
213  {
214  $a_obj_id = array($a_obj_id);
215  }
216 
217  // read amet data
218  foreach ($a_obj_id as $obj_id)
219  {
220  $values = self::_getValuesByObjIdAndSubtype($obj_id, $a_subtype);
221  foreach ($values as $v)
222  {
223  $val[$obj_id][$v["sub_id"]][$v["field_id"]] = $v;
224  }
225  }
226 //var_dump($a_amet_filter);
227  // add amet data to records
228  foreach ($a_records as $rec)
229  {
230  // check filter
231  $skip = false;
232  if (is_array($a_amet_filter))
233  {
234  foreach ($a_amet_filter as $fk => $fv)
235  {
236  if (!$skip && substr($fk, 0, 3) == "md_")
237  {
238  $fka = explode("_", $fk);
239  $fka = $fka[1];
240 
241  if (is_array($fv))
242  {
244 
245  // #12511 - currently supports only date/datetime
246  if (($advmd_type == ilAdvancedMDFieldDefinition::TYPE_DATE ||
248  array_key_exists("from", $fv) &&
249  array_key_exists("to", $fv) &&
250  ($fv["from"] !== null || $fv["to"] !== null))
251  {
252  if (!isset($val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]][$fka]["value"]))
253  {
254  $skip = true;
255  }
256  else
257  {
258  $from = $fv["from"];
259  $to = $fv["to"];
260  $md_val = $val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]][$fka]["value"];
261 
263  {
264  $from = $from ? $from->get(IL_CAL_UNIX) : null;
265  $to = $to ? $to->get(IL_CAL_UNIX) : null;
266  }
267  else
268  {
269  $from = $from ? $from->get(IL_CAL_DATE) : null;
270  $to = $to ? $to->get(IL_CAL_DATE) : null;
271  $md_val = date("Y-m-d", $md_val);
272  }
273 
274  if($from && $to)
275  {
276  if($md_val < $from || $md_val > $to)
277  {
278  $skip = true;
279  }
280  }
281  else if($from)
282  {
283  if($md_val < $from)
284  {
285  $skip = true;
286  }
287  }
288  else
289  {
290  if($md_val > $to)
291  {
292  $skip = true;
293  }
294  }
295  }
296  }
297  }
298  else if($fv != "")
299  {
300  if (!isset($val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]][$fka]["value"]))
301  {
302  $skip = true;
303  }
304  else
305  {
306  $md_val = $val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]][$fka]["value"];
307  if (trim($md_val) != trim($fv))
308  {
309  $skip = true;
310  }
311  }
312  }
313  }
314  }
315  }
316  if ($skip)
317  {
318  continue;
319  }
320 
321 
322  if (is_array($val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]]))
323  {
324  foreach ($val[$rec[$a_obj_id_key]][$rec[$a_obj_subid_key]] as $k => $v)
325  {
326  $rec["md_".$k] = $v["value"];
327  }
328  }
329  $results[] = $rec;
330  }
331 
332  return $results;
333  }
334 
335 }
336 ?>