Нема описа

StudentTableSeeder.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class StudentTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Set term
  6. $term = "B91";
  7. // Disable query logging to avoid memory exhaustion
  8. DB::disableQueryLog();
  9. $this->command->info('Students table seeding started for term '.$term);
  10. // Initiates time
  11. $time_start = microtime(true);
  12. // Open file
  13. $file_path = 'http://136.145.180.72/tools/proceso_files/';
  14. $file_name = 'webmatri_vivienda_b83-b91.dat';
  15. if($file = fopen($file_path.$file_name, 'r'))
  16. {
  17. // Initialize count variables
  18. $read = 0;
  19. $added =0;
  20. $updated = 0;
  21. $error = 0;
  22. $warning = 0;
  23. // Read each row
  24. while($data = fgetcsv($file, 5000, ";"))
  25. {
  26. // Skip iterations from previous semesters
  27. if(trim($data[8]) != $term)
  28. {
  29. continue;
  30. }
  31. // Add read count
  32. $read++;
  33. try
  34. {
  35. // Get row info
  36. $number = str_pad(trim($data[1]), 9, '000000000', STR_PAD_LEFT);
  37. $name = trim(ucwords(strtoupper($data[2])));
  38. $email = trim($data[15]).'@upr.edu';
  39. $school_code = trim($data[5]);
  40. $conc_code = trim($data[7]);
  41. // If any row is empty, raise an exception
  42. if(!$number || !$name)
  43. {
  44. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing name or student number, read \''.implode(",", $data).'\'');
  45. }
  46. // If user has no email, issue warning and set default
  47. if(!$email)
  48. {
  49. $this->command->info('WARNING(line '.$read.'): Missing email, read \''.implode(",", $data).'\'; users will not be able to send messages to students without an email');
  50. $email = 'olas.rrp@upr.edu';
  51. $warning++;
  52. }
  53. // Check if student exists
  54. $student = Student::where('number', $number)->count();
  55. // If s/he does, update information and add to updated
  56. if($student)
  57. {
  58. DB::table('students')
  59. ->where('number', $number)
  60. ->update(
  61. array(
  62. 'name' => $name,
  63. 'email' => $email,
  64. 'school_code' => $school_code,
  65. 'conc_code' => $conc_code,
  66. 'updated_at' => date("Y-m-d H:i:s"),
  67. )
  68. );
  69. $this->command->info($read.' - Updated '.$number.': '.$name.' ['.$school_code.' '.$conc_code.']');
  70. $updated++;
  71. }
  72. // Otherwise, add student and add to added
  73. else
  74. {
  75. Student::create(array(
  76. 'number' => $number,
  77. 'name' => $name,
  78. 'email' => $email,
  79. 'school_code' => $school_code,
  80. 'conc_code' => $conc_code,
  81. 'created_at' => date("Y-m-d H:i:s"),
  82. 'updated_at' => date("Y-m-d H:i:s")
  83. )
  84. );
  85. $this->command->info($read.' - Added '.$number.': '.$name);
  86. $added++;
  87. }
  88. }
  89. // If an exception is raised, show the message and add to error
  90. catch(Exception $e)
  91. {
  92. $this->command->info($e->getMessage());
  93. $error++;
  94. };
  95. }
  96. // Stop time
  97. $time_end = microtime(true);
  98. // Display results
  99. $this->command->info('------------------------------------------------------------');
  100. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  101. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  102. $this->command->info('- Read: '.$read);
  103. $this->command->info('- Updated (active): '.$updated);
  104. $this->command->info('- Added (new active, all): '.$added);
  105. $this->command->info('- Added (new active, without email): '.$warning);
  106. $this->command->info('- Not updated (inactive): '.(Student::count() - $updated - $added));
  107. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  108. // Close file
  109. fclose($file);
  110. }
  111. // File cannot be opened, display error and exit
  112. else
  113. {
  114. $this->command->info('File '.$file_name.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  115. }
  116. }
  117. }