course_export.php 5.1 KB

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