special_exports.php 12 KB

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