course_import.class.php 2.0 KB

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