course_intro_pdf_import.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to upload a massive amount of PDFs to be
  5. * uploaded in each course
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. // temporary configuration of in which folder to upload the file in each course.
  13. // Should default to '', and start with a '/' and end without it, if defined
  14. $subDir = '';
  15. $tool_name = get_lang('ImportPDFIntroToCourses');
  16. $errors = [];
  17. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  18. set_time_limit(0);
  19. if ($_POST['formSent']) {
  20. if (empty($_FILES['import_file']['tmp_name'])) {
  21. $error_message = get_lang('UplUploadFailed');
  22. Display::addFlash(Display::return_message($error_message, 'error', false));
  23. } else {
  24. $allowed_file_mimetype = array('zip');
  25. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  26. if (!in_array($ext_import_file, $allowed_file_mimetype)) {
  27. Display::addFlash(Display::return_message(get_lang('YouMustImportAZipFile'), 'error'));
  28. } else {
  29. $errors = import_pdfs($subDir);
  30. if (count($errors) == 0) {
  31. error_log('Course intros imported successfully in '.__FILE__.', line '.__LINE__);
  32. }
  33. }
  34. }
  35. }
  36. if (count($errors) != 0) {
  37. $error_message = '<ul>';
  38. foreach ($errors as $index => $error_course) {
  39. $error_message .= '<li>'.get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')</li>';
  40. }
  41. $error_message .= '</ul>';
  42. Display::addFlash(Display::return_message($error_message, 'normal', false));
  43. } elseif ($_POST['formSent']) {
  44. Display::addFlash(Display::return_message('CourseIntroductionsAllImportesSuccessfully', 'confirmation', false));
  45. }
  46. Display::display_header($tool_name);
  47. ?>
  48. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin: 0;">
  49. <h3><?php echo $tool_name; ?></h3>
  50. <div class="control-group">
  51. <label><?php echo get_lang('ImportZipFileLocation'); ?></label>
  52. <div class="control">
  53. <input type="file" name="import_file"/>
  54. </div>
  55. </div>
  56. <div class="control-group">
  57. <div class="control">
  58. <button type="submit" class="save" value="<?php echo get_lang('Import'); ?>"><?php echo get_lang('Import'); ?></button>
  59. </div>
  60. </div>
  61. <input type="hidden" name="formSent" value="1"/>
  62. </form>
  63. <div style="clear: both;"></div>
  64. <p><?php echo get_lang('PDFsMustLookLike'); ?></p>
  65. <blockquote>
  66. <pre>
  67. <strong>CourseCode</strong>_<strong>NameOfDocument</strong>_<strong>CourseName</strong>.pdf
  68. e.g.
  69. MAT101_Introduction_to_Mathematics-101.pdf
  70. MAT102_Introduction_to_Mathematics-102.pdf
  71. ENG101_Introduction_to_English-101.pdf
  72. </pre>
  73. </blockquote>
  74. <?php
  75. Display::display_footer();
  76. /**
  77. * Import PDFs
  78. * @param string $subDir The subdirectory in which to put the files in each course
  79. * @return array List of possible errors found
  80. */
  81. function import_pdfs($subDir = '/')
  82. {
  83. $baseDir = api_get_path(SYS_ARCHIVE_PATH);
  84. $uploadPath = 'pdfimport/';
  85. $errors = array();
  86. if (!is_dir($baseDir.$uploadPath)) {
  87. @mkdir($baseDir.$uploadPath);
  88. }
  89. if (!unzip_uploaded_file($_FILES['import_file'], $uploadPath, $baseDir, 1024 * 1024 * 1024)) {
  90. error_log('Could not unzip uploaded file in '.__FILE__.', line '.__LINE__);
  91. return $errors;
  92. }
  93. $list = scandir($baseDir.$uploadPath);
  94. $i = 0;
  95. foreach ($list as $file) {
  96. if (substr($file, 0, 1) == '.' or !is_file($baseDir.$uploadPath.$file)) {
  97. continue;
  98. }
  99. $parts = preg_split('/_/', $file);
  100. $course = api_get_course_info($parts[0]);
  101. if (count($course) > 0) {
  102. // Build file info because handle_uploaded_document() needs it (name, type, size, tmp_name)
  103. $fileSize = filesize($baseDir.$uploadPath.$file);
  104. $docId = add_document($course, $subDir.'/'.$file, 'file', $fileSize, $parts[1].' '.substr($parts[2], 0, -4));
  105. if ($docId > 0) {
  106. if (!is_file($baseDir.$uploadPath.$file)) {
  107. error_log($baseDir.$uploadPath.$file.' does not exists in '.__FILE__);
  108. }
  109. if (is_file(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file)) {
  110. error_log(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file.' exists at destination in '.__FILE__);
  111. }
  112. if (!is_writeable(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir)) {
  113. error_log('Destination '.api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.' is NOT writeable in '.__FILE__);
  114. }
  115. // Place each file in its folder in each course
  116. rename($baseDir.$uploadPath.$file, api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file);
  117. api_item_property_update($course, TOOL_DOCUMENT, $docId, 'DocumentAdded', api_get_user_id());
  118. // Redo visibility
  119. api_set_default_visibility($docId, TOOL_DOCUMENT);
  120. $errors[] = array('Line' => 0, 'Code' => $course['code'], 'Title' => $course['title']);
  121. // Now add a link to the file from the Course description tool
  122. $link = '<p>Sílabo de la asignatura
  123. <a href="'.api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq_params($course['code']).'&action=download&id='.$docId.'" target="_blank">
  124. '.Display::return_icon('pdf.png').'
  125. </a></p>';
  126. $course_description = new CourseDescription();
  127. $session_id = api_get_session_id();
  128. $course_description->set_course_id($course['real_id']);
  129. $course_description->set_session_id($session_id);
  130. $course_description->set_title('Course presentation');
  131. $course_description->set_content($link);
  132. $course_description->set_description_type(1);
  133. $course_description->insert();
  134. }
  135. } else {
  136. error_log($parts[0].' is not a course, apparently');
  137. $errors[] = array('Line' => 0, 'Code' => $parts[0], 'Title' => $parts[0].' - '.get_lang('CodeDoesNotExists'));
  138. }
  139. $i++; //found at least one entry that is not a dir or a .
  140. }
  141. if ($i == 0) {
  142. $errors[] = array('Line' => 0, 'Code' => '.', 'Title' => get_lang('NoPDFFoundAtRoot'));
  143. }
  144. return $errors;
  145. }