Skip to content
Snippets Groups Projects
Unverified Commit 293a3b9d authored by briancanini's avatar briancanini Committed by GitHub
Browse files

Merge pull request #307 from ASCWebServices/courses

Major 'asc_courses' refactor
parents 1b7d69f8 8b548a16
No related branches found
No related tags found
No related merge requests found
{
"git.ignoreLimitWarning": true
}
\ No newline at end of file
......@@ -10,7 +10,7 @@
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\user\RoleInterface;
use \Drupal\Core\Database\Database;
function asc_courses_update_8001(&$sandbox) {
......@@ -55,40 +55,117 @@ function asc_courses_update_8001(&$sandbox) {
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current'] / $sandbox['max'];
}
/**
* Schema changes supporting pre-processing of API data
*/
function asc_courses_update_8002(&$sandbox) {
$schema = Database::getConnection()->schema();
$tables = asc_courses_schema();
// Rename 'asc_course_data' table to 'asc_courses_api_data'
$schema->renameTable('asc_course_data', 'asc_courses_api_data');
// Rename 'id' field to 'asc_courses_api_data_id'
$field_asc_courses_api_id = $tables['asc_courses_api_data']['fields']['asc_courses_api_data_id'];
$new_key = ['primary key' => ['asc_courses_api_data_id']];
$schema->changeField('asc_courses_api_data', 'id', 'asc_courses_api_data_id', $field_asc_courses_api_id);
// Add 'environment' field
$field_environment = $tables['asc_courses_api_data']['fields']['environment'];
$config = \Drupal::service('config.factory')->getEditable('asc_courses.settings');
$api_environment = $config->get('asc_courses.eip_environment');
if(empty($api_environment)) {
$api_environment = 'qa';
$config->set('asc_courses.eip_environment', $api_environment);
$config->save();
}
$field_environment['initial'] = $api_environment;
$schema->addField('asc_courses_api_data', 'environment', $field_environment);
// Add 'request_type' field
$field_request_type = $tables['asc_courses_api_data']['fields']['request_type'];
$field_request_type['initial'] = 'crseinfo';
$schema->addField('asc_courses_api_data', 'request_type', $field_request_type);
// Add 'processed' field
$field_processed = $tables['asc_courses_api_data']['fields']['processed'];
$field_processed['initial'] = FALSE;
$schema->addField('asc_courses_api_data', 'processed', $field_processed);
// update remaining field definitions
unset($tables['asc_courses_api_data']['fields']['asc_courses_api_data_id']);
unset($tables['asc_courses_api_data']['fields']['environment']);
unset($tables['asc_courses_api_data']['fields']['request_type']);
unset($tables['asc_courses_api_data']['fields']['processed']);
foreach($tables['asc_courses_api_data']['fields'] as $field_name => $field_spec) {
$schema->changeField('asc_courses_api_data', $field_name, $field_name, $field_spec);
}
$schema->createTable('asc_courses_processed', $tables['asc_courses_processed']);
}
/**
* Add 'asc_course_courses' table
*/
// function asc_courses_update_8003(&$sandbox) {
// $schema = Database::getConnection()->schema();
// $schema->createTable('asc_course_dorgs', $tables['asc_course_courses']);
// }
/**
* Implements hook_schema().
*
* This defines the database table which will hold the example item info.
*
* @ingroup tabledrag_example
*/
function asc_courses_schema() {
$schema['asc_course_data'] = [
$schema['asc_courses_api_data'] = [
'description' => 'Stores courses json data.',
'fields' => [
'id' => [
'asc_courses_api_data_id' => [
'description' => 'The primary identifier for each item',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'environment' => [
'description' => 'API environment: qa or prod',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
],
'request_type' => [
'description' => 'Type of API request the data is from, e.g. dorg-courses or dorg-list',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
],
'processed' => [
'description' => 'Whether this data has been processed yet by subsequent php',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
],
'date' => [
'description' => 'The date this data was fetched',
'type' => 'int',
'length' => 11,
'not_null' => TRUE,
'not null' => TRUE,
],
'dept_org' => [
'description' => 'D_ORG number used in request',
'type' => 'varchar',
'length' => 8,
'not_null' => TRUE
'not null' => TRUE,
],
'raw_json' => [
'description' => 'Verbatim JSON blob from EIP API',
'type' => 'text',
'length' => 16777215,
'not_null' => TRUE
'not null' => FALSE,
],
// 'name' => [
// 'description' => 'A name for this item',
......@@ -129,6 +206,210 @@ function asc_courses_schema() {
],
'primary key' => ['id'],
];
/*
$schema['asc_courses_dorgs'] = [
'description' => 'Stores D-org list data',
'fields' => [
'asc_courses_dorgs_id' => [
'description' => 'The primary identifier for each item',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'date' => [
'description' => 'The date this data was fetched',
'type' => 'int',
'length' => 11,
'not null' => TRUE,
],
'raw_json' => [
'description' => 'Verbatim JSON blob from EIP API',
'type' => 'text',
'length' => 16777215,
'not null' => TRUE,
],
],
'primary key' => ['asc_courses_dorgs_id'],
];
*/
$schema['asc_courses_processed'] = [
'fields' => [
'asc_courses_processed_id' => [
'description' => 'The primary identifier for each item',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'updated' => [
'description' => 'The date this data was fetched',
'type' => 'int',
'length' => 11,
'not null' => TRUE,
],
'asc_courses_api_data_id' => [
'description' => 'The date this data was fetched',
'type' => 'int',
'length' => 11,
'unsigned' => TRUE,
'not null' => TRUE,
],
'dept_org' => [
'description' => 'D_ORG number used in request',
'type' => 'varchar',
'length' => 8,
'not null' => TRUE,
],
'crse_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'strm' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
],
'effdt' => [
'type' => 'varchar',
'length' => 16,
'not null' => FALSE,
],
'eff_status' => [
'type' => 'char',
'length' => 1,
'not null' => FALSE,
],
'descr' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
'descrlong' => [
'type' => 'text',
'not null' => FALSE,
],
'crse_attribute' => [
'type' => 'text',
'not null' => FALSE,
],
'equiv_crse_id' => [
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
],
'allow_mult_enroll' => [
'type' => 'char',
'length' => 1,
'not null' => FALSE,
],
'units_minimum' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'units_maximum' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'acad_prog' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'crse_repeatable' => [
'type' => 'char',
'length' => 1,
'not null' => FALSE,
],
'units_repeat_limit' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'crse_repeat_limit' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'grading_basis' => [
'type' => 'varchar',
'length' => 4,
'not null' => FALSE,
],
'ssr_component' => [
'type' => 'varchar',
'length' => 4,
'not null' => FALSE,
],
'course_title_long' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
'crse_count' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'component_primary' => [
'type' => 'varchar',
'length' => 4,
'not null' => FALSE,
],
'crse_offer_nbr' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => FALSE,
],
'acad_group' => [
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
],
'subject' => [
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
],
'catalog_nbr' => [
'type' => 'varchar',
'length' => 8,
'not null' => FALSE,
],
'campus' => [
'type' => 'varchar',
'length' => 8,
'not null' => FALSE,
],
'acad_org' => [
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
],
'acad_career' => [
'type' => 'varchar',
'length' => 8,
'not null' => FALSE,
],
'cip_code' => [
'type' => 'varchar',
'length' => 12,
'not null' => FALSE,
],
],
'primary key' => ['asc_courses_processed_id'],
];
return $schema;
}
......@@ -150,14 +431,9 @@ function asc_courses_schema() {
// $connection->insert('tabledrag_example')->fields($row)->execute();
// }
// }
/**
* implements hook_install()
*/
// /**
// * implements hook_install()
// */
// function asc_courses_install() {
// }
......@@ -8,9 +8,7 @@
function asc_courses_help($route_name, RouteMatchInterface $route_match) {
switch($route_name) {
case 'help.page.asc_courses':
return t('
<2>ASC Courses Module</h2>
');
return t('<h2>ASC Courses Module</h2>');
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -9,6 +9,8 @@
use \Drupal\asc_courses\AscCoursesApi;
use \Drupal\asc_courses\AscCoursesImporter;
use \Drupal\Core\Database\Database;
/**
* A Drush commandfile.
......@@ -44,21 +46,181 @@ class AscCoursesCommands extends DrushCommands {
public function commandName($arg1, $options = ['option-name' => 'default']) {
//$this->logger()->success(dt('Achievement unlocked.'));
// $api = new AscCoursesApi();
// print_r($api->fetchSubjectCourses('D0506'));
$api = new AscCoursesApi();
$api->processPendingApiData();
// print_r($api->fetchDorgCourses('D0506'));
// $api->getAccessToken();
// $dorg = $api->config->get('asc_courses.dept_org');
// echo "D-org: $dorg\n";
// $courses = $api->fetchSubjectCourses($dorg);
// $courses = $api->fetchDorgCourses($dorg);
// module_load_install('asc_courses');
// $schema = Database::getConnection()->schema();
// $tables = asc_courses_schema();
// $importer = new AscCoursesImporter();
// $importer->debug = 2;
// $api_response = $importer->loadSubjectDataFromDatabase($arg1);
// $course_data = $api_response->getCourseCatalogResponse->catalog->course;
// echo "\n";
// echo count($course_data) . " courses in $arg1\n\n";
// foreach ($course_data as $course) {
// $eip_id = $course->{'crse-id'};
// $subj_abbrev = $course->{'subject'};
// $cat_num = $course->{'catalog-nbr'};
// $title = $course->{'course-title-long'};
// echo "$eip_id - $subj_abbrev - $cat_num - $title\n";
// }
// $importer->fetchAndImportAll();
// $importer->establishApiReferences();
}
/**
* Establish EIP references for course nodes lacking them
*
* @param array $options
* An associative array of options whose values come from cli, aliases, config, etc.
* @option option-name
* Description
* @usage asc_courses-link-nodes-to-api
* Usage description
*
* @command asc_courses:link-nodes-to-api
* @aliases asc-link-nodes-to-api
*/
public function linkNodesToApi($options = ['option-name' => 'default']) {
$importer = new AscCoursesImporter();
$importer->fetchAndImportAll();
$importer->debug = 2;
// $importer->fetchAndImportAll();
// print_r($importer->loadSubjectDataFromDatabase('D0506'));
$importer->establishApiReferences();
}
/**
* Pull fresh data for all D-Orgs from the API to database
*
* @param array $options
* An associative array of options whose values come from cli, aliases, config, etc.
* @option option-name
* Description
* @usage asc_courses-pull-all-dorgs
* Usage description
*
* @command asc_courses:pull-all-dorgs
* @aliases asc-pull-all-dorgs
*/
public function pullAllDorgs($options = ['option-name' => 'default']) {
$api = new AscCoursesApi();
$api->debug = 1;
$dorgs = $api->getDorgList();
$dorg_numbers = array_keys($dorgs);
echo "Dorg count: " . count($dorg_numbers) . "\n";
foreach($dorg_numbers as $dorg_number) {
echo "=== $dorg_number - " . $dorgs[$dorg_number]["name"] . " ===\n";
$courses = $api->fetchDorgCourses($dorg_number);
// echo "courses type: " . gettype($courses) . "\n";
// $courses_vars = get_object_vars($courses);
// echo "vars: " . print_r(array_keys($courses_vars), true) . "\n";
echo "===================================\n";
}
/*
$dorgs = $dorgs->getDeptOrgInfoResponse->OUT_VP_CATEGORY->OUT_VP_AREA;
// echo "dorgs type: " . gettype($dorgs) . "\n";
// $dorg_vars = get_object_vars($dorgs);
// echo "vars: " . print_r(array_keys($dorg_vars), true) . "\n";
$dorgs = $dorgs->OUT_VP_COLLEGE->OUT_COLLEGE1;
// print_r($dorgs);
// echo "dorgs type: " . gettype($dorgs) . "\n";
// $dorg_vars = get_object_vars($dorgs);
// print_r($dorg_vars);
// echo "vars: " . print_r(array_keys($dorgs), true) . "\n";
*/
/*
foreach($dorgs as $dorg) {
echo "==================\n";
// echo "dorg type: " . gettype($dorg) . "\n";
// $dorg_vars = get_object_vars($dorg);
// echo "dorg vars: " . print_r(array_keys($dorg_vars), true) . "\n";
echo "COLLEGE1: " . $dorg->COLLEGE1 . "\n";
echo "COLLEGE1_DESCR: " . $dorg->COLLEGE1_DESCR . "\n\n";
$out_college2 = $dorg->OUT_COLLEGE2;
// echo "out_college2 type: " . gettype($out_college2) . "\n";
// echo "out_college2 keys: " . print_r(array_keys($out_college2), true) . "\n";
foreach ($out_college2 as $college2) {
echo "------------\n";
// echo "college2 type: " . gettype($college2) . "\n";
// $college2_vars = get_object_vars($college2);
// echo "college2 vars: " . print_r(array_keys($college2_vars), true) . "\n";
echo "COLLEGE2: " . $college2->COLLEGE2 . "\n";
echo "COLLEGE2_DESCR: " . $college2->COLLEGE2_DESCR . "\n\n";
$out_department = $college2->OUT_DEPARTMENT;
echo "out_department type: " . gettype($out_department) . "\n";
$out_department_vars = get_object_vars($out_department);
echo "out_department vars: " . print_r(array_keys($out_department_vars), true) . "\n";
echo "------------\n";
}
// echo "out_vp_college type: " . gettype($dorg->OUT_VP_COLLEGE) . "\n";
// $out_vp_college_vars = get_object_vars($dorg->OUT_VP_COLLEGE);
// echo "vars: " . print_r(array_keys($out_vp_college_vars), true) . "\n";
echo "==================\n\n";
}
*/
}
/**
* Process all unprocessed API daata
*
* @param array $options
* An associative array of options whose values come from cli, aliases, config, etc.
* @option option-name
* Description
* @usage asc_courses-commandName [count]
* Usage description
*
* @command asc_courses:processApiData
* @aliases asc-courses-process-api-data
*/
public function processApiData($count = null, $options = ['option-name' => 'default']) {
//$this->logger()->success(dt('Achievement unlocked.'));
$api = new AscCoursesApi();
$api->debug = 1;
if(is_numeric($count)) {
echo "count argument specified: $count\n";
$api->processPendingApiData(null, $count);
}
else {
// echo "Count is NOT numeric: $count\n";
$api->processPendingApiData();
}
}
/**
* Command description here.
*
......@@ -251,57 +413,61 @@ public function classImport($options = ['option-name' => 'default']) {
* @aliases asc-create-courses
*/
public function createCourses($options = ['option-name' => 'default']) {
$connection = \Drupal::database();
$course_info_query = $connection->select('asc_course_data', 'acd')
// ->fields('acd', array('id', 'date', 'dept_org'))
->fields('acd')
->orderBy('date', 'DESC')
->range(0, 1);
$course_info_result = $course_info_query->execute();
// $result_count = $course_info_result->rowCount();
// echo "Rows: $result_count\n";
// $rows = $course_info_result->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
$row = $course_info_result->fetchAssoc();
// print_r($row);
$courses_json = $row['raw_json'];
// $len = strlen($courses_json);
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);
foreach($json_data->getCourseCatalogResponse->catalog->course as $json_course) {
if($i < 1) {
print_r($json_course);
// look up existing node
$node_query = \Drupal::entityQuery('node')
->condition('field_eip_id', $json_course->{'crse-id'}, '=');
$existing_node = $node_query->execute();
if(empty($existing_node)) {
// $course_node = Node::create([
$course_node = \Drupal::entityTypeManager()->getStorage('node')->create(array(
'type' => 'course',
'title' => $json_course->{'course-title-long'},
// 'field_course_description' => $json_course->{'descrlong'},
'field_course_number' => [$json_course->{'catalog-nbr'}],
'field_credit_hours' => [$json_course->{'acad-prog'}],
'field_subject_abbreviation' => [$json_course->{'subject'}],
'field_eip_id' => [$json_course->{'crse-id'}],
));
$course_node->save();
echo "New course [" . $json_course->{'catalog-nbr'} . "] ";
}
else {
// update existing node
echo "Update existing node!\n\n";
print_r($existing_node);
}
$importer = new AscCoursesImporter();
$importer->debug = 2;
$importer->fetchAndImportAll();
// $i++;
}
}
// $connection = \Drupal::database();
// $course_info_query = $connection->select('asc_course_data', 'acd')
// // ->fields('acd', array('id', 'date', 'dept_org'))
// ->fields('acd')
// ->orderBy('date', 'DESC')
// ->range(0, 1);
// $course_info_result = $course_info_query->execute();
// // $result_count = $course_info_result->rowCount();
// // echo "Rows: $result_count\n";
// // $rows = $course_info_result->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
// $row = $course_info_result->fetchAssoc();
// // print_r($row);
// $courses_json = $row['raw_json'];
// // $len = strlen($courses_json);
// 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);
// foreach($json_data->getCourseCatalogResponse->catalog->course as $json_course) {
// if($i < 1) {
// print_r($json_course);
// // look up existing node
// $node_query = \Drupal::entityQuery('node')
// ->condition('field_eip_id', $json_course->{'crse-id'}, '=');
// $existing_node = $node_query->execute();
// if(empty($existing_node)) {
// // $course_node = Node::create([
// $course_node = \Drupal::entityTypeManager()->getStorage('node')->create(array(
// 'type' => 'course',
// 'title' => $json_course->{'course-title-long'},
// // 'field_course_description' => $json_course->{'descrlong'},
// 'field_course_number' => [$json_course->{'catalog-nbr'}],
// 'field_credit_hours' => [$json_course->{'acad-prog'}],
// 'field_subject_abbreviation' => [$json_course->{'subject'}],
// 'field_eip_id' => [$json_course->{'crse-id'}],
// ));
// $course_node->save();
// echo "New course [" . $json_course->{'catalog-nbr'} . "] ";
// }
// else {
// // update existing node
// echo "Update existing node!\n\n";
// print_r($existing_node);
// }
// // $i++;
// }
// }
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment