ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDRights.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 class ilMDRights extends ilMDBase
27 {
31  private const YES_NO_TRANSLATION = [
32  'yes' => 'Yes',
33  'no' => 'No'
34  ];
35 
36  private string $costs = '';
37  private string $caor = '';
38  private string $description = '';
40 
46  public static function lookupRightsByTypeAndCopyright(array $a_types, array $a_copyright): array
47  {
48  global $DIC;
49 
50  $db = $DIC->database();
51 
52  $query = 'SELECT rbac_id FROM il_meta_rights ' .
53  'WHERE ' . $db->in('obj_type', $a_types, false, 'text') . ' ' .
54  'AND ' . $db->in('description', $a_copyright, false, 'text');
55  $res = $db->query($query);
56 
57  ilLoggerFactory::getLogger('meta')->info($query);
58 
59  $obj_ids = [];
60  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
61  $obj_ids[] = (int) $row->rbac_id;
62  }
63  return $obj_ids;
64  }
65 
66  // SET/GET
67  public function setCosts(string $a_costs): bool
68  {
69  switch ($a_costs) {
70  case 'Yes':
71  case 'No':
72  $this->costs = $a_costs;
73  return true;
74 
75  default:
76  return false;
77  }
78  }
79 
80  public function getCosts(): string
81  {
82  return $this->costs;
83  }
84 
85  public function setCopyrightAndOtherRestrictions(string $a_caor): bool
86  {
87  switch ($a_caor) {
88  case 'Yes':
89  case 'No':
90  $this->caor = $a_caor;
91  return true;
92 
93  default:
94  return false;
95  }
96  }
97 
98  public function getCopyrightAndOtherRestrictions(): string
99  {
100  return $this->caor;
101  }
102 
103  public function setDescription(string $a_description): void
104  {
105  $this->description = $a_description;
106  }
107 
108  public function getDescription(): string
109  {
110  return $this->description;
111  }
112 
113  public function setDescriptionLanguage(ilMDLanguageItem $lng_obj): void
114  {
115  $this->description_language = $lng_obj;
116  }
117 
119  {
120  return is_object($this->description_language) ? $this->description_language : null;
121  }
122 
123  public function getDescriptionLanguageCode(): string
124  {
125  return is_object($this->description_language) ? $this->description_language->getLanguageCode() : '';
126  }
127 
128  public function save(): int
129  {
130  $fields = $this->__getFields();
131  $fields['meta_rights_id'] = array('integer', $next_id = $this->db->nextId('il_meta_rights'));
132 
133  if ($this->db->insert('il_meta_rights', $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_rights',
144  $this->__getFields(),
145  array("meta_rights_id" => array('integer', $this->getMetaId()))
146  );
147  }
148 
149  public function delete(): bool
150  {
151  if ($this->getMetaId()) {
152  $query = "DELETE FROM il_meta_rights " .
153  "WHERE meta_rights_id = " . $this->db->quote($this->getMetaId(), 'integer');
154 
155  $this->db->query($query);
156 
157  return true;
158  }
159  return false;
160  }
161 
165  public function __getFields(): array
166  {
170  $costs = (string) array_search(
171  $this->getCosts(),
172  self::YES_NO_TRANSLATION
173  );
174  $cpr_and_or = (string) array_search(
176  self::YES_NO_TRANSLATION
177  );
178 
179  return array(
180  'rbac_id' => array('integer', $this->getRBACId()),
181  'obj_id' => array('integer', $this->getObjId()),
182  'obj_type' => array('text', $this->getObjType()),
183  'costs' => array('text', $costs),
184  'cpr_and_or' => array('text', $cpr_and_or),
185  'description' => array('text', $this->getDescription()),
186  'description_language' => array('text', $this->getDescriptionLanguageCode())
187  );
188  }
189 
190  public function read(): bool
191  {
192  if ($this->getMetaId()) {
193  $query = "SELECT * FROM il_meta_rights " .
194  "WHERE meta_rights_id = " . $this->db->quote($this->getMetaId(), 'integer');
195 
196  $res = $this->db->query($query);
197  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
201  if (key_exists($row->costs ?? '', self::YES_NO_TRANSLATION)) {
202  $row->costs = self::YES_NO_TRANSLATION[$row->costs ?? ''];
203  }
204  if (key_exists($row->cpr_and_or ?? '', self::YES_NO_TRANSLATION)) {
205  $row->cpr_and_or = self::YES_NO_TRANSLATION[$row->cpr_and_or ?? ''];
206  }
207 
208  $this->setRBACId((int) $row->rbac_id);
209  $this->setObjId((int) $row->obj_id);
210  $this->setObjType($row->obj_type ?? '');
211  $this->setDescription($row->description ?? '');
212  $this->setDescriptionLanguage(new ilMDLanguageItem($row->description_language ?? ''));
213  $this->setCosts($row->costs ?? '');
214  $this->setCopyrightAndOtherRestrictions($row->cpr_and_or ?? '');
215  }
216  return true;
217  }
218  return false;
219  }
220 
221  public function toXML(ilXmlWriter $writer): void
222  {
223  $writer->xmlStartTag('Rights', array(
224  'Cost' => $this->getCosts() ?: 'No',
225  'CopyrightAndOtherRestrictions' => $this->getCopyrightAndOtherRestrictions() ?: 'No'
226  ));
227 
228  $writer->xmlElement(
229  'Description',
230  [
231  'Language' => $this->getDescriptionLanguageCode() ?: 'en'
232  ],
234  );
235  $writer->xmlEndTag('Rights');
236  }
237 
238  public function parseDescriptionFromImport(string $a_description): void
239  {
240  $entry_id = ilMDCopyrightSelectionEntry::lookupCopyrightFromImport($a_description);
241  if (!$entry_id) {
242  $this->setDescription($a_description);
243  } else {
245  }
246  }
247 
248  public static function _lookupDescription(int $a_rbac_id, int $a_obj_id): string
249  {
250  global $DIC;
251 
252  $ilDB = $DIC->database();
253 
254  $query = "SELECT description FROM il_meta_rights " .
255  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
256  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
257  $res = $ilDB->query($query);
258  $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
259 
260  if (isset($row) && isset($row->description)) {
261  return $row->description;
262  }
263  return '';
264  }
265 
266  // STATIC
267  public static function _getId(int $a_rbac_id, int $a_obj_id): int
268  {
269  global $DIC;
270 
271  $ilDB = $DIC->database();
272 
273  $query = "SELECT meta_rights_id FROM il_meta_rights " .
274  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
275  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
276 
277  $res = $ilDB->query($query);
278  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
279  return (int) $row->meta_rights_id;
280  }
281  return 0;
282  }
283 }
$res
Definition: ltiservices.php:69
static getLogger(string $a_component_id)
Get component logger.
static _lookupCopyrightForExport(string $a_cp_string)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupDescription(int $a_rbac_id, int $a_obj_id)
setRBACId(int $a_id)
string $description
ilDBInterface $db
static _getId(int $a_rbac_id, int $a_obj_id)
toXML(ilXmlWriter $writer)
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
const string $costs
setDescriptionLanguage(ilMDLanguageItem $lng_obj)
const YES_NO_TRANSLATION
Compatibility fix for legacy MD classes for new db tables.
parseDescriptionFromImport(string $a_description)
query(string $query)
Run a (read-only) Query on the database.
getCopyrightAndOtherRestrictions()
static lookupCopyrightFromImport(string $copyright_text)
in(string $field, array $values, bool $negate=false, string $type="")
ilMDLanguageItem $description_language
setCopyrightAndOtherRestrictions(string $a_caor)
setDescription(string $a_description)
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
setCosts(string $a_costs)
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)
static lookupRightsByTypeAndCopyright(array $a_types, array $a_copyright)
setObjType(string $a_type)