Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

CBC attribute level to dispay product of 2 Attribute levels

I would like to have a CBC attribute’s level to include a value that is the product of 2 CBC attribute levels (duration, annual cost).

Here some details
Att1 [duration] has levels: 1, 2, 5, 10 years.
And displays as: The treatment lasts [duration] years.

Att2 [annualcost] has levels: £2500, £5000, £7500, £10000.
And displays as:
The treatment costs £[annualcost] each year.
The total cost of the treatment would be £[annualcost*dur]

As a first step I looked at the answer to this Question:
https://legacy.sawtoothsoftware.com/forum/25664/calculation-using-perl-in-cbc?show=25664#q25664

And attempted to use the code there in a CBC (CBCNew) with 3 concepts per task, att1 with 5 levels and att2 with 3 levels and a design generated.

I am not sure where to put this script – the attribute names and the attribute levels have to be unique, hence my confusion.

thanks
Dan

[% Begin Unverified Perl
my $concept = 1;
my $currency = "USD ";
my $price = 0;
my $subs = 0;
my $tot = 0;
 
# price
my $att1 = CBCDESIGNLEVELVALUE("CBCNew_Random1", $concept, 2);
if ($att1 == 1) { $price = 2300; }
elsif ($att1 == 2) { $price = 2600; }
elsif ($att1 == 3) { $price = 2900; }
elsif ($att1 == 4) { $price = 3200; }
elsif ($att1 == 5) { $price = 3500; }
 
# price level contingency of price
my $att2 = CBCDESIGNLEVELVALUE("CBCNew_Random1", $concept, 3);
if ($att2 == 1) { $subs = 1; }
elsif ($att2 == 2) { $subs = 3; }
elsif ($att2 == 3) { $subs = 6; }

$tot = $price * $subs;
 
# Return
return $tot;
End Unverified %]
asked Mar 24 by dan r Bronze (2,830 points)
What version of SSI Web / Lighthouse Studio are you running?
Thanks Zachary - I missed the email notifying me that you had replied.
This is still a  live query.
I am using latest version of LS (9.11.0)

Dan

1 Answer

0 votes
Start by adding these bits of HTML to your duration and price attribute labels:

<input type="hidden" class="durationAttribute"/>


<input type="hidden" class="priceAttribute"/>


(You may want to add internal labels at this point.)

Then add this script to your CBC:

<script>
$(document).ready(function(){
    // Params
    var attributeLabel = 'Total price';

    // Run
    var originalLabel = $('#[% QuestionName() %]_div .attribute_label_column .priceAttribute').closest('.cbc_cell');
    var cloneLabel = $(originalLabel).clone();
    $(cloneLabel).find('.att_label_text').text(attributeLabel);
    $(originalLabel).after(cloneLabel);
    
    $('#[% QuestionName() %]_div .cbc_concept:not(.none_concept)').each(function(){
        var duration = Number($(this).find('.durationAttribute').closest('.cbc_cell').text().replace(/\D/g, ''));
        var price = Number($(this).find('.priceAttribute').closest('.cbc_cell').text().replace(/\D/g, ''));
        var total = duration * price;
        
        var priceCell = $(this).find('.priceAttribute').closest('.cbc_cell');
        var cloneCell = $(priceCell).clone();
        $(cloneCell).find('.att_label_text').text(attributeLabel);
        $(cloneCell).find('.level_text').text('£' + total);
        $(priceCell).after(cloneCell);
    });
})
</script>
answered Apr 29 by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
Thanks Zachary, will work on implementing this and see how we go.
...