delete_old_courses_even_not_empty.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * This script synchronize the exercise score (track_e_exercises.exe_result)
  4. * with the LP score result (lp_item_view.score).
  5. * This script works only if 1 there's one attempt
  6. */
  7. exit;
  8. require_once '../../main/inc/global.inc.php';
  9. if (PHP_SAPI !== 'cli') {
  10. die('This script can only be executed from the command line');
  11. }
  12. // The date before which the course must have been created to be considered
  13. $creation = '2013-01-01';
  14. // The last date at which the course must have been accessed to be considered.
  15. // If it was accessed *after* that date, it will NOT be considered for deletion.
  16. $access = '2013-01-01';
  17. $tableExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  18. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  19. $sql = "SELECT
  20. id, code, directory, db_name, creation_date, last_visit
  21. FROM $tableCourse c
  22. WHERE creation_date < '$creation' AND last_visit < '$access'
  23. ORDER by code
  24. ";
  25. echo $sql.PHP_EOL;
  26. $result = Database::query($sql);
  27. $items = Database::store_result($result, 'ASSOC');
  28. $total = 0;
  29. $count = 0;
  30. if (!empty($items)) {
  31. foreach ($items as $item) {
  32. $size = exec('du -sh '.__DIR__.'/../../courses/'.$item['directory']);
  33. echo "Course ".$item['code'].'('.$item['id'].') created on '.$item['creation_date'].' and last used on '.$item['last_visit'].' uses '.substr($size, 0, 8).PHP_EOL;
  34. //if (substr($size, 0, 4) == '160K' or substr($size, 0, 4) == '9,1M') {
  35. CourseManager::delete_course($item['code']);
  36. // The normal procedure moves the course directory to archive, so
  37. // delete it there as well
  38. echo('rm -rf '.__DIR__.'/../../archive/'.$item['directory'].'_*').PHP_EOL;
  39. exec('rm -rf '.__DIR__.'/../../archive/'.$item['directory'].'_*');
  40. // The normal procedure also created a database dump, but it is
  41. // stored in the course folder, so no issue there...
  42. if (substr($size, 3, 1) == 'K') {
  43. $total += substr($size, 0, 3);
  44. }
  45. if (substr($size, 3, 1) == 'M') {
  46. //$total += ;
  47. $total += substr($size, 0, 3)*1024;
  48. }
  49. $count ++;
  50. if ($count%100 == 0) {
  51. echo '### Until now: '.$total.'K in '.$count.' courses'.PHP_EOL;
  52. }
  53. //}
  54. }
  55. }
  56. echo $total.'K in '.$count.' courses'.PHP_EOL;