Açıklama Yok

CourseTableSeeder.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/courses.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[6];
  28. $code = trim(substr($data[0], 0, 4));
  29. $number = trim(substr($data[0], 4, 4));
  30. $section = trim(substr($data[0], 8, 3));
  31. $user_id = User::select('id')->where('ssn', trim($data[14]))->first();
  32. $semester_id = Semester::select('id')->where('code', strtoupper(trim($data[2])))->first();
  33. $program_code = trim($data[7]);
  34. // If any row is empty, raise an exception
  35. if($name=='' || $code=='' || $number=='' || $section=='' || trim($data[14])=='' || trim($data[2])=='')
  36. {
  37. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  38. }
  39. // Fetch semester. If it doesn't exist, abort script
  40. if(!$semester_id['id'])
  41. {
  42. $this->command->info('FATAL ERROR(line '.$read.'): Semester not found in the database.');
  43. exit();
  44. }
  45. else
  46. {
  47. $semester_id = $semester_id['id'];
  48. }
  49. // Fetch program. If it doesn't exist, set default program
  50. $preexisting_course = Course::select('program_id')->where('code', $code)->where('number', $number)->first();
  51. $program_id = 999;
  52. if($preexisting_course)
  53. {
  54. $program_id = Program::find($preexisting_course->program_id)->id;
  55. }
  56. elseif(Program::select('id')->where('code', $program_code)->first())
  57. {
  58. $program_id = Program::select('id')->where('code', $program_code)->first()->id;
  59. }
  60. // Fetch user. If it doesn't exist, set default user
  61. if(!$user_id && !$user_id['id'])
  62. {
  63. $user_id = 60; // Id for default user. Check DB for user with ssn 000000000.
  64. }
  65. else
  66. {
  67. $user_id = $user_id['id'];
  68. }
  69. // Check if course exists
  70. $course = Course::where('code', $code)->where('number', $number)->where('section', $section)->where('semester_id', $semester_id)->first();
  71. //If it does, update information and add to updated
  72. if($course)
  73. {
  74. //print_r($course);
  75. DB::table('courses')
  76. ->where('id', $course->id)
  77. ->update(
  78. array(
  79. 'name' => $name,
  80. 'code' => $code,
  81. 'number' => $number,
  82. 'section' => $section,
  83. 'program_id' => $program_id,
  84. 'user_id' => $user_id,
  85. 'updated_at' => date("Y-m-d H:i:s", time())
  86. )
  87. );
  88. $this->command->info('Updated '.$code.$number.$section.': '.$name);
  89. $updated++;
  90. }
  91. // Otherwise, add course and add to added
  92. else
  93. {
  94. Course::create(array(
  95. 'name' => $name,
  96. 'code' => $code,
  97. 'number' => $number,
  98. 'section' => $section,
  99. 'program_id' => $program_id,
  100. 'user_id' => $user_id,
  101. 'semester_id' => $semester_id
  102. )
  103. );
  104. $this->command->info('Added '.$code.$number.$section.': '.$name);
  105. $added++;
  106. }
  107. }
  108. // If an exception is raised, show the message and add to error
  109. catch(Exception $e)
  110. {
  111. $this->command->info($e->getMessage().'[RECORD '.$read.']'.$e->getLine());
  112. $error++;
  113. };
  114. }
  115. // Stop time
  116. $time_end = microtime(true);
  117. // Display results
  118. $this->command->info('------------------------------------------------------------');
  119. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  120. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  121. $this->command->info('- Read: '.$read);
  122. $this->command->info('- Updated (active): '.$updated);
  123. $this->command->info('- Added (new active): '.$added);
  124. $this->command->info('- Not updated (inactive): '.(Course::count() - $updated - $added));
  125. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  126. $this->command->info('- Note: A negative number in the results may mean there are duplicate values.');
  127. // Close file
  128. fclose($file);
  129. }
  130. // File cannot be opened, display error and exit
  131. else
  132. {
  133. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  134. }
  135. }
  136. }