暂无描述

StudentTableSeeder.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. class StudentTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Set term
  6. $term = "C12";
  7. $semester_id = Semester::select('id')->where('code', $term)->first();
  8. // Disable query logging to avoid memory exhaustion
  9. DB::disableQueryLog();
  10. $this->command->info('Students table seeding started for term '.$term);
  11. // Initiates time
  12. $time_start = microtime(true);
  13. /*
  14. //Primero entra la informacion basica eg. numero_estu, nombre, email
  15. // Open file
  16. $file_path = 'app/database/csv/';
  17. // $file_name = 'ARCEXTRT-20-C12-20220411-164329.TXT';
  18. $file_name = 'ARCEXT-20-C12-20220411-164329.TXT';
  19. if($file = fopen($file_path.$file_name, 'r'))
  20. {
  21. // Initialize count variables
  22. $read = 0;
  23. $added =0;
  24. $updated = 0;
  25. $error = 0;
  26. $warning = 0;
  27. // Read each row
  28. while($data = fgetcsv($file, 0, ";"))
  29. {
  30. // Skip iterations from previous semesters
  31. // if(trim($data[8]) != $term)
  32. // {
  33. // continue;
  34. // }
  35. // Add read count
  36. $read++;
  37. try
  38. {
  39. // Get row info
  40. $number = str_pad(trim($data[2]), 9, '000000000', STR_PAD_LEFT);
  41. $name = trim(ucwords(strtoupper($data[4])));
  42. $email = trim($data[5]).'@upr.edu';
  43. // $school_code = trim($data[5]);
  44. $gender = trim($data[7]);
  45. // If any row is empty, raise an exception
  46. if(!$number || !$name)
  47. {
  48. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing name or student number, read \''.implode(",", $data).'\'');
  49. }
  50. // If user has no email, issue warning and set default
  51. if(!$email)
  52. {
  53. $this->command->info('WARNING(line '.$read.'): Missing email, read \''.implode(",", $data).'\'; users will not be able to send messages to students without an email');
  54. $email = 'olas.rrp@upr.edu';
  55. $warning++;
  56. }
  57. // Check if student exists
  58. $student = Student::where('number', $number)->count();
  59. // If s/he does, update information and add to updated
  60. if($student)
  61. {
  62. DB::table('students')
  63. ->where('number', $number)
  64. ->update(
  65. array(
  66. 'name' => $name,
  67. 'email' => $email,
  68. 'gender' => $gender,
  69. // 'school_code' => $school_code,
  70. // 'conc_code' => $conc_code,
  71. 'updated_at' => date("Y-m-d H:i:s"),
  72. )
  73. );
  74. $this->command->info($read.' - Updated '.$number.': '.$name.' ['.$gender.']');
  75. $updated++;
  76. }
  77. // Otherwise, add student and add to added
  78. else
  79. {
  80. Student::create(array(
  81. 'number' => $number,
  82. 'name' => $name,
  83. 'email' => $email,
  84. 'gender' => $gender,
  85. // 'school_code' => $school_code,
  86. // 'conc_code' => $conc_code,
  87. 'created_at' => date("Y-m-d H:i:s"),
  88. 'updated_at' => date("Y-m-d H:i:s")
  89. )
  90. );
  91. $this->command->info($read.' - Added '.$number.': '.$name);
  92. $added++;
  93. }
  94. }
  95. // If an exception is raised, show the message and add to error
  96. catch(Exception $e)
  97. {
  98. $this->command->info($e->getMessage());
  99. $error++;
  100. };
  101. }
  102. // Stop time
  103. $time_end = microtime(true);
  104. // Display results
  105. $this->command->info('------------------------------------------------------------');
  106. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  107. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  108. $this->command->info('- Read: '.$read);
  109. $this->command->info('- Updated (active): '.$updated);
  110. $this->command->info('- Added (new active, all): '.$added);
  111. $this->command->info('- Added (new active, without email): '.$warning);
  112. $this->command->info('- Not updated (inactive): '.(Student::count() - $updated - $added));
  113. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  114. // Close file
  115. fclose($file);
  116. }
  117. // File cannot be opened, display error and exit
  118. else
  119. {
  120. $this->command->info('File '.$file_name.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  121. }
  122. */
  123. // Segundo entra la informacion de programa, grado, nivel, anho y anhade en program_student_semester
  124. $file_path = 'app/database/csv/';
  125. $file_name = 'ARCEXTRT-20-C12-20220411-164329.TXT';
  126. // $file_name = 'ARCEXT-20-C12-20220411-164329.TXT';
  127. if($file = fopen($file_path.$file_name, 'r'))
  128. {
  129. // Initialize count variables
  130. $read = 0;
  131. $added =0;
  132. $updated = 0;
  133. $error = 0;
  134. $warning = 0;
  135. // Read each row
  136. while($data = fgetcsv($file, 0, ";"))
  137. {
  138. // Skip iterations from previous semesters
  139. // if(trim($data[8]) != $term)
  140. // {
  141. // continue;
  142. // }
  143. //
  144. // Add read count
  145. $read++;
  146. try
  147. {
  148. // Get row info
  149. $number = str_pad(trim($data[3]), 9, '000000000', STR_PAD_LEFT);
  150. // $name = trim(ucwords(strtoupper($data[4])));
  151. // $email = trim($data[5]).'@upr.edu';
  152. $school_code = trim($data[19]);
  153. $conc_code = trim($data[21]);
  154. $level = trim($data[16]);
  155. $degree = trim($data[23]);
  156. $year = trim($data[25]);
  157. // $school_code = trim($data[5]);
  158. // $conc_code = trim($data[7]);
  159. // If any row is empty, raise an exception
  160. if(!$number)
  161. {
  162. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing name or student number, read \''.implode(",", $data).'\'');
  163. }
  164. $program=DB::table('programs')
  165. ->where('code', $conc_code)
  166. ->where('degree', $degree)
  167. ->select('id')
  168. ->first();
  169. // var_dump($program_id);
  170. if(!$program)$program->id=null;
  171. // Check if student exists
  172. $student = Student::where('number', $number)->count();
  173. // If s/he does, update information and add to updated
  174. if($student)
  175. {
  176. $student_id = Student::select('id')->where('number', $number)->first();
  177. // print $read."\n";
  178. DB::table('students')
  179. ->where('number', $number)
  180. ->update(
  181. array(
  182. // 'name' => $name,
  183. // 'email' => $email,
  184. 'school_code' => $school_code,
  185. 'conc_code' => $conc_code,
  186. 'program_id' => $program->id,
  187. 'degree' => $degree,
  188. 'level' => $level,
  189. 'year' => $year,
  190. 'updated_at' => date("Y-m-d H:i:s"),
  191. )
  192. );
  193. $this->command->info($read.' - Updated '.$number.': '.$program->id.' ['.$school_code.' '.$conc_code.']');
  194. $updated++;
  195. DB::table('program_student_semester')->insert(
  196. array('student_id' => $student_id->id,
  197. 'program_id' => $program->id,
  198. 'semester_id' => $semester_id->id
  199. )
  200. );
  201. }
  202. // Otherwise, add student and add to added
  203. else
  204. {
  205. $this->command->info($read.' - Student not in system'.$number.': '.$name);
  206. $warning++;
  207. }
  208. }
  209. // If an exception is raised, show the message and add to error
  210. catch(Exception $e)
  211. {
  212. $this->command->info($e->getMessage());
  213. $error++;
  214. };
  215. }
  216. // Stop time
  217. $time_end = microtime(true);
  218. // Display results
  219. $this->command->info('------------------------------------------------------------');
  220. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  221. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  222. $this->command->info('- Read: '.$read);
  223. $this->command->info('- Updated (active): '.$updated);
  224. $this->command->info('- Added (new active, all): '.$added);
  225. $this->command->info('- Added (new active, without email): '.$warning);
  226. $this->command->info('- Not updated (inactive): '.(Student::count() - $updated - $added));
  227. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  228. // Close file
  229. fclose($file);
  230. }
  231. // File cannot be opened, display error and exit
  232. else
  233. {
  234. $this->command->info('File '.$file_name.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  235. }
  236. }
  237. }