course_intro_pdf_import.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /**
  9. * Initialization
  10. */
  11. $language_file = array('admin', 'document');
  12. $cidReset = true;
  13. //require '../inc/global.inc.php';
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. api_protect_admin_script();
  16. // temporary configuration of in which folder to upload the file in each course. Should default to '', and start with a '/' and end without it, if defined
  17. $subDir = '';
  18. //require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  19. //require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  20. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  21. $tool_name = get_lang('ImportPDFIntroToCourses');
  22. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  23. set_time_limit(0);
  24. Display :: display_header($tool_name);
  25. if ($_POST['formSent']) {
  26. if (empty($_FILES['import_file']['tmp_name'])) {
  27. $error_message = get_lang('UplUploadFailed');
  28. Display :: display_error_message($error_message, false);
  29. } else {
  30. $allowed_file_mimetype = array('zip');
  31. $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
  32. if (!in_array($ext_import_file, $allowed_file_mimetype)) {
  33. Display :: display_error_message(get_lang('YouMustImportAZipFile'));
  34. } else {
  35. $errors = import_pdfs($courses, $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 :: display_normal_message($error_message, false);
  49. } elseif ($_POST['formSent']) {
  50. Display :: display_confirmation_message('CourseIntroductionsAllImportesSuccessfully', false);
  51. }
  52. ?>
  53. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin: 0px;">
  54. <legend><?php echo $tool_name; ?></legend>
  55. <div class="control-group">
  56. <label><?php echo get_lang('ImportZipFileLocation'); ?></label>
  57. <div class="control">
  58. <input type="file" name="import_file"/>
  59. </div>
  60. </div>
  61. <div class="control-group">
  62. <div class="control">
  63. <button type="submit" class="save" value="<?php echo get_lang('Import'); ?>"><?php echo get_lang('Import'); ?></button>
  64. </div>
  65. </div>
  66. <input type="hidden" name="formSent" value="1"/>
  67. </form>
  68. <div style="clear: both;"></div>
  69. <p><?php echo get_lang('PDFsMustLookLike'); ?></p>
  70. <blockquote>
  71. <pre>
  72. <strong>CourseCode</strong>_<strong>NameOfDocument</strong>_<strong>CourseName</strong>.pdf
  73. e.g.
  74. MAT101_Introduction_Mathematics-101.pdf
  75. MAT102_Introduction_Mathematics-102.pdf
  76. ENG101_Introduction_English-101.pdf
  77. </pre>
  78. </blockquote>
  79. <?php
  80. Display :: display_footer();
  81. /**
  82. * Import PDFs
  83. * @param string Filename
  84. * @param string The subdirectory in which to put the files in each course
  85. */
  86. function import_pdfs($file, $subDir = '/') {
  87. global $_configuration;
  88. $baseDir = api_get_path(SYS_ARCHIVE_PATH);
  89. $uploadPath = 'pdfimport/';
  90. $errors = array ();
  91. if (!is_dir($baseDir.$uploadPath)) {
  92. @mkdir($baseDir.$uploadPath);
  93. }
  94. if (!unzip_uploaded_file($_FILES['import_file'], $uploadPath, $baseDir, 1024*1024*1024)) {
  95. error_log('Could not unzip uploaded file in '.__FILE__.', line '.__LINE__);
  96. return $errors;
  97. }
  98. require_once api_get_path(LIBRARY_PATH).'course_description.lib.php';
  99. require_once api_get_path(LIBRARY_PATH).'app_view.php';
  100. require_once '../course_description/course_description_controller.php';
  101. $list = scandir($baseDir.$uploadPath);
  102. $i = 0;
  103. foreach ($list as $file) {
  104. if (substr($file,0,1) == '.' or !is_file($baseDir.$uploadPath.$file)) {
  105. continue;
  106. }
  107. $parts = preg_split('/_/',$file);
  108. $course = api_get_course_info($parts[0]);
  109. if (count($course) > 0) {
  110. // Build file info because handle_uploaded_document() needs it (name, type, size, tmp_name)
  111. $fileSize = filesize($baseDir.$uploadPath.$file);
  112. $docId = add_document($course, $subDir.'/'.$file, 'file', $fileSize, $parts[1].' '.substr($parts[2],0,-4));
  113. if ($docId > 0) {
  114. if (!is_file($baseDir.$uploadPath.$file)) {
  115. error_log($baseDir.$uploadPath.$file.' does not exists in '.__FILE__);
  116. }
  117. if (is_file(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file)) {
  118. error_log(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file.' exists at destination in '.__FILE__);
  119. }
  120. if (!is_writeable(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir)) {
  121. error_log('Destination '.api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.' is NOT writeable in '.__FILE__);
  122. }
  123. // Place each file in its folder in each course
  124. $move = rename($baseDir.$uploadPath.$file, api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$subDir.'/'.$file);
  125. api_item_property_update($course, TOOL_DOCUMENT, $docId, 'DocumentAdded', api_get_user_id());
  126. // Redo visibility
  127. api_set_default_visibility(TOOL_DOCUMENT, $docId);
  128. $errors[] = array('Line' => 0, 'Code' => $course['code'], 'Title' => $course['title']);
  129. // Now add a link to the file from the Course description tool
  130. $link = '<p>Sílabo de la asignatura <a href="'.api_get_path(WEB_CODE_PATH).'document/document.php?cidReq='.$course['code'].'&id_session=0&gidReq=0&action=download&id='.$docId.'" target="_blank"><img src="'.api_get_path(WEB_IMG_PATH).'icons/32/pdf.png"></a></p>';
  131. $course_description = new CourseDescription();
  132. $session_id = api_get_session_id();
  133. $course_description->set_course_id($course['real_id']);
  134. $course_description->set_session_id($session_id);
  135. $course_description->set_title('Presentación de la asignatura');
  136. $course_description->set_content($link);
  137. $course_description->set_description_type(1);
  138. $course_description->insert();
  139. }
  140. } else {
  141. error_log($parts[0].' is not a course, apparently');
  142. $errors[] = array('Line' => 0, 'Code' => $parts[0], 'Title' => $parts[0].' - '.get_lang('CodeDoesNotExists'));
  143. }
  144. $i++; //found at least one entry that is not a dir or a .
  145. }
  146. if ($i == 0) {
  147. $errors[] = array('Line' => 0, 'Code' => '.', 'Title' => get_lang('NoPDFFoundAtRoot'));
  148. }
  149. return $errors;
  150. }