Нет описания

CourseTableSeeder.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class CourseTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Disable query logging to avoid memory exhaustion
  6. DB::disableQueryLog();
  7. $this->command->info('Courses table seeding started...');
  8. // Initiates time
  9. $time_start = microtime(true);
  10. // Open file
  11. if($file = fopen('app/database/csv/cursos.csv', 'r'))
  12. {
  13. // Initialize count variables
  14. $read = 0;
  15. $added =0;
  16. $updated = 0;
  17. $error = 0;
  18. // Read each row
  19. while($data = fgetcsv($file, 5000)) // se cambio delimitador para evitar problemas con nombres separados del apellido por coma
  20. {
  21. // Add read count
  22. $read++;
  23. // $this->command->info(print_r($data));
  24. try
  25. {
  26. // Get row info
  27. $name = $data[8];
  28. $code = trim(substr($data[1], 0, 4));
  29. $number = trim(substr($data[1], 4, 4));
  30. $section = trim(substr($data[1], 8, 3));
  31. $user_id = User::select('id')->where('ssn', trim($data[11]))->first();
  32. $semester_id = Semester::select('id')->where('code', strtoupper(trim($data[3])))->first();
  33. $program_code = trim($data[9]);
  34. $modality = trim($data[5]);
  35. // If any row is empty, raise an exception
  36. if($name=='' || $code=='' || $number=='' || $section=='' || trim($data[14])=='' || trim($data[2])=='')
  37. {
  38. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  39. }
  40. // Fetch semester. If it doesn't exist, abort script
  41. if(!$semester_id['id'])
  42. {
  43. $this->command->info('FATAL ERROR(line '.$read.'): Semester not found in the database.');
  44. exit();
  45. }
  46. else
  47. {
  48. $semester_id = $semester_id['id'];
  49. }
  50. // Fetch program. If it doesn't exist, set default program
  51. $preexisting_course = Course::select('program_id')->where('code', $code)->where('number', $number)->first();
  52. $program_id = 999;
  53. if($preexisting_course)
  54. {
  55. $program_id = Program::find($preexisting_course->program_id)->id;
  56. }
  57. elseif(Program::select('id')->where('code', $program_code)->first())
  58. {
  59. $program_id = Program::select('id')->where('code', $program_code)->first()->id;
  60. }
  61. // Fetch user. If it doesn't exist, set default user
  62. if(!$user_id && !$user_id['id'])
  63. {
  64. $user_id = 60; // Id for default user. Check DB for user with ssn 000000000.
  65. }
  66. else
  67. {
  68. $user_id = $user_id['id'];
  69. }
  70. // Check if course exists
  71. $course = Course::where('code', $code)->where('number', $number)->where('section', $section)->where('semester_id', $semester_id)->first();
  72. //If it does, update information and add to updated
  73. if($course)
  74. {
  75. //print_r($course);
  76. DB::table('courses')
  77. ->where('id', $course->id)
  78. ->update(
  79. array(
  80. 'name' => $name,
  81. 'code' => $code,
  82. 'number' => $number,
  83. 'section' => $section,
  84. 'modality' => $modality,
  85. 'program_id' => $program_id,
  86. 'user_id' => $user_id,
  87. 'updated_at' => date("Y-m-d H:i:s", time())
  88. )
  89. );
  90. $this->command->info('Updated '.$code.$number.$section.': '.$name);
  91. $updated++;
  92. }
  93. // Otherwise, add course and add to added
  94. else
  95. {
  96. Course::create(array(
  97. 'name' => $name,
  98. 'code' => $code,
  99. 'number' => $number,
  100. 'section' => $section,
  101. 'modality' => $modality,
  102. 'program_id' => $program_id,
  103. 'user_id' => $user_id,
  104. 'semester_id' => $semester_id
  105. )
  106. );
  107. $this->command->info('Added '.$code.$number.$section.': '.$name);
  108. $added++;
  109. }
  110. }
  111. // If an exception is raised, show the message and add to error
  112. catch(Exception $e)
  113. {
  114. $this->command->info($e->getMessage().'[RECORD '.$read.']'.$e->getLine());
  115. $error++;
  116. };
  117. }
  118. // Stop time
  119. $time_end = microtime(true);
  120. // Display results
  121. $this->command->info('------------------------------------------------------------');
  122. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  123. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  124. $this->command->info('- Read: '.$read);
  125. $this->command->info('- Updated (active): '.$updated);
  126. $this->command->info('- Added (new active): '.$added);
  127. $this->command->info('- Not updated (inactive): '.(Course::count() - $updated - $added));
  128. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  129. $this->command->info('- Note: A negative number in the results may mean there are duplicate values.');
  130. // Close file
  131. fclose($file);
  132. }
  133. // File cannot be opened, display error and exit
  134. else
  135. {
  136. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  137. }
  138. }
  139. }