Help in merging customPromptTypes

Many thanks Team
please I need help in merging the attached customPromptTypes

Much appreciation
for_merging.zip (2.4 KB)

I have tried merging them and the survey stops
Attached is my merge
The firstAttempt works but custom dates and async-assign does not work
The secondAttempt results into a showstopper
attempts.zip (4.6 KB)

Here is the merged script. The issue with both of your attempts was that you tried to defined multiple AMD modules but Survey only expects 1.

See requirejs documentation for more information.
https://requirejs.org/docs/api.html

define(['promptTypes','jquery','underscore', 'opendatakit', 'database', '../../soundex.js', '../../doublemetaphone.js', 'formulaFunctions', 'prompts'],
function(promptTypes, $,       _, opendatakit, database, soundex, doubleMetaphone, formulaFunctions) {
// custom functions are placed under 'window' to be visible in calculates...
// note that you need to be careful about naming -- should probably go somewhere else?
window.is_finalized = function() {
    return ('COMPLETE' === database.getInstanceMetaDataValue('_savepoint_type'));
};


var custom_date = promptTypes.datetime.extend({
    type: "date",
    showTime: false,
    timeFormat: "MM/DD/YYY",
    timeTemplate: "DD / MM / YYYY"
});

var async_assign = promptTypes.base.extend({
    type: "async_assign",
    debug: false,
    valid: true,
    templatePath: '../config/tables/quest1_02_0/forms/quest1_02_0/templates/async_assign.handlebars',
    _cachedSelection: null,
    getLinkedTableId: function() {
        var queryDefn = opendatakit.getQueriesDefinition(this.values_list);
        if ( queryDefn != null )
        {
            if ( queryDefn.linked_table_id == null ) {
                return queryDefn.linked_form_id;
            } else {
                return queryDefn.linked_table_id;
            }
        } else {
            odkCommon.log('E',"query definiton is null for " + this.type + " in getLinkedTableId");
            return null;
        }
    },
    getLinkedFormId: function() {
        var queryDefn = opendatakit.getQueriesDefinition(this.values_list);
        if ( queryDefn != null )
        {
            return queryDefn.linked_form_id;
        } else {
            odkCommon.log('E',"query definiton is null for " + this.type + " in getLinkedFormId");
            return null;
        }
    },
    getLinkedFieldName: function() {
        var queryDefn = opendatakit.getQueriesDefinition(this.values_list);
        if ( queryDefn != null )
        {
            return queryDefn.fieldName;
        } else {
            odkCommon.log('E',"query definiton is null for " + this.type + " in getLinkedFieldName");
            return null;
        }
    },
    getFormPath: function() {
        if ( this.getLinkedFormId() === "framework" ) {
            return '../config/assets/framework/forms/framework/';
        } else {
            return '../config/tables/' + this.getLinkedTableId() + '/forms/' + this.getLinkedFormId() + '/';
        }
    },
    convertSelection: function(linkedMdl) {
        var queryDefn = opendatakit.getQueriesDefinition(this.values_list);
        var that = this;
        if ( queryDefn.selection == null || queryDefn.selection.length === 0 ) {
            return null;
        }
        if ( that._cachedSelection != null ) {
            return that._cachedSelection;
        }
        that._cachedSelection = database.convertSelectionString(linkedMdl, queryDefn.selection);
        return that._cachedSelection;
    },
    _linkedCachedMdl: null,
    _linkedCachedInstanceName: null,
    getLinkedInstanceName: function() {
        return this._linkedCachedInstanceName;
    },
    getLinkedMdl: function(ctxt) {
        var that = this;
        if ( that._linkedCachedMdl != null ) {
            ctxt.success(that._linkedCachedMdl);
            return;
        }
        var filePath = that.getFormPath() + 'formDef.json';
        opendatakit.readFormDefFile($.extend({},ctxt,{success:function(formDef) {
             var ino = opendatakit.getSettingObject(formDef, 'instance_name');
             if ( ino !== null ) {
                that._linkedCachedInstanceName = ino.value;
            } else {
                that._linkedCachedInstanceName = null;
            }
            database.readTableDefinition($.extend({}, ctxt, {success:function(tlo) {
                ctxt.log('D',"prompts." + that.type +
                    'getLinkedMdl.readTableDefinition.success', "px: " + that.promptIdx );
                that._linkedCachedMdl = tlo;
                ctxt.success(tlo);
            }}), formDef, that.getLinkedTableId(), filePath);
        }}), filePath );
    },
    choice_filter: function(){ return true; },
    configureRenderContext: function(ctxt) {
        var that = this;
        var queryDefn = opendatakit.getQueriesDefinition(this.values_list);
        ctxt.log('D',"prompts." + that.type + ".configureRenderContext", "px: " + that.promptIdx);
        that.getLinkedMdl($.extend({},ctxt,{success:function(linkedMdl) {
            var dbTableName = linkedMdl.table_id;
            var selString = that.convertSelection(linkedMdl);
            var selArgs = queryDefn.selectionArgs();
            var displayElementName = that.getLinkedFieldName();
            ctxt.log('D',"prompts." + that.type + ".configureRenderContext.before.get_linked_instances", "px: " + that.promptIdx);
            database.get_linked_instances($.extend({},ctxt,{success:function(instanceList) {
                ctxt.log('D',"prompts." + that.type + ".configureRenderContext.success.get_linked_instances", "px: " + that.promptIdx);
                var filteredInstanceList = _.filter(instanceList, function(instance) {
                    return that.choice_filter(instance);
                });
                instanceList = filteredInstanceList;
                // get the value we are aggregating
                var valueList = _.map(instanceList, function(instance) {
                    return instance['display_field'];
                    });
                // discard any nulls or undefineds
                valueList = _.filter(valueList, function(value) {
                    return value !== null && value !== undefined;
                });

                var aggValue;
                if ( valueList.length === 0 ) {
                    // set aggValue to null
                    aggValue = null;
                    if ( that.type === "async_assign_total" ) {
                        aggValue = 0.0;
                    } else if ( that.type === "async_assign_count" ) {
                        aggValue = 0;
                    }
                } else {
                    if ( that.type === "async_assign_max" ) {
                        aggValue = _.max(valueList);
                    } else if ( that.type === "async_assign_min" ) {
                        aggValue = _.min(valueList);
                    } else if ( that.type === "async_assign_avg" ) {
                        var sum = _.reduce(valueList, function(memo, value) { return memo + value; }, 0.0);
                        aggValue = sum / valueList.length;
                    } else if ( that.type === "async_assign_sum"  || that.type === "async_assign_total" ) {
                        var sum = _.reduce(valueList, function(memo, value) { return memo + value; }, 0.0);
                        aggValue = sum;
                    } else if ( that.type === "async_assign_count" ) {
                        aggValue = valueList.length;
} else if (that.type === "async_assign_num_value" || that.type === "async_assign_text_value") {
                        aggValue = _.first(valueList);
                    } else {
                        ctxt.log('E',"prompts." + that.type + ".configureRenderContext.unrecognizedPromptType", "px: " + that.promptIdx);
                        aggValue = null;
                    }
                }

                that.setValueDeferredChange(aggValue);
                that.renderContext.type = that.type;
                that.renderContext.valueList = JSON.stringify(valueList);
                that.renderContext.aggValue = (aggValue === null) ? "null" : ((aggValue === undefined) ? "undefined" : aggValue);

                ctxt.log('D',"prompts." + that.type + ".configureRenderContext.success.get_linked_instances.success", "px: " + that.promptIdx + " instanceList: " + instanceList.length);
                ctxt.success();
            }}), dbTableName, selString, selArgs, displayElementName, null);
        }}));
    }
});

formulaFunctions.soundex = soundex;
formulaFunctions.doubleMetaphone = doubleMetaphone;

return {
"async_assign" : async_assign,
"custom_date" : custom_date,
"async_assign_num_value" : async_assign.extend({
    type: "async_assign_num_value",
	datatype: "number"
}),
"async_assign_text_value" : async_assign.extend({
    type: "async_assign_text_value",
	datatype: "string"
}),
"async_assign_max" : async_assign.extend({
    type: "async_assign_max",
    datatype: "number"
}),
"async_assign_min" : async_assign.extend({
    type: "async_assign_min",
    datatype: "number"
}),
"async_assign_avg" : async_assign.extend({
    type: "async_assign_avg",
    datatype: "number"
}),
"async_assign_sum" : async_assign.extend({
    type: "async_assign_sum",
    datatype: "number"
}),
"async_assign_total" : async_assign.extend({
    type: "async_assign_total",
    datatype: "number"
}),
"async_assign_count" : async_assign.extend({
    type: "async_assign_count",
    datatype: "integer"
})
}});
2 Likes

Many many Thanks @linl33.You are a blessing
Using the merged script am getting the attached error msg and when I click ok ,another one pops again when I click ok it allows me to proceed. I have checked the database everything seems ok When I want to view data via the contents tab again it does not allow me. It throws the error again

Is there anything am not doing right?
I have put the right template path in the CustomPromptTypes.js

Check the templatePath definition in your customPromptTypes.js. It looks like it still has the path from Caroline’s example. You need to change it to that of your configuration.

See Using ODK-X XLSX Converter — ODK-X Docs

2 Likes

Many thanks @linl33
I did forget to edit the path. I have edited the path but still getting the same error. Other than editing the templatePath is there another file I need to edit. Can framework.xlsx configuration be attributed to this?

1 Like

Hi @Duncan_Otieno! Error in template sometimes means that you are missing a variable in your model statement. Have you checked whether the variables you are trying to assign are in your model statement, with the right type for what you are trying to assign (e.g. integer for a count, string for a string)?