Skip to content

Commit

Permalink
5.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
scottjpearson committed Jun 30, 2023
1 parent f4e0060 commit e73f7bc
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 38 deletions.
3 changes: 1 addition & 2 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,12 @@ public static function getHeader($tokenName = "", $token = "", $server = "", $pi
if ($base64 = $module->getBrandLogo()) {
$str .= "<div class='topBar' style='float:right;'><img src='$base64' class='brandLogo'></div>";
} else {
$str .= "<div class='topBar' style='float:right;'><p class='alignright greytext padded nomargin bolded'>$tokenName</p></div>";
$str .= "<div class='topBar' style='float:right;'><p class='alignright darkgreytext padded nomargin bolded'>$tokenName</p></div>";
}
$str .= "</div>";
$str .= "</header>";

$switches = new FeatureSwitches($token, $server, $pid);
$switchValues = $switches->getSwitches();

$navBar = new NavigationBar();
$navBar->addFALink("home", "Home", CareerDev::getHomeLink());
Expand Down
2 changes: 1 addition & 1 deletion CareerDev.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CareerDev {
public static $passedModule = NULL;

public static function getVersion() {
return "5.10.3";
return "5.11.0";
}

public static function getLockFile($pid) {
Expand Down
3 changes: 2 additions & 1 deletion FlightTrackerExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,12 @@ function preprocessScholarPortal($pids) {
if ($needToUpdate) {
$firstName = $firstNamesByPid[$pid][$recordId] ?? "";
$lastName = $lastNamesByPid[$pid][$recordId] ?? "";
list($matches, $projectTitles) = Portal::getMatches($userid, $firstName, $lastName, $pids);
list($matches, $projectTitles, $photoBase64) = Portal::getMatches($userid, $firstName, $lastName, $pids);
$storedData = [
"date" => $today,
"matches" => $matches,
"projectTitles" => $projectTitles,
"photo" => $photoBase64,
"done" => TRUE,
];
Application::saveSystemSetting($userid, $storedData);
Expand Down
107 changes: 107 additions & 0 deletions charts/scholarGrantFunding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

use \Vanderbilt\CareerDevLibrary\Application;
use \Vanderbilt\CareerDevLibrary\LineGraph;
use \Vanderbilt\CareerDevLibrary\REDCapManagement;
use \Vanderbilt\CareerDevLibrary\Download;
use \Vanderbilt\CareerDevLibrary\Sanitizer;
use \Vanderbilt\CareerDevLibrary\Grants;

require_once(dirname(__FILE__)."/../small_base.php");
require_once(dirname(__FILE__)."/../classes/Autoload.php");

$allRecords = Download::recordIds($token, $server);
if (isset($_GET['record'])) {
$record = Sanitizer::getSanitizedRecord($_GET['record'], $allRecords);
if ($record) {
$records = [$record];
} else {
$records = [];
}
} else {
$records = $allRecords;
}
$metadata = Download::metadata($token, $server);
$grantFields = REDCapManagement::getMinimalGrantFields($metadata);
if (!in_array("record_id", $grantFields)) {
$grantFields[] = "record_id";
}

$budgetField = "total_budget";
$dollarsByYear = [];
foreach ($records as $recordId) {
$redcapData = Download::fieldsForRecords($token, $server, $grantFields, [$recordId]);
$grants = new Grants($token, $server, $metadata);
$grants->setRows($redcapData);
$grants->compileGrants();
foreach ($grants->getGrants("deduped") as $grant) {
$budget = $grant->getVariable($budgetField);
$budget = (int) str_replace("$", "", $budget);
if ($budget) {
$startTs = strtotime($grant->getVariable("start"));
$endDate = $grant->getVariable("end");
$endTs = $endDate ? strtotime($endDate) : time();
$totalTimespan = $endTs - $startTs;
$startYear = (int) date("Y", $startTs);
$endYear = (int) date("Y", $endTs);
if ($startYear == $endYear) {
if (!isset($dollarsByYear[$startYear])) {
$dollarsByYear[$startYear] = 0;
}
$dollarsByYear[$startYear] += $budget;
} else {
$startYearEndTs = strtotime(($startYear + 1)."-01-01");
$endYearStartTs = strtotime("$endYear-01-01");
$secsInStartYear = $startYearEndTs - $startTs;
$secsInEndYear = $endTs - $endYearStartTs;
$startYearBudget = round($secsInStartYear * $budget / $totalTimespan);
$endYearBudget = round($secsInEndYear * $budget / $totalTimespan);
if (!isset($dollarsByYear[$startYear])) {
$dollarsByYear[$startYear] = 0;
}
$dollarsByYear[$startYear] += $startYearBudget;
if (!isset($dollarsByYear[$endYear])) {
$dollarsByYear[$endYear] = 0;
}
$dollarsByYear[$endYear] += $endYearBudget;
if ($endYear - $startYear > 1) {
for ($year = $startYear + 1; $year <= $endYear - 1; $year++) {
$yearStartTs = strtotime($year."-01-01");
$yearEndTs = strtotime(($year + 1)."-01-01");
$secsInYear = $yearEndTs - $yearStartTs;
$yearBudget = round($secsInYear * $budget / $totalTimespan);
if (!isset($dollarsByYear[$year])) {
$dollarsByYear[$year] = 0;
}
$dollarsByYear[$year] += $yearBudget;
}
}
}
}
}
}
ksort($dollarsByYear);
$years = array_keys($dollarsByYear);
for ($year = $years[0]; $year <= $years[count($years) - 1]; $year++) {
if (!isset($dollarsByYear[$year])) {
$dollarsByYear[$year] = 0;
}
}
ksort($dollarsByYear);

$cssLink = Application::link("/css/career_dev.css");
echo "<link href='$cssLink' type='text/css' />";
if (empty($dollarsByYear)) {
echo "<h3>No Grant Funding to Display</h3>";
} else if (count($dollarsByYear) == 1) {
$year = array_keys($dollarsByYear)[0];
$dollars = $dollarsByYear[$year];
echo "<h3>You have one year represented: In $year, you brought in ".REDCapManagement::prettyMoney($dollars)." in grants.</h3>";
} else {
$graph = new LineGraph(array_values($dollarsByYear), array_keys($dollarsByYear), "scholarDollars_".$pid);
echo $graph->getImportHTML();
$graph->setXAxisLabel("Year");
$graph->setYAxisLabel("Dollars per Year");
$graph->setColor("#5764ae");
echo $graph->getHTML(800, 600);
}
37 changes: 23 additions & 14 deletions classes/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,25 @@ public static function metadataForms($token, $server) {
}
}

public static function metadataFieldsByPid($pid) {
$module = Application::getModule();
$sql = "SELECT field_name FROM redcap_metadata WHERE project_id = ? ORDER BY field_order";
if ($module) {
$q = $module->query($sql, [$pid]);
} else {
$q = db_query($sql, [$pid]);
}
$fields = [];
while ($row = $q->fetch_assoc()) {
$fields[] = $row['field_name'];
}
return $fields;
}

public static function metadataFields($token, $server) {
$pid = Application::getPID($token);
if ($pid && self::isCurrentServer($server)) {
$module = Application::getModule();
$sql = "SELECT field_name FROM redcap_metadata WHERE project_id = ? ORDER BY field_order";
if ($module) {
$q = $module->query($sql, [$pid]);
} else {
$q = db_query($sql, [$pid]);
}
$fields = [];
while ($row = $q->fetch_assoc()) {
$fields[] = $row['field_name'];
}
return $fields;
return self::metadataFieldsByPid($pid);
} else {
$metadata = self::metadata($token, $server);
return DataDictionaryManagement::getFieldsFromMetadata($metadata);
Expand Down Expand Up @@ -532,8 +536,13 @@ public static function alumniAssociations($token, $server) {
public static function fileAsBase64($pid, $field, $recordId, $instance = 1) {
$module = Application::getModule();
# assume classical project
$sql = "SELECT value FROM redcap_data WHERE project_id = ? AND field_name = ? AND record = ? AND instance = ?";
$params = [$pid, $field, $recordId, $instance];
if ($instance != 1) {
$sql = "SELECT value FROM redcap_data WHERE project_id = ? AND field_name = ? AND record = ? AND instance = ?";
$params = [$pid, $field, $recordId, $instance];
} else {
$sql = "SELECT value FROM redcap_data WHERE project_id = ? AND field_name = ? AND record = ? AND instance IS NULL";
$params = [$pid, $field, $recordId];
}
if ($module) {
$result = $module->query($sql, $params);
} else {
Expand Down
1 change: 0 additions & 1 deletion classes/Grants.php
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,6 @@ public static function makeArray($grant) {
"activity_type",
"funding_institute",
"institute_code",
"title",
"serial_number",
"support_year",
"other_suffixes",
Expand Down
2 changes: 1 addition & 1 deletion classes/NIHTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -3091,7 +3091,7 @@ public static function getTrainingTypesForGrantClass($requestedGrantClass = FALS
} else if (in_array($requestedGrantClass, ["K"])) {
return [2, 10];
} else if ($requestedGrantClass == "") {
return [2]; // K12 by default
return [2, 10]; // K12 by default
}
throw new \Exception("Invalid Grant Class: $grantClass");
}
Expand Down
82 changes: 74 additions & 8 deletions classes/Portal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vanderbilt\CareerDevLibrary;

use ZipStream\File;
use function Vanderbilt\FlightTrackerExternalModule\appendCitationLabel;

require_once(__DIR__ . '/ClassLoader.php');
Expand Down Expand Up @@ -91,11 +92,9 @@ public static function getMatches($username, $firstName, $lastName, $pids) {
$projectTitles[$pid] = Download::shortProjectTitle($token, $server);
}
Application::unsetPid();
} else {
error_log("Portal: Skipping $pid");
}
}
return [$matches, $projectTitles];
return [$matches, $projectTitles, self::getPhoto($matches)];
}

public static function getPage($relativeFileLocation, $pid, $getParams = []) {
Expand Down Expand Up @@ -167,6 +166,24 @@ public static function getLogo($name = "") {
return "";
}

public static function getPhoto($allMatches) {
$targetField = "identifier_picture";
if ($allMatches) {
foreach ($allMatches as $pid => $recordsAndNames) {
$fields = Download::metadataFieldsByPid($pid);
if (in_array($targetField, $fields)) {
foreach (array_keys($recordsAndNames) as $recordId) {
$base64 = Download::fileAsBase64($pid, $targetField, $recordId);
if ($base64) {
return $base64;
}
}
}
}
}
return "";
}

# 3 words max
public static function getMenu() {
$menu = [];
Expand All @@ -186,9 +203,13 @@ public static function getMenu() {
"action" => "honors",
"title" => "Honors &amp; Awards", // new survey
];
$menu["Your Info"][] = [
"action" => "photo",
"title" => "Photos", // should search all projects; also, should display
];
// TODO ORCID Bio Link in Your Info

// TODO encouraging message if pubs are blank
// TODO encouraging message if pubs/grants are blank
$menu["Your Graphs"][] = [
"action" => "scholar_collaborations",
"title" => "Publishing Collaborations", // social network graph
Expand All @@ -201,10 +222,14 @@ public static function getMenu() {
"action" => "timelines",
"title" => "Grant &amp; Publication Timelines", // Pubs & all grants; encouraging message if blank
];
// $menu["Your Graphs"][] = [
// "action" => "group_collaborations",
// "title" => "Group Publishing Collaborations",
// ];
$menu["Your Graphs"][] = [
"action" => "group_collaborations",
"title" => "Group Publishing Collaborations (Computationally Expensive)",
];
$menu["Your Graphs"][] = [
"action" => "grant_funding",
"title" => "Grant Funding by Year",
];

$menu["Your Network"][] = [
"action" => "mentoring",
Expand Down Expand Up @@ -244,6 +269,47 @@ public static function getHeaders() {
return $html;
}

# TODO Uploads to all projects - should it only upload to ones without an existing photo? Am checking with Arnita...
public static function uploadPhoto($filename, $mimeType, $matches) {
$extension = FileManagement::getMimeSuffix($mimeType);
$base64 = FileManagement::getBase64OfFile($filename, $mimeType);
$oneUploadSuccessful = FALSE;
foreach ($matches as $pid => $recordsAndNames) {
foreach ($recordsAndNames as $recordId => $name) {
$newFilename = FileManagement::makeSafeFilename(strtolower(REDCapManagement::makeHTMLId($name).".".$extension));
$result = Upload::file($pid, $recordId, "identifier_picture", $base64, $newFilename);
if (isset($result['error']) && $oneUploadSuccessful) {
throw new \Exception("Partially uploaded.".$result['error']);
} else if (isset($result['error'])) {
throw new \Exception($result['error']);
} else {
$oneUploadSuccessful = TRUE;
}
}
}
if ($oneUploadSuccessful) {
return $base64;
} else {
return "";
}
}

public static function getModifyPhotoPage($allMatches) {
$base64 = self::getPhoto($allMatches);
if ($base64) {
$html = "<h3>Replace Your Photo</h3>";
} else {
$html = "<h3>Add a Photo</h3>";
}
$driverLink = Application::link("portal/driver.php").(isset($_GET['uid']) ? "&uid=".$_GET['uid'] : "");
$html .= "<form action='$driverLink' method='POST' enctype='multipart/form-data' id='photoForm'>";
$html .= "<input type='hidden' name='action' value='upload_photo' />";
$html .= "<p class='centered'><label for='photoFile'>Photo:</label> <input type='file' id='photoFile' name='photoFile' onchange='portal.validateFile(this);' /><br/>";
$html .= "<button onclick='portal.uploadPhoto(\"#photoForm\"); return false;'>Upload</button></p>";
$html .= "</form>";
return $html;
}

public static function makeMentoringPortal($pid, $recordId, $projectTitle, $driverURL) {
$redcapData = Download::getDataByPid($pid, ["record_id", "summary_mentor", "summary_mentor_userid"], [$recordId]);
$mentorList = REDCapManagement::findField($redcapData, $recordId, "summary_mentor");
Expand Down
Loading

0 comments on commit e73f7bc

Please sign in to comment.