fix_sessions_access_dates.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Finds the sessions without access date and defines (for all sessions without access date older than 30
  4. * days in the past from now) a new access date based on the period (which can be found in the session
  5. * name).
  6. * @package chamilo.migrate
  7. */
  8. require_once '../../main/inc/global.inc.php';
  9. $log = 'sessions_dates_changes.txt';
  10. $ts = Database::get_main_table(TABLE_MAIN_SESSION);
  11. //$sql = "SELECT * FROM $ts WHERE access_start_date = '0000-00-00 00:00:00' AND access_end_date = '0000-00-00 00:00:00' ORDER BY name";
  12. //$sql = "SELECT * FROM $ts WHERE access_start_date = '0000-00-00 00:00:00' OR access_end_date = '0000-00-00 00:00:00' ORDER BY name";
  13. $sql = "SELECT * FROM $ts ORDER BY name";
  14. echo $sql."\n";
  15. $res = Database::query($sql);
  16. if ($res !== false) {
  17. echo "Found ".Database::num_rows($res)." sessions matching empty start or end date\n";
  18. while ($row = Database::fetch_assoc($res)) {
  19. //echo "Session ".$row['name']." has no start/end date\n";
  20. $matches = array();
  21. $match = preg_match('/-\s(\d{4})(\d{2})\s-/',$row['name'],$matches);
  22. $now = new DateTime(null);
  23. $cy = $now->format('Y');
  24. $cm = $now->format('m');
  25. if (!empty($match)) {
  26. $ny = $y = $matches[1];
  27. $nm = 1 + $m = $matches[2];
  28. //ignore current month
  29. if ($y == $cy && $m == $cm) { continue; }
  30. if ($m == 12) {
  31. $ny = $y+1;
  32. $nm = 1;
  33. }
  34. $start = new DateTime();
  35. $end = new DateTime();
  36. $start->setDate($y, $m, 1);
  37. $end->setDate($ny, $nm, 1);
  38. $end->modify('-1 day');
  39. $vstart = $start->format('Y-m-d H:i:s');
  40. $vend = $end->format('Y-m-d H:i:s');
  41. echo "Original period = $y$m, converted to start/stop: $vstart/$vend\n";
  42. $sql2 = "UPDATE $ts SET ";
  43. $comma = false;
  44. //if ($row['access_start_date'] == '0000-00-00 00:00:00') {
  45. $sql2 .= " access_start_date = '$vstart', coach_access_start_date = '$vstart' ";
  46. $comma = true;
  47. //}
  48. //if ($row['access_end_date'] == '0000-00-00 00:00:00') {
  49. if ($comma) { $sql2 .= ', '; }
  50. $sql2 .= " access_end_date = '$vend', coach_access_end_date = '$vend' ";
  51. //}
  52. $sql2 .= " WHERE id = ".$row['id'];
  53. $res2 = Database::query($sql2);
  54. file_put_contents($log,$row['id']."\n",FILE_APPEND);
  55. }
  56. }
  57. }