course_export.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to export courses to CSV file.
  5. *
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. $tool_name = get_lang('ExportCourses');
  13. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  14. set_time_limit(0);
  15. $course_list = CourseManager::get_courses_list();
  16. $formSent = null;
  17. $courses = $selected_courses = [];
  18. if (isset($_POST['formSent']) && $_POST['formSent']) {
  19. $formSent = $_POST['formSent'];
  20. $select_type = intval($_POST['select_type']);
  21. $file_type = $_POST['file_type'];
  22. if ($select_type == 2) {
  23. // Get selected courses from courses list in form sent
  24. $selected_courses = $_POST['course_code'];
  25. if (is_array($selected_courses)) {
  26. foreach ($course_list as $course) {
  27. if (!in_array($course['code'], $selected_courses)) {
  28. continue;
  29. }
  30. $courses[] = $course;
  31. }
  32. }
  33. } else {
  34. // Get all courses
  35. $courses = $course_list;
  36. }
  37. if (!empty($courses)) {
  38. $archiveFile = 'export_courses_list_'.api_get_local_time();
  39. $listToExport[] = [
  40. 'Code',
  41. 'Title',
  42. 'CourseCategory',
  43. 'CourseCategoryName',
  44. 'Teacher',
  45. 'Language',
  46. 'Users',
  47. 'OtherTeachers',
  48. ];
  49. $dataToExport = [];
  50. foreach ($courses as $course) {
  51. $dataToExport['code'] = str_replace(';', ',', $course['code']);
  52. $dataToExport['title'] = str_replace(';', ',', $course['title']);
  53. $dataToExport['category_code'] = str_replace(';', ',', $course['category_code']);
  54. $categoryInfo = CourseCategory::getCategory($course['category_code']);
  55. if ($categoryInfo) {
  56. $dataToExport['category_name'] = str_replace(';', ',', $categoryInfo['name']);
  57. } else {
  58. $dataToExport['category_name'] = '';
  59. }
  60. $dataToExport['tutor_name'] = str_replace(';', ',', $course['tutor_name']);
  61. $dataToExport['course_language'] = str_replace(';', ',', $course['course_language']);
  62. $dataToExport['students'] = '';
  63. $dataToExport['teachers'] = '';
  64. $usersInCourse = CourseManager::get_user_list_from_course_code($course['code']);
  65. if (is_array($usersInCourse) && !empty($usersInCourse)) {
  66. foreach ($usersInCourse as $user) {
  67. if ($user['status_rel'] == COURSEMANAGER) {
  68. $dataToExport['teachers'] .= $user['username'].'|';
  69. } else {
  70. $dataToExport['students'] .= $user['username'].'|';
  71. }
  72. }
  73. }
  74. $dataToExport['students'] = substr($dataToExport['students'], 0, -1);
  75. $dataToExport['teachers'] = substr($dataToExport['teachers'], 0, -1);
  76. $listToExport[] = $dataToExport;
  77. }
  78. switch ($file_type) {
  79. case 'xml':
  80. // Remove header
  81. unset($listToExport[0]);
  82. Export::arrayToXml($listToExport, $archiveFile);
  83. break;
  84. case 'csv':
  85. Export::arrayToCsv($listToExport, $archiveFile);
  86. break;
  87. case 'xls':
  88. Export::arrayToXls($listToExport, $archiveFile);
  89. break;
  90. }
  91. } else {
  92. Display::addFlash(
  93. Display::return_message(
  94. get_lang('ThereAreNotSelectedCoursesOrCoursesListIsEmpty')
  95. )
  96. );
  97. }
  98. }
  99. Display::display_header($tool_name);
  100. $form = new FormValidator('export', 'post', api_get_self());
  101. $form->addHeader($tool_name);
  102. $form->addHidden('formSent', 1);
  103. $form->addElement(
  104. 'radio',
  105. 'select_type',
  106. get_lang('Option'),
  107. get_lang('ExportAllCoursesList'),
  108. '1',
  109. ['onclick' => "javascript: if(this.checked){document.getElementById('div-course-list').style.display='none';}"]
  110. );
  111. $form->addElement(
  112. 'radio',
  113. 'select_type',
  114. '',
  115. get_lang('ExportSelectedCoursesFromCoursesList'),
  116. '2',
  117. ['onclick' => "javascript: if(this.checked){document.getElementById('div-course-list').style.display='block';}"]
  118. );
  119. if (!empty($course_list)) {
  120. $form->addHtml('<div id="div-course-list" style="display:none">');
  121. $coursesInList = [];
  122. foreach ($course_list as $course) {
  123. $coursesInList[$course['code']] = $course['title'].' ('.$course['code'].')';
  124. }
  125. $form->addSelect(
  126. 'course_code',
  127. get_lang('WhichCoursesToExport'),
  128. $coursesInList,
  129. ['multiple' => 'multiple']
  130. );
  131. $form->addHtml('</div>');
  132. }
  133. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'CSV', 'csv', null);
  134. $form->addElement('radio', 'file_type', '', 'XLS', 'xls', null);
  135. $form->addElement('radio', 'file_type', null, 'XML', 'xml', null, ['id' => 'file_type_xml']);
  136. $form->setDefaults(['select_type' => '1', 'file_type' => 'csv']);
  137. $form->addButtonExport(get_lang('ExportCourses'));
  138. $form->display();
  139. Display :: display_footer();