ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSCategoryMapping.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 
24 include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMappingRule.php';
25 
36 {
37  private static $cached_active_rules = null;
38 
45  public static function getActiveRules()
46  {
47  global $ilDB;
48 
49  $res = $ilDB->query('SELECT mapping_id FROM ecs_container_mapping');
50  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
51  {
52  $rules[] = new ilECSCategoryMappingRule($row->mapping_id);
53  }
54  return $rules ? $rules : array();
55  }
56 
64  public static function getMatchingCategory(ilECSEContent $econtent)
65  {
66  global $ilLog;
67 
68  if(is_null(self::$cached_active_rules))
69  {
70  self::$cached_active_rules = self::getActiveRules();
71  }
72  foreach(self::$cached_active_rules as $rule)
73  {
74  if($rule->matches($econtent))
75  {
76  $ilLog->write(__METHOD__.': Found assignment for field type: '.$rule->getFieldName());
77  return $rule->getContainerId();
78  }
79  $ilLog->write(__METHOD__.': Category assignment failed for field: '.$rule->getFieldName());
80  }
81  // Return default container
82  $ilLog->write(__METHOD__.': Using default container');
83  return ilECSSettings::_getInstance()->getImportId();
84  }
85 
92  public static function handleUpdate(ilECSEContent $econtent,$a_obj_id)
93  {
94  global $tree,$ilLog;
95 
96  $a_ref_id = current(ilObject::_getAllReferences($a_obj_id));
97 
98  $references = ilObject::_getAllReferences(ilObject::_lookupObjId($a_ref_id));
99  $cat = self::getMatchingCategory($econtent);
100  $all_cats = self::lookupHandledCategories();
101 
102 
103  $exists = false;
104  foreach($references as $ref_id => $null)
105  {
106  if($tree->getParentId($ref_id) == $cat)
107  {
108  $exists = true;
109  }
110  }
111  $ilLog->write(__METHOD__.': Creating/Deleting references...');
112  include_once './classes/class.ilObjectFactory.php';
113 
114  if(!$exists)
115  {
116  $ilLog->write(__METHOD__.': Add new reference. STEP 1');
117 
118  if($obj_data = ilObjectFactory::getInstanceByRefId($a_ref_id,false))
119  {
120  $new_ref_id = $obj_data->createReference();
121  $obj_data->putInTree($cat);
122  $obj_data->setPermissions($cat);
123  $ilLog->write(__METHOD__.': Add new reference.');
124  }
125  }
126  // Now delete old references
127  foreach($references as $ref_id => $null)
128  {
129  $parent = $tree->getParentId($ref_id);
130  if($parent == $cat)
131  {
132  continue;
133  }
134  if(!in_array($parent,$all_cats))
135  {
136  continue;
137  }
139  {
140  $to_delete->delete();
141  $ilLog->write(__METHOD__.': Deleted deprecated reference.');
142  }
143  }
144  return true;
145  }
146 
153  public static function lookupHandledCategories()
154  {
155  global $ilDB;
156 
157  $res = $ilDB->query("SELECT container_id FROM ecs_container_mapping ");
158  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
159  {
160  $ref_ids[] = $row->container_id;
161  }
162  return $ref_ids ? $ref_ids : array();
163  }
164 
171  public static function getPossibleFields()
172  {
173  return array(
174  'part_id',
175  'study_courses',
176  'courseType',
177  'term',
178  'credits',
179  'begin');
180  }
181 }
182 ?>