Skip to content

Commit

Permalink
Merge pull request #617 from papa-mb/master
Browse files Browse the repository at this point in the history
#12034 : add minimum id of answer for the export page
  • Loading branch information
Shnoulle committed Jan 9, 2017
2 parents 1824438 + 229ec58 commit f43bb27
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions application/controllers/admin/export.php
Expand Up @@ -209,8 +209,11 @@ public function exportresults()
$data['aFieldsOptions'] = $aFieldsOptions;
//get max number of datasets
$iMaximum = SurveyDynamic::model($iSurveyID)->getMaxId();
//get min number of datasets
$iMinimum = SurveyDynamic::model($iSurveyID)->getMinId();

$data['max_datasets'] = $iMaximum;
$data['min_datasets'] = $iMinimum;
$data['surveyid'] = $iSurveyID;
$data['imageurl'] = Yii::app()->getConfig('imageurl');
$data['thissurvey'] = $thissurvey;
Expand Down
36 changes: 36 additions & 0 deletions application/models/LSActiveRecord.php
Expand Up @@ -139,6 +139,42 @@ public function getMaxId($field = null, $forceRefresh = false)

return $maxIds[$field];
}

/**
* Return the min value for a field
*
* This is a convenience method, that uses the primary key of the model to
* retrieve the highest value.
*
* @param string $field The field that contains the Id, when null primary key is used if it is a single field
* @param boolean $forceRefresh Don't use value from static cache but always requery the database
* @return false|int
*/
public function getMinId($field = null, $forceRefresh = false)
{
static $minIds = array();

if (is_null($field)) {
$primaryKey = $this->getMetaData()->tableSchema->primaryKey;
if (is_string($primaryKey)) {
$field = $primaryKey;
} else {
// Composite key, throw a warning to the programmer
throw new Exception(sprintf('Table %s has a composite primary key, please explicitly state what field you need the min value for.', $this->tableName())); }
}

if ($forceRefresh || !array_key_exists($field, $minIds)) {
$minId = $this->dbConnection->createCommand()
->select('MIN(' . $this->dbConnection->quoteColumnName($field) . ')')
->from($this->tableName())
->queryScalar();

// Save so we can reuse in the same request
$minIds[$field] = $minId;
}

return $minIds[$field];
}

/**
* @todo This should also be moved to the behavior at some point.
Expand Down
6 changes: 3 additions & 3 deletions application/views/admin/export/exportresults_view.php
Expand Up @@ -67,11 +67,11 @@
</label>
<div class="col-sm-2">
<input
min="1"
min="<?php echo $min_datasets; ?>"
max="<?php echo $max_datasets; ?>"
step="1"
type="number"
value="1"
value="<?php echo $min_datasets; ?>"
name="export_from"
id="export_from"
class="form-control"
Expand All @@ -84,7 +84,7 @@ class="form-control"
</label>
<div class="col-sm-2">
<input
min="1"
min="<?php echo $min_datasets; ?>"
max="<?php echo $max_datasets; ?>"
step="1"
type="number"
Expand Down

0 comments on commit f43bb27

Please sign in to comment.