ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDRecord.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
35 {
36  private static $instances = array();
37 
38  protected $record_id;
39  protected $import_id;
40  protected $active;
41  protected $title;
42  protected $description;
43  protected $obj_types = array();
44  protected $db = null;
45 
55  public function __construct($a_record_id = 0)
56  {
57  global $ilDB;
58 
59  $this->record_id = $a_record_id;
60  $this->db = $ilDB;
61 
62  if($this->getRecordId())
63  {
64  $this->read();
65  }
66  }
67 
76  public static function _getInstanceByRecordId($a_record_id)
77  {
78  if(isset(self::$instances[$a_record_id]))
79  {
80  return self::$instances[$a_record_id];
81  }
82  return self::$instances[$a_record_id] = new ilAdvancedMDRecord($a_record_id);
83  }
84 
92  public static function _getActiveSearchableRecords()
93  {
94  global $ilDB;
95 
96  $query = "SELECT DISTINCT(amr.record_id) FROM adv_md_record amr ".
97  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
98  "WHERE searchable = 1 AND active = 1 ";
99 
100  $res = $ilDB->query($query);
101  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
102  {
103  $records[] = self::_getInstanceByRecordId($row->record_id);
104  }
105  return $records ? $records : array();
106  }
107 
116  public static function _lookupTitle($a_record_id)
117  {
118  static $title_cache = array();
119 
120  if(isset($title_cache[$a_record_id]))
121  {
122  return $title_cache[$a_record_id];
123  }
124 
125  global $ilDB;
126 
127  $query = "SELECT title FROM adv_md_record ".
128  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
129  $res = $ilDB->query($query);
130  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
131 
132  return $title_cache[$a_record_id] = $row->title;
133  }
134 
143  public static function _lookupRecordIdByImportId($a_ilias_id)
144  {
145  global $ilDB;
146 
147  $query = "SELECT record_id FROM adv_md_record ".
148  "WHERE import_id = ".$ilDB->quote($a_ilias_id ,'text')." ";
149  $res = $ilDB->query($query);
150  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
151  {
152  return $row->record_id;
153  }
154  return 0;
155  }
156 
163  public static function _getAssignableObjectTypes()
164  {
165  return array('cat','crs','rcrs');
166  }
167 
176  public static function _getActivatedObjTypes()
177  {
178  global $ilDB;
179 
180  $query = "SELECT DISTINCT(obj_type) FROM adv_md_record_objs amo ".
181  "JOIN adv_md_record amr ON amo.record_id = amr.record_id ".
182  "WHERE active = 1 ";
183  $res = $ilDB->query($query);
184  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
185  {
186  $obj_types[] = $row->obj_type;
187  }
188  return $obj_types ? $obj_types : array();
189  }
190 
199  public static function _getRecords()
200  {
201  global $ilDB;
202 
203  $query = "SELECT record_id FROM adv_md_record ";
204  $res = $ilDB->query($query);
205  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
206  {
207  $records[] = ilAdvancedMDRecord::_getInstanceByRecordId($row->record_id);
208  }
209  return $records ? $records : array();
210  }
211 
220  public static function _getAllRecordsByObjectType()
221  {
222  global $ilDB;
223 
224  $records = array();
225 
226  $query = "SELECT * FROM adv_md_record_objs ";
227  $res = $ilDB->query($query);
228  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
229  {
230  $records[$row->obj_type][] = self::_getInstanceByRecordId($row->record_id);
231  }
232  return $records;
233  }
234 
243  public static function _getActivatedRecordsByObjectType($a_obj_type)
244  {
245  global $ilDB;
246 
247  $records = array();
248 
249  $query = "SELECT amro.record_id record_id FROM adv_md_record_objs amro ".
250  "JOIN adv_md_record amr ON amr.record_id = amro.record_id ".
251  "WHERE active = 1 ".
252  "AND obj_type = ".$ilDB->quote($a_obj_type ,'text')." ";
253 
254  $res = $ilDB->query($query);
255  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
256  {
257  $records[] = self::_getInstanceByRecordId($row->record_id);
258  }
259  return $records;
260  }
261 
262 
263 
272  public static function _delete($a_record_id)
273  {
274  global $ilDB;
275 
276  // Delete fields
278 
279  $query = "DELETE FROM adv_md_record ".
280  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
281  $res = $ilDB->manipulate($query);
282 
283  $query = "DELETE FROM adv_md_record_objs ".
284  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
285  $res = $ilDB->manipulate($query);
286  }
287 
288 
295  public function delete()
296  {
298  }
299 
306  public function save()
307  {
308  global $ilDB;
309 
310  // Save import id if given
311  $next_id = $ilDB->nextId('adv_md_record');
312 
313  $query = "INSERT INTO adv_md_record (record_id,import_id,active,title,description) ".
314  "VALUES(".
315  $ilDB->quote($next_id,'integer').", ".
316  $this->db->quote($this->getImportId(),'text').", ".
317  $this->db->quote($this->isActive() ,'integer').", ".
318  $this->db->quote($this->getTitle() ,'text').", ".
319  $this->db->quote($this->getDescription() ,'text')." ".
320  ")";
321  $res = $ilDB->manipulate($query);
322  $this->record_id = $next_id;
323 
324  if(!strlen($this->getImportId()))
325  {
326  // set import id to default value
327  $query = "UPDATE adv_md_record ".
328  "SET import_id = ".$this->db->quote($this->generateImportId() ,'text')." ".
329  "WHERE record_id = ".$this->db->quote($this->record_id ,'integer')." ";
330  $res = $ilDB->manipulate($query);
331  }
332 
333  foreach($this->getAssignedObjectTypes() as $type)
334  {
335  global $ilDB;
336 
337  $query = "INSERT INTO adv_md_record_objs (record_id,obj_type) ".
338  "VALUES( ".
339  $this->db->quote($this->getRecordId() ,'integer').", ".
340  $this->db->quote($type ,'text')." ".
341  ")";
342  $res = $ilDB->manipulate($query);
343  }
344  }
345 
352  public function update()
353  {
354  global $ilDB;
355 
356  $query = "UPDATE adv_md_record ".
357  "SET active = ".$this->db->quote($this->isActive() ,'integer').", ".
358  "title = ".$this->db->quote($this->getTitle() ,'text').", ".
359  "description = ".$this->db->quote($this->getDescription() ,'text')." ".
360  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
361  $res = $ilDB->manipulate($query);
362 
363  // Delete assignments
364  $query = "DELETE FROM adv_md_record_objs ".
365  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
366  $res = $ilDB->manipulate($query);
367 
368  // Insert assignments
369  foreach($this->getAssignedObjectTypes() as $type)
370  {
371  $query = "INSERT INTO adv_md_record_objs (record_id,obj_type) ".
372  "VALUES ( ".
373  $this->db->quote($this->getRecordId() ,'integer').", ".
374  $this->db->quote($type ,'text')." ".
375  ")";
376  $res = $ilDB->manipulate($query);
377  }
378  }
379 
387  public function validate()
388  {
389  global $ilErr,$lng;
390 
391  if(!strlen($this->getTitle()))
392  {
393  $ilErr->setMessage('fill_out_all_required_fields');
394  return false;
395  }
396  return true;
397  }
398 
405  public function getRecordId()
406  {
407  return $this->record_id;
408  }
409 
417  public function setActive($a_active)
418  {
419  $this->active = $a_active;
420  }
421 
428  public function isActive()
429  {
430  return (bool) $this->active;
431  }
432 
440  public function setTitle($a_title)
441  {
442  $this->title = $a_title;
443  }
444 
451  public function getTitle()
452  {
453  return $this->title;
454  }
455 
463  public function setDescription($a_description)
464  {
465  $this->description = $a_description;
466  }
467 
474  public function getDescription()
475  {
476  return $this->description;
477  }
478 
486  public function setImportId($a_id_string)
487  {
488  $this->import_id = $a_id_string;
489  }
490 
497  public function getImportId()
498  {
499  return $this->import_id;
500  }
501 
509  public function setAssignedObjectTypes($a_obj_types)
510  {
511  $this->obj_types = $a_obj_types;
512  }
513 
521  public function appendAssignedObjectType($a_obj_type)
522  {
523  $this->obj_types[] = $a_obj_type;
524  }
525 
532  public function getAssignedObjectTypes()
533  {
534  return $this->obj_types ? $this->obj_types : array();
535  }
536 
546  public function toXML(ilXmlWriter $writer)
547  {
548  $writer->xmlStartTag('Record',array('active' => $this->isActive() ? 1 : 0,
549  'id' => $this->generateImportId()));
550  $writer->xmlElement('Title',null,$this->getTitle());
551  $writer->xmlElement('Description',null,$this->getDescription());
552 
553  foreach($this->getAssignedObjectTypes() as $obj_type)
554  {
555  $writer->xmlElement('ObjectType',null,$obj_type);
556  }
557 
558  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
560  {
561  $definition->toXML($writer);
562  }
563  $writer->xmlEndTag('Record');
564  }
565 
573  private function read()
574  {
575  global $ilDB;
576 
577  $query = "SELECT * FROM adv_md_record ".
578  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
579  $res = $this->db->query($query);
580  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
581  {
582  $this->setImportId($row->import_id);
583  $this->setActive($row->active);
584  $this->setTitle($row->title);
585  $this->setDescription($row->description);
586  }
587  $query = "SELECT * FROM adv_md_record_objs ".
588  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
589  $res = $this->db->query($query);
590  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
591  {
592  $this->obj_types[] = $row->obj_type;
593  }
594  }
595 
602  protected function generateImportId()
603  {
604  return 'il_'.IL_INST_ID.'_adv_md_record_'.$this->getRecordId();
605  }
606 
613  public function __destruct()
614  {
615  unset(self::$instances[$this->getRecordId()]);
616  }
617 }
618 ?>