course_intro_pdf_import.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * This tool allows platform admins to upload a massive amount of PDFs to be
  6. * uploaded in each course
  7. * @package chamilo.admin
  8. */
  9. $cidReset = true;
  10. //require '../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. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin'));
  18. set_time_limit(0);
  19. Display :: display_header($tool_name);
  20. if ($_POST['formSent']) {
  21. if (empty($_FILES['import_file']['tmp_name'])) {
  22. $error_message = get_lang('UplUploadFailed');
  23. Display :: display_error_message($error_message, false);
  24. } else {
  25. $allowed_file_mimetype = array('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 :: display_error_message(get_lang('YouMustImportAZipFile'));
  29. } else {
  30. $errors = import_pdfs($courses, $subDir);
  31. if (count($errors) == 0) {
  32. error_log('Course intros imported successfully in '.__FILE__.', line '.__LINE__);
  33. }
  34. }
  35. }
  36. }
  37. if (count($errors) != 0) {
  38. $error_message = '<ul>';
  39. foreach ($errors as $index => $error_course) {
  40. $error_message .= '<li>'.get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')</li>';
  41. }
  42. $error_message .= '</ul>';
  43. Display :: display_normal_message($error_message, false);
  44. } elseif ($_POST['formSent']) {
  45. Display :: display_confirmation_message('CourseIntroductionsAllImportesSuccessfully', false);
  46. }
  47. ?>
  48. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin: 0px;">
  49. <legend><?php echo $tool_name; ?></legend>
  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_Mathematics-101.pdf
  70. MAT102_Introduction_Mathematics-102.pdf
  71. ENG101_Introduction_English-101.pdf
  72. </pre>
  73. </blockquote>
  74. <?php
  75. Display :: display_footer();
  76. /**
  77. * Import PDFs
  78. * @param string Filename
  79. * @param string The subdirectory in which to put the files in each course
  80. */
  81. function import_pdfs($file, $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. $move = 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('Presentación de la asignatura');
  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. }