ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDContribute.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 class ilMDContribute extends ilMDBase
28 {
32  private const ROLE_TRANSLATION = [
33  'author' => 'Author',
34  'publisher' => 'Publisher',
35  'unknown' => 'Unknown',
36  'initiator' => 'Initiator',
37  'terminator' => 'Terminator',
38  'editor' => 'Editor',
39  'graphical designer' => 'GraphicalDesigner',
40  'technical implementer' => 'TechnicalImplementer',
41  'content provider' => 'ContentProvider',
42  'technical validator' => 'TechnicalValidator',
43  'educational validator' => 'EducationalValidator',
44  'script writer' => 'ScriptWriter',
45  'instructional designer' => 'InstructionalDesigner',
46  'subject matter expert' => 'SubjectMatterExpert',
47  'creator' => 'Creator',
48  'validator' => 'Validator'
49  ];
50 
51  // Subelements
52  private string $date = '';
53  private string $role = '';
54 
58  public function getEntityIds(): array
59  {
60  return ilMDEntity::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_contribute');
61  }
62 
63  public function getEntity(int $a_entity_id): ?ilMDEntity
64  {
65  if (!$a_entity_id) {
66  return null;
67  }
68  $ent = new ilMDEntity();
69  $ent->setMetaId($a_entity_id);
70 
71  return $ent;
72  }
73 
74  public function addEntity(): ilMDEntity
75  {
76  $ent = new ilMDEntity($this->getRBACId(), $this->getObjId(), $this->getObjType());
77  $ent->setParentId($this->getMetaId());
78  $ent->setParentType('meta_contribute');
79 
80  return $ent;
81  }
82 
83  // SET/GET
84  public function setRole(string $a_role): bool
85  {
86  switch ($a_role) {
87  case 'Author':
88  case 'Publisher':
89  case 'Unknown':
90  case 'Initiator':
91  case 'Terminator':
92  case 'Editor':
93  case 'GraphicalDesigner':
94  case 'TechnicalImplementer':
95  case 'ContentProvider':
96  case 'TechnicalValidator':
97  case 'EducationalValidator':
98  case 'ScriptWriter':
99  case 'InstructionalDesigner':
100  case 'SubjectMatterExpert':
101  case 'Creator':
102  case 'Validator':
103  case 'PointOfContact':
104  $this->role = $a_role;
105  return true;
106 
107  default:
108  return false;
109  }
110  }
111 
112  public function getRole(): string
113  {
114  return $this->role;
115  }
116 
117  public function setDate(string $a_date): void
118  {
119  $this->date = $a_date;
120  }
121 
122  public function getDate(): string
123  {
124  return $this->date;
125  }
126 
127  public function save(): int
128  {
129  $fields = $this->__getFields();
130  $fields['meta_contribute_id'] = array('integer', $next_id = $this->db->nextId('il_meta_contribute'));
131 
132  if ($this->db->insert('il_meta_contribute', $fields)) {
133  $this->setMetaId($next_id);
134  return $this->getMetaId();
135  }
136  return 0;
137  }
138 
139  public function update(): bool
140  {
141  return $this->getMetaId() && $this->db->update(
142  'il_meta_contribute',
143  $this->__getFields(),
144  array("meta_contribute_id" => array('integer', $this->getMetaId()))
145  );
146  }
147 
148  public function delete(): bool
149  {
150  if ($this->getMetaId()) {
151  $query = "DELETE FROM il_meta_contribute " .
152  "WHERE meta_contribute_id = " . $this->db->quote($this->getMetaId(), 'integer');
153  $res = $this->db->manipulate($query);
154 
155  foreach ($this->getEntityIds() as $id) {
156  $ent = $this->getEntity($id);
157  $ent->delete();
158  }
159  return true;
160  }
161  return false;
162  }
163 
167  public function __getFields(): array
168  {
172  $role = (string) array_search(
173  $this->getRole(),
174  self::ROLE_TRANSLATION
175  );
176 
177  return array(
178  'rbac_id' => array('integer', $this->getRBACId()),
179  'obj_id' => array('integer', $this->getObjId()),
180  'obj_type' => array('text', $this->getObjType()),
181  'parent_type' => array('text', $this->getParentType()),
182  'parent_id' => array('integer', $this->getParentId()),
183  'role' => array('text', $role),
184  'c_date' => array('text', $this->getDate())
185  );
186  }
187 
188  public function read(): bool
189  {
190  if ($this->getMetaId()) {
191  $query = "SELECT * FROM il_meta_contribute " .
192  "WHERE meta_contribute_id = " . $this->db->quote($this->getMetaId(), 'integer');
193 
194  $res = $this->db->query($query);
195  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
199  if (key_exists($row->role ?? '', self::ROLE_TRANSLATION)) {
200  $row->role = self::ROLE_TRANSLATION[$row->role ?? ''];
201  }
202 
203  $this->setRBACId((int) $row->rbac_id);
204  $this->setObjId((int) $row->obj_id);
205  $this->setObjType($row->obj_type ?? '');
206  $this->setParentId((int) $row->parent_id);
207  $this->setParentType($row->parent_type ?? '');
208  $this->setRole($row->role ?? '');
209  $this->setDate($row->c_date ?? '');
210  }
211  }
212  return true;
213  }
214 
215  public function toXML(ilXmlWriter $writer): void
216  {
217  $writer->xmlStartTag('Contribute', array(
218  'Role' => $this->getRole() ?: 'Author'
219  ));
220 
221  // Entities
222  $entities = $this->getEntityIds();
223  foreach ($entities as $id) {
224  $ent = $this->getEntity($id);
225  $ent->toXML($writer);
226  }
227  if (!count($entities)) {
228  $ent = new ilMDEntity($this->getRBACId(), $this->getObjId());
229  $ent->toXML($writer);
230  }
231 
232  $writer->xmlElement('Date', null, $this->getDate());
233  $writer->xmlEndTag('Contribute');
234  }
235 
236  // STATIC
237 
241  public static function _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type): array
242  {
243  global $DIC;
244 
245  $ilDB = $DIC['ilDB'];
246 
247  $query = "SELECT meta_contribute_id FROM il_meta_contribute " .
248  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
249  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
250  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
251  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text');
252 
253  $res = $ilDB->query($query);
254  $ids = [];
255  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
256  $ids[] = (int) $row->meta_contribute_id;
257  }
258  return $ids;
259  }
260 
264  public static function _lookupAuthors(int $a_rbac_id, int $a_obj_id, string $a_obj_type): array
265  {
266  global $DIC;
267 
268  $ilDB = $DIC['ilDB'];
269 
270  // Ask for 'author' later to use indexes
271  $authors = [];
272  $query = "SELECT entity,ent.parent_type,role FROM il_meta_entity ent " .
273  "JOIN il_meta_contribute con ON ent.parent_id = con.meta_contribute_id " .
274  "WHERE ent.rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
275  "AND ent.obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
276  $res = $ilDB->query($query);
277  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
278  if ($row->role === 'Author' && $row->parent_type === 'meta_contribute') {
279  $authors[] = trim($row->entity);
280  }
281  }
282  return $authors;
283  }
284 }
$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...
setRBACId(int $a_id)
toXML(ilXmlWriter $writer)
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
setRole(string $a_role)
getEntity(int $a_entity_id)
static _lookupAuthors(int $a_rbac_id, int $a_obj_id, string $a_obj_type)
setDate(string $a_date)
setParentId(int $a_id)
const ROLE_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
$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.
setParentType(string $a_parent_type)
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)
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)