Няма описание

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