course_intro_pdf_import.php 7.1 KB

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