ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjectiveMaterials.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 
34 {
35  var $db = null;
36 
37  var $objective_id = null;
38  var $lms;
39 
40  public function __construct($a_objective_id)
41  {
42  global $ilDB;
43 
44  $this->db =& $ilDB;
45 
46  $this->objective_id = $a_objective_id;
47 
48  $this->__read();
49  }
50 
60  public function cloneDependencies($a_new_objective,$a_copy_id)
61  {
62  global $ilObjDataCache,$ilLog;
63 
64  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
65  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
66  $mappings = $cwo->getMappings();
67  #$ilLog->write(__METHOD__.': 1');
68  foreach($this->getMaterials() as $material)
69  {
70  #$ilLog->write(__METHOD__.': 2');
71  // Copy action omit ?
72  if(!isset($mappings["$material[ref_id]"]) or !$mappings["$material[ref_id]"])
73  {
74  continue;
75  }
76  #$ilLog->write(__METHOD__.': 3');
77  $material_ref_id = $material['ref_id'];
78  $material_rbac_obj_id = $ilObjDataCache->lookupObjId($material_ref_id);
79  $material_obj_id = $material['obj_id'];
80  $new_ref_id = $mappings[$material_ref_id];
81  $new_rbac_obj_id = $ilObjDataCache->lookupObjId($new_ref_id);
82  #$ilLog->write(__METHOD__.': 4');
83 
84  // Link
85  if($new_rbac_obj_id == $material_rbac_obj_id)
86  {
87  #$ilLog->write(__METHOD__.': 5');
88  $ilLog->write(__METHOD__.': Material has been linked. Keeping object id.');
89  $new_obj_id = $material_obj_id;
90  }
91  elseif($material['type'] == 'st')
92  {
93  #$ilLog->write(__METHOD__.': 6');
94  // Chapter assignment
95  $new_material_info = isset($mappings[$material_ref_id.'_'.$material_obj_id]) ?
96  $mappings[$material_ref_id.'_'.$material_obj_id] :
97  array();
98  $new_material_arr = explode('_',$new_material_info);
99  if(!isset($new_material_arr[1]) or !$new_material_arr[1])
100  {
101  $ilLog->write(__METHOD__.': No mapping found for chapter: '.$material_obj_id);
102  continue;
103  }
104  $new_obj_id = $new_material_arr[1];
105  $ilLog->write(__METHOD__.': New material id is: '.$new_obj_id);
106  }
107  else
108  {
109  #$ilLog->write(__METHOD__.': 7');
110  // Any type
111  $new_obj_id = $ilObjDataCache->lookupObjId($mappings[$material_ref_id]);
112  }
113 
114  #$ilLog->write(__METHOD__.': 8');
115  $new_material = new ilCourseObjectiveMaterials($a_new_objective);
116  #$ilLog->write(__METHOD__.': 8.1');
117  $new_material->setLMRefId($new_ref_id);
118  #$ilLog->write(__METHOD__.': 8.2');
119  $new_material->setLMObjId($new_obj_id);
120  #$ilLog->write(__METHOD__.': 8.3');
121  $new_material->setType($material['type']);
122  #$ilLog->write(__METHOD__.': 8.4');
123  $new_material->add();
124  #$ilLog->write(__METHOD__.': 9');
125 
126  }
127  }
128 
137  public static function _getAssignedMaterials($a_objective_id)
138  {
139  global $ilDB;
140 
141  $query = "SELECT DISTINCT(ref_id) AS ref_id FROM crs_objective_lm ".
142  "WHERE objective_id = ".$ilDB->quote($a_objective_id)." ";
143  $res = $ilDB->query($query);
144  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
145  {
146  $ref_ids[] = $row->ref_id;
147  }
148  return $ref_ids ? $ref_ids : array();
149  }
150 
151 
152 
163  public static function _getAssignableMaterials($a_container_id)
164  {
165  global $tree,$ilDB;
166 
167  $all_materials = $tree->getSubTree($tree->getNodeData($a_container_id),true);
168  $all_materials = ilUtil::sortArray($all_materials,'title','asc');
169 
170  // Filter
171  foreach($all_materials as $material)
172  {
173  switch($material['type'])
174  {
175  case 'tst':
176  case 'fold':
177  case 'grp':
178  case 'rolf':
179  case 'crs':
180  case 'sess':
181  continue;
182 
183  default:
184  $assignable[] = $material;
185  break;
186  }
187  }
188  return $assignable ? $assignable : array();
189  }
190 
199  public static function _getAllAssignedMaterials($a_container_id)
200  {
201  global $ilDB;
202 
203  $query = "SELECT DISTINCT(com.ref_id) as ref_id FROM crs_objectives as co ".
204  "JOIN crs_objective_lm as com ON co.objective_id = com.objective_id ".
205  "JOIN object_reference as obr ON com.ref_id = obr.ref_id ".
206  "JOIN object_data as obd ON obr.obj_id = obd.obj_id ".
207  "WHERE co.crs_id = ".$ilDB->quote($a_container_id)." ".
208  "ORDER BY obd.title ";
209 
210  $res = $ilDB->query($query);
211  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
212  {
213  $ref_ids[] = $row->ref_id;
214  }
215  return $ref_ids ? $ref_ids : array();
216  }
217 
218  public function getMaterials()
219  {
220  return $this->lms ? $this->lms : array();
221  }
222 
223  function getChapters()
224  {
225  foreach($this->lms as $lm_data)
226  {
227  if($lm_data['type'] == 'st')
228  {
229  $chapters[] = $lm_data;
230  }
231  if($lm_data['type'] == 'pg')
232  {
233  $chapters[] = $lm_data;
234  }
235  }
236  return $chapters ? $chapters : array();
237  }
238 
239  function getLM($lm_id)
240  {
241  return $this->lms[$lm_id] ? $this->lms[$lm_id] : array();
242  }
243 
244  function getObjectiveId()
245  {
246  return $this->objective_id;
247  }
248 
249  function setLMRefId($a_ref_id)
250  {
251  $this->lm_ref_id = $a_ref_id;
252  }
253  function getLMRefId()
254  {
255  return $this->lm_ref_id ? $this->lm_ref_id : 0;
256  }
257  function setLMObjId($a_obj_id)
258  {
259  $this->lm_obj_id = $a_obj_id;
260  }
261  function getLMObjId()
262  {
263  return $this->lm_obj_id ? $this->lm_obj_id : 0;
264  }
265  function setType($a_type)
266  {
267  $this->type = $a_type;
268  }
269  function getType()
270  {
271  return $this->type;
272  }
273 
282  public function isAssigned($a_ref_id)
283  {
284  $query = "SELECT * FROM crs_objective_lm ".
285  "WHERE ref_id = ".$this->db->quote($a_ref_id)." ".
286  "AND objective_id = ".$this->db->quote($this->getObjectiveId())." ".
287  "AND type != 'st' AND type != 'pg' ";
288  $res = $this->db->query($query);
289  return $res->numRows() ? true : false;
290  }
291 
300  public function isChapterAssigned($a_ref_id,$a_obj_id)
301  {
302  $query = "SELECT * FROM crs_objective_lm ".
303  "WHERE ref_id = ".$this->db->quote($a_ref_id)." ".
304  "AND obj_id = ".$this->db->quote($a_obj_id)." ".
305  "AND objective_id = ".$this->db->quote($this->getObjectiveId())." ".
306  "AND (type = 'st' OR type = 'pg')";
307  $res = $this->db->query($query);
308  return $res->numRows() ? true : false;
309  }
310  function checkExists()
311  {
312  global $ilDB;
313 
314  if($this->getLMObjId())
315  {
316  $query = "SELECT * FROM crs_objective_lm ".
317  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
318  "AND ref_id = ".$ilDB->quote($this->getLMRefId())." ".
319  "AND obj_id = ".$ilDB->quote($this->getLMObjId())." ";
320  }
321  else
322  {
323  $query = "SELECT * FROM crs_objective_lm ".
324  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
325  "AND ref_id = ".$ilDB->quote($this->getLMRefId())." ";
326  }
327 
328  $res = $this->db->query($query);
329 
330  return $res->numRows() ? true : false;
331  }
332 
333  function add()
334  {
335  global $ilDB;
336 
337  $query = "INSERT INTO crs_objective_lm ".
338  "SET objective_id = ".$ilDB->quote($this->getObjectiveId()).", ".
339  "ref_id = ".$ilDB->quote($this->getLMRefId()).", ".
340  "obj_id = ".$ilDB->quote($this->getLMObjId()).", ".
341  "type = ".$ilDB->quote($this->getType())."";
342 
343  $this->db->query($query);
344 
345  return true;
346  }
347  function delete($lm_id)
348  {
349  global $ilDB;
350 
351  if(!$lm_id)
352  {
353  return false;
354  }
355 
356  $query = "DELETE FROM crs_objective_lm ".
357  "WHERE lm_ass_id = ".$ilDB->quote($lm_id)." ";
358 
359  $this->db->query($query);
360 
361  return true;
362  }
363 
364  function deleteAll()
365  {
366  global $ilDB;
367 
368  $query = "DELETE FROM crs_objective_lm ".
369  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
370 
371  $this->db->query($query);
372 
373  return true;
374  }
375 
376  // PRIVATE
377  function __read()
378  {
379  global $tree,$ilDB;
380 
381  include_once('Modules/Course/classes/class.ilCourseObjective.php');
382  $container_ref_ids = ilObject::_getAllReferences(ilCourseObjective::_lookupContainerIdByObjectiveId($this->objective_id));
383  $container_ref_id = current($container_ref_ids);
384 
385  $this->lms = array();
386  $query = "SELECT lm_ass_id,lm.ref_id,lm.obj_id,lm.type FROM crs_objective_lm as lm ".
387  "JOIN object_reference as obr ON lm.ref_id = obr.ref_id ".
388  "JOIN object_data as obd ON obr.obj_id = obd.obj_id ".
389  "LEFT JOIN lm_data as lmd ON lmd.obj_id = lm.obj_id ".
390  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
391  "ORDER BY obd.title,lmd.title";
392 
393  $res = $this->db->query($query);
394  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
395  {
396  if(!$tree->isInTree($row->ref_id) or !$tree->isGrandChild($container_ref_id,$row->ref_id))
397  {
398  $this->delete($row->lm_ass_id);
399  continue;
400  }
401  $lm['ref_id'] = $row->ref_id;
402  $lm['obj_id'] = $row->obj_id;
403  $lm['type'] = $row->type;
404  $lm['lm_ass_id'] = $row->lm_ass_id;
405 
406  $this->lms[$row->lm_ass_id] = $lm;
407  }
408  return true;
409  }
410 
411 }
412 ?>