special_exports.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  5. /**
  6. * Special exports.
  7. *
  8. * @author Jhon Hinojosa
  9. * @author Julio Montoya Fixing pclzip folder + some clean <gugli100@gmail.com>
  10. */
  11. $cidReset = true;
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_protect_admin_script(true);
  14. api_set_more_memory_and_time_limits();
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  17. $nameTools = get_lang('Special exports');
  18. $export = '';
  19. $querypath = '';
  20. Display::display_header($nameTools);
  21. echo Display::page_header($nameTools);
  22. if (count($_POST) == 0) {
  23. echo Display::return_message(get_lang('Special exportsIntroduction'));
  24. }
  25. $error = 0;
  26. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  27. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  28. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
  29. (isset($_POST['backup_option']) && $_POST['backup_option'] == 'full_backup')
  30. ) {
  31. $export = false;
  32. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  33. $FileZip = create_zip();
  34. $to_group_id = 0;
  35. $sql_session = "SELECT id, name FROM $tbl_session ";
  36. $query_session = Database::query($sql_session);
  37. $ListSession = [];
  38. while ($rows_session = Database::fetch_assoc($query_session)) {
  39. $ListSession[$rows_session['id']] = $rows_session['name'];
  40. }
  41. $groupCondition = " props.to_group_id = $to_group_id";
  42. if (empty($to_group_id)) {
  43. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  44. }
  45. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  46. if (!isset($_POST['resource']) || count($_POST['resource']) == 0) {
  47. echo Display::return_message(get_lang('There were no courses registered or may not have made the association with the sessions'), 'error');
  48. } else {
  49. $Resource = $_POST['resource'];
  50. foreach ($Resource as $Code_course => $Sessions) {
  51. $_course = api_get_course_info($Code_course);
  52. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  53. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  54. $course_id = $_course['real_id'];
  55. //Add item to the zip file course
  56. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  57. WHERE props.tool='".TOOL_DOCUMENT."'
  58. AND docs.id=props.ref
  59. AND docs.path LIKE '".$querypath."/%'
  60. AND docs.filetype='file'
  61. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  62. AND props.visibility<>'2'
  63. AND $groupCondition
  64. AND docs.c_id = $course_id
  65. AND props.c_id = $course_id";
  66. $query = Database::query($sql);
  67. while ($rows_course_file = Database::fetch_assoc($query)) {
  68. $zip_folder->add(
  69. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  70. PCLZIP_OPT_ADD_PATH,
  71. $_course['directory'],
  72. PCLZIP_OPT_REMOVE_PATH,
  73. $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  74. );
  75. }
  76. foreach ($Sessions as $IdSession => $value) {
  77. $session_id = (int) $IdSession;
  78. //Add tem to the zip file session course
  79. $sql_session_doc = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  80. WHERE props.tool='".TOOL_DOCUMENT."'
  81. AND docs.id=props.ref
  82. AND docs.path LIKE '".$querypath."/%'
  83. AND docs.filetype='file'
  84. AND docs.session_id = '$session_id'
  85. AND props.visibility<>'2'
  86. AND $groupCondition
  87. AND docs.c_id = $course_id
  88. AND props.c_id = $course_id";
  89. $query_session_doc = Database::query($sql_session_doc);
  90. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  91. $zip_folder->add(
  92. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  93. PCLZIP_OPT_ADD_PATH,
  94. $_course['directory']."/".$ListSession[$session_id],
  95. PCLZIP_OPT_REMOVE_PATH,
  96. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  97. );
  98. }
  99. }
  100. }
  101. $name = rename_zip($FileZip);
  102. $export = true;
  103. }
  104. } else {
  105. $name = fullexportspecial();
  106. }
  107. }
  108. if ($export && $name) {
  109. echo Display::return_message(get_lang('The backup has been created. The download of this file will start in a few moments. If your download does not start, click the following link'), 'confirm');
  110. echo '<br /><a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'course_info/download.php?archive_path=&archive='.urlencode($name).'">'.get_lang('Download').'</a>';
  111. } else {
  112. // Display forms especial export
  113. if (isset($_POST['backup_option']) && $_POST['backup_option'] == 'select_items') {
  114. $cb = new CourseBuilder();
  115. $course = $cb->build_session_course();
  116. if ($course === false) {
  117. echo Display::return_message(get_lang('There were no courses registered or may not have made the association with the sessions'), 'error');
  118. form_special_export();
  119. } else {
  120. echo Display::return_message(get_lang('If you want to export courses containing sessions, which will ensure that these seansido included in the export to any of that will have to choose in the list.'), 'normal');
  121. CourseSelectForm :: display_form_session_export($course);
  122. }
  123. } else {
  124. form_special_export();
  125. }
  126. }
  127. Display::display_footer();
  128. function form_special_export()
  129. {
  130. $form = new FormValidator('special_exports', 'post');
  131. $renderer = $form->defaultRenderer();
  132. $renderer->setCustomElementTemplate('<div>{element}</div> ');
  133. $form->addElement('radio', 'backup_option', '', get_lang('Special create full backup'), 'full_backup');
  134. $form->addElement('radio', 'backup_option', '', get_lang('Special let me select items'), 'select_items');
  135. $form->addElement('html', '<br />');
  136. $form->addButtonExport(get_lang('Create a backup'));
  137. $form->addProgress();
  138. $values['backup_option'] = 'full_backup';
  139. $form->setDefaults($values);
  140. $form->display();
  141. }
  142. function create_zip()
  143. {
  144. $path = '';
  145. if (empty($path)) {
  146. $path = '/';
  147. }
  148. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  149. $sys_archive_path = api_get_path(SYS_ARCHIVE_PATH).'special_export/';
  150. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  151. $temp_zip_dir = $sys_archive_path;
  152. if (!is_dir($temp_zip_dir)) {
  153. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  154. } else {
  155. $handle = opendir($temp_zip_dir);
  156. while (false !== ($file = readdir($handle))) {
  157. if ($file != "." && $file != "..") {
  158. $Diff = (time() - filemtime("$temp_zip_dir/$file")) / 60 / 60; //the "age" of the file in hours
  159. if ($Diff > 4) {
  160. unlink("$temp_zip_dir/$file");
  161. } //delete files older than 4 hours
  162. }
  163. }
  164. closedir($handle);
  165. }
  166. $temp_zip_file = $temp_zip_dir."/".md5(time()).".zip"; //create zipfile of given directory
  167. return [
  168. 'PATH' => $path,
  169. 'PATH_TEMP_ARCHIVE' => $temp_zip_dir,
  170. 'PATH_COURSE' => $sys_course_path,
  171. 'TEMP_FILE_ZIP' => $temp_zip_file,
  172. 'PATH_REMOVE' => $remove_dir,
  173. ];
  174. }
  175. function rename_zip($FileZip)
  176. {
  177. Event::event_download(($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip (folder)' : basename($FileZip['PATH']).'.zip (folder)');
  178. $name = ($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip' : basename($FileZip['PATH']).'.zip';
  179. if (file_exists($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name)) {
  180. unlink($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name);
  181. }
  182. if (file_exists($FileZip['TEMP_FILE_ZIP'])) {
  183. rename(
  184. $FileZip['TEMP_FILE_ZIP'],
  185. $FileZip['PATH_TEMP_ARCHIVE'].'/'.$name
  186. );
  187. return $name;
  188. } else {
  189. return false;
  190. }
  191. }
  192. function fullexportspecial()
  193. {
  194. global $tbl_session, $tbl_session_course, $export;
  195. $FileZip = create_zip();
  196. $to_group_id = 0;
  197. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  198. $list_course = CourseManager::get_course_list();
  199. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  200. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  201. $groupCondition = " props.to_group_id = $to_group_id";
  202. if (empty($to_group_id)) {
  203. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  204. }
  205. if (count($list_course) > 0) {
  206. foreach ($list_course as $_course) {
  207. if ($FileZip['PATH'] == '/') {
  208. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  209. } else {
  210. $querypath = $FileZip['PATH'];
  211. }
  212. $course_id = $_course['real_id'];
  213. //Add tem to the zip file course
  214. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  215. WHERE props.tool='".TOOL_DOCUMENT."'
  216. AND docs.id=props.ref
  217. AND docs.path LIKE '".$querypath."/%'
  218. AND docs.filetype='file'
  219. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  220. AND props.visibility<>'2'
  221. AND $groupCondition
  222. AND docs.c_id = $course_id
  223. AND props.c_id = $course_id";
  224. $query = Database::query($sql);
  225. while ($rows_course_file = Database::fetch_assoc($query)) {
  226. $zip_folder->add(
  227. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  228. PCLZIP_OPT_ADD_PATH,
  229. $_course['directory'],
  230. PCLZIP_OPT_REMOVE_PATH,
  231. $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  232. );
  233. }
  234. //Add tem to the zip file session course
  235. $sql = "SELECT s.id, name, c_id
  236. FROM $tbl_session_course sc
  237. INNER JOIN $tbl_session s
  238. ON sc.session_id = s.id
  239. WHERE c_id = '$course_id' ";
  240. $query_session = Database::query($sql);
  241. while ($rows_session = Database::fetch_assoc($query_session)) {
  242. $session_id = $rows_session['id'];
  243. $sql_session_doc = "SELECT path
  244. FROM $tbl_document AS docs, $tbl_property AS props
  245. WHERE props.tool='".TOOL_DOCUMENT."'
  246. AND docs.id=props.ref
  247. AND docs.path LIKE '".$querypath."/%'
  248. AND docs.filetype='file'
  249. AND docs.session_id = '$session_id'
  250. AND props.visibility<>'2'
  251. AND $groupCondition
  252. AND docs.c_id = $course_id
  253. AND props.c_id = $course_id ";
  254. $query_session_doc = Database::query($sql_session_doc);
  255. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  256. $zip_folder->add(
  257. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  258. PCLZIP_OPT_ADD_PATH,
  259. $_course['directory']."/".$rows_session['name'],
  260. PCLZIP_OPT_REMOVE_PATH,
  261. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  262. );
  263. }
  264. }
  265. }
  266. $name = rename_zip($FileZip);
  267. if ($name === false) {
  268. $export = false;
  269. return false;
  270. } else {
  271. $export = true;
  272. return $name;
  273. }
  274. } else {
  275. echo Display::return_message(get_lang('There were no courses registered or may not have made the association with the sessions'), 'error'); //main API
  276. $export = false;
  277. return false;
  278. }
  279. }