Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Md 2172 #1225

Open
wants to merge 5 commits into
base: md-dev
Choose a base branch
from
Open

Md 2172 #1225

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion web/modules/custom/campuschampions/campuschampions.install
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,21 @@ function campuschampions_update_9000() {
],
]
]);
}
}

/**
* @file
* Set initial stats.
*/
function campuschampions_update_10001() {
$stats = [
'nationwide' => 827,
'institutions' => 362,
'epscor' => 87,
'msi' => 61
];
$stats = json_encode($stats);
\Drupal::state()->set('cc_stats',$stats);

}

16 changes: 16 additions & 0 deletions web/modules/custom/campuschampions/campuschampions.module
Original file line number Diff line number Diff line change
Expand Up @@ -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();
//}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
cc.getStats:
class: Drupal\campuschampions\Plugin\StoreStats
arguments: ['@renderer']
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
112 changes: 112 additions & 0 deletions web/modules/custom/campuschampions/src/Plugin/StoreStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Drupal\campuschampions\Plugin;

/**
* Store stats.
*/
class StoreStats {

/**
* Store stats here.
*/
private $data;

/**
* Constructor.
*/
public function __construct() {
}

/**
* Get stats.
*
* @param string $stabbr
* @return array
*/
public function getStats() {
$data = [
// Champions natiowide
'nationwide' => 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Loading