course_intro_pdf_import.php 7.1 KB

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