Skip to content

Commit

Permalink
Dev: sliders, manage thousand separator and EM display
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Mar 23, 2016
1 parent 5545138 commit 8218d42
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 128 deletions.
62 changes: 32 additions & 30 deletions application/helpers/qanda_helper.php
Expand Up @@ -2985,22 +2985,6 @@ function do_multiplenumeric($ia)
$slider_separator= (trim($aQuestionAttributes['slider_separator'])!='')?$aQuestionAttributes['slider_separator']:"";
$slider_reset=($aQuestionAttributes['slider_reset'])?1:0;

if ($slider_default != "")
{
$slider_startvalue = $slider_default;
$slider_displaycallout=1;
}
elseif ($slider_middlestart != '')
{
$slider_startvalue = $slider_middlestart;
$slider_displaycallout=0;
}
else
{
$slider_startvalue = 'NULL';
$slider_displaycallout=0;
}

}
else
{
Expand All @@ -3012,7 +2996,6 @@ function do_multiplenumeric($ia)
$slider_max = '';
$slider_maxtext = '';
$slider_default = '';
$slider_default = '';
$slider_orientation= '';
$slider_handle = '';
$slider_custom_handle = '';
Expand Down Expand Up @@ -3082,26 +3065,45 @@ function do_multiplenumeric($ia)
//list($htmltbody2, $hiddenfield)=return_array_filter_strings($ia, $aQuestionAttributes, $thissurvey, $ansrow, $myfname, '', $myfname, "div","form-group question-item answer-item text-item numeric-item".$extraclass);
$sDisplayStyle = return_display_style($ia, $aQuestionAttributes, $thissurvey, $myfname);

// TODO : check why it's done here a second time
$sSeparator = getRadixPointData($thissurvey['surveyls_numberformat']);
$sSeparator = $sSeparator['separator'];

// Default answer is stored in _SESSION
$dispVal='';

// The value of the slider depends on many possible different parameters, by order of priority :
// 1. The value stored in the session
// 2. Else the default Answer --> Supposed to be set by EM and stored in session ?
// 3. Else the middle start
// 4. Else the init value
// 5. If no value at all, the "user no action" is recorded as null in the database
//
// Dev team is invited to tell if they agree/disagree with this order.

$sValue = 'NULL';
$slider_displaycallout=0;

// value stored in _SESSION
if (isset($_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname]))
{
$dispVal = $_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname];
if(strpos($dispVal,"."))
{
$dispVal=rtrim(rtrim($dispVal,"0"),".");
}
$dispVal = str_replace('.',$sSeparator,$dispVal);
$sValue = $_SESSION['survey_'.Yii::app()->getConfig('surveyID')][$myfname];
}
elseif( $slider_default != "" )
{
$sValue = $slider_default;
}
elseif( isset($slider_middlestart) && $slider_middlestart!='')
{
$sValue = $slider_middlestart;
}


if( isset($slider_middlestart) && $slider_middlestart!='')
if(strpos($sValue,"."))
{
$slider_default = $slider_middlestart;
$sValue = rtrim(rtrim($sValue,"0"),".");
$sValue = str_replace('.',$sSeparator,$sValue);
}


$itemDatas = array(
'qid'=>$ia[0],
'extraclass'=>$extraclass,
Expand All @@ -3118,7 +3120,7 @@ function do_multiplenumeric($ia)
'suffix'=>$suffix,
'tiwidth'=>$tiwidth,
'myfname'=>$myfname,
'dispVal'=>$dispVal,
'dispVal'=>$sValue,
'maxlength'=>$maxlength,
'labelText'=>$labelText,
'checkconditionFunction'=>$checkconditionFunction.'(this.getAttribute(\'stringvalue\'), this.name, this.type)',
Expand All @@ -3132,8 +3134,8 @@ function do_multiplenumeric($ia)
'slider_handle' => $slider_handle,
'slider_reset' => $slider_reset,
'slider_custom_handle' => $slider_custom_handle,
'slider_startvalue' => $slider_startvalue,
'slider_displaycallout' => $slider_displaycallout
'slider_displaycallout' => $slider_displaycallout,
'sSeparator'=> $sSeparator,
);
$answer .= Yii::app()->getController()->renderPartial('/survey/questions/multiplenumeric/item', $itemDatas, true);

Expand Down
118 changes: 20 additions & 98 deletions application/views/survey/questions/multiplenumeric/item.php
Expand Up @@ -44,6 +44,7 @@ class="text form-control numeric <?php echo $kpclass;?>"
<?php echo $maxlength; ?>
/>
<?php else:?>
<?php echo "IKI" . $dispVal;?>
<input
class="text form-control <?php echo $kpclass;?>"
type="text"
Expand All @@ -53,6 +54,7 @@ class="text form-control <?php echo $kpclass;?>"
value="<?php echo $dispVal;?>"
onkeyup="<?php echo $checkconditionFunction; ?>"
<?php echo $maxlength; ?>
data-slider-value="<?php // echo $dispVal;?>5.2"
data-slider-min='<?php echo $slider_min;?>'
data-slider-max='<?php echo $slider_max;?>'
data-slider-step='<?php echo $slider_step;?>'
Expand All @@ -63,8 +65,7 @@ class="text form-control <?php echo $kpclass;?>"
data-slider-reset='<?php echo $slider_reset; ?>'
data-slider-prefix='<?php echo $prefix; ?>'
data-slider-suffix='<?php echo $suffix; ?>'
data-slider-startvalue='<?php echo $slider_startvalue; ?>'
data-slider-displaycallout='<?php echo $slider_displaycallout; ?>'
data-separator='<?php echo $sSeparator;?>'
/>
<?php endif;?>
<?php echo $sliderright;?>
Expand Down Expand Up @@ -98,111 +99,32 @@ class="text form-control <?php echo $kpclass;?>"
</div>
<script type='text/javascript'>
<!--
// TODO: This code should be moved to e.g. numerical-slider.js
$(document).ready(function(){
var myfname = '<?php echo $myfname; ?>';
var dispVal = parseFloat('<?php echo $dispVal; ?>');
var id = '#answer' + myfname;
var resetSliderId = id + '_resetslider';
var slider_prefix = $(id).attr('data-slider-prefix')
var slider_suffix = $(id).attr('data-slider-suffix')
var $inputEl = $('#answer' + myfname);
console.log( $inputEl.attr('id'));
var $prefix = $inputEl.data('slider-prefix');
var $suffix = $inputEl.data('slider-suffix');
var $separator = $inputEl.data('separator');

var mySlider_<?php echo $myfname; ?> = $(id).bootstrapSlider({
var mySlider_<?php echo $myfname; ?> = $inputEl.bootstrapSlider({
formatter: function (value) {
var displayValue = '' + value;
var displayValue = displayValue.replace(/\./,LSvar.sLEMradix);
return slider_prefix + displayValue + slider_suffix;
displayValue = value.toString().replace('.',$separator);
return $prefix + displayValue + $suffix;
},
value: parseFloat('<?php echo $dispVal; ?>')
});

// Hide tooltip
$('#javatbd' + myfname).find('.tooltip').hide();

// Set value at init
var slider_startvalue = $(id).attr('data-slider-startvalue');
if (slider_startvalue === 'NULL' && !isNaN(dispVal)) {
$(id).attr('stringvalue', dispVal); // stringvalue is the value handed to EM, since Bootstrap slider can't handle ","
LEMrel<?php echo $qid; ?>();
$(id).triggerHandler("keyup");

// Show tooltip
$('#javatbd' + myfname).find('.tooltip').show();
}
else if (slider_startvalue !== 'NULL') {
var displayValue = '' + slider_startvalue;
var displayValue = displayValue.replace(/\./,LSvar.sLEMradix);
$(id).bootstrapSlider('setValue', parseFloat(slider_startvalue));
$(id).attr('stringvalue', displayValue); // stringvalue is the value handed to EM, since Bootstrap slider can't handle ","
LEMrel<?php echo $qid; ?>();
$(id).triggerHandler("keyup");

// Show tooltip
$('#javatbd' + myfname).find('.tooltip').show();
$(id).bootstrapSlider('relayout'); // Refresh the tooltip
}

// Reset on click on .slider-reset
$(resetSliderId).on("click", function() {
var slider_startvalue = $(id).attr('data-slider-startvalue');

// Callout is the thing above the slider that displays the
// current number. Boolean variable.
var slider_displaycallout = $(id).attr('data-slider-displaycallout');

if(slider_startvalue == "NULL") {
$(id).attr('value', ''); // Never use bootstrapSlider('setValue', ...), because it won't accept empty string (will convert to 0)
$(id).attr('stringvalue', '');

// Hide tooltip
$('#javatbd' + myfname).find('.tooltip').hide();
}
else {
$(id).bootstrapSlider('setValue', parseFloat(slider_startvalue));
$(id).attr('stringvalue', slider_startvalue);

// Show tooltip
$('#javatbd' + myfname).find('.tooltip').show();
}

if(slider_displaycallout && slider_startvalue != "NULL") {
$(id).attr('stringvalue', slider_prefix + slider_startvalue.replace(/\./,LSvar.sLEMradix) + slider_suffix);
$(id).bootstrapSlider('setValue', parseFloat(slider_startvalue));

// Show tooltip
$('#javatbd' + myfname).find('.tooltip').show();
}
else {
$(id).attr('value', '');
$(id).attr('stringvalue', '');

// Hide tooltip
$('#javatbd' + myfname).find('.tooltip').hide();
}

LEMrel<?php echo $qid; ?>();
$(id).triggerHandler("keyup"); // Needed for EM

mySlider_<?php echo $myfname; ?>.on('slideStop', function(){
value = $inputEl.val();
displayValue = value.toString().replace('.',$separator);
$inputEl.val(displayValue);
//console.log('LA '+$value);
LEMrel<?php echo $qid; ?>()
});
$("#vmsg_<?php echo $qid;?>_default").text('<?php eT('Please click and drag the slider handles to enter your answer.');?>');

mySlider_<?php echo $myfname; ?>.on('slideStop', function(event) {
var displayValue = '' + event.value; // Type-cast to string
var displayValue = displayValue.replace(/\./,LSvar.sLEMradix);

// fixnum_checkconditions can't handle dot if it expects comma, and
// Bootstrap won't save comma in value. So we need another attribute.
$(id).attr('stringvalue', displayValue);

LEMrel<?php echo $qid; ?>();

// EM needs this
$(id).triggerHandler("keyup");

});
mySlider_<?php echo $myfname; ?>.on('slideStart', function(event) {
// Show tooltip
$('#javatbd' + myfname).find('.tooltip').show();
});
// Hide tooltip
// $('#javatbd' + myfname).find('.tooltip').hide();

$("#vmsg_<?php echo $qid;?>_default").text('<?php eT('Please click and drag the slider handles to enter your answer.');?>');
});
Expand Down

7 comments on commit 8218d42

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still have to implement the "user no action".
I'll do it in PHP, with a new input (hidden, or temporary checkbox "No Answer" as asked by Jelo) , and modifying database.php. So if will not imply JavaScript.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

????
In 2.06 : the no action is managed in JS , we don't need PHP for this . It's a regression for me .....

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,to respect your point of view, no change to the php validation involved: bcdcdf9

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Sorry ... but think you have maybe right .... an idea can be :

  • Use $_SESSION to fille the input
  • if $_SESSION is null or empty : fill the input with slider_default

Then no difference between "JS vs no-JS" without action :).

Actually : it use the same behaviour than 2.06 : https://bugs.limesurvey.org/view.php?id=10744#c36756 ?

@SamMousa
Copy link
Contributor

@SamMousa SamMousa commented on 8218d42 Mar 24, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamMousa $_SESSION is the actual value filled by user.

If there are an, actual value : don't update it ....
If not maybe set a value in input :)

@SamMousa
Copy link
Contributor

@SamMousa SamMousa commented on 8218d42 Mar 24, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.