ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPCAMDPageList.php
Go to the documentation of this file.
1 <?php
2 
27 {
28  protected \ILIAS\Wiki\InternalDomainService $wiki_domain;
29  protected ilDBInterface $db;
30  protected ilLanguage $lng;
31 
32  public function init(): void
33  {
34  global $DIC;
35 
36  $this->db = $DIC->database();
37  $this->lng = $DIC->language();
38  $this->setType("amdpl");
39  $this->wiki_domain = $DIC->wiki()->internal()->domain();
40  }
41 
42  public static function getLangVars(): array
43  {
44  return array("ed_insert_amd_page_list", "pc_amdpl");
45  }
46 
47  public function create(
48  ilPageObject $a_pg_obj,
49  string $a_hier_id,
50  string $a_pc_id = ""
51  ): void {
52  $this->createPageContentNode();
53  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
54  $amdpl_node = $this->dom_doc->createElement("AMDPageList");
55  $amdpl_node = $this->getDomNode()->appendChild($amdpl_node);
56  }
57 
58  public function setData(
59  array $a_fields_data,
60  ?int $a_mode = null
61  ): void {
62  $ilDB = $this->db;
63 
64  $data_id = $this->getChildNode()->getAttribute("Id");
65  if ($data_id) {
66  $ilDB->manipulate("DELETE FROM pg_amd_page_list" .
67  " WHERE id = " . $ilDB->quote($data_id, "integer"));
68  } else {
69  $data_id = $ilDB->nextId("pg_amd_page_list");
70  $this->getChildNode()->setAttribute("Id", $data_id);
71  }
72 
73  $this->getChildNode()->setAttribute("Mode", (int) $a_mode);
74 
75  foreach ($a_fields_data as $field_id => $field_data) {
76  $fields = array(
77  "id" => array("integer", $data_id)
78  ,"field_id" => array("integer", $field_id)
79  ,"sdata" => array("text", serialize($field_data))
80  );
81  $ilDB->insert("pg_amd_page_list", $fields);
82  }
83  }
84 
85  public function getMode(): int
86  {
87  if (is_object($this->getChildNode())) {
88  return (int) $this->getChildNode()->getAttribute("Mode");
89  }
90  return 0;
91  }
92 
96  public function getFieldValues(
97  ?int $a_data_id = null
98  ): array {
99  $ilDB = $this->db;
100 
101  $res = array();
102 
103  if (!$a_data_id) {
104  if (is_object($this->getChildNode())) {
105  $a_data_id = $this->getChildNode()->getAttribute("Id");
106  }
107  }
108 
109  if ($a_data_id) {
110  $set = $ilDB->query("SELECT * FROM pg_amd_page_list" .
111  " WHERE id = " . $ilDB->quote($a_data_id, "integer"));
112  while ($row = $ilDB->fetchAssoc($set)) {
113  $res[$row["field_id"]] = unserialize((string) $row["sdata"], ["allowed_classes" => false]);
114  }
115  }
116  return $res;
117  }
118  public static function handleCopiedContent(
119  DOMDocument $a_domdoc,
120  bool $a_self_ass = true,
121  bool $a_clone_mobs = false,
122  int $new_parent_id = 0,
123  int $obj_copy_id = 0
124  ): void {
125  global $DIC;
126 
127  $ilDB = $DIC->database();
128  $old_id = 0;
129  $node = null;
130 
131  // #15688
132 
133  $xpath = new DOMXPath($a_domdoc);
134  $nodes = $xpath->query("//AMDPageList");
135  foreach ($nodes as $node) {
136  $old_id = $node->getAttribute("Id");
137  break;
138  }
139 
140  if ($old_id) {
141  $new_id = $ilDB->nextId("pg_amd_page_list");
142 
143  $set = $ilDB->query("SELECT * FROM pg_amd_page_list" .
144  " WHERE id = " . $ilDB->quote($old_id, "integer"));
145  while ($row = $ilDB->fetchAssoc($set)) {
146  $fields = array(
147  "id" => array("integer", $new_id)
148  ,"field_id" => array("integer", $row["field_id"])
149  ,"sdata" => array("text", $row["sdata"])
150  );
151  $ilDB->insert("pg_amd_page_list", $fields);
152  }
153 
154  $node->setAttribute("Id", $new_id);
155  }
156  }
157 
158 
159  //
160  // presentation
161  //
162 
163  protected function findPages(
164  int $a_list_id
165  ): array {
166  $ilDB = $this->db;
167 
168  $list_values = $this->getFieldValues($a_list_id);
169 
171  $wpage = $this->getPage();
172  $wiki_id = $wpage->getWikiId();
173  $wiki_ref_id = $wpage->getWikiRefId();
174 
175  $found_result = array();
176 
177  // only search in active fields
178  $found_ids = null;
179  $recs = ilAdvancedMDRecord::_getSelectedRecordsByObject("wiki", $wiki_ref_id, "wpg");
180  foreach ($recs as $record) {
181  foreach (ilAdvancedMDFieldDefinition::getInstancesByRecordId($record->getRecordId(), true) as $field) {
182  if (isset($list_values[$field->getFieldId()]) && $list_values[$field->getFieldId()] !== "") {
183  $field_form = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($field->getADTDefinition(), true, false);
184  $field->setSearchValueSerialized($field_form, $list_values[$field->getFieldId()]);
185  $found_pages = $field->searchSubObjects($field_form, $wiki_id, "wpg");
186  if (is_array($found_ids)) {
187  $found_ids = array_intersect($found_ids, $found_pages);
188  } else {
189  $found_ids = $found_pages;
190  }
191  }
192  }
193  }
194 
195  if (is_array($found_ids) && count($found_ids) > 0) {
196  $sql = "SELECT id,title FROM il_wiki_page" .
197  " WHERE " . $ilDB->in("id", $found_ids, "", "integer") .
198  " AND lang = " . $ilDB->quote($wpage->getLanguage(), "text") .
199  " ORDER BY title";
200  $set = $ilDB->query($sql);
201  while ($row = $ilDB->fetchAssoc($set)) {
202  $found_result[$row["id"]] = $row["title"];
203  }
204  }
205 
206  return $found_result;
207  }
208 
209  public function modifyPageContentPostXsl(
210  string $a_output,
211  string $a_mode,
212  bool $a_abstract_only = false
213  ): string {
214  if ($this->getPage()->getParentType() !== "wpg") {
215  return $a_output;
216  }
217  $end = 0;
218  $wiki_id = $this->getPage()->getWikiId();
219  $pm = $this->wiki_domain->page()->page($this->getPage()->getWikiRefId());
220 
221  $start = strpos($a_output, "[[[[[AMDPageList;");
222  if (is_int($start)) {
223  $end = strpos($a_output, "]]]]]", $start);
224  }
225  while ($end > 0) {
226  $parts = explode(";", substr($a_output, $start + 17, $end - $start - 17));
227 
228  $list_id = (int) $parts[0];
229  $list_mode = (count($parts) === 2)
230  ? (int) $parts[1]
231  : 0;
232 
233  $ltpl = new ilTemplate("tpl.wiki_amd_page_list.html", true, true, "components/ILIAS/Wiki");
234 
235  $pages = $this->findPages($list_id);
236  if (count($pages)) {
237  $ltpl->setCurrentBlock("page_bl");
238  foreach ($pages as $page_id => $page_title) {
239  // see ilWikiUtil::makeLink()
240  $frag = new stdClass();
241  $frag->mFragment = null;
242  $frag->mTextform = $page_title;
243 
244  $href = $pm->getPermaLink($page_id, $this->getPage()->getLanguage());
245  $ltpl->setVariable("PAGE", "<a href='" . $href . "'>" . $page_title . "</a>");
246  $ltpl->parseCurrentBlock();
247  }
248  } else {
249  $ltpl->touchBlock("no_hits_bl");
250  }
251 
252  $ltpl->setVariable("LIST_MODE", $list_mode ? "ol" : "ul");
253 
254  $a_output = substr($a_output, 0, $start) .
255  $ltpl->get() .
256  substr($a_output, $end + 5);
257 
258  $start = strpos($a_output, "[[[[[AMDPageList;", $start + 5);
259  $end = 0;
260  if (is_int($start)) {
261  $end = strpos($a_output, "]]]]]", $start);
262  }
263  }
264 
265  return $a_output;
266  }
267 
274  public static function migrateField(
275  int $a_field_id,
276  array $mapping
277  ): void {
278  global $DIC;
279 
280  $ilDB = $DIC->database();
281 
282  // this does only work for select and select multi
283 
284  $set = $ilDB->query("SELECT * FROM pg_amd_page_list" .
285  " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
286  while ($row = $ilDB->fetchAssoc($set)) {
287  $data = unserialize(unserialize((string) $row["sdata"], ["allowed_classes" => false]), ["allowed_classes" => false]);
288  if (!is_array($data)) {
289  continue;
290  }
291  $updated_data = $data;
292  foreach ($mapping as $old_option => $new_option) {
293  if (!in_array($old_option, $data)) {
294  continue;
295  }
296  $idx = array_search($old_option, $data);
297  if ($new_option !== '') {
298  $updated_data[$idx] = $new_option;
299  } else {
300  unset($updated_data[$idx]);
301  }
302  }
303 
304  $serialized_updated_data = serialize(empty($updated_data) ? '' : serialize($updated_data));
305 
306  $fields = array(
307  "sdata" => array("text", $serialized_updated_data)
308  );
309  $primary = array(
310  "id" => array("integer", $row["id"]),
311  "field_id" => array("integer", $row["field_id"])
312  );
313  $ilDB->update("pg_amd_page_list", $fields, $primary);
314  }
315  }
316 }
setType(string $a_type)
Set Type.
$res
Definition: ltiservices.php:66
create(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="")
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
modifyPageContentPostXsl(string $a_output, string $a_mode, bool $a_abstract_only=false)
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
Content object of ilPageObject (see ILIAS DTD).
static migrateField(int $a_field_id, array $mapping)
Migrate search/filter values on advmd change.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getLanguage()
ILIAS Wiki InternalDomainService $wiki_domain
insertContent(ilPageContent $a_cont_obj, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="", bool $remove_placeholder=true)
insert a content node before/after a sibling or as first child of a parent
setData(array $a_fields_data, ?int $a_mode=null)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
global $DIC
Definition: shib_login.php:22
getFieldValues(?int $a_data_id=null)
Get filter field values.
const IL_INSERT_AFTER
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element) ...
static handleCopiedContent(DOMDocument $a_domdoc, bool $a_self_ass=true, bool $a_clone_mobs=false, int $new_parent_id=0, int $obj_copy_id=0)