course_import.class.php 1.8 KB

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