course_export.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if ($_POST['formSent']) {
  23. $formSent =$_POST['formSent'];
  24. $select_type=intval($_POST['select_type']);
  25. $file_type = 'csv';
  26. $courses = $selected_courses = array();
  27. if ($select_type == 2) {
  28. // Get selected courses from courses list in form sent
  29. $selected_courses = $_POST['course_code'];
  30. if (is_array($selected_courses)) {
  31. foreach ($course_list as $course) {
  32. if (!in_array($course['code'],$selected_courses)) continue;
  33. $courses[] = $course;
  34. }
  35. }
  36. } else {
  37. // Get all courses
  38. $courses = $course_list;
  39. }
  40. if (!empty($courses)) {
  41. if (!file_exists($archivePath)) {
  42. mkdir($archivePath, api_get_permissions_for_new_directories(), true);
  43. }
  44. $archiveFile = 'export_courses_list_'.date('Y-m-d_H-i-s').'.'.$file_type;
  45. $fp = fopen($archivePath.$archiveFile,'w');
  46. if ($file_type == 'csv') {
  47. $add = "Code;Title;CourseCategory;Teacher;Language;".PHP_EOL;
  48. foreach($courses as $course) {
  49. $course['code'] = str_replace(';',',',$course['code']);
  50. $course['title'] = str_replace(';',',',$course['title']);
  51. $course['category_code'] = str_replace(';',',',$course['category_code']);
  52. $course['tutor_name'] = str_replace(';',',',$course['tutor_name']);
  53. $course['course_language'] = str_replace(';',',',$course['course_language']);
  54. $add.= $course['code'].';'.$course['title'].';'.$course['category_code'].';'.$course['tutor_name'].';'.$course['course_language'].';'.PHP_EOL;
  55. }
  56. fputs($fp, $add);
  57. }
  58. fclose($fp);
  59. $msg = get_lang('CoursesListHasBeenExported').'<br/><a class="btn" href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  60. } else {
  61. $msg = get_lang('ThereAreNotSelectedCoursesOrCoursesListIsEmpty');
  62. }
  63. }
  64. if (!empty($msg)) {
  65. Display::display_normal_message($msg, false);
  66. }
  67. ?>
  68. <form method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  69. <input type="hidden" name="formSent" value="1">
  70. <legend><?php echo $tool_name; ?></legend>
  71. <?php if (!empty($course_list)) { ?>
  72. <div class="control-group">
  73. <div class="controls">
  74. <label class="radio" for="all-courses">
  75. <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';}"/>
  76. <?php echo get_lang('ExportAllCoursesList')?>
  77. </label>
  78. <label class="radio" for="select-courses">
  79. <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';}"/>
  80. <?php echo get_lang('ExportSelectedCoursesFromCoursesList')?>
  81. </label>
  82. </div>
  83. </div>
  84. <div id="div-course-list" style="<?php echo (!$formSent || ($formSent && $select_type == 1))?'display:none':'display:block';?>">
  85. <table border="0" cellpadding="5" cellspacing="0">
  86. <tr>
  87. <td valign="top"><?php echo get_lang('WhichCoursesToExport'); ?> :</td>
  88. <td>
  89. <select name="course_code[]" multiple="multiple" size="10">
  90. <?php
  91. foreach($course_list as $course) {
  92. ?>
  93. <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>
  94. <?php
  95. }
  96. ?>
  97. </select>
  98. </td>
  99. </tr>
  100. </table>
  101. </div>
  102. <br />
  103. <div id="actions">
  104. <button class="save" type="submit" name="name" value="<?php echo get_lang('ExportCourses') ?>"><?php echo get_lang('ExportCourses') ?></button>
  105. </div>
  106. <?php } else { echo get_lang('ThereAreNotCreatedCourses'); }?>
  107. </form>
  108. <?php
  109. Display :: display_footer();