ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGroupXMLParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/Xml/classes/class.ilSaxParser.php");
25 require_once('./Services/User/classes/class.ilObjUser.php');
26 include_once('./Services/Calendar/classes/class.ilDateTime.php');
27 include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
28 
29 
40 {
41  public static $CREATE = 1;
42  public static $UPDATE = 2;
43 
44  private $participants = null;
45 
48 
49  var $parent;
50  var $counter;
51 
52  var $mode;
53  var $grp;
54 
63  function ilGroupXMLParser($a_xml, $a_parent_id)
64  {
65  define('EXPORT_VERSION',2);
66 
67  parent::ilSaxParser(null);
68 
69  $this->mode = ilGroupXMLParser::$CREATE;
70  $this->grp = null;
71 
72  $this->setXMLContent($a_xml);
73 
74  // SET MEMBER VARIABLES
75  $this->__pushParentId($a_parent_id);
76 
77 
78  }
79 
80  function __pushParentId($a_id)
81  {
82  $this->parent[] = $a_id;
83  }
84  function __popParentId()
85  {
86  array_pop($this->parent);
87 
88  return true;
89  }
90  function __getParentId()
91  {
92  return $this->parent[count($this->parent) - 1];
93  }
94 
100  function setHandlers($a_xml_parser)
101  {
102  xml_set_object($a_xml_parser,$this);
103  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
104  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
105  }
106 
110  function startParsing()
111  {
113 
114  if ($this->mode == ilGroupXMLParser::$CREATE)
115  {
116  return is_object($this->group_obj) ? $this->group_obj->getRefId() : false;
117  }
118  else
119  {
120  return is_object($this->group_obj) ? $this->group_obj->update() : false;
121  }
122  }
123 
124 
128  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
129  {
130  global $ilErr;
131 
132  switch($a_name)
133  {
134  // GROUP DATA
135  case "group":
136  #if($a_attribs["exportVersion"] < EXPORT_VERSION)
137  #{
138  # $ilErr->raiseError("!!! This export Version isn't supported, update your ILIAS 2 installation"
139  # ,$ilErr->WARNING);
140  #}
141  // DEFAULT
142  $this->group_data["admin"] = array();
143  $this->group_data["member"] = array();
144 
145  $this->group_data["type"] = $a_attribs["type"];
146  $this->group_data["id"] = $a_attribs["id"];
147 
148  break;
149 
150  case 'title':
151  break;
152 
153  case "owner":
154  $this->group_data["owner"] = $a_attribs["id"];
155  break;
156 
157  case 'registration':
158  $this->group_data['registration_type'] = $a_attribs['type'];
159  $this->group_data['waiting_list_enabled'] = $a_attribs['waitingList'] == 'Yes' ? true : false;
160  break;
161 
162  case 'maxMembers':
163  $this->group_data['max_members_enabled'] = $a_attribs['enabled'] == 'Yes' ? true : false;
164  break;
165 
166  case "admin":
167  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
168  {
169  $this->group_data["admin"]["attach"][] = $a_attribs["id"];
170  }
171  elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
172  {
173  $this->group_data["admin"]["detach"][] = $a_attribs["id"];
174  }
175 
176  if(isset($a_attribs['notification']) and $a_attribs['notification'] == 'Yes')
177  {
178  $this->group_data['notifications'][] = $a_attribs['id'];
179  }
180 
181  break;
182 
183  case "member":
184  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
185  {
186  $GLOBALS['ilLog']->write(__METHOD__.': new member with id '.$a_attribs['id']);
187  $this->group_data["member"]["attach"][] = $a_attribs["id"];
188  } elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
189  {
190  $GLOBALS['ilLog']->write(__METHOD__.': deprecated member with id '.$a_attribs['id']);
191  $this->group_data["member"]["detach"][] = $a_attribs["id"];
192  }
193 
194  break;
195 
196  case "folder":
197  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
198  $this->__save();
199  break;
200 
201  case "file":
202  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
203  $this->__save();
204 
205  $this->file["fileName"] = $a_attribs["fileName"];
206  $this->file["id"] = $a_attribs["id"];
207 
208  // SAVE IT
209  $this->__saveFile();
210  break;
211  }
212  }
213 
214 
215  function handlerEndTag($a_xml_parser, $a_name)
216  {
217  switch($a_name)
218  {
219  case "title":
220  $this->group_data["title"] = trim($this->cdata);
221  break;
222 
223  case "description":
224  $this->group_data["description"] = trim($this->cdata);
225  break;
226 
227  case 'information':
228  $this->group_data['information'] = trim($this->cdata);
229  break;
230 
231  case 'password':
232  $this->group_data['password'] = trim($this->cdata);
233  break;
234 
235  case 'maxMembers':
236  $this->group_data['max_members'] = trim($this->cdata);
237  break;
238 
239  case 'expiration':
240  $this->group_data['expiration_end'] = trim($this->cdata);
241  break;
242 
243  case 'start':
244  $this->group_data['expiration_start'] = trim($this->cdata);
245  break;
246 
247  case 'end':
248  $this->group_data['expiration_end'] = trim($this->cdata);
249  break;
250 
251  case "folder":
252  $this->__popParentId();
253  break;
254 
255  case "folderTitle":
256  $this->folder = trim($this->cdata);
257  $this->__saveFolder();
258  break;
259 
260  case "group":
261  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
262  $this->__save();
263  break;
264  }
265  $this->cdata = '';
266  }
267 
268 
272  function handlerCharacterData($a_xml_parser, $a_data)
273  {
274  // i don't know why this is necessary, but
275  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
276  // in character data, but we don't want that, because it's the
277  // way we mask user html in our content, so we convert back...
278  $a_data = str_replace("<","&lt;",$a_data);
279  $a_data = str_replace(">","&gt;",$a_data);
280 
281  if(!empty($a_data))
282  {
283  $this->cdata .= $a_data;
284  }
285  }
286 
287  // PRIVATE
288  function __save()
289  {
290  if($this->group_imported)
291  {
292  return true;
293  }
294 
295  $this->__initGroupObject();
296 
297  $this->group_obj->setImportId($this->group_data["id"]);
298  $this->group_obj->setTitle($this->group_data["title"]);
299  $this->group_obj->setDescription($this->group_data["description"]);
300 
301  $ownerChanged = false;
302  if (isset($this->group_data["owner"]))
303  {
304  $owner = $this->group_data["owner"];
305  if (!is_numeric($owner))
306  {
307  $owner = ilUtil::__extractId ($owner, IL_INST_ID);
308  }
309  if (is_numeric($owner) && $owner > 0)
310  {
311  $this->group_obj->setOwner($owner);
312  $ownerChanged = true;
313  }
314  }
315 
319  if ($this->mode == ilGroupXMLParser::$CREATE)
320  {
321  $this->group_obj->create();
322  $this->group_obj->createReference();
323  $this->group_obj->putInTree($this->__getParentId());
324  $this->group_obj->setPermissions($this->__getParentId());
325  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? GRP_TYPE_PUBLIC : GRP_TYPE_CLOSED);
326  }
327  else
328  {
329  switch($this->group_data['type'])
330  {
331  case 'open':
332  $grp_status = GRP_TYPE_PUBLIC;
333  break;
334 
335  case 'closed':
336  $grp_status = GRP_TYPE_CLOSED;
337  break;
338 
339  }
340 
341  $this->group_obj->updateOwner();
342  if($this->group_obj->getGroupStatus() != $grp_status)
343  {
344  $this->group_obj->setGroupType($grp_status);
345  $this->group_obj->updateGroupType();
346  }
347  }
348 
349  // SET GROUP SPECIFIC DATA
350  switch($this->group_data['registration_type'])
351  {
352  case 'direct':
353  case 'enabled':
354  $flag = GRP_REGISTRATION_DIRECT;
355  break;
356 
357  case 'disabled':
359  break;
360 
361  case 'confirmation':
362  $flag = GRP_REGISTRATION_REQUEST;
363  break;
364 
365  case 'password':
367  break;
368 
369  default:
370  $flag = GRP_REGISTRATION_DIRECT;
371  }
372  $this->group_obj->setRegistrationType($flag);
373 
374  $end = new ilDateTime(time(),IL_CAL_UNIX);
375  if($this->group_data['expiration_end'])
376  {
377  $end = new ilDateTime($this->group_data['expiration_end'],IL_CAL_UNIX);
378  }
379 
380  $start = clone $end;
381  if($this->group_data['expiration_start'])
382  {
383  $start = new ilDateTime($this->group_data['expiration_start'],IL_CAL_UNIX);
384  }
385 
386  $this->group_obj->setRegistrationStart($start);
387  $this->group_obj->setRegistrationEnd($end);
388  $this->group_obj->setPassword($this->group_data['password']);
389  $this->group_obj->enableUnlimitedRegistration(!isset($this->group_data['expiration_end']));
390  $this->group_obj->enableMembershipLimitation($this->group_data['max_members_enabled']);
391  $this->group_obj->setMaxMembers($this->group_data['max_members'] ? $this->group_data['max_members'] : 0);
392  $this->group_obj->enableWaitingList($this->group_data['waiting_list_enabled']);
393 
394 
395  if ($this->mode == ilGroupXMLParser::$CREATE)
396  {
397  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? 0 : 1);
398  }
399 
400  $this->group_obj->update();
401 
402  // ASSIGN ADMINS/MEMBERS
403  $this->__assignMembers();
404 
405  $this->__pushParentId($this->group_obj->getRefId());
406 
407 
408  $this->group_imported = true;
409 
410  return true;
411  }
412 
413  function __saveFolder()
414  {
415  $this->__initFolderObject();
416 
417  $this->folder_obj->setTitle($this->folder);
418  $this->folder_obj->create();
419  $this->folder_obj->createReference();
420  $this->folder_obj->putInTree($this->__getParentId());
421 
422  $this->__pushParentId($this->folder_obj->getRefId());
423 
424  $this->__destroyFolderObject();
425 
426  return true;
427  }
428 
429  function __saveFile()
430  {
431  $this->__initFileObject();
432 
433  $this->file_obj->setType("file");
434  $this->file_obj->setTitle($this->file["fileName"]);
435  $this->file_obj->setFileName($this->file["fileName"]);
436  $this->file_obj->create();
437  $this->file_obj->createReference();
438  $this->file_obj->putInTree($this->__getParentId());
439  $this->file_obj->setPermissions($this->__getParentId());
440 
441  // COPY FILE
442  $this->file_obj->createDirectory();
443 
444  $this->__initImportFileObject();
445 
446  if($this->import_file_obj->findObjectFile($this->file["id"]))
447  {
448  $this->file_obj->copy($this->import_file_obj->getObjectFile(),$this->file["fileName"]);
449  }
450 
451  unset($this->file_obj);
452  unset($this->import_file_obj);
453 
454  return true;
455  }
456 
457  function __assignMembers()
458  {
459  global $ilias,$ilUser;
460 
461  $this->participants = new ilGroupParticipants($this->group_obj->getId());
462  $this->participants->add($ilUser->getId(),IL_GRP_ADMIN);
463  $this->participants->updateNotification($ilUser->getId(),true);
464 
465  // attach ADMINs
466  if (count($this->group_data["admin"]["attach"]))
467  {
468  foreach($this->group_data["admin"]["attach"] as $user)
469  {
470  if($id_data = $this->__parseId($user))
471  {
472  if($id_data['local'] or $id_data['imported'])
473  {
474  $this->participants->add($id_data['usr_id'],IL_GRP_ADMIN);
475  if(in_array($user,(array) $this->group_data['notifications']))
476  {
477  $this->participants->updateNotification($id_data['usr_id'],true);
478  }
479  }
480  }
481  }
482  }
483  // detach ADMINs
484  if (count($this->group_data["admin"]["detach"]))
485  {
486  foreach($this->group_data["admin"]["detach"] as $user)
487  {
488  if($id_data = $this->__parseId($user))
489  {
490  if($id_data['local'] or $id_data['imported'])
491  {
492  if($this->participants->isAssigned($id_data['usr_id']))
493  {
494  $this->participants->delete($id_data['usr_id']);
495  }
496  }
497  }
498  }
499  }
500  // MEMBER
501  if (count($this->group_data["member"]["attach"]))
502  {
503  foreach($this->group_data["member"]["attach"] as $user)
504  {
505  if($id_data = $this->__parseId($user))
506  {
507  if($id_data['local'] or $id_data['imported'])
508  {
509  $this->participants->add($id_data['usr_id'],IL_GRP_MEMBER);
510  }
511  }
512  }
513  }
514 
515  if (count($this->group_data["member"]["detach"]))
516  {
517  foreach($this->group_data["member"]["detach"] as $user)
518  {
519  if($id_data = $this->__parseId($user))
520  {
521  if($id_data['local'] or $id_data['imported'])
522  {
523  if($this->participants->isAssigned($id_data['usr_id']))
524  {
525  $this->participants->delete($id_data['usr_id']);
526  }
527  }
528  }
529  }
530  }
531  return true;
532  }
533 
534  function __initGroupObject()
535  {
536  include_once "./Modules/Group/classes/class.ilObjGroup.php";
537 
538  if ($this->mode == ilGroupXMLParser::$CREATE)
539  {
540  $this->group_obj =& new ilObjGroup();
541  } elseif ($this->mode == ilGroupXMLParser::$UPDATE) {
542  $this->group_obj = $this->grp;
543  }
544 
545  return true;
546  }
547 
549  {
550  include_once "./Modules/Folder/classes/class.ilObjFolder.php";
551 
552  $this->folder_obj =& new ilObjFolder();
553 
554  return true;
555  }
556 
558  {
559  include_once "./Modules/Group/classes/class.ilFileDataImportGroup.php";
560 
561  $this->import_file_obj =& new ilFileDataImportGroup();
562 
563  return true;
564  }
565 
566  function __initFileObject()
567  {
568  include_once "./Modules/File/classes/class.ilObjFile.php";
569 
570  $this->file_obj =& new ilObjFile();
571 
572  return true;
573  }
574 
576  {
577  unset($this->folder_obj);
578  }
579 
580  function __parseId($a_id)
581  {
582  global $ilias;
583 
584  $fields = explode('_',$a_id);
585 
586  if(!is_array($fields) or
587  $fields[0] != 'il' or
588  !is_numeric($fields[1]) or
589  $fields[2] != 'usr' or
590  !is_numeric($fields[3]))
591  {
592  return false;
593  }
594  if($id = ilObjUser::_getImportedUserId($a_id))
595  {
596  return array('imported' => true,
597  'local' => false,
598  'usr_id' => $id);
599  }
600  if(($fields[1] == $ilias->getSetting('inst_id',0)) and ($user = ilObjUser::_lookupName($fields[3])))
601  {
602  if(strlen($user['login']))
603  {
604  return array('imported' => false,
605  'local' => true,
606  'usr_id' => $fields[3]);
607  }
608  }
609  $GLOBALS['ilLog']->write(__METHOD__.' Parsing id failed: '.$a_id);
610  return false;
611  }
612 
613 
614  public function setMode($mode) {
615  $this->mode = $mode;
616  }
617 
618  public function setGroup(& $grp) {
619  $this->grp = $grp;
620  }
621 }
622 ?>