course_export.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * This tool allows platform admins to export courses to CSV file
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script();
  11. $tool_name = get_lang('ExportCourses');
  12. $interbreadcrumb[] = array ('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin'));
  13. set_time_limit(0);
  14. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  15. $archiveURL = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive=';
  16. $course_list = CourseManager::get_courses_list();
  17. $formSent = null;
  18. $courses = $selected_courses = array();
  19. if (isset($_POST['formSent']) && $_POST['formSent']) {
  20. $formSent = $_POST['formSent'];
  21. $select_type = intval($_POST['select_type']);
  22. $file_type = $_POST['file_type'];
  23. if ($select_type == 2) {
  24. // Get selected courses from courses list in form sent
  25. $selected_courses = $_POST['course_code'];
  26. if (is_array($selected_courses)) {
  27. foreach ($course_list as $course) {
  28. if (!in_array($course['code'], $selected_courses)) {
  29. continue;
  30. }
  31. $courses[] = $course;
  32. }
  33. }
  34. } else {
  35. // Get all courses
  36. $courses = $course_list;
  37. }
  38. if (!empty($courses)) {
  39. $archiveFile = 'export_courses_list_'.api_get_local_time();
  40. $listToExport[] = [
  41. 'Code',
  42. 'Title',
  43. 'CourseCategory',
  44. 'CourseCategoryName',
  45. 'Teacher',
  46. 'Language',
  47. 'Users',
  48. 'OtherTeachers'
  49. ];
  50. $dataToExport = [];
  51. foreach ($courses as $course) {
  52. $dataToExport['code'] = str_replace(';',',',$course['code']);
  53. $dataToExport['title'] = str_replace(';',',',$course['title']);
  54. $dataToExport['category_code'] = str_replace(';',',',$course['category_code']);
  55. $categoryInfo = CourseCategory::getCategory($course['category_code']);
  56. if ($categoryInfo) {
  57. $dataToExport['category_name'] = str_replace(';',',',$categoryInfo['name']);
  58. } else {
  59. $dataToExport['category_name'] = '';
  60. }
  61. $dataToExport['tutor_name'] = str_replace(';',',',$course['tutor_name']);
  62. $dataToExport['course_language'] = str_replace(';',',',$course['course_language']);
  63. $dataToExport['students'] = '';
  64. $dataToExport['teachers'] = '';
  65. $usersInCourse = CourseManager::get_user_list_from_course_code($course['code']);
  66. if (is_array($usersInCourse) && !empty($usersInCourse)) {
  67. foreach ($usersInCourse as $user) {
  68. if ($user['status_rel'] == COURSEMANAGER) {
  69. $dataToExport['teachers'] .= $user['username'].'|';
  70. } else {
  71. $dataToExport['students'] .= $user['username'].'|';
  72. }
  73. }
  74. }
  75. $dataToExport['students'] = substr($dataToExport['students'], 0, -1);
  76. $dataToExport['teachers'] = substr($dataToExport['teachers'], 0, -1);
  77. $listToExport[] = $dataToExport;
  78. }
  79. switch ($file_type) {
  80. case 'xml':
  81. // Remove header
  82. unset($listToExport[0]);
  83. Export::arrayToXml($listToExport, $archiveFile);
  84. break;
  85. case 'csv':
  86. Export::arrayToCsv($listToExport, $archiveFile);
  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, array('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();