course_intro_pdf_import.php 7.1 KB

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