Skip to content

Commit

Permalink
Support IF on public surveys
Browse files Browse the repository at this point in the history
  • Loading branch information
grezniczek committed May 31, 2022
1 parent ca83500 commit 0902f92
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ When a value for _success_ is defined, the field with the action tag will be set

### Combining **@SURVEY-AUTH** with **@IF**

The **@SURVEY-AUTH** action tag can be used inside **@IF** (since v1.4.0), but only on non-public surveys.
For public surveys, as long as **@SURVEY-AUTH** is present _anywhere_ (even when inside the _false part_ of an @IF), the survey taker will need to log in before being able to proceed.
The **@SURVEY-AUTH** action tag can be used inside **@IF** action tags. Note that in public surveys, the record does not exist yet, and does any logic should be restricted to record-independent elements, such as e.g. the [arm-number], [arm-label] or the aggregate smart variables.

## [Changelog](#changelog)

Release | Description
------- | ---------------------
v1.4.1 | Bugfix: @SURVEY-AUTH with @IF works now in public surveys.
v1.4.0 | Compatiblity fix: @SURVEY-AUTH can now be used inside @IF; Bugfix: Some JavaScript was missing from the login page; Minimum REDCap version is 12.0.7.
v1.3.1 | Changed the way the url for login form submission is generated.
v1.3.0 | New feature: Login for non-public surveys. Simply add the @SURVEY-AUTH action tag (this must first be enabled in the system settings of the module). Enhancement: The token is not required any longer, but instead replaced by a setting that controls whether the module may write data to a record (during an upgrade, tokens are deleted and write mode set to ON).
Expand Down
12 changes: 10 additions & 2 deletions SurveyAuthExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ function redcap_survey_page_top($project_id, $record, $instrument, $event_id, $g
$record = \Survey::getRecordFromPartId([$participant_id])[$participant_id];
if ($record != null) {
// We can be sure that the record exists! So we can safely do this:
// We must set this to something other than 0 in order to get @IF action tag parsing to work. Duh.
// We must set this to something other than 0 in order to get @IF action tag parsing to work (Form::evaluateIfActionTag() relies on this global).
$GLOBALS["hidden_edit"] = 1;
}
}
if (\Survey::isPublicSurvey() && $record == null) {
// This is a hack to work around a bug in Form::evaluateIfActionTag() /REDCap::evaluateLogic()
$record = "__some_record_id_that_should_definitely_NOT_exist___" . \Crypto::getGuid();
}

$this->settings = new SurveyAuthSettings($this);

Expand Down Expand Up @@ -208,7 +212,7 @@ private function getTaggedFields($dataDictionary, $project_id, $record, $event_i
{
$fields = array();
foreach ($dataDictionary as $fieldInfo) {
$evaluatedFieldAnnotation = \Survey::isPublicSurvey() ? $fieldInfo->field_annotation : \Form::replaceIfActionTag($fieldInfo->field_annotation, $project_id, $record, $event_id, $instrument, $repeat_instance);
$evaluatedFieldAnnotation = \Form::replaceIfActionTag($fieldInfo->field_annotation, $project_id, $record, $event_id, $instrument, $repeat_instance);
if (strpos($evaluatedFieldAnnotation, "@".SurveyAuthExternalModule::$ACTIONTAG)) {
array_push($fields, new SurveyAuthInfo($fieldInfo, $dataDictionary));
}
Expand Down Expand Up @@ -292,6 +296,10 @@ function authenticate($username, $password, $project_id, $instrument, $event_id,
else {
// Use first, any further are ignored
$tf = $taggedFields[0];
// If this is a public survey, we need to set $record to null
if (\Survey::isPublicSurvey()) {
$record = null;
}
// If this is a nonpublic survey, $record will be set so
// just update that record. Otherwise, create new record.
$record = $record ?? $this->addAutoNumberedRecord();
Expand Down

0 comments on commit 0902f92

Please sign in to comment.