ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDLifecycle.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 class ilMDLifecycle extends ilMDBase
28 {
32  private const STATUS_TRANSLATION = [
33  'draft' => 'Draft',
34  'final' => 'Final',
35  'revised' => 'Revised',
36  'unavailable' => 'Unavailable'
37  ];
38 
40  private string $version = "";
41  private string $status = "";
42 
46  public function getPossibleSubelements(): array
47  {
48  $subs['Contribute'] = 'meta_contribute';
49 
50  return $subs;
51  }
52 
56  public function getContributeIds(): array
57  {
58  return ilMDContribute::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_lifecycle');
59  }
60 
61  public function getContribute(int $a_contribute_id): ?ilMDContribute
62  {
63  if (!$a_contribute_id) {
64  return null;
65  }
66  $con = new ilMDContribute();
67  $con->setMetaId($a_contribute_id);
68 
69  return $con;
70  }
71 
72  public function addContribute(): ilMDContribute
73  {
74  $con = new ilMDContribute($this->getRBACId(), $this->getObjId(), $this->getObjType());
75  $con->setParentId($this->getMetaId());
76  $con->setParentType('meta_lifecycle');
77 
78  return $con;
79  }
80 
81  // SET/GET
82  public function setStatus(string $a_status): void
83  {
84  switch ($a_status) {
85  case 'Draft':
86  case 'Final':
87  case 'Revised':
88  case 'Unavailable':
89  $this->status = $a_status;
90  break;
91  }
92  }
93 
94  public function getStatus(): string
95  {
96  return $this->status;
97  }
98 
99  public function setVersion(string $a_version): void
100  {
101  $this->version = $a_version;
102  }
103 
104  public function getVersion(): string
105  {
106  return $this->version;
107  }
108 
109  public function setVersionLanguage(ilMDLanguageItem $lng_obj): void
110  {
111  $this->version_language = $lng_obj;
112  }
113 
115  {
117  }
118 
119  public function getVersionLanguageCode(): string
120  {
121  return is_object($this->version_language) ? $this->version_language->getLanguageCode() : '';
122  }
123 
124  public function save(): int
125  {
126  $fields = $this->__getFields();
127  $fields['meta_lifecycle_id'] = array('integer', $next_id = $this->db->nextId('il_meta_lifecycle'));
128 
129  if ($this->db->insert('il_meta_lifecycle', $fields)) {
130  $this->setMetaId($next_id);
131  return $this->getMetaId();
132  }
133  return 0;
134  }
135 
136  public function update(): bool
137  {
138  return $this->getMetaId() && $this->db->update(
139  'il_meta_lifecycle',
140  $this->__getFields(),
141  array("meta_lifecycle_id" => array('integer', $this->getMetaId()))
142  );
143  }
144 
145  public function delete(): bool
146  {
147  // Delete 'contribute'
148  foreach ($this->getContributeIds() as $id) {
149  $con = $this->getContribute($id);
150  $con->delete();
151  }
152 
153  if ($this->getMetaId()) {
154  $query = "DELETE FROM il_meta_lifecycle " .
155  "WHERE meta_lifecycle_id = " . $this->db->quote($this->getMetaId(), 'integer');
156  $res = $this->db->manipulate($query);
157  return true;
158  }
159  return false;
160  }
161 
165  public function __getFields(): array
166  {
170  $status = (string) array_search(
171  $this->getStatus(),
172  self::STATUS_TRANSLATION
173  );
174 
175  return array(
176  'rbac_id' => array('integer', $this->getRBACId()),
177  'obj_id' => array('integer', $this->getObjId()),
178  'obj_type' => array('text', $this->getObjType()),
179  'lifecycle_status' => array('text', $status),
180  'meta_version' => array('text', $this->getVersion()),
181  'version_language' => array('text', $this->getVersionLanguageCode())
182  );
183  }
184 
185  public function read(): bool
186  {
187  if ($this->getMetaId()) {
188  $query = "SELECT * FROM il_meta_lifecycle " .
189  "WHERE meta_lifecycle_id = " . $this->db->quote($this->getMetaId(), 'integer');
190 
191  $res = $this->db->query($query);
192  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
196  if (key_exists($row->lifecycle_status ?? '', self::STATUS_TRANSLATION)) {
197  $row->lifecycle_status = self::STATUS_TRANSLATION[$row->lifecycle_status ?? ''];
198  }
199 
200  $this->setRBACId((int) $row->rbac_id);
201  $this->setObjId((int) $row->obj_id);
202  $this->setObjType($row->obj_type ?? '');
203  $this->setStatus($row->lifecycle_status ?? '');
204  $this->setVersion($row->meta_version ?? '');
205  $this->setVersionLanguage(new ilMDLanguageItem($row->version_language ?? ''));
206  }
207  }
208  return true;
209  }
210 
211  public function toXML(ilXmlWriter $writer): void
212  {
213  $writer->xmlStartTag('Lifecycle', array(
214  'Status' => $this->getStatus() ?: 'Draft'
215  ));
216  $writer->xmlElement(
217  'Version',
218  array(
219  'Language' => $this->getVersionLanguageCode() ?: 'en'
220  ),
221  $this->getVersion()
222  );
223 
224  // contribute
225  $contributes = $this->getContributeIds();
226  foreach ($contributes as $id) {
227  $con = $this->getContribute($id);
228  $con->toXML($writer);
229  }
230  if (!count($contributes)) {
231  $con = new ilMDContribute($this->getRBACId(), $this->getObjId());
232  $con->toXML($writer);
233  }
234  $writer->xmlEndTag('Lifecycle');
235  }
236 
237  // STATIC
238  public static function _getId(int $a_rbac_id, int $a_obj_id): int
239  {
240  global $DIC;
241 
242  $ilDB = $DIC->database();
243 
244  $query = "SELECT meta_lifecycle_id FROM il_meta_lifecycle " .
245  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
246  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
247 
248  $res = $ilDB->query($query);
249  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
250  return (int) $row->meta_lifecycle_id;
251  }
252  return 0;
253  }
254 }
$res
Definition: ltiservices.php:69
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVersion(string $a_version)
setVersionLanguage(ilMDLanguageItem $lng_obj)
getContribute(int $a_contribute_id)
setRBACId(int $a_id)
const ilMDLanguageItem $version_language
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
const STATUS_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
static _getId(int $a_rbac_id, int $a_obj_id)
setStatus(string $a_status)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
toXML(ilXmlWriter $writer)
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
setMetaId(int $a_meta_id, bool $a_read_data=true)
setObjType(string $a_type)