fix_missing_attendances.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Fix missing attendances by checking all empty attendances in the
  4. * c_attendance_sheet table and calling, for each miss, the corresponding
  5. * web service (transaction_34) to update the data.
  6. */
  7. require '../../main/inc/global.inc.php';
  8. require '../../main/inc/lib/attendance.lib.php';
  9. require 'config.php';
  10. require 'db_matches.php';
  11. require 'migration.class.php';
  12. require 'migration.mssql.class.php';
  13. require 'migration.custom.class.php';
  14. $sql = "SELECT count(*) FROM c_attendance_sheet WHERE presence = 4";
  15. $res = Database::query($sql);
  16. $count = Database::result($res);
  17. echo "Found $count undefined attendances\n";
  18. $s1 = "SELECT id FROM user_field WHERE field_variable = 'uidIdPersona'";
  19. $r1 = Database::query($s1);
  20. $uidf = Database::result($r1);
  21. $s2 = "SELECT id FROM session_field WHERE field_variable = 'sede'";
  22. $r2 = Database::query($s2);
  23. $sedef = Database::result($r2);
  24. $s3 = "SELECT id FROM session_field WHERE field_variable = 'uidIdPrograma'";
  25. $r3 = Database::query($s3);
  26. $sidf = Database::result($r3);
  27. $sedes = array(
  28. '8F67B2B3-667E-4EBC-8605-766D2FF71B55' => 1,
  29. '7379A7D3-6DC5-42CA-9ED4-97367519F1D9' => 2,
  30. '30DE73B6-8203-4F81-96C8-3B27977BB924' => 3,
  31. '8BA65461-60B5-4716-BEB3-22BC7B71BC09' => 4,
  32. '257AD17D-91F7-4BC8-81D4-71EBD35A4E50' => 5,
  33. );
  34. $a = new Attendance();
  35. $sqlsc = "SELECT s.id_session, c.id as course_id FROM session_rel_course s INNER JOIN course c ON s.course_code=c.code ORDER BY id_session, course_id";
  36. $ressc = Database::query($sqlsc);
  37. $sessions_list = array();
  38. while ($rowsc = Database::fetch_assoc($ressc)) {
  39. $sessions_list[$rowsc['id_session']] = $rowsc['course_id'];
  40. }
  41. //$sessions_list = SessionManager::get_sessions_list();
  42. $min = 145150;
  43. $max = 145150;
  44. // Get sessions
  45. foreach ($sessions_list as $session_id => $course_id) {
  46. if ($session_id < $min) { continue; }
  47. $out1 = "Session ".$session_id.", course ".$course_id."\n";
  48. echo $out1;
  49. // Get branch for session
  50. $ss1 = "SELECT field_value FROM session_field_values WHERE field_id = $sedef AND session_id = ".$session_id;
  51. $rs1 = Database::query($ss1);
  52. $sede = Database::result($rs1);
  53. // Get uidIdPrograma
  54. $ss2 = "SELECT field_value FROM session_field_values WHERE field_id = $sidf AND session_id = ".$session_id;
  55. $rs2 = Database::query($ss2);
  56. $sid = Database::result($rs2);
  57. // Get users in session to build users list
  58. $users = SessionManager::get_users_by_session($session_id);
  59. $u = array();
  60. foreach ($users as $user) {
  61. $u[] = $user['user_id'];
  62. }
  63. // Get courses list to get the right course (only one in each session)
  64. //$courses = SessionManager::get_course_list_by_session_id($session_id);
  65. //if (count($courses)>0) {
  66. // foreach ($courses as $course) {
  67. // $course_id = $course['id'];
  68. // break;
  69. // }
  70. if (!empty($course_id)) {
  71. $out2 = "-- Course ".$course_id."\n";
  72. // Get attendances sheets from course (only one in each course)
  73. $att = $a->get_attendances_list($course_id,$session_id);
  74. if (count($att)>0) {
  75. foreach ($att as $at) {
  76. $at_id = $at['id'];
  77. break; //get out after first result
  78. }
  79. $out3 = "---- Attendance ".$at_id."\n";
  80. $a->set_course_int_id($course_id);
  81. $cal_list = $a->get_attendance_calendar($at_id);
  82. $cal_count = count($cal_list);
  83. foreach ($cal_list as $cal) {
  84. $cal_id = $cal['id'];
  85. $sql = "SELECT * FROM c_attendance_sheet WHERE c_id = $course_id AND attendance_calendar_id = $cal_id";
  86. $res_att = Database::query($sql);
  87. $att_count = Database::num_rows($res_att);
  88. if ($att_count < count($u)) {
  89. $out4 = "------ Found $att_count when should have found ".count($u)." attendances for ".$cal['date_time']."\n";
  90. echo $out1.$out2.$out3.$out4;
  91. while ($row_att = Database::fetch_assoc($res_att)) {
  92. $atts[] = $row_att['user_id'];
  93. }
  94. $missing = array_diff($u,$atts);
  95. foreach ($missing as $u1) {
  96. $sqlu = "SELECT field_value FROM user_field_values where field_id = $uidf and user_id = $u1";
  97. $resu = Database::query($sqlu);
  98. $uid = Database::result($resu);
  99. $params = array(
  100. 'item_id' => $uid,
  101. 'orig_id' => $sid,
  102. 'info' => substr($cal['date_time'],0,10),
  103. 'branch_id' => $sedes[$sede],
  104. );
  105. $r8 = MigrationCustom::transaction_34($params,$matches['web_service_calls']);
  106. var_dump($r8);
  107. }
  108. }
  109. }
  110. //$a->update_users_results($u,$at_id);
  111. } else {
  112. //var_dump($att);
  113. }
  114. }
  115. if ($session_id>=$max) { break; }
  116. }
  117. die('Finished processing '.$max."\n");