暫無描述

CourseStudentTableSeeder.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. class CourseStudentTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Disable query logging to avoid memory exhaustion
  6. DB::disableQueryLog();
  7. $this->command->info('-----------------------------------------');
  8. $this->command->info('-----------------------------------------');
  9. $this->command->info('Course student table seeding started...');
  10. // Initiates time
  11. $time_start = microtime(true);
  12. // Open file
  13. if($file = fopen('app/database/csv/course_student.csv', 'r'))
  14. {
  15. // Initialize count variables
  16. $read = 0;
  17. $added =0;
  18. $updated = 0;
  19. $error = 0;
  20. $semester_id = NULL;
  21. // Read each row
  22. while($data = fgetcsv($file, 5000, "|")) // se cambio delimitador para evitar problemas con nombre separados del apellido con coma
  23. {
  24. // Add read count
  25. $read++;
  26. try
  27. {
  28. $code_number_section = trim($data[3]);
  29. $student_number = trim($data[0]);
  30. $term_code = trim($data[2]);
  31. // If any required field is empty, raise an exception
  32. if(!$code_number_section || !$student_number || !$term_code)
  33. {
  34. throw new Exception('WARNING (line '.($read+1).'): Missing information, read \''.implode(",", $data).'\'');
  35. }
  36. if(!$semester_id)
  37. {
  38. // Get semester
  39. $semester_id = Semester::select('id')->where('code', strtoupper($term_code))->first()['id'];
  40. // If semester code is not in the first line, abort script
  41. if(!$semester_id)
  42. {
  43. $this->command->info('FATAL ERROR(line '.$read.'): The semester code (i.e. B71) is required. Please check the file. Field contents: \''.$term_code.'\'');
  44. exit();
  45. }
  46. }
  47. // Get row info
  48. $course = Course::select('id')->where('code', trim(substr($code_number_section, 0, 4)))->where('number', trim(substr($code_number_section, 4, 4)))->where('section', trim(substr($code_number_section, 8, 3)))->where('semester_id', $semester_id)->first();
  49. $student_id = Student::select('id')->where('number', trim($student_number))->first()['id'];
  50. // If information is incorrect or not present in the database, raise an exception
  51. if(!$course['id'])
  52. {
  53. throw new Exception('WARNING (line '.($read+1).'): Course '.$code_number_section.' not found in the database.');
  54. }
  55. if(!$student_id)
  56. {
  57. throw new Exception('WARNING (line '.($read+1).'): Student '.$student_number.' not found in the database.');
  58. }
  59. // If course-student combination exists, touch the timestamp
  60. if(DB::table('course_student')->where('course_id', $course->id )->where('student_id', $student_id)->count())
  61. {
  62. $course->students()->updateExistingPivot($student_id, array('updated_at' => date("Y-m-d H:i:s", time())));
  63. // $this->command->info('Touched (line '.($read+1).'): ['.$student_number.', '.$code_number_section.'].');
  64. $updated++;
  65. }
  66. // Otherwise, add the combination
  67. else
  68. {
  69. // Assign student to course
  70. $course->students()->attach($student_id, array('semester_id' => $semester_id));
  71. $this->command->info('Added (line '.($read+1).'): ['.$student_number.', '.$code_number_section.'].');
  72. }
  73. $added++;
  74. }
  75. // If an exception is raised, show the message and add to error
  76. catch(Exception $e)
  77. {
  78. $this->command->info($e->getMessage());
  79. $error++;
  80. };
  81. }
  82. // Delete all inactive users for current term
  83. DB::table('course_student')
  84. ->where('updated_at', '<', date('Y-m-d'))
  85. ->where('semester_id', $semester_id)
  86. ->delete();
  87. // Stop time
  88. $time_end = microtime(true);
  89. // Display results
  90. $this->command->info('------------------------------------------------------------');
  91. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  92. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  93. $this->command->info('- Read: '.$read);
  94. $this->command->info('- Updated (active): '.$updated);
  95. $this->command->info('- Added: '.$added);
  96. $this->command->info('- Not added (errors or missing information): '.($error));
  97. // Close file
  98. fclose($file);
  99. }
  100. // File cannot be opened, display error and exit
  101. else
  102. {
  103. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  104. }
  105. }
  106. }