Skip to content

Commit

Permalink
Fixed issue : Median calculation of timing response
Browse files Browse the repository at this point in the history
Dev : add a statistics function on Survey_timings
Dev : min, sec can be translated
  • Loading branch information
Shnoulle committed Sep 22, 2012
1 parent 1f90ead commit a022482
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 66 deletions.
57 changes: 2 additions & 55 deletions application/controllers/admin/responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public function time($iSurveyID)
$oCriteria->offset = $start;
$oCriteria->limit = $limit;

$dtresult = Survey_timings::model($iSurveyID)->findAllAsArray($oCriteria) or die("Couldn't get surveys");
$dtresult = Survey_timings::model($iSurveyID)->findAllAsArray($oCriteria);
$dtcount2 = count($dtresult);
$cells = $fncount + 1;

Expand Down Expand Up @@ -769,60 +769,7 @@ public function time($iSurveyID)
}

//interview Time statistics
$count = false;
//$survstats=substr($surveytableNq);
$oCriteria = new CDbCriteria;
$oCriteria->select = 'AVG(interviewtime) AS avg, COUNT(*) as count';
$oCriteria->join = " INNER JOIN {{survey_{$iSurveyID}}} s ON t.id = s.id";
$oCriteria->condition = 'submitdate IS NOT NULL';
$oCriteria->order = 'interviewtime';
$queryAvg = Survey_timings::model($iSurveyID)->find($oCriteria);

$oCriteria->select = 'interviewtime';
$queryAll = Survey_timings::model($iSurveyID)->findAll($oCriteria);

$count = count($queryAll);
if ($aData['result'] = $row = $queryAvg)
{
$aData['avgmin'] = (int) ($row['avg'] / 60);
$aData['avgsec'] = $row['avg'] % 60;
$aData['count'] = $row['count'];
}

if ($count && $result = $queryAll)
{

$middleval = floor(($count - 1) / 2);
$i = 0;
if ($count % 2)
{
foreach ($result as $row)
{
if ($i == $middleval)
{
$median = $row['interviewtime'];
break;
}
$i++;
}
}
else
{
foreach ($result as $row)
{
if ($i == $middleval)
{
$nextrow = next($result);
$median = ($row['interviewtime'] + $nextrow['interviewtime']) / 2;
break;
}
$i++;
}
}
$aData['allmin'] = (int) ($median / 60);
$aData['allsec'] = $median % 60;
}

$aData['statistics'] = Survey_timings::model($iSurveyId)->statistics();
$aData['num_total_answers'] = Survey_dynamic::model($iSurveyID)->count();
$aData['num_completed_answers'] = Survey_dynamic::model($iSurveyID)->count('submitdate IS NOT NULL');
$aViewUrls[] = 'browsetimefooter_view';
Expand Down
55 changes: 53 additions & 2 deletions application/models/Survey_timings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Survey_timings extends LSActiveRecord
{

protected static $sid = 0;
public $avg;
public $count;
/**
* Returns the static model
*
Expand Down Expand Up @@ -60,6 +58,59 @@ public function tableName()
return '{{survey_' . intval(self::$sid) . '_timings}}';
}

/**
* Returns Time statistics for this answer table
*
* @access public
* @return array
*/
public function statistics()
{
$sid = self::$sid;
if(Yii::app()->db->schema->getTable($this->tableName())){
$queryAvg=Yii::app()->db->createCommand()
->select("AVG(interviewtime) AS avg, COUNT(*) as count")
->from($this->tableName()." t")
->join("{{survey_{$sid}}} s","t.id = s.id")
->where("s.submitdate IS NOT NULL")
->queryRow();
if($queryAvg['count']){
$statistics['avgmin'] = (int) ($queryAvg['avg'] / 60);
$statistics['avgsec'] = $queryAvg['avg'] % 60;
$statistics['count'] = $queryAvg['count'];
$queryAll=Yii::app()->db->createCommand()
->select("interviewtime")
->from($this->tableName()." t")
->join("{{survey_{$sid}}} s","t.id = s.id")
->where("s.submitdate IS NOT NULL")
->order("t.interviewtime")
->queryAll();
$middleval = intval($statistics['count'] / 2);
$statistics['middleval'] = $middleval;
if ($statistics['count'] % 2)
{
$median=($queryAll[$middleval]['interviewtime'] + $queryAll[$middleval-1]['interviewtime']) / 2;
}
else
{
$median=$queryAll[$middleval]['interviewtime'];
}
$statistics['median'] = $median;
$statistics['allmin'] = (int) ($median / 60);
$statistics['allsec'] = $median % 60;
}
else
{
$statistics['count'] = 0;
}
}
else
{
$statistics['count'] = 0;
}
return $statistics;
}

}

?>
15 changes: 6 additions & 9 deletions application/views/admin/responses/browsetimefooter_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
<input id='deleteanswer' name='deleteanswer' value='' type='hidden' />
</form>
<br />

<?php if ($statistics['count']) { ?>
<div class="header ui-widget-header"><?php $clang->eT('Interview time'); ?></div>
<table class="statisticssummary">
<?php if ($result) { ?>
<tr><th><?php $clang->eT('Average interview time:'); ?></th><td><?php echo $avgmin; ?> min. <?php echo $avgsec; ?> sec.</td></tr>
<?php } ?>
<?php if ($count) { ?>
<tr><th><?php $clang->eT('Median:'); ?></th><td><?php echo $allmin; ?> min. <?php echo $allsec; ?> sec.</td></tr>
<?php } ?>
</table>
<table class="statisticssummary">
<tr><th><?php $clang->eT('Average interview time:'); ?></th><td title=""><?php printf($clang->gT("%s min. %s sec."),$statistics['avgmin'],$statistics['avgsec']) ?></td></tr>
<tr><th><?php $clang->eT('Median:'); ?></th><td><?php printf($clang->gT("%s min. %s sec."),$statistics['allmin'],$statistics['allsec']) ?> </td></tr>
</table>
<?php } ?>

0 comments on commit a022482

Please sign in to comment.