aboutsummaryrefslogtreecommitdiff
path: root/vendor/ilias/class.ilObjectXMLParser.php
blob: f5e636874a30dfd1aff505243f73a9fe3aaba4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
	+-----------------------------------------------------------------------------+
	| ILIAS open source                                                           |
	+-----------------------------------------------------------------------------+
	| Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
	|                                                                             |
	| This program is free software; you can redistribute it and/or               |
	| modify it under the terms of the GNU General Public License                 |
	| as published by the Free Software Foundation; either version 2              |
	| of the License, or (at your option) any later version.                      |
	|                                                                             |
	| This program is distributed in the hope that it will be useful,             |
	| but WITHOUT ANY WARRANTY; without even the implied warranty of              |
	| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
	| GNU General Public License for more details.                                |
	|                                                                             |
	| You should have received a copy of the GNU General Public License           |
	| along with this program; if not, write to the Free Software                 |
	| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
	+-----------------------------------------------------------------------------+
*/


/**
* Object XML Parser
*
* @author Stefan Meyer <meyer@leifos.com>
* @version $Id$
*
* @extends ilSaxParser
*/

//include_once './Services/Xml/classes/class.ilSaxParser.php';
//include_once('./webservice/soap/classes/class.ilObjectXMLException.php');

class ilObjectXMLParser extends ilSaxParser
{
	var $object_data = array();
	
	private $ref_id;
	private $parent_id;

	/**
	* Constructor
	*
	* @param	object		$a_content_object	must be of type ilObjContentObject
	*											ilObjTest or ilObjQuestionPool
	* @param	string		$a_xml_file			xml data
	* @param	string		$a_subdir			subdirectory in import directory
	* @access	public
	*/
	function __construct($a_xml_data = '', $throwException = false)
	{
		parent::__construct('', $throwException);
		$this->setXMLContent($a_xml_data);
	}

	function getObjectData()
	{
		return $this->object_data ? $this->object_data : array();
	}
	
	/**
	* parse xml file
	* 
	* @access	private
	* @throws ilSaxParserException
	* @throws ilObjectXMLException
	*/
	function parse($a_xml_parser,$a_fp = null)
	{
		parent::parse($a_xml_parser,$a_fp);
	}
	

	/**
	* set event handlers
	*
	* @param	resource	reference to the xml parser
	* @access	private
	*/
	function setHandlers($a_xml_parser)
	{
		xml_set_object($a_xml_parser,$this);
		xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
		xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
	}

	


	/**
	* handler for begin of element
	*
	* @param	resource	$a_xml_parser		xml parser
	* @param	string		$a_name				element name
	* @param	array		$a_attribs			element attributes array
	*/
	function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
	{
		switch($a_name)
		{
			case 'Objects':
				$this->curr_obj = -1;
				break;

			case 'Object':
				++$this->curr_obj;
				
				$this->addProperty__('type',$a_attribs['type']);
				$this->addProperty__('obj_id',is_numeric($a_attribs['obj_id'])?(int) $a_attribs["obj_id"] :  ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID));
				break;

			case 'Title':
				break;

			case 'Description':
				break;

			case 'Owner':
				break;

			case 'CreateDate':
				break;

			case 'LastUpdate':
				break;
				
			case 'ImportId':
				break;

			case 'References':
				$this->time_target = array();
				$this->ref_id = $a_attribs["ref_id"];
				$this->parent_id = $a_attribs['parent_id'];
				$this->operations = array();
				break;
				
			case 'TimeTarget':
				$this->time_target['timing_type'] = $a_attribs['type'];
				break;
			
			case 'Timing':
				$this->time_target['timing_visibility'] = $a_attribs['visibility'];
				if(isset($a_attribs['starting_time']))
				{
					$this->time_target['starting_time'] = $a_attribs['starting_time'];
				}
				if(isset($a_attribs['ending_time']))
				{
					$this->time_target['ending_time'] = $a_attribs['ending_time'];
				}
					
				if($a_attribs['ending_time'] < $a_attribs['starting_time'])
					throw new ilObjectXMLException('Starting time must be earlier than ending time.');
				break;
				
			case 'Suggestion':
				$this->time_target['changeable'] = $a_attribs['changeable'];
				
				
				if(isset($a_attribs['starting_time']))
				{
					$this->time_target['suggestion_start'] = $a_attribs['starting_time'];
				}
				if(isset($a_attribs['ending_time']))
				{
					$this->time_target['suggestion_end'] = $a_attribs['ending_time'];
				}
				if(isset($a_attribs['earliest_start']))
				{
					$this->time_target['earliest_start'] = $a_attribs['earliest_start'];
				}
				if(isset($a_attribs['latest_end']))
				{
					$this->time_target['latest_end'] = $a_attribs['latest_end'];
				}

				if($a_attribs['latest_end'] < $a_attribs['earliest_start'])
					throw new ilObjectXMLException('Earliest start time must be earlier than latest ending time.');
				if($a_attribs['ending_time'] < $a_attribs['starting_time'])
					throw new ilObjectXMLException('Starting time must be earlier than ending time.');
				break;
				
				////////////////////////////////////////////
				///////////// MODIFIED /////////////////////
				////////////////////////////////////////////
				case 'Operation':
				    $this->operations[] = $a_attribs['operations'];
				break;
			    ////////////////////////////////////////////
		}
	}

	/**
	* handler for end of element
	*
	* @param	resource	$a_xml_parser		xml parser
	* @param	string		$a_name				element name
	*/
	function handlerEndTag($a_xml_parser,$a_name)
	{
		switch($a_name)
		{
			case 'Objects':
				break;

			case 'Object':
				break;

			case 'Title':
				$this->addProperty__('title',trim($this->cdata));
				break;

			case 'Description':
				$this->addProperty__('description',trim($this->cdata));
				break;

			case 'Owner':
				$this->addProperty__('owner',trim($this->cdata));
				break;

			case 'CreateDate':
				$this->addProperty__('create_date',trim($this->cdata));
				break;

			case 'LastUpdate':
				$this->addProperty__('last_update',trim($this->cdata));
				break;
				
			case 'ImportId':
				$this->addProperty__('import_id',trim($this->cdata));
				break;

			case 'References':
				$this->addReference__($this->ref_id,$this->parent_id,$this->time_target);
				break;
				////////////////////////////////////////////
				///////////// MODIFIED /////////////////////
				////////////////////////////////////////////
			case 'Operation':
			    $this->addOperation__(trim($this->cdata));
			    break;
			    ////////////////////////////////////////////
		}

		$this->cdata = '';

		return;
	}

	/**
	* handler for character data
	*
	* @param	resource	$a_xml_parser		xml parser
	* @param	string		$a_data				character data
	*/
	function handlerCharacterData($a_xml_parser,$a_data)
	{
		if($a_data != "\n")
		{
			// Replace multiple tabs with one space
			$a_data = preg_replace("/\t+/"," ",$a_data);

			$this->cdata .= $a_data;
		}


	}

	// PRIVATE
	function addProperty__($a_name,$a_value)
	{
		$this->object_data[$this->curr_obj][$a_name] = $a_value;
	}

	/**
	 * @throws ilObjectXMLException
	 */
	function addReference__($a_ref_id,$a_parent_id,$a_time_target)
	{
		$reference['ref_id'] = $a_ref_id;
		$reference['parent_id'] = $a_parent_id;
		$reference['time_target'] = $a_time_target;

		if(isset($reference['time_target']['changeable']) and $reference['time_target']['changeable'])
		{
			if(!isset($reference['time_target']['earliest_start']) or !isset($reference['time_target']['latest_end']))
			{
				throw new ilObjectXMLException('Missing attributes: "earliest_start" and "latest_end" required for attribute "changeable"');
			}
			if(!isset($reference['time_target']['suggestion_start']) or !isset($reference['time_target']['suggestion_end']))
			{
				throw new ilObjectXMLException('Missing attributes: "starting_time" and "ending_time" required for attribute "changeable"');
			}
			if(($reference['time_target']['earliest_start'] < $reference['time_target']['suggestion_start']) or
				($reference['time_target']['earliest_start'] > $reference['time_target']['suggestion_end']))
			{
				throw new ilObjectXMLException('Invalid attributes: "earliest_start" must be within "starting_time" and "ending_time"');
			}
			if(($reference['time_target']['latest_end'] < $reference['time_target']['suggestion_start']) or
				($reference['time_target']['latest_end'] > $reference['time_target']['suggestion_end']))
			{
				throw new ilObjectXMLException('Invalid attributes: "latest_end" must be within "starting_time" and "ending_time"');
			}
		}
		
		$this->object_data[$this->curr_obj]['references'][] = $reference;
	}

	////////////////////////////////////////////
	///////////// MODIFIED /////////////////////
	////////////////////////////////////////////
	function addOperation__($a_value)
	{
	    if($a_value)
	        $this->object_data[$this->curr_obj]['references'][$this->reference_count]["operations"][] = $a_value;
	}
	////////////////////////////////////////////
}
?>