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