ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMDRights.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 class ilMDRights extends ilMDBase
28 {
32  private const YES_NO_TRANSLATION = [
33  'yes' => 'Yes',
34  'no' => 'No'
35  ];
36 
37  private string $costs = '';
38  private string $caor = '';
39  private string $description = '';
41 
47  public static function lookupRightsByTypeAndCopyright(array $a_types, array $a_copyright): array
48  {
49  global $DIC;
50 
51  $db = $DIC->database();
52 
53  $query = 'SELECT rbac_id FROM il_meta_rights ' .
54  'WHERE ' . $db->in('obj_type', $a_types, false, 'text') . ' ' .
55  'AND ' . $db->in('description', $a_copyright, false, 'text');
56  $res = $db->query($query);
57 
58  ilLoggerFactory::getLogger('meta')->info($query);
59 
60  $obj_ids = [];
61  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
62  $obj_ids[] = (int) $row->rbac_id;
63  }
64  return $obj_ids;
65  }
66 
67  // SET/GET
68  public function setCosts(string $a_costs): bool
69  {
70  switch ($a_costs) {
71  case 'Yes':
72  case 'No':
73  $this->costs = $a_costs;
74  return true;
75 
76  default:
77  return false;
78  }
79  }
80 
81  public function getCosts(): string
82  {
83  return $this->costs;
84  }
85 
86  public function setCopyrightAndOtherRestrictions(string $a_caor): bool
87  {
88  switch ($a_caor) {
89  case 'Yes':
90  case 'No':
91  $this->caor = $a_caor;
92  return true;
93 
94  default:
95  return false;
96  }
97  }
98 
99  public function getCopyrightAndOtherRestrictions(): string
100  {
101  return $this->caor;
102  }
103 
104  public function setDescription(string $a_description): void
105  {
106  $this->description = $a_description;
107  }
108 
109  public function getDescription(): string
110  {
111  return $this->description;
112  }
113 
114  public function setDescriptionLanguage(ilMDLanguageItem $lng_obj): void
115  {
116  $this->description_language = $lng_obj;
117  }
118 
120  {
121  return is_object($this->description_language) ? $this->description_language : null;
122  }
123 
124  public function getDescriptionLanguageCode(): string
125  {
126  return is_object($this->description_language) ? $this->description_language->getLanguageCode() : '';
127  }
128 
129  public function save(): int
130  {
131  $fields = $this->__getFields();
132  $fields['meta_rights_id'] = array('integer', $next_id = $this->db->nextId('il_meta_rights'));
133 
134  if ($this->db->insert('il_meta_rights', $fields)) {
135  $this->setMetaId($next_id);
136  return $this->getMetaId();
137  }
138  return 0;
139  }
140 
141  public function update(): bool
142  {
143  return $this->getMetaId() && $this->db->update(
144  'il_meta_rights',
145  $this->__getFields(),
146  array("meta_rights_id" => array('integer', $this->getMetaId()))
147  );
148  }
149 
150  public function delete(): bool
151  {
152  if ($this->getMetaId()) {
153  $query = "DELETE FROM il_meta_rights " .
154  "WHERE meta_rights_id = " . $this->db->quote($this->getMetaId(), 'integer');
155 
156  $this->db->query($query);
157 
158  return true;
159  }
160  return false;
161  }
162 
166  public function __getFields(): array
167  {
171  $costs = (string) array_search(
172  $this->getCosts(),
173  self::YES_NO_TRANSLATION
174  );
175  $cpr_and_or = (string) array_search(
177  self::YES_NO_TRANSLATION
178  );
179 
180  return array(
181  'rbac_id' => array('integer', $this->getRBACId()),
182  'obj_id' => array('integer', $this->getObjId()),
183  'obj_type' => array('text', $this->getObjType()),
184  'costs' => array('text', $costs),
185  'cpr_and_or' => array('text', $cpr_and_or),
186  'description' => array('text', $this->getDescription()),
187  'description_language' => array('text', $this->getDescriptionLanguageCode())
188  );
189  }
190 
191  public function read(): bool
192  {
193  if ($this->getMetaId()) {
194  $query = "SELECT * FROM il_meta_rights " .
195  "WHERE meta_rights_id = " . $this->db->quote($this->getMetaId(), 'integer');
196 
197  $res = $this->db->query($query);
198  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
202  if (key_exists($row->costs ?? '', self::YES_NO_TRANSLATION)) {
203  $row->costs = self::YES_NO_TRANSLATION[$row->costs ?? ''];
204  }
205  if (key_exists($row->cpr_and_or ?? '', self::YES_NO_TRANSLATION)) {
206  $row->cpr_and_or = self::YES_NO_TRANSLATION[$row->cpr_and_or ?? ''];
207  }
208 
209  $this->setRBACId((int) $row->rbac_id);
210  $this->setObjId((int) $row->obj_id);
211  $this->setObjType($row->obj_type ?? '');
212  $this->setDescription($row->description ?? '');
213  $this->setDescriptionLanguage(new ilMDLanguageItem($row->description_language ?? ''));
214  $this->setCosts($row->costs ?? '');
215  $this->setCopyrightAndOtherRestrictions($row->cpr_and_or ?? '');
216  }
217  return true;
218  }
219  return false;
220  }
221 
222  public function toXML(ilXmlWriter $writer): void
223  {
224  $writer->xmlStartTag('Rights', array(
225  'Cost' => $this->getCosts() ?: 'No',
226  'CopyrightAndOtherRestrictions' => $this->getCopyrightAndOtherRestrictions() ?: 'No'
227  ));
228 
229  $writer->xmlElement(
230  'Description',
231  [
232  'Language' => $this->getDescriptionLanguageCode() ?: 'en'
233  ],
235  );
236  $writer->xmlEndTag('Rights');
237  }
238 
239  public function parseDescriptionFromImport(string $a_description): void
240  {
241  $entry_id = ilMDCopyrightSelectionEntry::lookupCopyrightFromImport($a_description);
242  if (!$entry_id) {
243  $this->setDescription($a_description);
244  } else {
246  }
247  }
248 
249  public static function _lookupDescription(int $a_rbac_id, int $a_obj_id): string
250  {
251  global $DIC;
252 
253  $ilDB = $DIC->database();
254 
255  $query = "SELECT description FROM il_meta_rights " .
256  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
257  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
258  $res = $ilDB->query($query);
259  $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
260 
261  if (isset($row) && isset($row->description)) {
262  return $row->description;
263  }
264  return '';
265  }
266 
267  // STATIC
268  public static function _getId(int $a_rbac_id, int $a_obj_id): int
269  {
270  global $DIC;
271 
272  $ilDB = $DIC->database();
273 
274  $query = "SELECT meta_rights_id FROM il_meta_rights " .
275  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
276  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
277 
278  $res = $ilDB->query($query);
279  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
280  return (int) $row->meta_rights_id;
281  }
282  return 0;
283  }
284 }
$res
Definition: ltiservices.php:66
static getLogger(string $a_component_id)
Get component logger.
static _lookupCopyrightForExport(string $a_cp_string)
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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.
global $DIC
Definition: shib_login.php:22
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)