ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMDContribute.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /*
5  +-----------------------------------------------------------------------------+
6  | ILIAS open source |
7  +-----------------------------------------------------------------------------+
8  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
9  | |
10  | This program is free software; you can redistribute it and/or |
11  | modify it under the terms of the GNU General Public License |
12  | as published by the Free Software Foundation; either version 2 |
13  | of the License, or (at your option) any later version. |
14  | |
15  | This program is distributed in the hope that it will be useful, |
16  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18  | GNU General Public License for more details. |
19  | |
20  | You should have received a copy of the GNU General Public License |
21  | along with this program; if not, write to the Free Software |
22  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23  +-----------------------------------------------------------------------------+
24 */
25 
32 class ilMDContribute extends ilMDBase
33 {
34  // Subelements
35  private string $date = '';
36  private string $role = '';
37 
41  public function getEntityIds(): array
42  {
43  return ilMDEntity::_getIds($this->getRBACId(), $this->getObjId(), (int) $this->getMetaId(), 'meta_contribute');
44  }
45 
46  public function getEntity(int $a_entity_id): ?ilMDEntity
47  {
48  if (!$a_entity_id) {
49  return null;
50  }
51  $ent = new ilMDEntity();
52  $ent->setMetaId($a_entity_id);
53 
54  return $ent;
55  }
56 
57  public function addEntity(): ilMDEntity
58  {
59  $ent = new ilMDEntity($this->getRBACId(), $this->getObjId(), $this->getObjType());
60  $ent->setParentId($this->getMetaId());
61  $ent->setParentType('meta_contribute');
62 
63  return $ent;
64  }
65 
66  // SET/GET
67  public function setRole(string $a_role): bool
68  {
69  switch ($a_role) {
70  case 'Author':
71  case 'Publisher':
72  case 'Unknown':
73  case 'Initiator':
74  case 'Terminator':
75  case 'Editor':
76  case 'GraphicalDesigner':
77  case 'TechnicalImplementer':
78  case 'ContentProvider':
79  case 'TechnicalValidator':
80  case 'EducationalValidator':
81  case 'ScriptWriter':
82  case 'InstructionalDesigner':
83  case 'SubjectMatterExpert':
84  case 'Creator':
85  case 'Validator':
86  case 'PointOfContact':
87  $this->role = $a_role;
88  return true;
89 
90  default:
91  return false;
92  }
93  }
94 
95  public function getRole(): string
96  {
97  return $this->role;
98  }
99 
100  public function setDate(string $a_date): void
101  {
102  $this->date = $a_date;
103  }
104 
105  public function getDate(): string
106  {
107  return $this->date;
108  }
109 
110  public function save(): int
111  {
112  $fields = $this->__getFields();
113  $fields['meta_contribute_id'] = array('integer', $next_id = $this->db->nextId('il_meta_contribute'));
114 
115  if ($this->db->insert('il_meta_contribute', $fields)) {
116  $this->setMetaId($next_id);
117  return $this->getMetaId();
118  }
119  return 0;
120  }
121 
122  public function update(): bool
123  {
124  return $this->getMetaId() && $this->db->update(
125  'il_meta_contribute',
126  $this->__getFields(),
127  array("meta_contribute_id" => array('integer', $this->getMetaId()))
128  );
129  }
130 
131  public function delete(): bool
132  {
133  if ($this->getMetaId()) {
134  $query = "DELETE FROM il_meta_contribute " .
135  "WHERE meta_contribute_id = " . $this->db->quote($this->getMetaId(), 'integer');
136  $res = $this->db->manipulate($query);
137 
138  foreach ($this->getEntityIds() as $id) {
139  $ent = $this->getEntity($id);
140  $ent->delete();
141  }
142  return true;
143  }
144  return false;
145  }
146 
150  public function __getFields(): array
151  {
152  return array(
153  'rbac_id' => array('integer', $this->getRBACId()),
154  'obj_id' => array('integer', $this->getObjId()),
155  'obj_type' => array('text', $this->getObjType()),
156  'parent_type' => array('text', $this->getParentType()),
157  'parent_id' => array('integer', $this->getParentId()),
158  'role' => array('text', $this->getRole()),
159  'c_date' => array('text', $this->getDate())
160  );
161  }
162 
163  public function read(): bool
164  {
165  if ($this->getMetaId()) {
166  $query = "SELECT * FROM il_meta_contribute " .
167  "WHERE meta_contribute_id = " . $this->db->quote($this->getMetaId(), 'integer');
168 
169  $res = $this->db->query($query);
170  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
171  $this->setRBACId((int) $row->rbac_id);
172  $this->setObjId((int) $row->obj_id);
173  $this->setObjType((string) $row->obj_type);
174  $this->setParentId((int) $row->parent_id);
175  $this->setParentType((string) $row->parent_type);
176  $this->setRole((string) $row->role);
177  $this->setDate((string) $row->c_date);
178  }
179  }
180  return true;
181  }
182 
183  public function toXML(ilXmlWriter $writer): void
184  {
185  $writer->xmlStartTag('Contribute', array(
186  'Role' => $this->getRole() ?: 'Author'
187  ));
188 
189  // Entities
190  $entities = $this->getEntityIds();
191  foreach ($entities as $id) {
192  $ent = $this->getEntity($id);
193  $ent->toXML($writer);
194  }
195  if (!count($entities)) {
196  $ent = new ilMDEntity($this->getRBACId(), $this->getObjId());
197  $ent->toXML($writer);
198  }
199 
200  $writer->xmlElement('Date', null, $this->getDate());
201  $writer->xmlEndTag('Contribute');
202  }
203 
204  // STATIC
205 
209  public static function _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type): array
210  {
211  global $DIC;
212 
213  $ilDB = $DIC['ilDB'];
214 
215  $query = "SELECT meta_contribute_id FROM il_meta_contribute " .
216  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
217  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
218  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
219  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text');
220 
221  $res = $ilDB->query($query);
222  $ids = [];
223  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
224  $ids[] = (int) $row->meta_contribute_id;
225  }
226  return $ids;
227  }
228 
232  public static function _lookupAuthors(int $a_rbac_id, int $a_obj_id, string $a_obj_type): array
233  {
234  global $DIC;
235 
236  $ilDB = $DIC['ilDB'];
237 
238  // Ask for 'author' later to use indexes
239  $authors = [];
240  $query = "SELECT entity,ent.parent_type,role FROM il_meta_entity ent " .
241  "JOIN il_meta_contribute con ON ent.parent_id = con.meta_contribute_id " .
242  "WHERE ent.rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
243  "AND ent.obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
244  $res = $ilDB->query($query);
245  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
246  if ($row->role === 'Author' && $row->parent_type === 'meta_contribute') {
247  $authors[] = trim($row->entity);
248  }
249  }
250  return $authors;
251  }
252 }
$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)
$query
setParentId(int $a_id)
$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)