sessions_filler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * This script fills the session_* tables with users relationships,
  4. * taking into account an homogeneous distribution withing x buildings, y rooms
  5. * and z schedules. Not for use on production servers
  6. * @packacke chamilo.datafiller
  7. */
  8. /**
  9. * Initialization - remove or comment first line to execute
  10. */
  11. die('Please remove line '.__LINE__.' of this scripts if you are sure you know'."\n".'what you\'re doing'."\n");
  12. require_once '../../main/inc/global.inc.php';
  13. $t0 = time();
  14. $user_offset = 30; //30 students per room
  15. $user = 0;
  16. $course_id = 1;
  17. $session = 0;
  18. $session_id = 0;
  19. //$building_offset = 4; //number of buildings per UGEL - not active yet
  20. $building = 0;
  21. $room_offset = 4;
  22. $room = 0;
  23. $turn_offset = 4;
  24. $turn = 0;
  25. // Query caching
  26. $sqlsc_header = "INSERT INTO ".TABLE_MAIN_SESSION_COURSE." (id_session, c_id, nbr_users) values ";
  27. $sqlsu_header = "INSERT INTO ".TABLE_MAIN_SESSION_USER." (id_session, id_user) values ";
  28. $sqlscu_header = "INSERT INTO ".TABLE_MAIN_SESSION_COURSE_USER." (id_session, c_id, id_user, visibility) values ";
  29. $sqlsc = $sqlsc_header;
  30. $sqlsu = $sqlsu_header;
  31. $sqlscu = $sqlscu_header;
  32. /**
  33. * Code core
  34. */
  35. // Users range
  36. $sql = "SELECT user_id FROM user WHERE username like 'user%'";
  37. $res = Database::query($sql);
  38. while ($rowu = Database::fetch_array($res)) {
  39. $user_id = $rowu['user_id'];
  40. echo "User id: $user_id\n";
  41. $user++;
  42. if ($user % $user_offset === 1) {
  43. // if we reached 30 students or so, switch turns
  44. $turn++;
  45. // we passed 30, create new session
  46. if ($turn % $turn_offset === 1) {
  47. //switch turn *and* room
  48. $turn = 1;
  49. $room++;
  50. if ($room % $room_offset === 1) {
  51. //switch room *and* building
  52. $room = 1;
  53. $building++;
  54. }
  55. }
  56. //flush insert buffer before inserting new session
  57. if ($session_id != 0) {
  58. $ressc = Database::query(substr($sqlsc,0,-1));
  59. $ressu = Database::query(substr($sqlsu,0,-1));
  60. $resscu = Database::query(substr($sqlscu,0,-1));
  61. $sqlsc = $sqlsc_header;
  62. $sqlsu = $sqlsu_header;
  63. $sqlscu = $sqlscu_header;
  64. }
  65. // now insert new session
  66. $params = array(
  67. 'id_coach' => 1,
  68. 'name' => 'Directivos - Local '.$building.' - Aula '.$room.' - Turno '.$turn,
  69. 'nbr_courses' => 1,
  70. 'nbr_users' => 30,
  71. 'nbr_classes' => 0,
  72. 'session_admin_id' => 1,
  73. 'visibility' => SESSION_INVISIBLE,
  74. 'session_category_id' => 0,
  75. 'promotion_id' => 0,
  76. 'display_start_date' => '2013-09-01 00:00:00',
  77. 'display_end_date' => '2013-09-30 00:00:00',
  78. 'access_start_date' => '2013-09-01 00:00:00',
  79. 'access_end_date' => '2013-09-30 00:00:00',
  80. 'coach_access_start_date' => '2013-09-01 00:00:00',
  81. 'coach_access_end_date' => '2013-09-30 00:00:00'
  82. );
  83. $session_id = SessionManager::add($params);
  84. if ($session_id === false) {
  85. die('Error inserting session with params '.print_r($params,1));
  86. }
  87. echo "New session: $session_id\n";
  88. $session++;
  89. }
  90. $sqlsc .= "($session_id, 1, 30),";
  91. $sqlsu .= "($session_id,$user_id),";
  92. $sqlscu .= "($session_id, 1, $user_id, 4),";
  93. }
  94. //flush the last pending bits
  95. $ressc = Database::query(substr($sqlsc,0,-1));
  96. $ressu = Database::query(substr($sqlsu,0,-1));
  97. $resscu = Database::query(substr($sqlscu,0,-1));
  98. echo "Inserted $user users in $session sessions, approximately $user_offset per session\n";
  99. $tf = time() - $t0;
  100. echo "Process took $tf for $user users\n";