Keine Beschreibung

CourseTableSeeder.php 6.9KB

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