ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilAdvancedMDRecordParser Class Reference

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V. More...

+ Inheritance diagram for ilAdvancedMDRecordParser:
+ Collaboration diagram for ilAdvancedMDRecordParser:

Public Member Functions

 __construct (string $a_file)
 
 setMode (int $a_mode)
 
 getMode ()
 
 startParsing ()
 
 setHandlers ($a_xml_parser)
 set event handlers More...
 
 setContext (int $a_obj_id, string $a_obj_type, ?string $a_sub_type=null)
 
 getRecordMap ()
 
- Public Member Functions inherited from ilSaxParser
 __construct (?string $path_to_file='', ?bool $throw_exception=false)
 
 setXMLContent (string $a_xml_content)
 
 getXMLContent ()
 
 getInputType ()
 
 startParsing ()
 stores xml data in array More...
 
 createParser ()
 
 setHandlers ($a_xml_parser)
 
 parse ($a_xml_parser, $a_fp=null)
 

Data Fields

const MODE_UNDEFINED = 0
 
const MODE_UPDATE = 1
 
const MODE_INSERT = 2
 
const MODE_UPDATE_VALIDATION = 3
 
const MODE_INSERT_VALIDATION = 4
 
- Data Fields inherited from ilSaxParser
string $xml_file
 
bool $throw_exception = false
 

Protected Member Functions

 handlerBeginTag ($a_xml_parser, $a_name, $a_attribs)
 Handler for start tags protected. More...
 
 handlerEndTag ($a_xml_parser, $a_name)
 Handler for end tags protected. More...
 
 handlerCharacterData ($a_xml_parser, string $a_data)
 handler for character data More...
 
- Protected Member Functions inherited from ilSaxParser
 openXMLFile ()
 
 handleError (string $message)
 
 setThrowException (bool $throw_exception)
 

Protected Attributes

array $context
 
array $scopes = []
 
array $translations = []
 
string $translation_language = ''
 
array $field_translations = []
 
string $field_translation_language = ''
 
ilAdvancedMDRecord $current_record = null
 
ilAdvancedMDFieldDefinition $current_field = null
 
string $cdata = ''
 
ilLogger $log
 
- Protected Attributes inherited from ilSaxParser
ilLanguage $lng = null
 

Private Member Functions

 initRecordObject (string $a_id)
 
 initFieldObject (int $a_id, string $a_type)
 Init field definition object. More...
 
 getCurrentRecord ()
 
 getCurrentField ()
 
 extractRecordId (string $a_id_string)
 
 appendErrorMessage (string $a_msg)
 
 storeRecords ()
 

Private Attributes

int $mode = self::MODE_UNDEFINED
 
array $fields = []
 
bool $is_error = false
 
array $error_msg = []
 
string $field_value_id = ''
 
array $rec_map
 

Detailed Description

This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Learning e.V.

ILIAS is licensed with the GPL-3.0, see https://www.gnu.org/licenses/gpl-3.0.en.html You should have received a copy of said license along with the source code, too.

If this is not the case or you just want to try ILIAS, you'll find us at: https://www.ilias.de https://github.com/ILIAS-eLearning SAX based XML parser for record import files

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Todo:
remove update mode completely

Definition at line 27 of file class.ilAdvancedMDRecordParser.php.

Constructor & Destructor Documentation

◆ __construct()

ilAdvancedMDRecordParser::__construct ( string  $a_file)

Definition at line 60 of file class.ilAdvancedMDRecordParser.php.

References ILIAS\GlobalScreen\Provider\__construct(), and ilLoggerFactory\getLogger().

61  {
62  parent::__construct($a_file, true);
63  $this->log = ilLoggerFactory::getLogger('amet');
64  }
static getLogger(string $a_component_id)
Get component logger.
__construct(Container $dic, ilPlugin $plugin)
+ Here is the call graph for this function:

Member Function Documentation

◆ appendErrorMessage()

ilAdvancedMDRecordParser::appendErrorMessage ( string  $a_msg)
private

Definition at line 329 of file class.ilAdvancedMDRecordParser.php.

Referenced by handlerBeginTag().

329  : void
330  {
331  $this->is_error = true;
332  $this->error_msg[] = $a_msg;
333  }
+ Here is the caller graph for this function:

◆ extractRecordId()

ilAdvancedMDRecordParser::extractRecordId ( string  $a_id_string)
private

Definition at line 320 of file class.ilAdvancedMDRecordParser.php.

References ilAdvancedMDRecord\_lookupRecordIdByImportId().

Referenced by initRecordObject().

320  : int
321  {
322  // first lookup import id
323  if ($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string)) {
324  return $record_id;
325  }
326  return 0;
327  }
static _lookupRecordIdByImportId(string $a_ilias_id)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCurrentField()

ilAdvancedMDRecordParser::getCurrentField ( )
private

Definition at line 315 of file class.ilAdvancedMDRecordParser.php.

References $current_field.

Referenced by handlerBeginTag(), and handlerEndTag().

316  {
317  return $this->current_field;
318  }
ilAdvancedMDFieldDefinition $current_field
+ Here is the caller graph for this function:

◆ getCurrentRecord()

ilAdvancedMDRecordParser::getCurrentRecord ( )
private

Definition at line 310 of file class.ilAdvancedMDRecordParser.php.

References $current_record.

Referenced by handlerBeginTag(), handlerEndTag(), and storeRecords().

+ Here is the caller graph for this function:

◆ getMode()

ilAdvancedMDRecordParser::getMode ( )

Definition at line 71 of file class.ilAdvancedMDRecordParser.php.

References $mode.

Referenced by initFieldObject(), initRecordObject(), and storeRecords().

71  : int
72  {
73  return $this->mode;
74  }
+ Here is the caller graph for this function:

◆ getRecordMap()

ilAdvancedMDRecordParser::getRecordMap ( )

Definition at line 418 of file class.ilAdvancedMDRecordParser.php.

References $rec_map.

418  : array
419  {
420  return $this->rec_map;
421  }

◆ handlerBeginTag()

ilAdvancedMDRecordParser::handlerBeginTag (   $a_xml_parser,
  $a_name,
  $a_attribs 
)
protected

Handler for start tags protected.

Definition at line 100 of file class.ilAdvancedMDRecordParser.php.

References $scope, ilObject\_exists(), appendErrorMessage(), fields, getCurrentField(), getCurrentRecord(), IL_INST_ID, initFieldObject(), initRecordObject(), ILIAS\Repository\lng(), ilLanguage\lookupId(), and ilUtil\parseImportId().

100  : void
101  {
102  switch ($a_name) {
103  case 'AdvancedMetaDataRecords':
104  $this->is_error = false;
105  $this->error_msg = array();
106  // Nothing to do
107  break;
108 
109  case 'Scope':
110  $this->scopes = [];
111  break;
112 
113  case 'ScopeEntry':
114  $parsed_id = ilUtil::parseImportId($a_attribs['id']);
115  if (
116  $parsed_id['inst_id'] == IL_INST_ID &&
117  ilObject::_exists($parsed_id['id'], true, $parsed_id['type'])
118  ) {
120  $scope->setRefId((int) $parsed_id['id']);
121  $this->scopes[] = $scope;
122  }
123  break;
124 
125  case 'Record':
126  $this->fields = array();
127  $this->current_field = null;
128  $this->current_record = null;
129  if (!strlen($a_attribs['id']) or !isset($a_attribs['active'])) {
130  $this->appendErrorMessage('Missing XML attribute for element "Record".');
131  }
132  if (!$this->initRecordObject((string) $a_attribs['id'])) {
133  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
134  }
135  $this->getCurrentRecord()->setActive((bool) $a_attribs['active']);
136  $this->getCurrentRecord()->setImportId($a_attribs['id']);
137  $this->getCurrentRecord()->setAssignedObjectTypes(array());
138 
139  if (isset($a_attribs['defaultLanguage'])) {
140  $language = (string) $a_attribs['defaultLanguage'];
141  if (ilLanguage::lookupId($language)) {
142  $this->getCurrentRecord()->setDefaultLanguage($language);
143  }
144  } else {
145  $this->getCurrentRecord()->setDefaultLanguage($this->lng->getDefaultLanguage());
146  }
147  break;
148 
149  case 'RecordTranslations':
150  $this->translations = [];
151  $this->field_translations = [];
152  $this->getCurrentRecord()->setDefaultLanguage(
153  $a_attribs['defaultLanguage'] ?? $this->getCurrentRecord()->getDefaultLanguage()
154  );
155  break;
156 
157  case 'RecordTranslation':
158  $this->translation_language = $a_attribs['language'] ?? $this->lng->getDefaultLanguage();
159  break;
160 
161  case 'FieldTranslations':
162  $this->field_translations[$this->getCurrentField()->getImportId()] = [];
163  break;
164 
165  case 'FieldTranslation':
166  $this->field_translation_language = $a_attribs['language'] ?? $this->lng->getDefaultLanguage();
167  break;
168 
169  case 'Title':
170  break;
171 
172  case 'Field':
173  if (!strlen($a_attribs['id']) or !isset($a_attribs['searchable']) or !isset($a_attribs['fieldType'])) {
174  $this->appendErrorMessage('Missing XML attribute for element "Field".');
175  }
176  if (!$this->initFieldObject((int) $a_attribs['id'], (string) $a_attribs['fieldType'])) {
177  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
178  }
179  $this->getCurrentField()->setImportId($a_attribs['id']);
180  $this->getCurrentField()->setSearchable($a_attribs['searchable'] == 'Yes');
181  break;
182 
183  case 'FieldTitle':
184  case 'FieldDescription':
185  case 'FieldPosition':
186  case 'FieldValue':
187  $this->field_value_id = (string) ($a_attribs['id'] ?? "");
188  break;
189  }
190  }
Scope restrictions for advanced md records.
initFieldObject(int $a_id, string $a_type)
Init field definition object.
$scope
Definition: ltiregstart.php:53
const IL_INST_ID
Definition: constants.php:40
$errors fields
Definition: imgupload.php:67
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static lookupId(string $a_lang_key)
Lookup obj_id of language.
static parseImportId(string $a_import_id)
Parse an ilias import id Typically of type il_[IL_INST_ID]_[OBJ_TYPE]_[OBJ_ID] returns array( 'orig' ...
+ Here is the call graph for this function:

◆ handlerCharacterData()

ilAdvancedMDRecordParser::handlerCharacterData (   $a_xml_parser,
string  $a_data 
)
protected

handler for character data

Parameters
resource$a_xml_parserxml parser
string$a_datacharacter data

Definition at line 267 of file class.ilAdvancedMDRecordParser.php.

267  : void
268  {
269  if ($a_data != "\n") {
270  // Replace multiple tabs with one space
271  $a_data = preg_replace("/\t+/", " ", $a_data);
272 
273  $this->cdata .= $a_data;
274  }
275  }

◆ handlerEndTag()

ilAdvancedMDRecordParser::handlerEndTag (   $a_xml_parser,
  $a_name 
)
protected

Handler for end tags protected.

Definition at line 196 of file class.ilAdvancedMDRecordParser.php.

References $field_translation_language, $parts, $translation_language, getCurrentField(), getCurrentRecord(), and storeRecords().

196  : void
197  {
198  switch ($a_name) {
199  case 'AdvancedMetaDataRecords':
200  break;
201 
202  case 'Record':
203  $this->storeRecords();
204  break;
205 
206  case 'Scope':
207  $this->getCurrentRecord()->setScopes($this->scopes);
208  break;
209 
210  case 'Title':
211  $this->getCurrentRecord()->setTitle(trim($this->cdata));
212  break;
213 
214  case 'Description':
215  $this->getCurrentRecord()->setDescription(trim($this->cdata));
216  break;
217 
218  case 'ObjectType':
219  // #12980
220  $parts = explode(":", trim($this->cdata));
221  $this->getCurrentRecord()->appendAssignedObjectType($parts[0], $parts[1]);
222  break;
223 
224  case 'Field':
225  break;
226 
227  case 'RecordTranslationTitle':
228  $this->translations[$this->translation_language]['title'] = trim($this->cdata);
229  break;
230 
231  case 'RecordTranslationDescription':
232  $this->translations[$this->translation_language]['description'] = trim($this->cdata);
233  break;
234 
235  case 'FieldTranslationTitle':
236  $this->field_translations[$this->getCurrentField()->getImportId()][$this->field_translation_language]['title'] = trim($this->cdata);
237  break;
238 
239  case 'FieldTranslationDescription':
240  $this->field_translations[$this->getCurrentField()->getImportId()][$this->field_translation_language]['description'] = trim($this->cdata);
241  break;
242 
243  case 'FieldTitle':
244  $this->getCurrentField()->setTitle(trim($this->cdata));
245  break;
246 
247  case 'FieldDescription':
248  $this->getCurrentField()->setDescription(trim($this->cdata));
249  break;
250 
251  case 'FieldPosition':
252  $this->getCurrentField()->setPosition((int) trim($this->cdata));
253  break;
254 
255  case 'FieldValue':
256  $this->getCurrentField()->importXMLProperty($this->field_value_id, trim($this->cdata));
257  break;
258  }
259  $this->cdata = '';
260  }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
+ Here is the call graph for this function:

◆ initFieldObject()

ilAdvancedMDRecordParser::initFieldObject ( int  $a_id,
string  $a_type 
)
private

Init field definition object.

Definition at line 294 of file class.ilAdvancedMDRecordParser.php.

References $current_field, fields, ilAdvancedMDFieldDefinition\getInstanceByTypeString(), and getMode().

Referenced by handlerBeginTag().

295  {
296  switch ($this->getMode()) {
297  case self::MODE_INSERT:
298  case self::MODE_INSERT_VALIDATION:
299  $this->current_field = ilAdvancedMDFieldDefinition::getInstanceByTypeString($a_type);
300  $this->fields[] = $this->current_field;
301  return true;
302 
303  default:
304  throw new InvalidArgumentException(
305  'Current parsing mode is not supported. Mode: ' . $this->getMode()
306  );
307  }
308  }
ilAdvancedMDFieldDefinition $current_field
$errors fields
Definition: imgupload.php:67
static getInstanceByTypeString(string $a_type)
Get instance by type string (used by import)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initRecordObject()

ilAdvancedMDRecordParser::initRecordObject ( string  $a_id)
private

Definition at line 277 of file class.ilAdvancedMDRecordParser.php.

References ilAdvancedMDRecord\_getInstanceByRecordId(), extractRecordId(), and getMode().

Referenced by handlerBeginTag().

277  : bool
278  {
279  switch ($this->getMode()) {
280  case self::MODE_INSERT:
281  case self::MODE_INSERT_VALIDATION:
282  $this->current_record = new ilAdvancedMDRecord(0);
283  return true;
284 
285  default:
286  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
287  return true;
288  }
289  }
static _getInstanceByRecordId(int $a_record_id)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setContext()

ilAdvancedMDRecordParser::setContext ( int  $a_obj_id,
string  $a_obj_type,
?string  $a_sub_type = null 
)

Definition at line 405 of file class.ilAdvancedMDRecordParser.php.

Referenced by ilAdvancedMDSettingsGUI\importRecord().

405  : void
406  {
407  if (!$a_sub_type) {
408  $a_sub_type = "-";
409  }
410 
411  $this->context = array(
412  "obj_id" => $a_obj_id,
413  "obj_type" => $a_obj_type,
414  "sub_type" => $a_sub_type
415  );
416  }
+ Here is the caller graph for this function:

◆ setHandlers()

ilAdvancedMDRecordParser::setHandlers (   $a_xml_parser)

set event handlers

Parameters
resourcereference to the xml parser private

Definition at line 89 of file class.ilAdvancedMDRecordParser.php.

89  : void
90  {
91  xml_set_object($a_xml_parser, $this);
92  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
93  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
94  }

◆ setMode()

ilAdvancedMDRecordParser::setMode ( int  $a_mode)

Definition at line 66 of file class.ilAdvancedMDRecordParser.php.

66  : void
67  {
68  $this->mode = $a_mode;
69  }

◆ startParsing()

ilAdvancedMDRecordParser::startParsing ( )

Definition at line 76 of file class.ilAdvancedMDRecordParser.php.

76  : void
77  {
78  parent::startParsing();
79  if ($this->is_error) {
80  throw new ilSaxParserException(implode('<br/>', $this->error_msg));
81  }
82  }
SaxParserException thrown by ilSaxParser if property throwException is set.

◆ storeRecords()

ilAdvancedMDRecordParser::storeRecords ( )
private

Definition at line 335 of file class.ilAdvancedMDRecordParser.php.

References fields, getCurrentRecord(), ilAdvancedMDRecordTranslations\getInstanceByRecordId(), getMode(), and ILIAS\LTI\ToolProvider\getRecordId().

Referenced by handlerEndTag().

335  : void
336  {
337  switch ($this->getMode()) {
338  case self::MODE_INSERT_VALIDATION:
339  case self::MODE_UPDATE_VALIDATION:
340  return;
341 
342  case self::MODE_INSERT:
343  // set local context
344  if (isset($this->context) && is_array($this->context)) {
345  $this->getCurrentRecord()->setParentObject($this->context["obj_id"]);
346  $this->getCurrentRecord()->setAssignedObjectTypes(array(
347  array(
348  "obj_type" => $this->context["obj_type"],
349  "sub_type" => $this->context["sub_type"],
350  "optional" => false
351  )
352  ));
353  }
354 
355  $this->getCurrentRecord()->save();
356  break;
357  }
358  foreach ($this->fields as $field) {
359  $field->setRecordId($this->getCurrentRecord()->getRecordId());
360  switch ($this->getMode()) {
361  case self::MODE_INSERT:
362  $field->save();
363  foreach ($this->field_translations as $field_id => $field_info) {
364  if (strcmp($field_id, $field->getImportId()) !== 0) {
365  continue;
366  }
367  foreach ((array) $field_info as $language => $field_translation) {
368  $translation = new ilAdvancedMDFieldTranslation(
369  (int) $field->getFieldId(),
370  (string) $field_translation['title'],
371  (string) $field_translation['description'],
372  (string) $language
373  );
374  $translation->insert();
375  }
376  }
377 
378  // see getRecordMap()
379  $this->log->debug("add to record map, rec id: " . $this->getCurrentRecord()->getRecordId() .
380  ", import id: " . $field->getImportId() . ", field id:" . $field->getFieldId());
381  $this->rec_map[$this->getCurrentRecord()->getRecordId()][$field->getImportId()] = $field->getFieldId();
382  break;
383  }
384  }
386  $translations->addTranslationEntry($this->getCurrentRecord()->getDefaultLanguage(), true);
387  $translations->updateTranslations(
388  $this->getCurrentRecord()->getDefaultLanguage(),
389  $this->getCurrentRecord()->getTitle(),
390  $this->getCurrentRecord()->getDescription()
391  );
392 
393  foreach ($this->translations as $lang_key => $translation_info) {
394  if (!$translations->isConfigured($lang_key)) {
395  $translations->addTranslationEntry($lang_key);
396  }
397  $translations->updateTranslations(
398  $lang_key,
399  (string) $translation_info['title'],
400  (string) $translation_info['description']
401  );
402  }
403  }
getRecordId()
Get the system record ID.
Definition: System.php:214
$errors fields
Definition: imgupload.php:67
Class ilAdvancedMDFieldTranslation.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $cdata

string ilAdvancedMDRecordParser::$cdata = ''
protected

Definition at line 56 of file class.ilAdvancedMDRecordParser.php.

◆ $context

array ilAdvancedMDRecordParser::$context
protected

Definition at line 44 of file class.ilAdvancedMDRecordParser.php.

◆ $current_field

ilAdvancedMDFieldDefinition ilAdvancedMDRecordParser::$current_field = null
protected

Definition at line 55 of file class.ilAdvancedMDRecordParser.php.

Referenced by getCurrentField(), and initFieldObject().

◆ $current_record

ilAdvancedMDRecord ilAdvancedMDRecordParser::$current_record = null
protected

Definition at line 54 of file class.ilAdvancedMDRecordParser.php.

Referenced by getCurrentRecord().

◆ $error_msg

array ilAdvancedMDRecordParser::$error_msg = []
private

Definition at line 41 of file class.ilAdvancedMDRecordParser.php.

◆ $field_translation_language

string ilAdvancedMDRecordParser::$field_translation_language = ''
protected

Definition at line 51 of file class.ilAdvancedMDRecordParser.php.

Referenced by handlerEndTag().

◆ $field_translations

array ilAdvancedMDRecordParser::$field_translations = []
protected

Definition at line 50 of file class.ilAdvancedMDRecordParser.php.

◆ $field_value_id

string ilAdvancedMDRecordParser::$field_value_id = ''
private

Definition at line 42 of file class.ilAdvancedMDRecordParser.php.

◆ $fields

array ilAdvancedMDRecordParser::$fields = []
private

Definition at line 38 of file class.ilAdvancedMDRecordParser.php.

◆ $is_error

bool ilAdvancedMDRecordParser::$is_error = false
private

Definition at line 40 of file class.ilAdvancedMDRecordParser.php.

◆ $log

ilLogger ilAdvancedMDRecordParser::$log
protected

Definition at line 58 of file class.ilAdvancedMDRecordParser.php.

◆ $mode

int ilAdvancedMDRecordParser::$mode = self::MODE_UNDEFINED
private

Definition at line 36 of file class.ilAdvancedMDRecordParser.php.

Referenced by getMode().

◆ $rec_map

array ilAdvancedMDRecordParser::$rec_map
private

Definition at line 53 of file class.ilAdvancedMDRecordParser.php.

Referenced by getRecordMap().

◆ $scopes

array ilAdvancedMDRecordParser::$scopes = []
protected

Definition at line 45 of file class.ilAdvancedMDRecordParser.php.

◆ $translation_language

string ilAdvancedMDRecordParser::$translation_language = ''
protected

Definition at line 48 of file class.ilAdvancedMDRecordParser.php.

Referenced by handlerEndTag().

◆ $translations

array ilAdvancedMDRecordParser::$translations = []
protected

Definition at line 47 of file class.ilAdvancedMDRecordParser.php.

◆ MODE_INSERT

const ilAdvancedMDRecordParser::MODE_INSERT = 2

◆ MODE_INSERT_VALIDATION

const ilAdvancedMDRecordParser::MODE_INSERT_VALIDATION = 4

◆ MODE_UNDEFINED

const ilAdvancedMDRecordParser::MODE_UNDEFINED = 0

Definition at line 29 of file class.ilAdvancedMDRecordParser.php.

◆ MODE_UPDATE

const ilAdvancedMDRecordParser::MODE_UPDATE = 1

Definition at line 30 of file class.ilAdvancedMDRecordParser.php.

◆ MODE_UPDATE_VALIDATION

const ilAdvancedMDRecordParser::MODE_UPDATE_VALIDATION = 3

Definition at line 33 of file class.ilAdvancedMDRecordParser.php.


The documentation for this class was generated from the following file: