course_intro_pdf_import.php 6.6 KB

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