Newer
Older
<?php
namespace Drupal\asc_courses;
// use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
// use Drush\Commands\DrushCommands;
use \Drupal\Core\Database\Database;
use \Drupal\Core\Config;
use \Drupal\node\Entity\Node;
class AscCoursesImporter {
public $debug = 0;
public $config;
public $api;
/**
* {@inheritdoc}
*/
public function __construct($config = null) {
// Get settings
if (!empty($config)) {
$this->config = $config;
}
else {
$this->config = \Drupal::service('config.factory')->get('asc_courses.settings');
}
}
public function importCourseNodes($courses_data) {
if($this->debug) echo "importCourseNodes()\n";
$i = 0;
foreach($courses_data as $course_data) {
// echo "importCourseNodes(): " . print_r($course_data, true) . "\n";
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
// if($this->debug) echo "$i catalog-nbr: " . $course_data['catalog-nbr'] . "\n";
// if($this->debug > 1) echo "importCourseNodes() - " . print_r($course_data, true);
// look up existing node
$node_query = \Drupal::entityQuery('node')
->condition('field_eip_id', $course_data['crse_id'], '=');
$existing_node = $node_query->execute();
if(empty($existing_node)) {
$this->createCourseNode($node_storage, $course_data);
}
else {
// update existing node
$existing_nid = array_shift($existing_node);
$this->updateCourseNode($node_storage, $existing_nid, $course_data);
// if($this->debug) echo "\n";
}
}
}
public function createCourseNode($node_storage, $course_data) {
if($this->debug) echo "createCourseNode() - create new node\n";
// $course_node = Node::create([
$course_node = $node_storage->create(array(
'type' => 'course',
'title' => $course_data['course_title_long'],
'field_course_description' => [$course_data['descrlong']],
'field_course_number' => [$course_data['catalog_nbr']],
'field_credit_hours' => [$course_data['acad_prog']],
'field_subject_abbreviation' => [$course_data['subject']],
'field_eip_id' => [$course_data['crse_id']],
));
$course_node->save();
// $new_course_nid = $course_node->get('nid')->getValue()[0]['value'];
$new_course_nid = $course_node->id();
// if($this->debug) echo "createCourseNode() - $new_course_nid\n";
if($this->debug) echo "createCourseNode() - New course nid[$new_course_nid] catalog[" . $course_data['catalog-nbr'] . "]\n";
}
public function updateCourseNode($node_storage, $nid, $course_data) {
if($this->debug) echo "updateCourseNode() - Existing nid: $nid\n";
$course_node = $node_storage->load($nid);
// Compare values and update if necessary
$content_changed = false;
// $title = $course_node->get('title')->getValue()[0]['value'];
// echo "title: " . print_r($title, true);
$title = $course_node->title->value;
if ($title != $course_data['course_title_long']) {
if($this->debug) {
echo "### Title changed..\n";
echo "Node title: $title\n";
echo "API title: " . $course_data['course_title_long'] . "\n";
// $course_node->setTitle($course_data['course_title_long']);
$course_node->title->value = $course_data['course_title_long'];
$description = $course_node->field_course_description->value;
// echo "description: $description\n";
if ($description != $course_data['descrlong']) {
echo "### description changed..\n";
echo "Node description: $description\n";
echo "API description: " . $course_data['descrlong'] . "\n";
$course_node->field_course_description->value = $course_data['descrlong'];
}
// $course_number = $course_node->get('field_course_number')->getValue()[0]['value'];
// echo "course_number: " . print_r($course_number, true);
$course_number = $course_node->field_course_number->value;
if ($course_number != $course_data['catalog_nbr']) {
if($this->debug) {
echo "### course_number changed..\n";
echo "Node course_number: $course_number\n";
echo "API course_number: " . $course_data['catalog_nbr'] . "\n";
// $course_node->set('field_course_number', $course_data['catalog_nbr']);
$course_node->field_course_number->value = $course_data['catalog_nbr'];
}
$credit_hours = $course_node->field_credit_hours->value;
if ($credit_hours != $course_data['acad_prog']) {
if($this->debug) {
echo "### credit_hours changed..\n";
echo "Node credit_hours: $credit_hours\n";
echo "API credit_hours: " . $course_data['acad_prog'] . "\n";
$course_node->field_credit_hours->value = $course_data['acad_prog'];
}
$subj_abbrev = $course_node->field_subject_abbreviation->value;
if ($subj_abbrev != $course_data['subject']) {
if($this->debug) {
echo "### subj_abbrev changed..\n";
echo "Node subj_abbrev: $subj_abbrev\n";
echo "API subj_abbrev: " . $course_data['subject'] . "\n";
$course_node->field_subject_abbreviation->value = $course_data['subject'];
}
if($content_changed) {
if($this->debug) echo "\nContent has changed.. save updated node.\n\n";
$course_node->save();
}
}
public function fetchAndImportAll() {
// $this->config = \Drupal::service('config.factory')->get('asc_courses.settings');
if(!isset($this->api)) $this->api = new AscCoursesApi($this->config);
$config_dorgs = $this->config->get('asc_courses.dept_org');
$dorgs = explode(',', trim($config_dorgs));
foreach($dorgs as $dorg) {
$dorg = trim($dorg);
if ($this->debug) echo "D-org: $dorg\n";
$courses_data = $this->loadSubjectDataFromDatabase($dorg);
// if(empty($db_data)) {
// $courses_data = $this->api->fetchDorgCourses($dorg);
// }
// else {
// $courses_data = $db_data;
// }
$this->importCourseNodes($courses_data);
}
}
public function loadSubjectDataFromDatabase($dorg) {
if ($this->debug) echo "loadSubjectDataFromDatabase(): dorg[$dorg]\n";
// $course_info_query = $connection->select('asc_courses_api_data', 'acd')
// // ->fields('acd', array('id', 'date', 'dept_org'))
// ->fields('acd')
// ->condition('dept_org', $dorg, "=")
// ->orderBy('date', 'DESC')
// ->range(0, 1);
$course_info_query = $connection->select('asc_courses_processed', 'acp')
->fields('acp')
->condition('dept_org', $dorg, '=');
$course_info_result = $course_info_query->execute();
// $rows = $course_info_result->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
$rows = [];
while ($row = $course_info_result->fetchAssoc()) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
$rows[] = $row;
}
// print_r($rows);
// die();
return $rows;
// if($row = $course_info_result->fetchAssoc()) {
// // print_r($row);
// $data_date = $row['updated'];
// $data_age = time() - $data_date;
// if($this->debug) echo "Data date: $data_date - " . date("Y-m-d h:i:s", $data_date) . " - $data_age seconds old\n";
// // $json_data = json_decode($row['raw_json']);
// // return $json_data;
// // if($data_age < 3600) {
// // if($this->debug) echo "Data is less than an hour old.. reusing data from database.\n";
// // $courses_json = $row['raw_json'];
// // // $len = strlen($courses_json);
// // if($this->debug) echo "JSON Length:" . strlen($courses_json) . "\n";
// // // echo substr($courses_json, 0, 1024) . "\n\n";
// // $json_data = json_decode($courses_json);
// // // print_r($json_data);
// // return $json_data;
// // }
// // else {
// // return false;
// // }
// }
// else {
// return false;
// }
}
public function getDorgToSubject() {
$dorg_to_name = [
'D0200' => [
'name' => "Arts Administration",
],
'D0205' => [
'name' => "Diversity & Identity Studies C",
],
'D0206' => [
'name' => "Film Studies",
],
'D0208' => [
'name' => "Arts Initiatives",
],
'D0210' => [
'name' => "Adv Computing Ctr/Art & Des",
],
'D0215' => [
'name' => "Art",
],
'D0225' => [
'name' => "Arts Education",
],
'D0230' => [
'name' => "Ind, Intr & Visual Comm Design",
],
'D0235' => [
'name' => "History of Art",
],
'D0241' => [
'name' => "Dance",
],
'D0262' => [
'name' => "School of Music",
],
'D0280' => [
'name' => "Theatre",
],
'D0500' => [
'name' => "Humanities Administration",
],
'D0502' => [
'name' => "AfricanAmer&African Studies",
],
'D0505' => [
'name' => "Ctr Medieval & Ren Studies",
],
'D0506' => [
'name' => "Women's Studies",
],
// 'D0507' => [
// 'name' => "Humanities Information Svcs",
// ],
'D0508' => [
'name' => "Melton Ctr for Jew Studies",
],
'D0509' => [
'name' => "Greek and Latin",
],
'D0518' => [
'name' => "Dept of Comp Stds in Hum",
],
'D0527' => [
'name' => "East Asian Languages & Lit",
],
'D0536' => [
'name' => "Ctr-Study&Teaching of Writing",
],
'D0537' => [
'name' => "English",
],
'D0543' => [
'name' => "Foreign Language Center",
],
'D0545' => [
'name' => "French and Italian",
],
'D0544' => [
'name' => "Cntr for the Study of Religion",
],
'D0547' => [
'name' => "Germanic Languages & Lit",
],
'D0554' => [
'name' => "Near Eastern Lang & Culture",
],
'D0557' => [
'name' => "History",
],
'D0564' => [
'name' => "Humanities Institute",
],
'D0566' => [
'name' => "Linguistics",
],
'D0575' => [
'name' => "Philosophy",
],
'D0593' => [
'name' => "Slavic & East European L&L",
],
'D0596' => [
'name' => "Spanish and Portugese",
],
// 'D0300' => [
// 'name' => "Biological Sciences Admin",
// ],
'D0303' => [
'name' => "Ancillary Fac & Services",
],
// 'D0321' => [
// 'name' => "Div of Sensory Biophysics",
// ],
'D0326' => [
'name' => "Introductory Biology",
],
'D0340' => [
'name' => "Molecular Genetics",
],
'D0350' => [
'name' => "Microbiology",
],
'D0385' => [
'name' => "Plant Biotechnology",
],
'D0386' => [
'name' => "PMGF",
],
'D0390' => [
'name' => "EEOB",
],
'D0397' => [
'name' => "Ohio Biological Survey",
],
// 'D0600' => [
// 'name' => "Math & Physical Sci Admin",
// ],
'D0614' => [
'name' => "Astronomy",
],
'D0628' => [
'name' => "Chemistry",
],
'D0656' => [
'name' => "Geological Sciences",
],
'D0671' => [
'name' => "Mathematics",
],
'D0684' => [
'name' => "Physics",
],
'D0694' => [
'name' => "Statistics",
],
// 'D0700' => [
// 'name' => "Social & Behav Sci Admin",
// ],
// 'D0701' => [
// 'name' => "Survey Research",
// ],
'D0703' => [
'name' => "Ctr/Human Resource Rsch",
],
'D0707' => [
'name' => "Mershon Center for Education",
],
'D0708' => [
'name' => "Population Research Center",
],
'D0709' => [
'name' => "UG Intl Studies Program",
],
'D0711' => [
'name' => "Anthropology",
],
'D0722' => [
'name' => "Economics",
],
'D0733' => [
'name' => "Geography",
],
'D0735' => [
'name' => "Urban & Regional Analysis Init",
],
'D0744' => [
'name' => "Journalism & Communication",
],
'D0755' => [
'name' => "Political Science",
],
'D0766' => [
'name' => "Psychology",
],
'D0777' => [
'name' => "Sociology",
],
'D0778' => [
'name' => "Criminal Justice Research Ctr",
],
'D0780' => [
'name' => "Cntr Cognitive & Brain Science",
],
'D0781' => [
'name' => "Cntr Cog & Behavioral Brain Im",
],
// 'D0782' => [
// 'name' => "Inst for American Democracy",
// ],
'D0783' => [
'name' => "Neuroscience Ungrad Major",
],
'D0784' => [
'name' => "Behavioral Decision Making",
],
'D0799' => [
'name' => "Speech and Hearing",
],
];
return $dorg_to_name;
}
public function getAllCoursesBySubject() {
$dorg_to_subject = $this->getDorgToSubject();
// echo "dorg-to-subject: " . print_r($dorg_to_subject) . "\n";
$connection = \Drupal::database();
$course_info_query = $connection->select('asc_courses_processed', 'acp')
// ->fields('acd', array('id', 'date', 'dept_org'))
->fields('acp')
->orderBy('updated', 'DESC');
// ->range(0, 1);
$course_info_result = $course_info_query->execute();
$all_courses_by_subject = [];
while($row = $course_info_result->fetchAssoc()) {
// if ($this->debug) echo "==============\n";
// echo "Row: " . print_r($row, true) . "\n";
$dorg_num = $row['dept_org'];
$dorg_subject = $dorg_to_subject[$dorg_num]['name'];
$course_subj = $row['subject'];
$course_cat_nbr = $row['catalog_nbr'];
// $course_title = $row['course-title-long'];
if(!array_key_exists($course_subj, $all_courses_by_subject)) {
$all_courses_by_subject[$course_subj] = [];
if(!array_key_exists($course_cat_nbr, $all_courses_by_subject[$course_subj])) {
$all_courses_by_subject[$course_subj][$course_cat_nbr] = [];
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
$all_courses_by_subject[$course_subj][$course_cat_nbr][] = $row;
/*
$json_length = strlen($row['raw_json']);
$json_data = json_decode($row['raw_json'])->getCourseCatalogResponse;
$result = $json_data->result;
$message = $json_data->message;
// if ($this->debug) echo "$dorg_num - $json_length bytes - $dorg_subject - ";
if(is_array($json_data->catalog->course)) {
// if ($this->debug) echo count($json_data->catalog->course) . " courses\n";
foreach($json_data->catalog->course as $course_data) {
$course_subj = (string)$course_data->{'subject'};
$course_cat_nbr = (string)$course_data->{'catalog-nbr'};
$course_title = (string)$course_data->{'course-title-long'};
// if ($this->debug) echo "$course_subj - $course_cat_nbr - $course_title\n";
if(!array_key_exists($course_subj, $all_courses_by_subject)) {
$all_courses_by_subject[$course_subj] = [];
}
if(!array_key_exists($course_cat_nbr, $all_courses_by_subject[$course_subj])) {
$all_courses_by_subject[$course_subj][$course_cat_nbr] = [];
}
$all_courses_by_subject[$course_subj][$course_cat_nbr][] = $course_data;
}
}
else {
// echo "no courses\n";
}
*/
// if ($this->debug) echo "===\n";
// echo "\n\nAll courses: \n";
// print_r($all_courses_by_subject);
// echo "\nDUN\n";
return $all_courses_by_subject;
public function establishApiReferences() {
// if(empty($this->config)) {
// $this->config = \Drupal::service('config.factory')->get('asc_courses.settings');
// }
// $config_dorgs = $this->config->get('asc_courses.dept_org');
// $dorgs = explode(',', trim($config_dorgs));
$all_courses = $this->getAllCoursesBySubject();
// print_r($all_courses);
// print_r($all_courses['GEOG']);
// print_r($all_courses['ATMOSSC']);
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_query = \Drupal::entityQuery('node')
->condition('field_eip_id', NULL, 'IS NULL')
->condition('type', 'course', '=');
$node_query_result = $node_query->execute();
// print_r($orphaned_nodes);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
$orphaned_nodes = [];
foreach ($node_query_result as $result_nid) {
$orphaned_nodes[] = $result_nid;
}
if ($this->debug) echo "\n" . count($orphaned_nodes) . " course nodes lack references.\n\n";
$lookup_failed = [];
foreach ($orphaned_nodes as $node_id) {
$node = $node_storage->load($node_id);
$cat_num = $node->field_course_number->value;
$subj_abbrev = $node->field_subject_abbreviation->value;
$title = $node->title->value;
if ($this->debug) echo "$node_id - $cat_num - $subj_abbrev - $title\n";
if(!array_key_exists($subj_abbrev, $all_courses)) {
if ($this->debug) echo "No courses with subject $subj_abbrev!!!\n";
if(!array_key_exists($subj_abbrev, $lookup_failed)) {
$lookup_failed[$subj_abbrev] = "Not in data set at all!";
}
}
else if (!array_key_exists($cat_num, $all_courses[$subj_abbrev])) {
if ($this->debug) echo "$subj_abbrev has no courses with catalog number $cat_num!!\n";
if(!array_key_exists($subj_abbrev, $lookup_failed)) {
$lookup_failed[$subj_abbrev] = [];
}
$lookup_failed[$subj_abbrev][$node_id] = $cat_num;
}
else {
$crse_id = false;
foreach($all_courses[$subj_abbrev][$cat_num] as $found_course) {
if ($this->debug) echo "ID: " . $found_course['crse_id'] . "\n";
$crse_id = $found_course['crse_id'];
}
if($crse_id) {
$node->field_eip_id->value = $crse_id;
$node->save();
}
}
// print_r($all_courses[$subj_abbrev][$cat_num]);
// echo "===========\n\n";
}
if ($this->debug) echo "===============\n";
if ($this->debug) print_r($lookup_failed);
}