special_exports.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * @package chamilo.include.export
  11. */
  12. // including the global file
  13. $cidReset = true;
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $this_section = SECTION_PLATFORM_ADMIN;
  16. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  17. // Access restrictions
  18. api_protect_admin_script(true);
  19. $nameTools = get_lang('SpecialExports');
  20. $export = '';
  21. $querypath = '';
  22. // include additional libraries
  23. if (function_exists('ini_set')) {
  24. api_set_memory_limit('256M');
  25. ini_set('max_execution_time', 0);
  26. }
  27. // Displaying the header
  28. Display::display_header($nameTools);
  29. // Display the tool title
  30. echo Display::page_header($nameTools);
  31. if (count($_POST) == 0) {
  32. Display::addFlash(Display::return_message(get_lang('SpecialExportsIntroduction')));
  33. }
  34. $error = 0;
  35. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  36. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  37. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
  38. (isset($_POST['backup_option']) && $_POST['backup_option'] == 'full_backup')
  39. ) {
  40. $export = false;
  41. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  42. $FileZip = create_zip();
  43. $to_group_id = 0;
  44. $sql_session = "SELECT id, name FROM $tbl_session ";
  45. $query_session = Database::query($sql_session);
  46. $ListSession = array();
  47. while ($rows_session = Database::fetch_assoc($query_session)) {
  48. $ListSession[$rows_session['id']] = $rows_session['name'];
  49. }
  50. $groupCondition = " props.to_group_id = $to_group_id";
  51. if (empty($to_group_id)) {
  52. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  53. }
  54. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  55. if (!isset($_POST['resource']) || count($_POST['resource']) == 0) {
  56. Display::addFlash(Display::return_message(get_lang('ErrorMsgSpecialExport'), 'error'));
  57. } else {
  58. $Resource = $_POST['resource'];
  59. foreach ($Resource as $Code_course => $Sessions) {
  60. $_course = api_get_course_info($Code_course);
  61. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  62. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  63. $course_id = $_course['real_id'];
  64. //Add item to the zip file course
  65. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  66. WHERE props.tool='".TOOL_DOCUMENT."'
  67. AND docs.id=props.ref
  68. AND docs.path LIKE '".$querypath."/%'
  69. AND docs.filetype='file'
  70. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  71. AND props.visibility<>'2'
  72. AND $groupCondition
  73. AND docs.c_id = $course_id
  74. AND props.c_id = $course_id";
  75. $query = Database::query($sql);
  76. while ($rows_course_file = Database::fetch_assoc($query)) {
  77. $zip_folder->add(
  78. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  79. PCLZIP_OPT_ADD_PATH,
  80. $_course['directory'],
  81. PCLZIP_OPT_REMOVE_PATH,
  82. $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  83. );
  84. }
  85. foreach ($Sessions as $IdSession => $value) {
  86. $session_id = (int) $IdSession;
  87. //Add tem to the zip file session course
  88. $sql_session_doc = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  89. WHERE props.tool='".TOOL_DOCUMENT."'
  90. AND docs.id=props.ref
  91. AND docs.path LIKE '".$querypath."/%'
  92. AND docs.filetype='file'
  93. AND docs.session_id = '$session_id'
  94. AND props.visibility<>'2'
  95. AND $groupCondition
  96. AND docs.c_id = $course_id
  97. AND props.c_id = $course_id";
  98. $query_session_doc = Database::query($sql_session_doc);
  99. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  100. $zip_folder->add(
  101. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  102. PCLZIP_OPT_ADD_PATH,
  103. $_course['directory']."/".$ListSession[$session_id],
  104. PCLZIP_OPT_REMOVE_PATH,
  105. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  106. );
  107. }
  108. }
  109. }
  110. $name = rename_zip($FileZip);
  111. $export = true;
  112. }
  113. } else {
  114. $name = fullexportspecial();
  115. }
  116. }
  117. if ($export && $name) {
  118. Display::addFlash(Display::return_message(get_lang('BackupCreated'), 'confirm'));
  119. 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>';
  120. } else {
  121. // Display forms especial export
  122. if (isset($_POST['backup_option']) && $_POST['backup_option'] == 'select_items') {
  123. $cb = new CourseBuilder();
  124. $course = $cb->build_session_course();
  125. if ($course === false) {
  126. Display::addFlash(Display::return_message(get_lang('ErrorMsgSpecialExport'), 'error'));
  127. form_special_export();
  128. } else {
  129. Display::addFlash(Display::return_message(get_lang('ToExportSpecialSelect'), 'normal'));
  130. CourseSelectForm :: display_form_session_export($course);
  131. }
  132. } else {
  133. form_special_export();
  134. }
  135. }
  136. /* FOOTER */
  137. Display::display_footer();
  138. function form_special_export()
  139. {
  140. $form = new FormValidator('special_exports', 'post');
  141. $renderer = $form->defaultRenderer();
  142. $renderer->setCustomElementTemplate('<div>{element}</div> ');
  143. $form->addElement('radio', 'backup_option', '', get_lang('SpecialCreateFullBackup'), 'full_backup');
  144. $form->addElement('radio', 'backup_option', '', get_lang('SpecialLetMeSelectItems'), 'select_items');
  145. $form->addElement('html', '<br />');
  146. $form->addButtonExport(get_lang('CreateBackup'));
  147. $form->addProgress();
  148. $values['backup_option'] = 'full_backup';
  149. $form->setDefaults($values);
  150. $form->display();
  151. }
  152. function create_zip()
  153. {
  154. $path = '';
  155. if (empty($path)) {
  156. $path = '/';
  157. }
  158. $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
  159. $sys_archive_path = api_get_path(SYS_ARCHIVE_PATH).'special_export/';
  160. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  161. $temp_zip_dir = $sys_archive_path;
  162. if (!is_dir($temp_zip_dir)) {
  163. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  164. } else {
  165. $handle = opendir($temp_zip_dir);
  166. while (false !== ($file = readdir($handle))) {
  167. if ($file != "." && $file != "..") {
  168. $Diff = (time() - filemtime("$temp_zip_dir/$file")) / 60 / 60; //the "age" of the file in hours
  169. if ($Diff > 4) {
  170. unlink("$temp_zip_dir/$file");
  171. } //delete files older than 4 hours
  172. }
  173. }
  174. closedir($handle);
  175. }
  176. $temp_zip_file = $temp_zip_dir."/".md5(time()).".zip"; //create zipfile of given directory
  177. return array(
  178. 'PATH' => $path,
  179. 'PATH_TEMP_ARCHIVE' => $temp_zip_dir,
  180. 'PATH_COURSE' => $sys_course_path,
  181. 'TEMP_FILE_ZIP' => $temp_zip_file,
  182. 'PATH_REMOVE' => $remove_dir
  183. );
  184. }
  185. function rename_zip($FileZip)
  186. {
  187. Event::event_download(($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip (folder)' : basename($FileZip['PATH']).'.zip (folder)');
  188. $name = ($FileZip['PATH'] == '/') ? 'full_export_'.date('Ymd').'.zip' : basename($FileZip['PATH']).'.zip';
  189. if (file_exists($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name)) {
  190. unlink($FileZip['PATH_TEMP_ARCHIVE'].'/'.$name);
  191. }
  192. if (file_exists($FileZip['TEMP_FILE_ZIP'])) {
  193. rename(
  194. $FileZip['TEMP_FILE_ZIP'],
  195. $FileZip['PATH_TEMP_ARCHIVE'].'/'.$name
  196. );
  197. return $name;
  198. } else {
  199. return false;
  200. }
  201. }
  202. function fullexportspecial()
  203. {
  204. global $tbl_session, $tbl_session_course, $export;
  205. $FileZip = create_zip();
  206. $to_group_id = 0;
  207. $zip_folder = new PclZip($FileZip['TEMP_FILE_ZIP']);
  208. $list_course = CourseManager::get_course_list();
  209. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  210. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  211. $groupCondition = " props.to_group_id = $to_group_id";
  212. if (empty($to_group_id)) {
  213. $groupCondition = " (props.to_group_id = 0 OR props.to_group_id IS NULL)";
  214. }
  215. if (count($list_course) > 0) {
  216. foreach ($list_course as $_course) {
  217. if ($FileZip['PATH'] == '/') {
  218. $querypath = ''; // to prevent ...path LIKE '//%'... in query
  219. } else {
  220. $querypath = $FileZip['PATH'];
  221. }
  222. $course_id = $_course['real_id'];
  223. //Add tem to the zip file course
  224. $sql = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  225. WHERE props.tool='".TOOL_DOCUMENT."'
  226. AND docs.id=props.ref
  227. AND docs.path LIKE '".$querypath."/%'
  228. AND docs.filetype='file'
  229. AND (docs.session_id = '0' OR docs.session_id IS NULL)
  230. AND props.visibility<>'2'
  231. AND $groupCondition
  232. AND docs.c_id = $course_id
  233. AND props.c_id = $course_id";
  234. $query = Database::query($sql);
  235. while ($rows_course_file = Database::fetch_assoc($query)) {
  236. $zip_folder->add(
  237. $FileZip['PATH_COURSE'].$_course['directory']."/document".$rows_course_file['path'],
  238. PCLZIP_OPT_ADD_PATH,
  239. $_course['directory'],
  240. PCLZIP_OPT_REMOVE_PATH, $FileZip['PATH_COURSE'].$_course['directory']."/document".$FileZip['PATH_REMOVE']
  241. );
  242. }
  243. //Add tem to the zip file session course
  244. $sql = "SELECT s.id, name, c_id
  245. FROM $tbl_session_course sc
  246. INNER JOIN $tbl_session s
  247. ON sc.session_id = s.id
  248. WHERE c_id = '$course_id' ";
  249. $query_session = Database::query($sql);
  250. while ($rows_session = Database::fetch_assoc($query_session)) {
  251. $session_id = $rows_session['id'];
  252. $sql_session_doc = "SELECT path FROM $tbl_document AS docs, $tbl_property AS props
  253. WHERE props.tool='".TOOL_DOCUMENT."'
  254. AND docs.id=props.ref
  255. AND docs.path LIKE '".$querypath."/%'
  256. AND docs.filetype='file'
  257. AND docs.session_id = '$session_id'
  258. AND props.visibility<>'2'
  259. AND $groupCondition
  260. AND docs.c_id = $course_id
  261. AND props.c_id = $course_id ";
  262. $query_session_doc = Database::query($sql_session_doc);
  263. while ($rows_course_session_file = Database::fetch_assoc($query_session_doc)) {
  264. $zip_folder->add(
  265. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$rows_course_session_file['path'],
  266. PCLZIP_OPT_ADD_PATH,
  267. $_course['directory']."/".$rows_session['name'],
  268. PCLZIP_OPT_REMOVE_PATH,
  269. $FileZip['PATH_COURSE'].$_course['directory'].'/document'.$FileZip['PATH_REMOVE']
  270. );
  271. }
  272. }
  273. }
  274. $name = rename_zip($FileZip);
  275. if ($name === false) {
  276. $export = false;
  277. return false;
  278. } else {
  279. $export = true;
  280. return $name;
  281. }
  282. } else {
  283. Display::display_error_message(get_lang('ErrorMsgSpecialExport')); //main API
  284. $export = false;
  285. return false;
  286. }
  287. }