使用嵌入式JavaScript将问题导入Qulatrics [英] Importing questions into Qulatrics with embedded javascript

查看:96
本文介绍了使用嵌入式JavaScript将问题导入Qulatrics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到解决此问题的简单方法.我了解,有三种方法可以自动使用txt文件创建Qualtrics调查:使用

I am trying to find a straightforward solution to this problem. I understand that there are three ways to automate the creation of a Qualtrics survey with a txt file: either using the Simple format TXT file, the Advanced format TXT file, or using a QSF file, which is basically a serialized json object.

为了在大量问题中自动包含相同的javascript代码(并避免手动复制粘贴),我想创建一个包含这些问题的可导入文件.但是,似乎所有TXT文件格式都不允许包含javascript.

In order to automatically include the same javascript code in a massive set of questions (and avoid copy-pasting it manually), I wanted to create an importable file with the questions. However, none of the TXT file formats seems to allow including javascript.

似乎剩下的唯一选择是导入QSF文件.实话实说,鉴于Qualtrics没有提供帮助理解这种格式的API,试图几乎从头开始自动构建QSF真是一场噩梦.到目前为止,我一直在尝试弄清导出的文件,但是我开始认为这是不值得的.

It seems that the only option left would be to import a QSF file. Put if truth be told, given there is no API provided by Qualtrics to help understand this format, it's been hell of a nightmare to try to automatically build a QSF almost from scratch. So far I've been trying to make sense of an exported file, but I'm starting to consider that it's just not worth the effort.

关于如何更好地解决此问题的任何想法?为了让您大致了解这个问题,我要讲的是使用相同的javascript代码编写的大约250个不同的问题.

Any ideas on how I could better solve this problem? To give you an idea of the magnitude, what I'm talking about is around 250 different questions with the same javascript code.

非常感谢您获得的任何见解.

Thank you so much in advance for any insight I may receive.

有人要求我提供一些示例javascript代码,所以这里是:

I was asked for some sample javascript code, so here it is:

Qualtrics.SurveyEngine.addOnload(
    function()
    {
        document.getElementById('SkinContent').style.backgroundColor = "Lightblue";
    }
);

推荐答案

我花了相当长的时间才能做到这一点,但是我最终设法找到了一个解决方案(请注意,我的JavaScript代码非常复杂,与嵌入式元数据和因此,与我上面的示例不同,@ T.Gibbons的解决方案对我不起作用.

It took me quite a while to do this, but I finally managed to find a solution (please note my javascript code is quite complex, interacting with embedded metadata and so, unlike the example I put above, so @T. Gibbons' solution didn't work for me).

我按照以下步骤解决了这个问题:

I followed these steps to solve the problem:

  1. 使用一些示例项创建原型"调查.
  2. 将此调查作为QSF文件导出.
  3. 将QSF文件作为JSON对象读取.
  4. 标识与项目相关的属性.就我而言,我使用了R和包rjson,所以结果列表对象questionnaire的重要属性是questionnaire$SurveyElements[[6]]$SecondaryAttribute(包含项数),questionnaire$SurveyElements[[1]]$Payload[[1]]$BlockElements(形成一个块的元素列表) ),以及questionnaire$SurveyElements中的元素(从元素8开始)(有时可以从元素7开始,我认为这取决于垃圾是否为空),这些都是项目本身的定义.
  5. 从导出的调查中存在的示例项中创建项,然后将其添加到questionnaire对象中,修改这些属性.
  6. 将该对象序列化为JSON格式,并将其另存为QSF文件.
  7. 将QSF文件作为新调查导入Qualtrics.
  8. 为方便起见,将块与新项目一起使用(例如,我将块复制到库中,然后将其导入到主项目中).
  1. Create a "prototype" survey with a few sample items.
  2. Export this survey as a QSF file.
  3. Read the QSF file as a JSON object.
  4. Identify the properties that relate to the items. In my case, I used R and the package rjson, so the important properties of the resulting list object questionnaire were questionnaire$SurveyElements[[6]]$SecondaryAttribute (contains the number of items), questionnaire$SurveyElements[[1]]$Payload[[1]]$BlockElements (list of elements that form a block, in order), and the elements in questionnaire$SurveyElements, from element 8 on (sometimes it can be from element 7 on, I think it depends on whether the trash is empty or not) which are the definitions of the items themselves.
  5. Create items from the sample ones present in the exported suvey, and add them to the questionnaire object, modifying those properties.
  6. Serialize the object into the JSON format, and save it as a QSF file.
  7. Import the QSF file as a new survey in Qualtrics.
  8. Use the block with the new items at convenience (e.g. I copied the block to the library and then imported it into my master project).

这是一个简单的代码,可以执行R中所需的操作:

Here is a simple code that can do the operations needed, in R:

library(rjson)
library(tidyverse)
library(magrittr)

PROTOTYPE.FILE <- "Prototype.qsf"
JAVASCRIPT.FILE <- "Item_javascript_code.txt"

# Computes the number of questions in the survey (returns a 'character', as stored in the QSF file)
get.item.count <- function(questionnaire)
    questionnaire$SurveyElements %>% length() %>% subtract(7)

# Sets the property "nº of questions" in the questionnaire JSON object
set.num.items <- function(questionnaire, num.items = questionnaire %>% get.item.count) {

    questionnaire$SurveyElements[[6]]$SecondaryAttribute <- num.items %>% as.character

    return(questionnaire)
}

create.item <- function(item.num, stem, choices, javascript = NULL, html.text = FALSE) {

    item <- list(
        SurveyID = "SV_0rLrTOSkjnIWa2x",
        Element = "SQ",
        PrimaryAttribute = paste0("QID", item.num),
        SecondaryAttribute = stem,
        TertiaryAttribute = NULL,
        Payload = list(
            QuestionText = stem,
            DataExportTag = paste0("Q", item.num),
            QuestionType = "MC",
            Selector = "SAVR",
            SubSelector = "TX",
            Configuration = list(
                QuestionDescriptionOption = "UseText"
            ),
            QuestionDescription = stem,
            Choices = list(),
            ChoiceOrder = choices %>% seq_along,
            Validation = list(
                Settings = list(
                    ForceResponse = "OFF", ForceResponseType = "ON", Type = "None"
                )
            ),
            Language = list(),
            QuestionID = paste0("QID", item.num)
        )
    )

    for(choice in choices) {
        item$Payload$Choices <- item$Payload$Choices %>% append(list(list(Display = choice)))
    }
    names(item$Payload$Choices) <- item$Payload$Choices %>% seq_along %>% as.character

    if(javascript %>% is.null %>% not) {
        item$Payload$QuestionJS = javascript
        item$Payload$PrivateData = FALSE
    }

    return(item)
}

add.item <- function(questionnaire, stem, choices, javascript = NULL, html.text = FALSE) {

    item.num <- questionnaire %>% get.item.count +1

    questionnaire$SurveyElements %<>% append(
        list(
            create.item(questionnaire %>% get.item.count +1, stem, choices, javascript)
        )
    )

    questionnaire$SurveyElements[[1]]$Payload[[1]]$BlockElements %<>% append(
        list(
            list(
                Type = "Question",
                QuestionID = paste0("QID", item.num)
            )
        )
    )

    return(questionnaire)
}


questionnaire <- fromJSON(file = PROTOTYPE.FILE)

questionnaire$SurveyElements <- questionnaire$SurveyElements[1:7] # Drop items in the list
questionnaire$SurveyElements[[1]]$Payload[[1]]$BlockElements <- NULL # Empty item list in the block


for(question in 1:10) {

    questionnaire %<>% add.item(
        "Question stem here",
        c("Choice 1", "Choice 2", "etc..."),
        ## Javascript code here
    )
}

questionnaire %<>% set.num.items()


questionnaire %>% toJSON() %>% writeLines(con = "Output_prototype.qsf")

这可能更加复杂,包括不同类型的问题,计时器,分页符等,但是对于手头的问题,我认为这已经足够了.

This can be much more sophisticated, including different types of questions, timers, page breaks, etc., but for the question at hand I think it's quite enough with this.

这篇关于使用嵌入式JavaScript将问题导入Qulatrics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆