course_import.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course description import class definition
  5. * @package chamilo.course_description
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace CourseDescription;
  11. /**
  12. * Import course descriptions into a course/session.
  13. *
  14. * @license /licence.txt
  15. * @author Laurent Opprecht <laurent@opprecht.info>
  16. */
  17. class CourseImport
  18. {
  19. protected $course = false;
  20. protected $update_existing_entries = false;
  21. protected $objects_imported = 0;
  22. protected $objects_skipped = 0;
  23. public function __construct($course)
  24. {
  25. $this->course = $course;
  26. }
  27. public function get_course()
  28. {
  29. return $this->course;
  30. }
  31. public function get_objects_imported()
  32. {
  33. return $this->objects_imported;
  34. }
  35. public function get_objects_skipped()
  36. {
  37. return $this->objects_skipped;
  38. }
  39. /**
  40. *
  41. * @param array $descriptions
  42. */
  43. public function add($descriptions)
  44. {
  45. $this->objects_imported = 0;
  46. $this->objects_skipped = 0;
  47. foreach ($descriptions as $description) {
  48. $title = $description->title;
  49. $content = $description->content;
  50. $type = $description->type;
  51. if (empty($type)) {
  52. $type = CourseDescriptionType::repository()->find_one_by_name('general');
  53. $description->description_type = $type->id;
  54. }
  55. if (empty($title) || empty($content)) {
  56. $this->objects_skipped++;
  57. continue;
  58. }
  59. // $description = $this->find_by_title($title);
  60. // if ($description && $this->update_existing_entries == false) {
  61. // $this->objects_skipped++;
  62. // continue;
  63. // }
  64. $description->c_id = $this->course->c_id;
  65. $description->session_id = $this->course->session_id;
  66. $repo = CourseDescription::repository();
  67. $success = $repo->save($description);
  68. if ($success) {
  69. $this->objects_imported++;
  70. } else {
  71. $this->objects_skipped++;
  72. }
  73. }
  74. }
  75. function find_by_title($title)
  76. {
  77. $c_id = $this->c_id;
  78. $session_id = $this->session_id;
  79. $repo = CourseDescriptionRepository::instance();
  80. $link = $repo->find_one_by_course_and_title($c_id, $session_id, $title);
  81. return $link;
  82. }
  83. }