Keine Beschreibung

UserTableSeeder.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. class UserTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Disable query logging to avoid memory exhaustion
  6. DB::disableQueryLog();
  7. $this->command->info('Users table seeding started...');
  8. // Initiates time
  9. $time_start = microtime(true);
  10. // Remove access privileges to users that haven't logged in in 1 year
  11. User::whereNull('last_login')
  12. ->orWhere('last_login', '<', DB::raw('DATE_SUB(NOW(), INTERVAL 1 YEAR)'))
  13. ->update(array(
  14. 'has_access' => 0
  15. ));
  16. // Open file
  17. if($file = fopen('app/database/csv/users.csv', 'r'))
  18. {
  19. // Initialize count variables
  20. $read = 0;
  21. $added = 0;
  22. $updated = 0;
  23. $error = 0;
  24. // Read each row
  25. while($data = fgetcsv($file, 5000))
  26. {
  27. // Add read count
  28. $read++;
  29. try
  30. {
  31. // Get row info
  32. $ssn = trim($data[0]);
  33. // $number = (NOT YET ADDED)
  34. $first_name = strtoupper(trim($data[1]));
  35. $surnames = strtoupper(trim($data[2]));
  36. $email = strtolower(trim($data[3]));
  37. $emp_number = strtolower(trim($data[4]));
  38. if($email=="null" or $email=="")
  39. {
  40. $email="email".rand()."@upr.edu";
  41. // print($email);
  42. // var_dump($data);exit();
  43. }
  44. if($emp_number=="null" or $emp_number=="")
  45. {
  46. $emp_number="NO".rand(10000000,99999999);
  47. // print($emp_number);
  48. // var_dump($data);exit();
  49. }
  50. // Program unavailable.For the moment, I'm setting it to null, but it should eventually be the commented line below
  51. // $program_id = trim($data[4]);
  52. $program_id = NULL;
  53. // To output info for debugging purposes
  54. // $this->command->info(substr($ssn, -4));
  55. // $this->command->info($first_name);
  56. // $this->command->info($surnames);
  57. // $this->command->info($email);
  58. // $this->command->info($program_id);
  59. // $this->command->info('---');
  60. // If any required field is empty, raise an exception
  61. if(!$ssn || !$first_name || !$surnames || !$email)
  62. {
  63. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  64. }
  65. // If program is empty, assign default
  66. if(!$program_id)
  67. {
  68. $program_id = 999; // Default program id, see DB.
  69. }
  70. // If user exists by ssn
  71. if(User::where('ssn', $ssn)->count())
  72. {
  73. DB::table('users')
  74. ->where('ssn', ($ssn))
  75. ->update(array(
  76. 'first_name' => $first_name,
  77. 'surnames' => $surnames,
  78. 'email' => $email,
  79. 'number' => $emp_number,
  80. // 'program_id' => $program_id, //should change when I know how this info will be given
  81. 'has_access' => 1,
  82. 'updated_at' => date("Y-m-d H:i:s", time()),
  83. )
  84. );
  85. $this->command->info('Updated (via ssn): '.$email.': '.$surnames.', '.$first_name);
  86. $updated++;
  87. }
  88. // If user exists by employee number, update information and add to updated
  89. elseif(User::where('number', $emp_number)->count())
  90. {
  91. DB::table('users')
  92. ->where('number', ($emp_number))
  93. ->update(array(
  94. 'ssn' => $ssn,
  95. 'email' => $email,
  96. 'first_name' => $first_name,
  97. 'surnames' => $surnames,
  98. // 'program_id' => $program_id, //should change when I know how this info will be given
  99. 'has_access' => 1,
  100. 'updated_at' => date("Y-m-d H:i:s", time()),
  101. )
  102. );
  103. $this->command->info('Updated (via name): '.$email.': '.$surnames.', '.$first_name);
  104. $updated++;
  105. }
  106. // If user exists by email, update information and add to updated
  107. elseif(User::where('email', $email)->count())
  108. {
  109. DB::table('users')
  110. ->where('email', ($email))
  111. ->update(array(
  112. 'ssn' => $ssn,
  113. 'first_name' => $first_name,
  114. 'surnames' => $surnames,
  115. 'number' => $emp_number,
  116. // 'program_id' => $program_id, //should change when I know how this info will be given
  117. 'has_access' => 1,
  118. 'updated_at' => date("Y-m-d H:i:s", time()),
  119. )
  120. );
  121. $this->command->info('Updated (via email): '.$email.': '.$surnames.', '.$first_name);
  122. $updated++;
  123. }
  124. // Otherwise, create user and add to added
  125. else
  126. {
  127. $user = User::create(array(
  128. 'ssn' => $ssn,
  129. // 'number' => $number,
  130. 'first_name' => $first_name,
  131. 'surnames' => $surnames,
  132. 'email' => $email,
  133. 'number' => $emp_number,
  134. 'has_access' => 1,
  135. 'role' => 4,
  136. // 'program_id' => $program_id, //should change when I know how this info will be given
  137. )
  138. );
  139. DB::table('program_user')->insert(
  140. array('program_id' => $program_id, 'user_id' => $user->id)
  141. );
  142. $this->command->info('Added '.$email.': '.$surnames.', '.$first_name);
  143. $added++;
  144. }
  145. }
  146. // If an exception is raised, show the message and add to error
  147. catch(Exception $e)
  148. {
  149. $this->command->info($e->getMessage());
  150. $error++;
  151. };
  152. }
  153. // Stop time
  154. $time_end = microtime(true);
  155. // Display results
  156. $this->command->info('------------------------------------------------------------');
  157. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  158. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  159. $this->command->info('- Read: '.$read);
  160. $this->command->info('- Updated (active): '.$updated);
  161. $this->command->info('- Added (new active): '.$added);
  162. $this->command->info('- Not updated (inactive): '.(User::count() - $updated - $added));
  163. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  164. // Close file
  165. fclose($file);
  166. }
  167. // File cannot be opened, display error and exit
  168. else
  169. {
  170. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  171. }
  172. }
  173. }