course_export.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  8. * Code
  9. */
  10. $language_file = array ('admin', 'registration','create_course', 'document');
  11. $cidReset = true;
  12. ////require_once '../inc/global.inc.php';
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. api_protect_admin_script();
  15. $tool_name = get_lang('ExportCourses').' CSV';
  16. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  17. set_time_limit(0);
  18. Display :: display_header($tool_name);
  19. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  20. $archiveURL = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive=';
  21. $course_list = CourseManager::get_courses_list();
  22. $formSent = null;
  23. if (isset($_POST['formSent']) && $_POST['formSent']) {
  24. $formSent = $_POST['formSent'];
  25. $select_type=intval($_POST['select_type']);
  26. $file_type = 'csv';
  27. $courses = $selected_courses = array();
  28. if ($select_type == 2) {
  29. // Get selected courses from courses list in form sent
  30. $selected_courses = $_POST['course_code'];
  31. if (is_array($selected_courses)) {
  32. foreach ($course_list as $course) {
  33. if (!in_array($course['code'],$selected_courses)) continue;
  34. $courses[] = $course;
  35. }
  36. }
  37. } else {
  38. // Get all courses
  39. $courses = $course_list;
  40. }
  41. if (!empty($courses)) {
  42. if (!file_exists($archivePath)) {
  43. mkdir($archivePath, api_get_permissions_for_new_directories(), true);
  44. }
  45. $archiveFile = 'export_courses_list_'.date('Y-m-d_H-i-s').'.'.$file_type;
  46. $fp = fopen($archivePath.$archiveFile,'w');
  47. if ($file_type == 'csv') {
  48. $add = "Code;Title;CourseCategory;Teacher;Language;OtherTeachers;Users;".PHP_EOL;
  49. foreach($courses as $course) {
  50. $course['code'] = str_replace(';',',',$course['code']);
  51. $course['title'] = str_replace(';',',',$course['title']);
  52. $course['category_code'] = str_replace(';',',',$course['category_code']);
  53. $course['tutor_name'] = str_replace(';',',',$course['tutor_name']);
  54. $course['course_language'] = str_replace(';',',',$course['course_language']);
  55. $course['course_users'] = CourseManager::get_user_list_from_course_code($course['code']);
  56. $course['students'] = '';
  57. $course['teachers'] = '';
  58. foreach ($course['course_users'] as $user) {
  59. if ($user['status_rel'] == 1) {
  60. $course['teachers'] .= $user['username'].'|';
  61. } else {
  62. $course['students'] .= $user['username'].'|';
  63. }
  64. }
  65. $course['students'] = substr($course['students'],0,-1);
  66. $course['teachers'] = substr($course['teachers'],0,-1);
  67. $add.= $course['code'].';'.$course['title'].';'.$course['category_code'].';'.$course['tutor_name'].';'.$course['course_language'].';'.$course['teachers'].';'.$course['students'].';'.PHP_EOL;
  68. }
  69. fputs($fp, $add);
  70. }
  71. fclose($fp);
  72. $msg = get_lang('CoursesListHasBeenExported').'<br/><a class="btn" href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  73. } else {
  74. $msg = get_lang('ThereAreNotSelectedCoursesOrCoursesListIsEmpty');
  75. }
  76. }
  77. if (!empty($msg)) {
  78. Display::display_normal_message($msg, false);
  79. }
  80. ?>
  81. <form method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  82. <input type="hidden" name="formSent" value="1">
  83. <legend><?php echo $tool_name; ?></legend>
  84. <?php if (!empty($course_list)) { ?>
  85. <div class="control-group">
  86. <div class="controls">
  87. <label class="radio" for="all-courses">
  88. <input id="all-courses" class="checkbox" type="radio" value="1" name="select_type" <?php if(!$formSent || ($formSent && $select_type == 1)) echo 'checked="checked"'; ?> onclick="javascript: if(this.checked){document.getElementById('div-course-list').style.display='none';}"/>
  89. <?php echo get_lang('ExportAllCoursesList')?>
  90. </label>
  91. <label class="radio" for="select-courses">
  92. <input id="select-courses" class="checkbox" type="radio" value="2" name="select_type" <?php if($formSent && $select_type == 2) echo 'checked="checked"'; ?> onclick="javascript: if(this.checked){document.getElementById('div-course-list').style.display='block';}"/>
  93. <?php echo get_lang('ExportSelectedCoursesFromCoursesList')?>
  94. </label>
  95. </div>
  96. </div>
  97. <div id="div-course-list" style="<?php echo (!$formSent || ($formSent && $select_type == 1))?'display:none':'display:block';?>">
  98. <table border="0" cellpadding="5" cellspacing="0">
  99. <tr>
  100. <td valign="top"><?php echo get_lang('WhichCoursesToExport'); ?> :</td>
  101. <td>
  102. <select name="course_code[]" multiple="multiple" size="10">
  103. <?php
  104. foreach($course_list as $course) {
  105. ?>
  106. <option value="<?php echo $course['code']; ?>" <?php if(is_array($selected_courses) && in_array($course['code'],$selected_courses)) echo 'selected="selected"'; ?>><?php echo $course['title'].' ('.$course['code'].') ' ?></option>
  107. <?php
  108. }
  109. ?>
  110. </select>
  111. </td>
  112. </tr>
  113. </table>
  114. </div>
  115. <br />
  116. <div id="actions">
  117. <button class="save" type="submit" name="name" value="<?php echo get_lang('ExportCourses') ?>"><?php echo get_lang('ExportCourses') ?></button>
  118. </div>
  119. <?php } else { echo get_lang('ThereAreNotCreatedCourses'); }?>
  120. </form>
  121. <?php
  122. Display :: display_footer();