1<?php
2
3/*
4    +-----------------------------------------------------------------------------+
5    | ILIAS open source                                                           |
6    +-----------------------------------------------------------------------------+
7    | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
8    |                                                                             |
9    | This program is free software; you can redistribute it and/or               |
10    | modify it under the terms of the GNU General Public License                 |
11    | as published by the Free Software Foundation; either version 2              |
12    | of the License, or (at your option) any later version.                      |
13    |                                                                             |
14    | This program is distributed in the hope that it will be useful,             |
15    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
16    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
17    | GNU General Public License for more details.                                |
18    |                                                                             |
19    | You should have received a copy of the GNU General Public License           |
20    | along with this program; if not, write to the Free Software                 |
21    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
22    +-----------------------------------------------------------------------------+
23*/
24
25include_once "./Services/Xml/classes/class.ilXmlWriter.php";
26
27/**
28* XML writer class
29*
30* Class to simplify manual writing of xml documents.
31* It only supports writing xml sequentially, because the xml document
32* is saved in a string with no additional structure information.
33* The author is responsible for well-formedness and validity
34* of the xml document.
35*
36* @author Stefan Meyer <meyer@leifos.com>
37* @version $Id$
38*/
39class ilCourseXMLWriter extends ilXmlWriter
40{
41    const MODE_SOAP = 1;
42    const MODE_EXPORT = 2;
43
44    private $mode = self::MODE_SOAP;
45
46
47    private $ilias;
48
49    /**
50     * @var string
51     */
52    private $xml;
53
54    /**
55     * @var \ilObjCourse
56     */
57    private $course_obj;
58
59    private $attach_users = true;
60
61
62    /**
63     * constructor
64     *
65     * @param ilObject $course_obj
66     *
67     * @access	public
68     */
69    public function __construct($course_obj)
70    {
71        global $DIC;
72
73        $ilias = $DIC['ilias'];
74
75        parent::__construct();
76
77        $this->EXPORT_VERSION = "2";
78
79        $this->ilias = $ilias;
80        $this->course_obj = $course_obj;
81    }
82
83    public function setMode($a_mode)
84    {
85        $this->mode = $a_mode;
86    }
87
88    public function getMode()
89    {
90        return $this->mode;
91    }
92
93    public function start()
94    {
95        if ($this->getMode() == self::MODE_SOAP) {
96            $this->__buildHeader();
97            $this->__buildCourseStart();
98            $this->__buildMetaData();
99            $this->__buildAdvancedMetaData();
100            if ($this->attach_users) {
101                $this->__buildAdmin();
102                $this->__buildTutor();
103                $this->__buildMember();
104            }
105            $this->__buildSubscriber();
106            $this->__buildWaitingList();
107
108            $this->__buildSetting();
109            include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
110            ilContainerSortingSettings::_exportContainerSortingSettings($this, $this->course_obj->getId());
111            ilContainer::_exportContainerSettings($this, $this->course_obj->getId());
112            $this->__buildFooter();
113        } elseif ($this->getMode() == self::MODE_EXPORT) {
114            $this->__buildCourseStart();
115            $this->__buildMetaData();
116            $this->__buildAdvancedMetaData();
117            $this->__buildSetting();
118            include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
119            ilContainerSortingSettings::_exportContainerSortingSettings($this, $this->course_obj->getId());
120            ilContainer::_exportContainerSettings($this, $this->course_obj->getId());
121            $this->__buildFooter();
122        }
123    }
124
125    public function getXML()
126    {
127        #var_dump("<pre>", htmlentities($this->xmlDumpMem()),"<pre>");
128        return $this->xmlDumpMem(true);
129    }
130
131    // Called from nested class
132    public function modifyExportIdentifier($a_tag, $a_param, $a_value)
133    {
134        if ($a_tag == "Identifier" && $a_param == "Entry") {
135            $a_value = "il_" . $this->ilias->getSetting('inst_id') . "_crs_" . $this->course_obj->getId();
136        }
137
138        return $a_value;
139    }
140
141    // PRIVATE
142    public function __buildHeader()
143    {
144        $this->xmlSetDtdDef("<!DOCTYPE Course PUBLIC \"-//ILIAS//DTD Course//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_crs_5_0.dtd\">");
145        $this->xmlSetGenCmt("Export of ILIAS course " . $this->course_obj->getId() . " of installation " . $this->ilias->getSetting('inst_id') . ".");
146        $this->xmlHeader();
147
148
149        return true;
150    }
151
152    public function __buildCourseStart()
153    {
154        $attrs["exportVersion"] = $this->EXPORT_VERSION;
155        $attrs["id"] = "il_" . $this->ilias->getSetting('inst_id') . '_crs_' . $this->course_obj->getId();
156        $attrs['showMembers'] = ($this->course_obj->getShowMembers() ? 'Yes' : 'No');
157        $this->xmlStartTag("Course", $attrs);
158    }
159
160    public function __buildMetaData()
161    {
162        include_once 'Services/MetaData/classes/class.ilMD2XML.php';
163
164        $md2xml = new ilMD2XML($this->course_obj->getId(), $this->course_obj->getId(), 'crs');
165        $md2xml->startExport();
166        $this->appendXML($md2xml->getXML());
167
168        return true;
169    }
170
171    /**
172     * Build advanced meta data
173     *
174     * @access private
175     *
176     */
177    private function __buildAdvancedMetaData()
178    {
179        include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
180        ilAdvancedMDValues::_appendXMLByObjId($this, $this->course_obj->getId());
181    }
182
183    public function __buildAdmin()
184    {
185        $admins = $this->course_obj->getMembersObject()->getAdmins();
186        $admins = $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
187            'manage_members',
188            ilOrgUnitOperation::OP_MANAGE_MEMBERS,
189            $this->course_obj->getRefId(),
190            $admins
191        );
192
193        foreach ($admins as $id) {
194            $attr['id'] = 'il_' . $this->ilias->getSetting('inst_id') . '_usr_' . $id;
195            $attr['notification'] = ($this->course_obj->getMembersObject()->isNotificationEnabled($id)) ? 'Yes' : 'No';
196            $attr['passed'] = $this->course_obj->getMembersObject()->hasPassed($id) ? 'Yes' : 'No';
197            $attr['contact'] = $this->course_obj->getMembersObject()->isContact($id) ? 'Yes' : 'No';
198
199            $this->xmlStartTag('Admin', $attr);
200            $this->xmlEndTag('Admin');
201        }
202        return true;
203    }
204
205    public function __buildTutor()
206    {
207        $tutors = $this->course_obj->getMembersObject()->getTutors();
208        $tutors = $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
209            'manage_members',
210            ilOrgUnitOperation::OP_MANAGE_MEMBERS,
211            $this->course_obj->getRefId(),
212            $tutors
213        );
214        foreach ($tutors as $id) {
215            $attr['id'] = 'il_' . $this->ilias->getSetting('inst_id') . '_usr_' . $id;
216            $attr['notification'] = ($this->course_obj->getMembersObject()->isNotificationEnabled($id)) ? 'Yes' : 'No';
217            $attr['passed'] = $this->course_obj->getMembersObject()->hasPassed($id) ? 'Yes' : 'No';
218            $attr['contact'] = $this->course_obj->getMembersObject()->isContact($id) ? 'Yes' : 'No';
219
220            $this->xmlStartTag('Tutor', $attr);
221            $this->xmlEndTag('Tutor');
222        }
223        return true;
224    }
225    public function __buildMember()
226    {
227        $members = $this->course_obj->getMembersObject()->getMembers();
228        $members = $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
229            'manage_members',
230            ilOrgUnitOperation::OP_MANAGE_MEMBERS,
231            $this->course_obj->getRefId(),
232            $members
233        );
234        foreach ($members as $id) {
235            $attr['id'] = 'il_' . $this->ilias->getSetting('inst_id') . '_usr_' . $id;
236            $attr['blocked'] = ($this->course_obj->getMembersObject()->isBlocked($id)) ? 'Yes' : 'No';
237            $attr['passed'] = $this->course_obj->getMembersObject()->hasPassed($id) ? 'Yes' : 'No';
238
239            $this->xmlStartTag('Member', $attr);
240            $this->xmlEndTag('Member');
241        }
242        return true;
243    }
244
245    public function __buildSubscriber()
246    {
247        $subs = $this->course_obj->getMembersObject()->getSubscribers();
248        $subs = $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
249            'manage_members',
250            ilOrgUnitOperation::OP_MANAGE_MEMBERS,
251            $this->course_obj->getRefId(),
252            $subs
253        );
254
255        foreach ($subs as $id) {
256            $data = $this->course_obj->getMembersObject()->getSubscriberData($id);
257
258            $attr['id'] = 'il_' . $this->ilias->getSetting('inst_id') . '_usr_' . $id;
259            $attr['subscriptionTime'] = $data['time'];
260
261            $this->xmlStartTag('Subscriber', $attr);
262            $this->xmlEndTag('Subscriber');
263        }
264        return true;
265    }
266
267    public function __buildWaitingList()
268    {
269        include_once 'Modules/Course/classes/class.ilCourseWaitingList.php';
270        $waiting_list = new ilCourseWaitingList($this->course_obj->getId());
271
272        $wait = $waiting_list->getAllUsers();
273
274        foreach ($wait as $data) {
275            $is_accessible = $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
276                'manage_members',
277                ilOrgUnitOperation::OP_MANAGE_MEMBERS,
278                $this->course_obj->getRefId(),
279                [$data['usr_id']]
280            );
281            if (!count($is_accessible)) {
282                continue;
283            }
284
285            $attr['id'] = 'il_' . $this->ilias->getSetting('inst_id') . '_usr_' . $data['usr_id'];
286            $attr['position'] = $data['position'];
287            $attr['subscriptionTime'] = $data['time'];
288
289            $this->xmlStartTag('WaitingList', $attr);
290            $this->xmlEndTag('WaitingList');
291        }
292        return true;
293    }
294
295
296    /**
297     * @return bool
298     */
299    public function __buildSetting()
300    {
301        $this->xmlStartTag('Settings');
302
303        // Availability
304        $this->xmlStartTag('Availability');
305        if ($this->course_obj->getOfflineStatus()) {
306            $this->xmlElement('NotAvailable');
307        } elseif ($this->course_obj->getActivationUnlimitedStatus()) {
308            $this->xmlElement('Unlimited');
309        } else {
310            $this->xmlStartTag('TemporarilyAvailable');
311            $this->xmlElement('Start', null, $this->course_obj->getActivationStart());
312            $this->xmlElement('End', null, $this->course_obj->getActivationEnd());
313            $this->xmlEndTag('TemporarilyAvailable');
314        }
315        $this->xmlEndTag('Availability');
316
317        // Syllabus
318        $this->xmlElement('Syllabus', null, $this->course_obj->getSyllabus());
319        $this->xmlElement('ImportantInformation', null, $this->course_obj->getImportantInformation());
320        $this->xmlElement('TargetGroup', null, $this->course_obj->getTargetGroup());
321
322
323        // Contact
324        $this->xmlStartTag('Contact');
325        $this->xmlElement('Name', null, $this->course_obj->getContactName());
326        $this->xmlElement('Responsibility', null, $this->course_obj->getContactResponsibility());
327        $this->xmlElement('Phone', null, $this->course_obj->getContactPhone());
328        $this->xmlElement('Email', null, $this->course_obj->getContactEmail());
329        $this->xmlElement('Consultation', null, $this->course_obj->getContactConsultation());
330        $this->xmlEndTag('Contact');
331
332        // Registration
333        $attr = array();
334
335        if ($this->course_obj->getSubscriptionType() == IL_CRS_SUBSCRIPTION_CONFIRMATION) {
336            $attr['registrationType'] = 'Confirmation';
337        } elseif ($this->course_obj->getSubscriptionType() == IL_CRS_SUBSCRIPTION_DIRECT) {
338            $attr['registrationType'] = 'Direct';
339        } else {
340            $attr['registrationType'] = 'Password';
341        }
342
343        $attr['maxMembers'] = $this->course_obj->isSubscriptionMembershipLimited() ?
344            $this->course_obj->getSubscriptionMaxMembers() : 0;
345        $attr['notification'] = $this->course_obj->getSubscriptionNotify() ? 'Yes' : 'No';
346        $attr['waitingList'] = $this->course_obj->enabledWaitingList() ? 'Yes' : 'No';
347
348        $this->xmlStartTag('Registration', $attr);
349
350        if ($this->course_obj->getSubscriptionLimitationType() == IL_CRS_SUBSCRIPTION_DEACTIVATED) {
351            $this->xmlElement('Disabled');
352        } elseif ($this->course_obj->getSubscriptionLimitationType() == IL_CRS_SUBSCRIPTION_UNLIMITED) {
353            $this->xmlElement('Unlimited');
354        } else {
355            $this->xmlStartTag('TemporarilyAvailable');
356            $this->xmlElement('Start', null, $this->course_obj->getSubscriptionStart());
357            $this->xmlElement('End', null, $this->course_obj->getSubscriptionEnd());
358            $this->xmlEndTag('TemporarilyAvailable');
359        }
360        if (strlen($pwd = $this->course_obj->getSubscriptionPassword())) {
361            $this->xmlElement('Password', null, $pwd);
362        }
363        $this->xmlEndTag('Registration');
364
365
366        $this->xmlStartTag('Period', ['withTime' => $this->course_obj->getCourseStartTimeIndication() ? 1 : 0]);
367        $this->xmlElement(
368            'Start',
369            null,
370            $this->course_obj->getCourseStart()
371                ? $this->course_obj->getCourseStart()->get(IL_CAL_UNIX)
372                : null
373        );
374        $this->xmlElement(
375            'End',
376            null,
377            $this->course_obj->getCourseEnd()
378                ? $this->course_obj->getCourseEnd()->get(IL_CAL_UNIX)
379                : null
380        );
381        $this->xmlEndTag('Period');
382        $this->xmlElement('WaitingListAutoFill', null, (int) $this->course_obj->hasWaitingListAutoFill());
383        $this->xmlElement('CancellationEnd', null, ($this->course_obj->getCancellationEnd() && !$this->course_obj->getCancellationEnd()->isNull()) ? $this->course_obj->getCancellationEnd()->get(IL_CAL_UNIX) : null);
384        $this->xmlElement('MinMembers', null, (int) $this->course_obj->getSubscriptionMinMembers());
385
386        $this->xmlElement('ViewMode', null, $this->course_obj->getViewMode());
387
388        // cognos-blu-patch: begin
389        $this->xmlElement('ViewMode', null, $this->course_obj->getViewMode());
390
391        if ($this->course_obj->getViewMode() == IL_CRS_VIEW_TIMING) {
392            $this->xmlElement('TimingMode', null, $this->course_obj->getTimingMode());
393        }
394        // cognos-blu-patch: end
395
396        $this->xmlElement(
397            'WelcomeMail',
398            [
399                'status' => $this->course_obj->getAutoNotification() ? 1 : 0
400            ]
401        );
402
403
404        $this->xmlEndTag('Settings');
405
406        return true;
407    }
408
409    public function __buildFooter()
410    {
411        $this->xmlEndTag('Course');
412    }
413
414    /**
415     * write access to attach user property, if set to false no users will be attached.
416     *
417     * @param unknown_type $value
418     */
419    public function setAttachUsers($value)
420    {
421        $this->attach_users = $value ? true : false;
422    }
423}
424