course_export.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 '../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. $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. '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. $dataToExport['tutor_name'] = str_replace(';',',',$course['tutor_name']);
  55. $dataToExport['course_language'] = str_replace(';',',',$course['course_language']);
  56. $dataToExport['students'] = '';
  57. $dataToExport['teachers'] = '';
  58. $usersInCourse = CourseManager::get_user_list_from_course_code($course['code']);
  59. foreach ($usersInCourse as $user) {
  60. if ($user['status_rel'] == COURSEMANAGER) {
  61. $dataToExport['teachers'] .= $user['username'].'|';
  62. } else {
  63. $dataToExport['students'] .= $user['username'].'|';
  64. }
  65. }
  66. $dataToExport['students'] = substr($dataToExport['students'], 0, -1);
  67. $dataToExport['teachers'] = substr($dataToExport['teachers'], 0, -1);
  68. $listToExport[] = $dataToExport;
  69. }
  70. switch ($file_type) {
  71. case 'xml':
  72. // Remove header
  73. unset($listToExport[0]);
  74. Export::arrayToXml($listToExport, $archiveFile);
  75. break;
  76. case 'csv':
  77. Export::arrayToCsv($listToExport, $archiveFile);
  78. case 'xls':
  79. Export::arrayToXls($listToExport, $archiveFile);
  80. break;
  81. }
  82. } else {
  83. Display::addFlash(
  84. Display::return_message(
  85. get_lang('ThereAreNotSelectedCoursesOrCoursesListIsEmpty')
  86. )
  87. );
  88. }
  89. }
  90. Display:: display_header($tool_name);
  91. $form = new FormValidator('export', 'post', api_get_self());
  92. $form->addHeader($tool_name);
  93. $form->addHidden('formSent', 1);
  94. $form->addElement(
  95. 'radio',
  96. 'select_type',
  97. get_lang('Option'),
  98. get_lang('ExportAllCoursesList'),
  99. '1',
  100. ['onclick' => "javascript: if(this.checked){document.getElementById('div-course-list').style.display='none';}"]
  101. );
  102. $form->addElement(
  103. 'radio',
  104. 'select_type',
  105. '',
  106. get_lang('ExportSelectedCoursesFromCoursesList'),
  107. '2',
  108. ['onclick' => "javascript: if(this.checked){document.getElementById('div-course-list').style.display='block';}"]
  109. );
  110. if (!empty($course_list)) {
  111. $form->addHtml('<div id="div-course-list" style="display:none">');
  112. $coursesInList = [];
  113. foreach ($course_list as $course) {
  114. $coursesInList[$course['code']] = $course['title'].' ('.$course['code'].')';
  115. }
  116. $form->addSelect(
  117. 'course_code',
  118. get_lang('WhichCoursesToExport'),
  119. $coursesInList,
  120. ['multiple' => 'multiple']
  121. );
  122. $form->addHtml('</div>');
  123. }
  124. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'CSV' , 'csv', null);
  125. $form->addElement('radio', 'file_type', '' , 'XLS' , 'xls', null);
  126. $form->addElement('radio', 'file_type', null, 'XML', 'xml', null, array('id' => 'file_type_xml'));
  127. $form->setDefaults(['select_type' => '1', 'file_type' => 'csv']);
  128. $form->addButtonExport(get_lang('ExportCourses'));
  129. $form->display();
  130. Display :: display_footer();