ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSContentWriter.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 include_once './Services/WebServices/ECS/classes/class.ilECSContentWriterException.php';
25 include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
26 
37 {
38  const UPDATE_ALL = 1;
40 
41  private $mode = 0;
42 
43  protected $log;
44 
45  protected $content_obj = null;
46  protected $export_settings = null;
47 
48  protected $server_id = 0;
49  protected $exportable = true;
50  protected $owner = 0;
51  protected $mids = null;
52 
60  public function __construct($a_cont_obj,$a_server_id)
61  {
62  global $ilLog;
63 
64  $this->log = $ilLog;
65 
66  $this->content_obj = $a_cont_obj;
67  $this->server_id = $a_server_id;
68 
69  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
70  $this->export_settings = new ilECSExport($this->getServerId(),$this->content_obj->getId());
71  }
72 
82  public static function _handleDelete($a_subbtree_nodes)
83  {
84  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
85  if(!ilECSServerSettings::getInstance()->activeServerExists())
86  {
87  return false;
88  }
89 
90  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
91  $exported = ilECSExport::getExportedIds();
92  foreach($a_subbtree_nodes as $node)
93  {
94  if(in_array($node['obj_id'],$exported))
95  {
96  if($content_obj = ilObjectFactory::getInstanceByRefId($node['child'],false))
97  {
98  try
99  {
100  // Read export server ids
101  foreach(ilECSExport::getExportServerIds($node['obj_id']) as $sid)
102  {
103  $writer = new ilECSContentWriter($content_obj, $sid);
104  $writer->deleteECSContent();
105  }
106  }
107  catch(ilECSContentWriterException $exc)
108  {
109  continue;
110  }
111  }
112  }
113  }
114 
115 
116  }
117 
122  public function getServerId()
123  {
124  return $this->server_id;
125  }
126 
134  public function setExportable($a_status)
135  {
136  $this->exportable = $a_status;
137  }
138 
146  public function isExportable()
147  {
148  return $this->exportable;
149  }
150 
158  public function setOwnerId($a_id)
159  {
160  $this->owner = $a_id;
161  }
162 
169  public function getOwnerId()
170  {
171  return $this->owner;
172  }
173 
181  public function setParticipantIds($a_mids)
182  {
183  $this->mids = $a_mids;
184  }
185 
192  public function getParticipantIds()
193  {
194  return $this->mids;
195  }
196 
203  public function refresh()
204  {
205  $this->mode = self::UPDATE_ALL;
206  try
207  {
208  if($this->export_settings->isExported())
209  {
210  if($this->isExportable())
211  {
212  // Update Econtent
213  return $this->updateECSContent();
214  }
215  else
216  {
217  // Delete EContent
218  return $this->deleteECSContent();
219  }
220  }
221  else
222  {
223  if($this->isExportable())
224  {
225  // Add Econtent
226  return $this->addECSContent();
227  }
228  else
229  {
230  // Nothing to do
231  }
232  }
233  return true;
234  }
235  catch(ilECSConnectorException $exc)
236  {
237  $this->log->write(__METHOD__.': Error connecting to ECS server. '.$exc->getMessage());
238  throw $exc;
239  }
240  catch(ilECSContentWriterException $exc)
241  {
242  $this->log->write(__METHOD__.': Cannot update ECS content. '.$exc->getMessage());
243  throw $exc;
244  }
245  }
246 
253  public function refreshSettings()
254  {
255  $this->mode = self::UPDATE_SETTINGS_ONLY;
256  try
257  {
258  if($this->export_settings->isExported())
259  {
260  // Update Econtent
261  return $this->updateECSContent();
262  }
263  else
264  {
265  // Nothing to do
266  return true;
267  }
268  }
269  catch(ilECSConnectorException $exc)
270  {
271  $this->log->write(__METHOD__.': Error connecting to ECS server. '.$exc->getMessage());
272  throw $exc;
273  }
274  catch(ilECSContentWriterException $exc)
275  {
276  $this->log->write(__METHOD__.': Cannot update ECS content. '.$exc->getMessage());
277  throw $exc;
278  }
279  }
280 
287  public function deleteECSContent()
288  {
289  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
290  include_once('./Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
291 
292  try
293  {
294  $this->log->write(__METHOD__.': Start deleting ECS content...');
295 
297  if(!$this->export_settings->getEContentId())
298  {
299  $this->log->write(__METHOD__.': Missing eid. Aborting.');
300  throw new ilECSContentWriterException('Missing ECS content ID. Aborting.');
301  }
302  $connector->deleteResource($this->export_settings->getEContentId());
303  $this->export_settings->setExported(false);
304  $this->export_settings->save();
305  }
306  catch(ilECSConnectorException $exc)
307  {
308  throw $exc;
309  }
310  return true;
311  }
312 
319  public function addECSContent()
320  {
321  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
322  include_once('./Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
323 
324  try
325  {
326  $this->log->write(__METHOD__.': Starting course export...');
327 
328  // construct new json object and set settings from content obj
329  $this->createJSON();
330  $this->updateJSON();
331 
333  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$this->getParticipantIds()));
334  #$connector->addHeader(ilECSConnector::HEADER_COMMUNITIES, )
335  $econtent_id = $connector->addResource(json_encode($this->json));
336 
337  $this->export_settings->setExported(true);
338  $this->export_settings->setEContentId($econtent_id);
339  $this->export_settings->save();
340 
341  // Send mail
343 
344  }
345  catch(ilECSConnectorException $exc)
346  {
347  throw $exc;
348  }
349  catch(ilECSContentWriterException $exc)
350  {
351  throw $exc;
352  }
353  }
354 
361  public function updateECSContent()
362  {
363  try
364  {
365  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentReader.php');
366  $reader = new ilECSEContentReader($this->getServerId(),$this->export_settings->getEContentId());
367  $reader->read();
368  $reader->read(true);
369  $content = $reader->getEContent();
370  $details = $reader->getEContentDetails();
371 
372  if(!$content instanceof ilECSEContent or !$details instanceof ilECSEContentDetails)
373  {
374  $this->log->write(__METHOD__.': Error reading EContent with id: '.$this->export_settings->getEContentId());
375  include_once('./Services/WebServices/ECS/classes/class.ilECSContentWriterException.php');
376  throw new ilECSContentWriterException('Error reading E-Content. Aborting');
377  }
378  $this->json = $content;
379  $this->updateJSON();
381  #$connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$this->getParticipantIds()));
382  if($this->getParticipantIds() == null)
383  {
384  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$details->getReceivers()));
385  }
386  else
387  {
388  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$this->getParticipantIds()));
389  }
390 
391  $connector->updateResource($this->export_settings->getEContentId(),json_encode($this->json));
392  }
393  catch(ilECSConnectorException $exc)
394  {
395  throw $exc;
396  }
397  }
398 
405  private function sendNewContentNotification()
406  {
407  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
408  $settings = ilECSSetting::getInstanceByServerId($this->getServerId());
409  if(!count($rcps = $settings->getApprovalRecipients()))
410  {
411  return true;
412  }
413 
414  include_once('./Services/Mail/classes/class.ilMail.php');
415  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
416 
418  $lang->loadLanguageModule('ecs');
419 
420  // @TODO: read mail
421  $mail = new ilMail(6);
422  $message = $lang->txt('ecs_export_created_body_a')."\n\n";
423  $message .= $lang->txt('title').': '.$this->content_obj->getTitle()."\n";
424  if(strlen($desc = $this->content_obj->getDescription()))
425  {
426  $message .= $lang->txt('desc').': '.$desc."\n";
427  }
428 
429  // Participant info
430  $message .= ("\n".$lang->txt('ecs_published_for'));
431 
432  $export = new ilECSExport($this->getServerId(),$this->content_obj->getId());
433  try
434  {
435  include_once './Services/WebServices/ECS/classes/class.ilECSEContentReader.php';
436  $reader = new ilECSEContentReader($this->getServerId(),$export->getEContentId());
437  $reader->read(true);
438 
439  $found = false;
440  if($reader->getEContentDetails() instanceof ilECSEContentDetails)
441  {
442  foreach($reader->getEContentDetails()->getReceivers() as $member)
443  {
444  $found = true;
445 
446  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
447  $part = ilECSCommunityReader::getInstanceByServerId($this->getServerId())->getParticipantByMID($member);
448 
449  $message .= ("\n\n".$part->getParticipantName()."\n");
450  $message .= ($part->getDescription());
451 
452  }
453  }
454  if($found)
455  {
456  $message .= "\n\n";
457  }
458  else
459  {
460  $message .= (' '.$lang->txt('ecs_not_published')."\n\n");
461  }
462  }
463  catch(ilECSConnectorException $e)
464  {
465  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read approvements.');
466  return false;
467  }
468  catch(ilECSReaderException $e)
469  {
470  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read approvements.');
471  return false;
472  }
473 
474  include_once('classes/class.ilLink.php');
475  $href = ilLink::_getStaticLink($this->content_obj->getRefId(),'crs',true);
476  $message .= $lang->txt("perma_link").': '.$href."\n\n";
478 
479  $error = $mail->sendMail($settings->getApprovalRecipientsAsString(),
480  '','',
481  $lang->txt('ecs_new_approval_subject'),
482  $message,array(),array('normal'));
483 
484  return true;
485 
486  }
487 
493  private function createJSON()
494  {
495  include_once('./Services/WebServices/ECS/classes/class.ilECSEContent.php');
496  $this->json = new ilECSEContent();
497  return true;
498  }
499 
506  private function updateJSON()
507  {
508  // General fields
509  ######################################################
510  include_once('./classes/class.ilLink.php');
511  $this->json->setURL(ilLink::_getLink($this->content_obj->getRefId(),$this->content_obj->getType()));
512  $this->json->setTitle($this->content_obj->getTitle());
513 
514  // Ownership EligibleMembers
515  ######################################################
516  if($this->mode == self::UPDATE_ALL)
517  {
518  // Eligible members [0] is owner
519  if(!$this->getOwnerId())
520  {
521  throw new ilECSContentWriterException('Missing ECS owner id.');
522  }
523  }
524 
525  // Add organisation
526  // todo: this must be cached in ecs_part_settings
527  if($this->mode == self::UPDATE_ALL)
528  {
529  try {
530  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
532  $part = $reader->getParticipantByMID($this->getOwnerId());
533  $this->json->organisation = $part->getOrganisation()->getName();
534  }
535  catch(Exception $e)
536  {
537  $GLOBALS['ilLog']->write(__METHOD__.': '.$e->getMessage());
538  }
539  }
540 
541  // meta language
542  include_once('./Services/MetaData/classes/class.ilMDLanguage.php');
543  $this->json->setLanguage(ilMDLanguage::_lookupFirstLanguage($this->content_obj->getId(),$this->content_obj->getId(),$this->content_obj->getType()));
544  $this->json->setStatus($this->content_obj->isActivated() ? 'online' : 'offline');
545  $this->json->setInfo($this->content_obj->getDescription());
546 
547  // Optional fields
548  ######################################################
549  include_once('./Services/WebServices/ECS/classes/class.ilECSUtils.php');
550  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
551  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
552  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
554  $values = ilAdvancedMDValues::_getValuesByObjId($this->content_obj->getId());
555 
556  // Study courses
557  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, 'study_courses'))
558  {
559  $value = isset($values[$field]) ? $values[$field] : '';
560  $this->json->setStudyCourses($value);
561  }
562  // Lecturer
563  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, 'lecturer'))
564  {
565  $value = isset($values[$field]) ? $values[$field] : '';
566  $this->json->setLecturers($value);
567  }
568  // CourseType
569  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, 'courseType'))
570  {
571  $value = isset($values[$field]) ? $values[$field] : '';
572  $this->json->setCourseType($value);
573  }
574  // Course ID
575  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'courseID'))
576  {
577  $value = isset($values[$field]) ? $values[$field] : '';
578  $this->json->setCourseID($value);
579  }
580  // Credits
581  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'credits'))
582  {
583  $value = isset($values[$field]) ? $values[$field] : '';
584  $this->json->setCredits($value);
585  }
586  // SWS
587  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'semester_hours'))
588  {
589  $value = isset($values[$field]) ? $values[$field] : '';
590  $this->json->setSemesterHours($value);
591  }
592  // Term
593  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'term'))
594  {
595  $value = isset($values[$field]) ? $values[$field] : '';
596  $this->json->setTerm($value);
597  }
598  // TIME PLACE OBJECT ########################
599  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'begin'))
600  {
601  $value = isset($values[$field]) ? $values[$field] : '';
602  $this->json->getTimePlace()->setBegin($value);
603  }
604  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'end'))
605  {
606  $value = isset($values[$field]) ? $values[$field] : '';
607  $this->json->getTimePlace()->setEnd($value);
608  }
609  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'room'))
610  {
611  $value = isset($values[$field]) ? $values[$field] : '';
612  $this->json->getTimePlace()->setRoom($value);
613  }
614  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT,'cycle'))
615  {
616  $value = isset($values[$field]) ? $values[$field] : '';
617  $this->json->getTimePlace()->setCycle($value);
618  }
619  return true;
620  }
621 }
622 
623 
624 ?>