Qualtrics-从行为任务(BART)获取数据

时间:2018-07-02 21:20:28

标签: javascript html qualtrics

我目前正在尝试使用Javascript获得行为任务以与Qualtrics一起使用。从本质上讲,任务包括泵气球的人员。每个泵使它更接近弹出气球,但它们根据泵的次数获得更多奖励。他们可以随时收集奖励。总共,他们重复抽气球并收集30次奖励(30发)。我对他们每轮按下泵按钮多少次感兴趣。

我能够在线找到一个脚本,该脚本可用于在Qualtrics中显示任务(不是我自己编写的)。在这里:

<script>$(document).ready(function() {

    var saveThis = 'hidden'; // text fields that saves data should not be shown; can be shown in testing

    // initialize values
    var round = 0;
    var start_size = 150; // start value of widht & height of the image; must correspond to the value that is specified for the #ballon id in style.css
    var increase = 2; // number of pixels by which balloon is increased each pump
    var size; // start_size incremented by 'increase'
    var pumps;
    var total = 0; // money that has been earned in total
    var rounds_played = 30;
    var explode_array =  [31, 80,  63, 103, 20,  26, 100,  75, 109,  72,  88,  77, 113, 22,  83,  86,  57,  14, 9,  90,  56,  41,  56,  27, 108,  42, 116,  18,  43,  95];
    var maximal_pumps = 128;
    var pumpmeup; // number pumps in a given round; is updated each round
    var number_pumps = []; // arrays for saving number of pumps
    var exploded = []; // array for saving whether ballon has exploded
    var explosion; // will an explosion occur? 1 = yes, 0 = no
    var last_win = 0; // initialize variable that contains the win of the previous round

    // initialize language
    var label_press = 'Pump Balloon';
    var label_collect = 'Collect Money';
    var label_balance = 'Total money collected:';
    var label_last = 'Money gained last round:';
    var label_currency = ' USD';
    var label_header = 'Balloon Pump Round ';
    var label_gonext1 = 'Start Next Round';
    var label_gonext2 = 'Spiel beenden';
    var msg_explosion1 = '<p>The Balloon exploded after ';
    var msg_explosion2 = ' pumps. No money will be transfered to the bank. <p>Click below to start the next round.</p>';

    var msg_collect1 = '<p>The balloon did not explode!</p><p>The money collected in Round 1 is ';
    var msg_collect2 = ' USD.</p><p> The money you collected will be put into the bank</p>';

    var msg_end1 = '<p>Damit ist dieser Teil der Studie abgeschlossen. Sie haben im Ballon-Spiel ';
    var msg_end2 = ' Taler Gewinn gemacht. </p><p>Klicken Sie auf <i>Weiter</i>, um mit der Studie fortzufahren.</p>';

    var err_msg = 'You need to pump the balloon at least once before collecting the money. Please click "Pump Balloon"';


    // initialize labels
    $('#press').html(label_press);
    $('#collect').html(label_collect);
    $('#total_term').html(label_balance);
    $('#total_value').html(total+label_currency);
    $('#last_term').html(label_last);
    $('#last_value').html(last_win+label_currency);

    // below: create functions that define game functionality

    // what happens when a new round starts
    var new_round = function() {
        console.log(number_pumps);
        console.log(exploded);
        $('#gonext').hide();
        $('#message').hide();
        $('#collect').show();
        $('#press').show();
        round += 1;
        size = start_size;
        pumps = 0;
        $('#ballon').width(size);
        $('#ballon').height(size);
        $('#ballon').show();
        $('#round').html('<h2>'+label_header+round+'<h2>');
    };

    // what happens when the game ends
    var end_game = function() {
        $('#total').remove();
        $('#collect').remove();
        $('#ballon').remove();
        $('#press').remove();
        $('#gonext').remove();
        $('#round').remove();
        $('#last_round').remove();
        $('#goOn').show();
        $('#message').html(msg_end1+total+msg_end2).show();
        store_data(); // note: this function needs to be defined properly
    };

    // Important: this function will have to be replaced to ensure that
    // the data is actually sent to _your_ server:
    var store_data = function() {
        $('#saveThis1').html('<input type='+saveThis+' name ="v_177" value="'+number_pumps+'" />');
        $('#saveThis2').html('<input type='+saveThis+' name ="v_178" value="'+exploded+'" />');
        $('#saveThis3').html('<input type='+saveThis+' name ="v_577" value="'+total+'" />');
    };

    // message shown if balloon explodes
    var explosion_message = function() {
        $('#collect').hide();
        $('#press').hide();
        $('#message').html(msg_explosion1+pumpmeup+msg_explosion2).show();
    };

    // message shown if balloon does not explode
    var collected_message = function() {
        $('#collect').hide();
        $('#press').hide();
        $('#message').html(msg_collect1+pumpmeup+msg_collect2).show();
    };

    // animate explosion using jQuery UI explosion
    var balloon_explode = function() {
        $('#ballon').hide( "explode", {pieces: 48}, 1000 );

        // activate this if you have a sound file to play a sound
        // when the balloon explodes:

        // document.getElementById('explosion_sound').play();
    };

    // show button that starts next round
    var gonext_message = function() {
        $('#ballon').hide();
        if (round < rounds_played) {
            $('#gonext').html(label_gonext1).show();
        } else {
            $('#gonext').html(label_gonext2).show();
        }
    };

    // add money to bank
    var increase_value = function() {
        $('#total_value').html(total+label_currency);
    };

    var show_last = function() {
        $('#last_value').html(last_win+label_currency);
    };

    // button functionalities

    // pump button functionality
    $('#press').click(function() {
        if (pumps >= 0 && pumps < maximal_pumps) { // interacts with the collect function, which sets pumps to -1, making the button temporarily unclickable
            explosion = 0; // is set to one if pumping goes beyond explosion point; see below
            pumps += 1;
            if (pumps < explode_array[round-1]) {
	        size +=increase;
	        $('#ballon').width(size);
                $('#ballon').height(size);

            } else {
	        last_win = 0;
	        pumpmeup = pumps;
          pumps = -1; // makes pumping button unclickable until new round starts
	        explosion = 1; // save that balloon has exploded this round
	        balloon_explode();
	        exploded.push(explosion); // save whether balloon has exploded or not
	        number_pumps.push(pumpmeup); // save number of pumps
          setTimeout(explosion_message, 1200);
	        setTimeout(gonext_message, 1200);
	        setTimeout(show_last, 1200);
            }
        }
    });

    // collect button: release pressure and hope for money
    $('#collect').click(function() {
        if (pumps === 0) {
	    alert(err_msg);
        } else if (pumps > 0) { // only works after at least one pump has been made
      exploded.push(explosion); // save whether balloon has exploded or not
            // activate this if you have a sound file to play a sound
            // when the balloon does not explode:

	    // document.getElementById('tada_sound').play();
      number_pumps.push(pumps); // save number of pumps
	    pumpmeup = pumps;
      pumps = -1; // makes pumping button unclickable until new round starts
	    $('#ballon').hide();
	    collected_message();
	    gonext_message();
	    total += pumpmeup;
	    last_win = pumpmeup;
	    increase_value();
	    show_last();
    });

    // click this button to start the next round (or end game when all rounds are played)
    $('#gonext').click(function() {
        if (round < rounds_played) {
            new_round();
        } else {
            end_game();
        }
    });

    // continue button is shown when the game has ended. This needs to be replaced
    // by a function that takes into account on which platform the BART runs (i.e.
    // how will the page be submitted?)
    $("#goOn").click(function() {
        $("form[name=f1]").submit();
    });
    // start the game!
    new_round();

});

</script>

唯一缺少的是让Qualtrics记录我想要的数据的方法。我浏览了各个论坛,发现以下脚本可用于让Qualtrics记录数据:

Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', value );
Qualtrics.SurveyEngine.getEmbeddedData( 'embeddedDataVariable');

我还必须在Qualtrics中的调查流程中设置和嵌入数据变量。我认为最好的方法是在每个循环中都有一个代表泵的嵌入式数据变量(例如,第一轮为BART1,第二轮为BART2)。如果回合号与我要记录的嵌入数据变量匹配,则将代码设置为具有条件(if)语句,该条件语句将使用上述代码记录泵的数量。例如:

} if (round = 1){
            Qualtrics.SurveyEngine.setEmbeddedData("BART1", value);
            Qualtrics.SurveyEngine.getEmbeddedData("BART1");
          } else if (round = 2){
            Qualtrics.SurveyEngine.setEmbeddedData("BART2", value);
            Qualtrics.SurveyEngine.getEmbeddedData("BART2");
          } 

我有2个要解决的问题。首先,我不确定应该在脚本的哪两个地方放入Qualtrics两行代码。我尝试将其放在脚本的不同位置(在Pump按钮功能,collect按钮功能和Continue按钮功能中),但它们似乎都不起作用。

与第一个相关的另一个问题是我应该记录什么变量(我在Qualtrics代码的value字段中输入什么变量)。我认为我应该根据变量在Qualtrics中设置和添加代码行的位置来放置“ pumps”或“ pumpmeup”变量,但是如果您还有其他建议,请告诉我。

如果你们中的任何一个可以帮助我,我将非常感谢!我是Java语言的初学者(很少有经验),因此可以提供任何帮助。

0 个答案:

没有答案