From 8334f8e589500386f07585cb9f1dbe8d50d63fc2 Mon Sep 17 00:00:00 2001 From: protitude Date: Fri, 19 Jul 2024 12:44:56 -0600 Subject: [PATCH 1/5] campuschampions: fix stats export maybe --- .../campuschampions/src/Commands/CampusChampionsCommands.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/campuschampions/src/Commands/CampusChampionsCommands.php b/web/modules/custom/campuschampions/src/Commands/CampusChampionsCommands.php index 6d079f49d..11f886a7a 100644 --- a/web/modules/custom/campuschampions/src/Commands/CampusChampionsCommands.php +++ b/web/modules/custom/campuschampions/src/Commands/CampusChampionsCommands.php @@ -91,7 +91,8 @@ public function exportAffinityGroups() { * @usage campuschampions:generateBreakdownStats */ public function generateBreakdownStats($options = array()) { - $dir = 'public://'; + $env = getenv('PANTHEON_ENVIRONMENT'); + $dir = $env == 'local' ? 'public://' : '/files' ; $file = 'cc-breakdown-stats.json'; $path = $dir . $file; From 5a6aad8e20f4386ff9525f0df4a960e0c33312d6 Mon Sep 17 00:00:00 2001 From: protitude Date: Thu, 8 Aug 2024 12:07:19 -0600 Subject: [PATCH 2/5] campuschampiions: store stats via cron d8-2172 --- .../campuschampions/campuschampions.install | 18 ++- .../campuschampions/campuschampions.module | 16 +++ .../campuschampions.services.yml | 4 + .../src/Plugin/Block/CampusChampionsBlock.php | 10 +- .../campuschampions/src/Plugin/StoreStats.php | 112 ++++++++++++++++++ 5 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 web/modules/custom/campuschampions/campuschampions.services.yml create mode 100644 web/modules/custom/campuschampions/src/Plugin/StoreStats.php diff --git a/web/modules/custom/campuschampions/campuschampions.install b/web/modules/custom/campuschampions/campuschampions.install index f8cf72658..579d429f8 100644 --- a/web/modules/custom/campuschampions/campuschampions.install +++ b/web/modules/custom/campuschampions/campuschampions.install @@ -548,4 +548,20 @@ function campuschampions_update_9000() { ], ] ]); -} \ No newline at end of file +} + +/** + * @file + * Set initial stats. + */ +function campuschampions_update_10000() { + $stats = [ + 'nationwide' => 827, + 'institutions' => 362, + 'epscor' => 87, + 'msi' => 61 + ]; + \Drupal::state()->set('cc_stats',$stats); + +} + diff --git a/web/modules/custom/campuschampions/campuschampions.module b/web/modules/custom/campuschampions/campuschampions.module index 2a8e288e2..4f80fb71f 100644 --- a/web/modules/custom/campuschampions/campuschampions.module +++ b/web/modules/custom/campuschampions/campuschampions.module @@ -357,3 +357,19 @@ function campuschampions_views_data_export_row_alter(&$row, ResultRow $result, V } } } + +/** + * Implements hook_cron(). + */ +function campuschampions_cron() { + // Run this cron job every day at 2am. + $currentTime = \Drupal::time()->getCurrentTime(); + $hour = date('H', $currentTime); + $min = date('i', $currentTime); + + // Cron runs every 10 minutes + if ($hour == 2 && $min < 20) { + \Drupal::service('cc.getStats')->setState(); + } +} + diff --git a/web/modules/custom/campuschampions/campuschampions.services.yml b/web/modules/custom/campuschampions/campuschampions.services.yml new file mode 100644 index 000000000..a661e151a --- /dev/null +++ b/web/modules/custom/campuschampions/campuschampions.services.yml @@ -0,0 +1,4 @@ +services: + cc.getStats: + class: Drupal\campuschampions\Plugin\StoreStats + arguments: ['@renderer'] diff --git a/web/modules/custom/campuschampions/src/Plugin/Block/CampusChampionsBlock.php b/web/modules/custom/campuschampions/src/Plugin/Block/CampusChampionsBlock.php index 4351bd6eb..826e30924 100644 --- a/web/modules/custom/campuschampions/src/Plugin/Block/CampusChampionsBlock.php +++ b/web/modules/custom/campuschampions/src/Plugin/Block/CampusChampionsBlock.php @@ -26,15 +26,7 @@ public function build() { 'msi' => 61 ]; - $file = \Drupal::service('file_system')->realpath('public://') . '/cc-breakdown-stats.json'; - - if (file_exists($file)) { - $contents = file_get_contents($file); - $data = json_decode($contents); - if (!empty($data)) { - $stats = $data; - } - } + $stats = json_decode(\Drupal::state()->get('cc_stats')); return [ '#theme' => 'campuschampions_block', diff --git a/web/modules/custom/campuschampions/src/Plugin/StoreStats.php b/web/modules/custom/campuschampions/src/Plugin/StoreStats.php new file mode 100644 index 000000000..7e085e38f --- /dev/null +++ b/web/modules/custom/campuschampions/src/Plugin/StoreStats.php @@ -0,0 +1,112 @@ + 0, + // Institutions Represented + 'institutions' => 0, + // Estab­lished Program to Stim­u­late Com­pet­i­tive Research + 'epscor' => 0, + // Minority Serving Institutes + 'msi' => 0 + ]; + + $region = 572; // Campus Champions + + // Nation wide + $query = \Drupal::database() + ->select('users_field_data', 'f') + ->fields('f', ['uid']); + $query->innerJoin('user__field_region', 'n', 'n.entity_id = f.uid'); + $query->condition('n.deleted', 0); + $query->condition('n.field_region_target_id', $region); + $query->condition('f.status', 1); + + $data['nationwide'] = $query->countQuery() + ->execute() + ->fetchField(); + + // Institutions + $query = \Drupal::database() + ->select('user__field_institution', 'i') + ->fields('i', ['field_institution_value']); + $query->innerJoin('users_field_data', 'f', 'i.entity_id = f.uid'); + $query->innerJoin('user__field_region', 'n', 'n.entity_id = f.uid'); + $query->condition('i.deleted', 0); + $query->condition('n.deleted', 0); + $query->condition('n.field_region_target_id', $region); + $query->condition('f.status', 1); + + $data['institutions'] = $query->distinct() + ->countQuery() + ->execute() + ->fetchField(); + + // EPSCoR states + $query = \Drupal::database() + ->select('user__field_carnegie_code', 'c') + ->fields('c', ['field_carnegie_code_value']); + $query->innerJoin('users_field_data', 'f', 'c.entity_id = f.uid'); + $query->innerJoin('user__field_region', 'n', 'n.entity_id = f.uid'); + $query->innerJoin('carnegie_codes', 'r', 'r.unitid = c.field_carnegie_code_value'); + $query->condition('n.deleted', 0); + $query->condition('n.field_region_target_id', $region); + $query->condition('f.status', 1); + $query->condition('r.stabbr', CarnegieCodesLookup::EPSCOR_STATES, 'IN'); + + $data['epscor'] = $query->distinct() + ->countQuery() + ->execute() + ->fetchField(); + + // MSI + $query = \Drupal::database() + ->select('user__field_carnegie_code', 'c') + ->fields('c', ['field_carnegie_code_value']); + $query->innerJoin('users_field_data', 'f', 'c.entity_id = f.uid'); + $query->innerJoin('user__field_region', 'n', 'n.entity_id = f.uid'); + $query->innerJoin('carnegie_codes', 'r', 'r.unitid = c.field_carnegie_code_value'); + $query->condition('n.deleted', 0); + $query->condition('n.field_region_target_id', $region); + $query->condition('f.status', 1); + $query->condition('r.msi', 1); + + $data['msi'] = $query->distinct()->countQuery()->execute()->fetchField(); + + $this->data = json_encode($data); + } + + + /** + * Add stats to cc_stats state. + */ + public function setState() { + $this->getStats(); + \Drupal::state()->set('cc_stats',$this->data); + } +} From 464e95db4ca3d532c31735bd1cd4bd562cd3c4b9 Mon Sep 17 00:00:00 2001 From: protitude Date: Thu, 8 Aug 2024 12:59:01 -0600 Subject: [PATCH 3/5] fix campuschampions initial state --- web/modules/custom/campuschampions/campuschampions.install | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/campuschampions/campuschampions.install b/web/modules/custom/campuschampions/campuschampions.install index 579d429f8..d049d33da 100644 --- a/web/modules/custom/campuschampions/campuschampions.install +++ b/web/modules/custom/campuschampions/campuschampions.install @@ -554,13 +554,14 @@ function campuschampions_update_9000() { * @file * Set initial stats. */ -function campuschampions_update_10000() { +function campuschampions_update_10001() { $stats = [ 'nationwide' => 827, 'institutions' => 362, 'epscor' => 87, 'msi' => 61 ]; + $stats = json_encode($stats); \Drupal::state()->set('cc_stats',$stats); } From 0a8e0b789e33eeda6855fd88b48942b81790f8d2 Mon Sep 17 00:00:00 2001 From: Andrew Pasquale Date: Wed, 28 Aug 2024 16:14:21 -0400 Subject: [PATCH 4/5] Run cron for testing --- web/modules/custom/campuschampions/campuschampions.module | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/modules/custom/campuschampions/campuschampions.module b/web/modules/custom/campuschampions/campuschampions.module index 4f80fb71f..dedad8de9 100644 --- a/web/modules/custom/campuschampions/campuschampions.module +++ b/web/modules/custom/campuschampions/campuschampions.module @@ -368,8 +368,8 @@ function campuschampions_cron() { $min = date('i', $currentTime); // Cron runs every 10 minutes - if ($hour == 2 && $min < 20) { + //if ($hour == 2 && $min < 20) { \Drupal::service('cc.getStats')->setState(); - } + //} } From 7a41e3ab2906de3ebb13d9525e5f453ff4784c8d Mon Sep 17 00:00:00 2001 From: Andrew Pasquale Date: Tue, 1 Oct 2024 18:31:47 +0000 Subject: [PATCH 5/5] Add org and institution fields to Campus Champions export --- ...iews.view.campus_champions_user_export.yml | 146 +++++++++++++++++- 1 file changed, 139 insertions(+), 7 deletions(-) diff --git a/web/sites/default/config/default/views.view.campus_champions_user_export.yml b/web/sites/default/config/default/views.view.campus_champions_user_export.yml index a604cdd30..6fc195d43 100644 --- a/web/sites/default/config/default/views.view.campus_champions_user_export.yml +++ b/web/sites/default/config/default/views.view.campus_champions_user_export.yml @@ -3,7 +3,9 @@ langcode: en status: true dependencies: config: + - field.storage.user.field_access_organization - field.storage.user.field_carnegie_code + - field.storage.user.field_institution - taxonomy.vocabulary.region - user.role.administrator - user.role.campuschampionsadmin @@ -218,15 +220,78 @@ display: multi_type: separator separator: ', ' field_api_classes: false - field_carnegie_code_site: - id: field_carnegie_code_site - table: user__field_carnegie_code - field: field_carnegie_code + field_access_organization: + id: field_access_organization + table: user__field_access_organization + field: field_access_organization relationship: none group_type: group - admin_label: Site + admin_label: '' + plugin_id: field + label: Organization + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: target_id + type: entity_reference_label + settings: + link: false + group_column: entity_id + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + field_institution: + id: field_institution + table: user__field_institution + field: field_institution + relationship: none + group_type: group + admin_label: '' plugin_id: field - label: Site + label: Institution exclude: false alter: alter_text: false @@ -271,7 +336,7 @@ display: type: string settings: link_to_entity: false - group_column: value + group_column: entity_id group_columns: { } group_rows: true delta_limit: 0 @@ -344,6 +409,69 @@ display: multi_type: separator separator: ', ' field_api_classes: false + field_carnegie_code_site: + id: field_carnegie_code_site + table: user__field_carnegie_code + field: field_carnegie_code + relationship: none + group_type: group + admin_label: Site + plugin_id: field + label: 'Carnegie Code Site' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: string + settings: + link_to_entity: false + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false field_carnegie_code_location: id: field_carnegie_code_location table: user__field_carnegie_code @@ -1037,7 +1165,9 @@ display: - url.query_args - user.roles tags: + - 'config:field.storage.user.field_access_organization' - 'config:field.storage.user.field_carnegie_code' + - 'config:field.storage.user.field_institution' ccusers_data_export: id: ccusers_data_export display_title: 'Data export' @@ -1074,4 +1204,6 @@ display: - request_format - user.roles tags: + - 'config:field.storage.user.field_access_organization' - 'config:field.storage.user.field_carnegie_code' + - 'config:field.storage.user.field_institution'