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