ILIAS  Release_4_1_x_branch Revision 61804
 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 
36 {
37  const UPDATE_ALL = 1;
39 
40  private $mode = 0;
41 
42  protected $log;
43 
44  protected $content_obj = null;
45  protected $export_settings = null;
46 
47  protected $exportable = true;
48  protected $owner = 0;
49  protected $mids = array();
50 
58  public function __construct($a_cont_obj)
59  {
60  global $ilLog;
61 
62  $this->log = $ilLog;
63 
64  $this->content_obj = $a_cont_obj;
65 
66  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
67  $this->export_settings = new ilECSExport($this->content_obj->getId());
68  }
69 
79  public static function _handleDelete($a_subbtree_nodes)
80  {
81  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
82  if(!ilECSSettings::_getInstance()->isEnabled())
83  {
84  return false;
85  }
86  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
87  $exported = ilECSExport::_getExportedIDs();
88  foreach($a_subbtree_nodes as $node)
89  {
90  if(in_array($node['obj_id'],$exported))
91  {
92  if($content_obj = ilObjectFactory::getInstanceByRefId($node['child'],false))
93  {
94  try
95  {
96  $writer = new ilECSContentWriter($content_obj);
97  $writer->deleteECSContent();
98  }
99  catch(ilECSContentWriterException $exc)
100  {
101  continue;
102  }
103  }
104  }
105  }
106 
107 
108  }
109 
117  public function setExportable($a_status)
118  {
119  $this->exportable = $a_status;
120  }
121 
129  public function isExportable()
130  {
131  return $this->exportable;
132  }
133 
141  public function setOwnerId($a_id)
142  {
143  $this->owner = $a_id;
144  }
145 
152  public function getOwnerId()
153  {
154  return $this->owner;
155  }
156 
164  public function setParticipantIds($a_mids)
165  {
166  $this->mids = $a_mids;
167  }
168 
175  public function getParticipantIds()
176  {
177  return $this->mids;
178  }
179 
186  public function refresh()
187  {
188  $this->mode = self::UPDATE_ALL;
189  try
190  {
191  if($this->export_settings->isExported())
192  {
193  if($this->isExportable())
194  {
195  // Update Econtent
196  return $this->updateECSContent();
197  }
198  else
199  {
200  // Delete EContent
201  return $this->deleteECSContent();
202  }
203  }
204  else
205  {
206  if($this->isExportable())
207  {
208  // Add Econtent
209  return $this->addECSContent();
210  }
211  else
212  {
213  // Nothing to do
214  }
215  }
216  return true;
217  }
218  catch(ilECSConnectorException $exc)
219  {
220  $this->log->write(__METHOD__.': Error connecting to ECS server. '.$exc->getMessage());
221  throw $exc;
222  }
223  catch(ilECSContentWriterException $exc)
224  {
225  $this->log->write(__METHOD__.': Cannot update ECS content. '.$exc->getMessage());
226  throw $exc;
227  }
228  }
229 
236  public function refreshSettings()
237  {
238  $this->mode = self::UPDATE_SETTINGS_ONLY;
239 
240  try
241  {
242  if($this->export_settings->isExported())
243  {
244  // Update Econtent
245  return $this->updateECSContent();
246  }
247  else
248  {
249  // Nothing to do
250  return true;
251  }
252  }
253  catch(ilECSConnectorException $exc)
254  {
255  $this->log->write(__METHOD__.': Error connecting to ECS server. '.$exc->getMessage());
256  throw $exc;
257  }
258  catch(ilECSContentWriterException $exc)
259  {
260  $this->log->write(__METHOD__.': Cannot update ECS content. '.$exc->getMessage());
261  throw $exc;
262  }
263  }
264 
271  public function deleteECSContent()
272  {
273  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
274  include_once('./Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
275 
276  try
277  {
278  $this->log->write(__METHOD__.': Start deleting ECS content...');
279 
280  $connector = new ilECSConnector();
281  if(!$this->export_settings->getEContentId())
282  {
283  $this->log->write(__METHOD__.': Missing eid. Aborting.');
284  throw new ilECSContentWriterException('Missing ECS content ID. Aborting.');
285  }
286  $connector->deleteResource($this->export_settings->getEContentId());
287  $this->export_settings->setExported(false);
288  $this->export_settings->save();
289  }
290  catch(ilECSConnectorException $exc)
291  {
292  throw $exc;
293  }
294  return true;
295  }
296 
303  public function addECSContent()
304  {
305  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
306  include_once('./Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
307 
308  try
309  {
310  $this->log->write(__METHOD__.': Starting course export...');
311 
312  // construct new json object and set settings from content obj
313  $this->createJSON();
314  $this->updateJSON();
315 
316  $connector = new ilECSConnector();
317  $econtent_id = $connector->addResource(json_encode($this->json));
318 
319  $this->export_settings->setExported(true);
320  $this->export_settings->setEContentId($econtent_id);
321  $this->export_settings->save();
322 
323  // Send mail
325 
326  }
327  catch(ilECSConnectorException $exc)
328  {
329  throw $exc;
330  }
331  catch(ilECSContentWriterException $exc)
332  {
333  throw $exc;
334  }
335  }
336 
343  public function updateECSContent()
344  {
345  try
346  {
347  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentReader.php');
348  $reader = new ilECSEContentReader($this->export_settings->getEContentId());
349  $reader->read();
350  $content = $reader->getEContent();
351  if(!is_array($content) or !is_object($content[0]))
352  {
353  $this->log->write(__METHOD__.': Error reading EContent with id: '.$this->export_settings->getEContentId());
354  include_once('./Services/WebServices/ECS/classes/class.ilECSContentWriterException.php');
355  throw new ilECSContentWriterException('Error reading EContent. Aborting');
356  }
357  $this->json = $content[0];
358  $this->updateJSON();
359  $connector = new ilECSConnector();
360  #var_dump("<pre>",json_encode($this->json),"</pre>");
361 
362  $connector->updateResource($this->export_settings->getEContentId(),json_encode($this->json));
363  }
364  catch(ilECSConnectorException $exc)
365  {
366  throw $exc;
367  }
368  }
369 
376  private function sendNewContentNotification()
377  {
378  include_once('Services/WebServices/ECS/classes/class.ilECSSettings.php');
379  $settings = ilECSSettings::_getInstance();
380  if(!count($rcps = $settings->getApprovalRecipients()))
381  {
382  return true;
383  }
384 
385  include_once('./Services/Mail/classes/class.ilMail.php');
386  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
387 
389  $lang->loadLanguageModule('ecs');
390 
391  $mail = new ilMail(6);
392  $message = $lang->txt('ecs_export_created_body_a')."\n\n";
393  $message .= $lang->txt('title').': '.$this->content_obj->getTitle()."\n";
394  if(strlen($desc = $this->content_obj->getDescription()))
395  {
396  $message .= $lang->txt('desc').': '.$desc."\n";
397  }
398 
399  // Participant info
400  $message .= ("\n".$lang->txt('ecs_published_for'));
401 
402  $export = new ilECSExport($this->content_obj->getId());
403  try
404  {
405  include_once './Services/WebServices/ECS/classes/class.ilECSEContentReader.php';
406  $reader = new ilECSEContentReader($export->getEContentId());
407  $reader->read();
408 
409  $found = false;
410  foreach($reader->getEContent() as $econ)
411  {
412  foreach($econ->getEligibleMembers() as $member)
413  {
414  $found = true;
415 
416  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
417  $part = ilECSCommunityReader::_getInstance()->getParticipantByMID($member);
418 
419  $message .= ("\n\n".$part->getParticipantName()."\n");
420  $message .= ($part->getDescription());
421  }
422  }
423  if($found)
424  {
425  $message .= "\n\n";
426  }
427  else
428  {
429  $message .= (' '.$lang->txt('ecs_not_published')."\n\n");
430  }
431  }
432  catch(ilECSConnectorException $e)
433  {
434  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read approvements.');
435  }
436  catch(ilECSReaderException $e)
437  {
438  $GLOBALS['ilLog']->write(__METHOD__.': Cannot read approvements.');
439  }
440 
441  include_once('classes/class.ilLink.php');
442  $href = ilLink::_getStaticLink($this->content_obj->getRefId(),'crs',true);
443  $message .= $lang->txt("perma_link").': '.$href."\n\n";
445 
446  $error = $mail->sendMail($settings->getApprovalRecipientsAsString(),
447  '','',
448  $lang->txt('ecs_new_approval_subject'),
449  $message,array(),array('normal'));
450 
451  return true;
452 
453  }
454 
460  private function createJSON()
461  {
462  include_once('./Services/WebServices/ECS/classes/class.ilECSEContent.php');
463  $this->json = new ilECSEContent();
464  return true;
465  }
466 
473  private function updateJSON()
474  {
475  // General fields
476  ######################################################
477  include_once('./classes/class.ilLink.php');
478  $this->json->setURL(ilLink::_getLink($this->content_obj->getRefId(),$this->content_obj->getType()));
479  $this->json->setTitle($this->content_obj->getTitle());
480 
481  // Ownership EligibleMembers
482  ######################################################
483  if($this->mode == self::UPDATE_ALL)
484  {
485  // Eligible members [0] is owner
486  if(!$this->getOwnerId())
487  {
488  throw new ilECSContentWriterException('Missing ECS owner id.');
489  }
490  $this->json->setOwner($this->getOwnerId());
491  $members = array_unique($this->getParticipantIds());
492  $this->json->setEligibleMembers($members);
493  }
494 
495  // meta language
496  include_once('./Services/MetaData/classes/class.ilMDLanguage.php');
497  $this->json->setLanguage(ilMDLanguage::_lookupFirstLanguage($this->content_obj->getId(),$this->content_obj->getId(),$this->content_obj->getType()));
498  $this->json->setStatus($this->content_obj->isActivated() ? 'online' : 'offline');
499  $this->json->setInfo($this->content_obj->getDescription());
500 
501  // Optional fields
502  ######################################################
503  include_once('./Services/WebServices/ECS/classes/class.ilECSUtils.php');
504  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
505  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
506  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
508  $values = ilAdvancedMDValues::_getValuesByObjId($this->content_obj->getId());
509 
510  // Study courses
511  if($field = $mappings->getMappingByECSName('study_courses'))
512  {
513  $value = isset($values[$field]) ? $values[$field] : '';
514  $this->json->setStudyCourses($value);
515  }
516  // Lecturer
517  if($field = $mappings->getMappingByECSName('lecturer'))
518  {
519  $value = isset($values[$field]) ? $values[$field] : '';
520  $this->json->setLecturers($value);
521  }
522  // CourseType
523  if($field = $mappings->getMappingByECSName('courseType'))
524  {
525  $value = isset($values[$field]) ? $values[$field] : '';
526  $this->json->setCourseType($value);
527  }
528  // Course ID
529  if($field = $mappings->getMappingByECSName('courseID'))
530  {
531  $value = isset($values[$field]) ? $values[$field] : '';
532  $this->json->setCourseID($value);
533  }
534  // Credits
535  if($field = $mappings->getMappingByECSName('credits'))
536  {
537  $value = isset($values[$field]) ? $values[$field] : '';
538  $this->json->setCredits($value);
539  }
540  // SWS
541  if($field = $mappings->getMappingByECSName('semester_hours'))
542  {
543  $value = isset($values[$field]) ? $values[$field] : '';
544  $this->json->setSemesterHours($value);
545  }
546  // Term
547  if($field = $mappings->getMappingByECSName('term'))
548  {
549  $value = isset($values[$field]) ? $values[$field] : '';
550  $this->json->setTerm($value);
551  }
552  // TIME PLACE OBJECT ########################
553  if($field = $mappings->getMappingByECSName('begin'))
554  {
555  $value = isset($values[$field]) ? $values[$field] : '';
556  $this->json->getTimePlace()->setBegin($value);
557  }
558  if($field = $mappings->getMappingByECSName('end'))
559  {
560  $value = isset($values[$field]) ? $values[$field] : '';
561  $this->json->getTimePlace()->setEnd($value);
562  }
563  if($field = $mappings->getMappingByECSName('room'))
564  {
565  $value = isset($values[$field]) ? $values[$field] : '';
566  $this->json->getTimePlace()->setRoom($value);
567  }
568  if($field = $mappings->getMappingByECSName('cycle'))
569  {
570  $value = isset($values[$field]) ? $values[$field] : '';
571  $this->json->getTimePlace()->setCycle($value);
572  }
573  return true;
574  }
575 }
576 
577 
578 ?>