ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDidacticTemplateSetting.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
12  const TYPE_CREATION = 1;
13 
14 
15  private $id = 0;
16  private $enabled = false;
17  private $title = '';
18  private $description = '';
19  private $info = '';
20  private $type = self::TYPE_CREATION;
21  private $assignments = array();
22  private $effective_from = array();
23  private $auto_generated = false;
24  private $exclusive = false;
25 
26 
31  public function __construct($a_id = 0)
32  {
33  $this->setId($a_id);
34  $this->read();
35  }
36 
41  protected function setId($a_id)
42  {
43  $this->id = $a_id;
44  }
45 
50  public function getId()
51  {
52  return $this->id;
53  }
54 
59  public function enable($a_status)
60  {
61  $this->enabled = $a_status;
62  }
63 
68  public function isEnabled()
69  {
70  return $this->enabled;
71  }
72 
77  public function setTitle($a_title)
78  {
79  $this->title = $a_title;
80  }
81 
86  public function getTitle()
87  {
88  return $this->title;
89  }
90 
95  public function getPresentationTitle($a_lng = "")
96  {
97  if ($this->isAutoGenerated()) {
98  global $lng;
99  return $lng->txt($this->getTitle());
100  }
101 
102  $tit = $this->getPresentation('title', $a_lng);
103  return $tit ? $tit : $this->getTitle();
104  }
105 
110  public function getDescription()
111  {
112  return $this->description;
113  }
114 
119  public function getPresentationDescription($a_lng = "")
120  {
121  if ($this->isAutoGenerated()) {
122  global $lng;
123  return $lng->txt($this->getDescription());
124  }
125 
126  $desc = $this->getPresentation('description', $a_lng);
127  return $desc ? $desc : $this->getDescription();
128  }
129 
134  public function setDescription($a_description)
135  {
136  $this->description = $a_description;
137  }
138 
143  public function setInfo($a_info)
144  {
145  $this->info = $a_info;
146  }
147 
152  public function getInfo()
153  {
154  return $this->info;
155  }
156 
161  public function setType($a_type)
162  {
163  $this->type = $a_type;
164  }
165 
170  public function getType()
171  {
172  return $this->type;
173  }
174 
179  public function setAssignments(array $a_ass)
180  {
181  $this->assignments = (array) $a_ass;
182  }
183 
188  public function getAssignments()
189  {
190  return (array) $this->assignments;
191  }
192 
197  public function addAssignment($a_obj_type)
198  {
199  $this->assignments[] = $a_obj_type;
200  }
201 
205  public function getEffectiveFrom()
206  {
207  return $this->effective_from;
208  }
209 
214  {
215  $this->effective_from = $effective_from;
216  }
217 
221  public function isAutoGenerated()
222  {
223  return $this->auto_generated;
224  }
225 
232  {
233  $this->auto_generated = $auto_generated;
234  }
235 
239  public function isExclusive()
240  {
241  return $this->exclusive;
242  }
243 
247  public function setExclusive($exclusive)
248  {
249  $this->exclusive = $exclusive;
250  }
251 
258  public function getTranslations()
259  {
260  $trans = $this->getTranslationObject();
261  $lang = $trans->getLanguages();
262 
263  foreach ($lang as $k => $v) {
264  if ($v['lang_default']) {
265  $lang[0] = $lang[$k];
266  }
267  }
268 
269  // fallback if translation object is empty
270  if (!isset($lang[0])) {
271  $lang[0]['title'] = $this->getTitle();
272  $lang[0]['description'] = $this->getDescription();
273  $lang[0]['lang_code'] = $trans->getDefaultLanguage();
274  }
275 
276  return $lang;
277  }
278 
279  protected function getPresentation($a_value, $a_lng)
280  {
281  $lang = $this->getTranslationObject()->getLanguages();
282 
283  if (!$lang) {
284  return "";
285  }
286 
287  if (!$a_lng) {
288  global $ilUser;
289  $a_lng = $ilUser->getCurrentLanguage();
290  }
291 
292  if (isset($lang[$a_lng][$a_value])) {
293  return $lang[$a_lng][$a_value] ;
294  } else {
295  return $lang[$a_lng][$this->getTranslationObject()->getDefaultLanguage()];
296  }
297  }
298 
302  public function delete()
303  {
304  global $ilDB;
305 
306  if ($this->isAutoGenerated()) {
307  return false;
308  }
309 
310  // Delete settings
311  $query = 'DELETE FROM didactic_tpl_settings ' .
312  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
313  $ilDB->manipulate($query);
314 
315  // Delete obj assignments
316  $query = 'DELETE FROM didactic_tpl_sa ' .
317  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
318  $ilDB->manipulate($query);
319 
320  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateActionFactory.php';
322  $action->delete();
323  }
324 
325  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateObjSettings.php';
327 
328  $this->getTranslationObject()->delete();
329  $this->deleteEffectiveNodes();
330  return true;
331  }
332 
336  public function save()
337  {
338  global $ilDB;
339 
340  $this->setId($ilDB->nextId('didactic_tpl_settings'));
341 
342  $query = 'INSERT INTO didactic_tpl_settings (id,enabled,title,description,info,type,auto_generated,exclusive_tpl) ' .
343  'VALUES( ' .
344  $ilDB->quote($this->getId(), 'integer') . ', ' .
345  $ilDB->quote($this->isEnabled(), 'integer') . ', ' .
346  $ilDB->quote($this->getTitle(), 'text') . ', ' .
347  $ilDB->quote($this->getDescription(), 'text') . ', ' .
348  $ilDB->quote($this->getInfo(), 'text') . ', ' .
349  $ilDB->quote($this->getType(), 'integer') . ', ' .
350  $ilDB->quote((int) $this->isAutoGenerated(), 'integer') . ', ' .
351  $ilDB->quote((int) $this->isExclusive(), 'integer') .
352  ')';
353 
354  $ilDB->manipulate($query);
355 
356  $this->saveAssignments();
357 
358  return true;
359  }
360 
365  private function saveAssignments()
366  {
367  if ($this->isAutoGenerated()) {
368  return false;
369  }
370 
371  foreach ($this->getAssignments() as $ass) {
372  $this->saveAssignment($ass);
373  }
374  return true;
375  }
376 
382  private function saveAssignment($a_obj_type)
383  {
384  global $ilDB;
385 
386  if ($this->isAutoGenerated()) {
387  return;
388  }
389 
390  $query = 'INSERT INTO didactic_tpl_sa (id,obj_type) ' .
391  'VALUES( ' .
392  $ilDB->quote($this->getId(), 'integer') . ', ' .
393  $ilDB->quote($a_obj_type, 'text') .
394  ')';
395  $ilDB->manipulate($query);
396  }
397 
401  protected function saveEffectiveNodes()
402  {
403  global $ilDB;
404 
405  if (!count($this->getEffectiveFrom())) {
406  return;
407  }
408 
409  foreach ($this->getEffectiveFrom() as $node) {
410  $values[] = '( ' .
411  $ilDB->quote($this->getId(), 'integer') . ', ' .
412  $ilDB->quote($node, 'integer') .
413  ')';
414  }
415 
416  $query = 'INSERT INTO didactic_tpl_en (id,node) ' .
417  'VALUES ' . implode(', ', $values);
418 
419  $ilDB->manipulate($query);
420  }
421 
422  protected function deleteEffectiveNodes()
423  {
424  global $ilDB;
425 
426  $query = 'DELETE FROM didactic_tpl_en ' .
427  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
428  $ilDB->manipulate($query);
429  return true;
430  }
431 
432  protected function readEffectiveNodes()
433  {
434  global $ilDB;
435  $effective_nodes = array();
436 
437  $query = 'SELECT * FROM didactic_tpl_en ' .
438  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
439  $res = $ilDB->query($query);
440  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
441  $effective_nodes[] = $row->node;
442  }
443 
444  $this->setEffectiveFrom($effective_nodes);
445  }
446 
452  private function deleteAssignments()
453  {
454  global $ilDB;
455 
456  if ($this->isAutoGenerated()) {
457  return false;
458  }
459 
460  $query = 'DELETE FROM didactic_tpl_sa ' .
461  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
462  $ilDB->manipulate($query);
463  return true;
464  }
465 
471  public function update()
472  {
473  global $ilDB;
474 
475  $query = 'UPDATE didactic_tpl_settings ' .
476  'SET ' .
477  'enabled = ' . $ilDB->quote($this->isEnabled(), 'integer') . ', ' .
478  'title = ' . $ilDB->quote($this->getTitle(), 'text') . ', ' .
479  'description = ' . $ilDB->quote($this->getDescription(), 'text') . ', ' .
480  'info = ' . $ilDB->quote($this->getInfo(), 'text') . ', ' .
481  'type = ' . $ilDB->quote($this->getType(), 'integer') . ', ' .
482  'exclusive_tpl = ' . $ilDB->quote((int) $this->isExclusive(), 'integer') . ' ' .
483  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
484  $ilDB->manipulate($query);
485  $this->deleteAssignments();
486  $this->saveAssignments();
487 
488  $this->deleteEffectiveNodes();
489  $this->saveEffectiveNodes();
490 
491  return true;
492  }
493 
498  protected function read()
499  {
500  global $ilDB;
501 
502  if (!$this->getId()) {
503  return false;
504  }
505 
509  $query = 'SELECT * FROM didactic_tpl_settings dtpl ' .
510  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
511  $res = $ilDB->query($query);
512  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
513  $this->setType($row->type);
514  $this->enable($row->enabled);
515  $this->setTitle($row->title);
516  $this->setDescription($row->description);
517  $this->setInfo($row->info);
518  $this->setAutoGenerated((bool) $row->auto_generated);
519  $this->setExclusive((bool) $row->exclusive_tpl);
520  }
521 
525  $query = 'SELECT * FROM didactic_tpl_sa ' .
526  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
527  $res = $ilDB->query($query);
528  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
529  $this->addAssignment($row->obj_type);
530  }
531 
532  $this->readEffectiveNodes();
533 
534  return true;
535  }
536 
542  public function toXml(ilXmlWriter $writer)
543  {
544  global $ilSetting;
545  switch ($this->getType()) {
546  case self::TYPE_CREATION:
547  $type = 'creation';
548  break;
549  }
550 
551  $writer->xmlStartTag('didacticTemplate', array('type' => $type));
552  $writer->xmlElement('title', array(), $this->getTitle());
553  $writer->xmlElement('description', array(), $this->getDescription());
554 
555  $writer = $this->getTranslationObject()->toXml($writer);
556 
557  // info text with p-tags
558  if (strlen($this->getInfo())) {
559  $writer->xmlStartTag('info');
560 
561  $info_lines = (array) explode("\n", $this->getInfo());
562  foreach ($info_lines as $info) {
563  $trimmed_info = trim($info);
564  if (strlen($trimmed_info)) {
565  $writer->xmlElement('p', array(), $trimmed_info);
566  }
567  }
568 
569  $writer->xmlEndTag('info');
570  }
571 
572  if ($this->isExclusive()) {
573  $writer->xmlElement("exclusive");
574  }
575 
576 
577  if (count($this->getEffectiveFrom()) > 0) {
578  $writer->xmlStartTag('effectiveFrom', array('nic_id'=> $ilSetting->get('inst_id')));
579 
580  foreach ($this->getEffectiveFrom() as $node) {
581  $writer->xmlElement('node', array(), $node);
582  }
583  $writer->xmlEndTag('effectiveFrom');
584  }
585 
586  // Assignments
587  $writer->xmlStartTag('assignments');
588  foreach ($this->getAssignments() as $assignment) {
589  $writer->xmlElement('assignment', array(), $assignment);
590  }
591  $writer->xmlEndTag('assignments');
592 
593 
594  $writer->xmlStartTag('actions');
595  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateActionFactory.php';
597  $action->toXml($writer);
598  }
599  $writer->xmlEndTag('actions');
600  $writer->xmlEndTag('didacticTemplate');
601 
602  return $writer;
603  }
604 
608  public function __clone()
609  {
610  $this->setId(0);
611  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateCopier.php';
613  $this->enable(false);
614  $this->setAutoGenerated(false);
615  }
616 
620  public function getTranslationObject()
621  {
622  include_once("./Services/Multilingualism/classes/class.ilMultilingualism.php");
623  return ilMultilingualism::getInstance($this->getId(), "dtpl");
624  }
625 
630  public function isEffective($a_node_id)
631  {
632  global $tree;
633 
634  if (!count($this->getEffectiveFrom()) || in_array($a_node_id, $this->getEffectiveFrom())) {
635  return true;
636  }
637 
638  foreach ($this->getEffectiveFrom() as $node) {
639  if ($tree->isGrandChild($node, $a_node_id)) {
640  return true;
641  }
642  }
643 
644  return false;
645  }
646 }
static getInstance($a_obj_id, $a_type)
Get instance.
setInfo($a_info)
Set installation info text.
isEnabled()
Check if template is enabled.
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
$action
XML writer class.
xmlEndTag($tag)
Writes an endtag.
addAssignment($a_obj_type)
Add one assignment obj type.
$a_type
Definition: workflow.php:92
setAutoGenerated($auto_generated)
DO NOT CHANGE THIS VALUE.
foreach($_POST as $key=> $value) $res
update()
Update settings ilDB $ilDB.
setAssignments(array $a_ass)
Set assignments.
static deleteByTemplateId($a_tpl_id)
Delete by template id ilDB $ilDB.
$ilUser
Definition: imgupload.php:18
$query
Create styles array
The data for the language used.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
enable($a_status)
Set enabled status.
saveAssignment($a_obj_type)
Add one object assignment ilDB $ilDB.
global $ilSetting
Definition: privfeed.php:17
global $lng
Definition: privfeed.php:17
global $ilDB
deleteAssignments()
Delete assignments ilDB $ilDB.
getInfo()
Get installation info text.
getTranslations()
get all translations from this object
setDescription($a_description)
Set description.
static getActionsByTemplateId($a_tpl_id)
Get actions of one template.