From 2d331e08d84cbc6118cccb145e8dc7eb51259d0e Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Tue, 27 Dec 2022 10:14:12 +0100 Subject: [PATCH] additional updates as part of refactor --- .gitignore | 22 +++++++ ExternalModule.php | 151 +++++++++++++++++++++++++++------------------ config.json | 11 +++- 3 files changed, 124 insertions(+), 60 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09f3849 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# IDE project files for IntelliJ - it tends to generate lots of files depending +# on your exact version, which plugins are installed, etc., so ignore everything +# except for a vetted list of things we want. In particular, we want code +# formatting style (which needs to match the checkstyle Maven configuration). +/.idea/* + +# Don't want any encryption keys in version control +*.jks +*.jceks +*.pkcs12 +*.pk12 +*.pem +*.pfx + +# Operating system garbage +.DS_Store +[Tt]humbs.db + +# Emacs backup files +*~ + +/vendor/ diff --git a/ExternalModule.php b/ExternalModule.php index e79e032..7560ceb 100644 --- a/ExternalModule.php +++ b/ExternalModule.php @@ -9,7 +9,7 @@ use ExternalModules\AbstractExternalModule; use ExternalModules\ExternalModules; use Form; -Use Stanford\Utility\ActionTagHelper; + /** * ExternalModule class for Image Map. @@ -22,95 +22,129 @@ class ExternalModule extends AbstractExternalModule { * @inheritdoc */ function redcap_every_page_top($project_id) { - if (PAGE == 'Design/online_designer.php' && $project_id) { - - echo ""; - echo ""; - echo ""; - echo " - - "; - - $this->includeJs('js/helper.js'); - } - if (!in_array(PAGE, array('DataEntry/index.php', 'surveys/index.php', 'Surveys/theme_view.php'))) { - return; + // Handle online designer view + if (PAGE == 'Design/online_designer.php' && $project_id) { + $this->injectActionTagHelper(); } - - if (empty($_GET['id'])) { - return; + // Handle Data Entry page + else if (PAGE === 'DataEntry/index.php') { + $this->injectImageMaps(); } - - // Checking additional conditions for survey pages. - if (PAGE == 'surveys/index.php' && !(isset($_GET['s']) && defined('NOAUTH'))) { - return; + // Handle Survey pages + else if (in_array(PAGE, ['surveys/index.php', 'Surveys/theme_view.php']) && !empty($GET['id'])) { + if (!isset($_GET['s']) && defined('NOAUTH')) { + // Do not remember this use case + return; + } + $this->injectImageMaps(); } + } + private function injectImageMaps() { global $Proj; $settings = array(); // Loop through action tags - $instrument = $_GET['page']; // This is a bit of a hack, but in surveys this is set before the every_page_top hook is called + // This is a bit of a hack, but in surveys this value is set before the every_page_top hook is called + $instrument = $_GET['page']; - //TODO: Consider switching over to ActionTagHelper to support single-param overrides in imagemap (such as hiding or showing the radio/checkboxes) + // TODO: Consider switching over to ActionTagHelper to support single-param overrides in imagemap + // (such as hiding or showing the radio/checkboxes) // Check action-tags for this page foreach (array_keys($Proj->forms[$instrument]['fields']) as $field_name) { $field_info = $Proj->metadata[$field_name]; - if (!$display_mode = Form::getValueInActionTag($field_info['misc'], $this->tag)) { + if (!$imagemap_name = Form::getValueInActionTag($field_info['misc'], $this->tag)) { continue; } - // $row = $this->getDefaultConfig($display_mode); - $row = $this->getImageMapParams($display_mode); + // load the imagemap + $row = $this->getImageMapParams($imagemap_name); if (empty($row)) { // The specified imagemap is not defined - } else { - // Add the imagemap to the settings - $row['field'] = $field_name; - - $dir = $this->getModulePath(); - - $b64 = base64_encode(file_get_contents($dir . $row['image'])); + \REDCap::logEvent("Missing ImageMap", "$imagemap_name is defined for field $field_name but does not exist.", + "", "", "", $project_id); + continue; + } - $src = "data:image/png;base64,$b64"; - $row['src'] = $src; + // Add the imagemap to the settings + $row['field'] = $field_name; + $row['type'] = $field_info['element_type']; - $row['areas'] = file_get_contents($dir . $row['map']); - $row['type'] = $field_info['element_type']; + $dir = $this->getModulePath(); + $row['areas'] = file_get_contents($dir . $row['map']); + $row['src'] = "data:image/png;base64," . base64_encode(file_get_contents($dir . $row['image']));; - $settings[] = $row; - } + $settings[] = $row; } if (empty($settings)) { return; } - echo ''; - echo ''; - + // Inject the javascript to the client $this->includeJs('js/imageMapster.js'); - $this->includeJs('js/imagemap.js'); + + // Inject the JSMO + echo $this->initializeJavascriptModuleObject(); + $data = [ + "settings" => $settings, + "enableConsoleLogs" => $this->getProjectSetting('enable-console-logs') + ]; + $this->includeJs('js/jsmo.js'); + ?> + + + initializeJavascriptModuleObject(); + $data = [ + "helpUrl" => $this->getUrl('documentation.php'), + "maps" => $this->getImageMapParams() + ]; + $this->includeJs('js/jsmo.js'); + ?> + + + getSystemSetting('use-api-urls'); $ext_path = $this->getUrl($path, true, $use_api_urls); - echo ''; } @@ -135,7 +168,7 @@ public function getImageMapParams($image_map = null) { //TODO: Support having custom-maps defined via the EM config $image_maps = $this->getConfig()['default-image-maps']; - if ($image_map !== null) { + if ($image_map !== null && isset($image_maps[$image_map])) { return $image_maps[$image_map]; } else { return $image_maps; diff --git a/config.json b/config.json index 65fbcbf..f185738 100644 --- a/config.json +++ b/config.json @@ -5,6 +5,7 @@ "permissions": [ "redcap_every_page_top" ], + "framework-version": 9, "authors": [ { "name": "Andy Martin", @@ -18,8 +19,16 @@ } ], "compatibility": { - "redcap-version-min": "8.0.3" + "redcap-version-min": "12.0.4" }, + "project-settings": [ + { + "key": "enable-console-logs", + "name": "Enable console logging
If you check this box, logs will be written out to the console", + "required": false, + "type": "checkbox" + } + ], "system-settings": [ { "key": "use-api-urls",