Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Dev Implemented AJAX search for surveys.
  • Loading branch information
SamMousa committed Jun 22, 2015
1 parent aba9ec7 commit d0bbbed
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 94 deletions.
5 changes: 4 additions & 1 deletion application/components/LocalizedFormatter.php
Expand Up @@ -16,7 +16,10 @@ public function formatSurveyStatus($value) {
}
return TbHtml::icon($icon);
}


public function formatBooleanIcon($value) {
return TbHtml::icon($value ? TbHtml::ICON_CHECK : TbHtml::ICON_UNCHECKED);
}
public function formatPercentage($factor) {
return number_format($factor * 100, 1) . '%';
}
Expand Down
7 changes: 6 additions & 1 deletion application/controllers/SurveysController.php
Expand Up @@ -30,7 +30,12 @@ public function actionOrganize($surveyId)

public function actionIndex() {
$this->layout = 'main';
$this->render('index', ['surveys' => new \CActiveDataProvider(Survey::model()->accessible())]);
$filter = new \ls\models\filter\Survey();
$filter->setAttributes(App()->request->getParam(\CHtml::modelName($filter)));
$surveys = Survey::model()->accessible();
$surveys->getDbCriteria()->mergeWith($filter->search());
$dataProvider = new \CActiveDataProvider($surveys);
$this->render('index', ['surveys' => $dataProvider, 'filter' => $filter]);
}

public function actionPublicList($sLanguage = null)
Expand Down
18 changes: 13 additions & 5 deletions application/controllers/TokensController.php
@@ -1,5 +1,6 @@
<?php
namespace ls\controllers;
use hafriedlander\Peg\Compiler\Token;
use Yii;
use ls\pluginmanager\PluginEvent;
class TokensController extends Controller
Expand Down Expand Up @@ -27,7 +28,7 @@ public function actionResponses($id, $surveyId) {
],
'sort' => false
]);
$this->survey = $survey;
$this->menus['survey'] = $survey;

if ($dataProvider->totalItemCount > 0) {
$this->render('/responses/index', [
Expand All @@ -47,7 +48,7 @@ public function actionResponses($id, $surveyId) {
public function actionCreate($surveyId)
{
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;
$this->menus['survey'] = $survey;
if (!$survey->bool_usetokens) {
throw new \CHttpException(412, "The survey you selected does not have tokens enabled.");
}
Expand All @@ -73,7 +74,7 @@ public function actionUpdate($surveyId, $id)
* @todo Add permission check.
*/
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;
$this->menus['survey'] = $survey;

if (!$survey->bool_usetokens) {
throw new \CHttpException(412, "The survey you selected does not have tokens enabled.");
Expand All @@ -99,14 +100,21 @@ public function actionIndex($surveyId)
* @todo Add permission check.
*/
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;
$this->menus['survey'] = $survey;
if (!$survey->bool_usetokens) {
throw new \CHttpException(412, "The survey you selected does not have tokens enabled.");
}

if (!\Token::valid($survey->sid)) {
\Token::createTable($survey->sid);
}

$dataProvider = new \CActiveDataProvider(\Token::model($survey->sid), [
'pagination' => [
'pageSize' => 50
]
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
return $this->render('index', ['dataProvider' => $dataProvider, 'survey' => $survey]);
}
public function actionRegister($surveyId)
{
Expand Down
6 changes: 4 additions & 2 deletions application/models/Dynamic.php
Expand Up @@ -62,17 +62,19 @@ public static function create($id, $scenario = 'insert')
* @param int $id
* @return boolean Returns true if the table is found.
*/
public static function valid($id)
public static function valid($id, $refresh = false)
{
$result = false;
if (is_numeric($id)) {
if (is_numeric($id) && (!isset(self::$valid) || $refresh)) {
try {
App()->db->createCommand("SELECT 1 FROM " . static::constructTableName($id))->execute();
$result = true;
} catch (\CDbException $e) {
$result = false;
}
self::$valid[$id] = $result;
}

return $result;
}

Expand Down
138 changes: 76 additions & 62 deletions application/models/Survey.php
Expand Up @@ -23,6 +23,10 @@
*/
class Survey extends LSActiveRecord
{
const STATUS_INACTIVE = 'inactive';
const STATUS_EXPIRED = 'expired';
const STATUS_ACTIVE = 'active';

const FORMAT_GROUP = 'G';
const FORMAT_ALL_IN_ONE = 'A';
const FORMAT_QUESTION = 'S';
Expand All @@ -37,6 +41,7 @@ public function attributeLabels() {
'bool_usecookie' => gT('Set cookie to prevent repeated participation?'),
'bool_listpublic' => gT('List survey publicly:'),
'bool_alloweditaftercompletion' => gT("Allow responses to be edited after completion"),
'bool_usetokens' => gT('Use tokens'),
'startdate' => gT("Start date/time:"),
'expires' => gT("Expiry date/time:"),
'usecaptcha' => gT("Use CAPTCHA for"),
Expand Down Expand Up @@ -180,70 +185,66 @@ public function scopes()
*/
public function rules()
{
return array(
array('datecreated', 'default','value'=>date("Y-m-d")),
array('startdate', 'default','value'=>NULL),
array('expires', 'default','value'=>NULL),
array('admin,faxto','LSYii_Validators'),
array('adminemail','filter', 'filter'=>'trim'),
array('bounce_email','LSYii_EmailIDNAValidator', 'allowEmpty'=>true),
array('adminemail','filter', 'filter'=>'trim'),
array('bounce_email','LSYii_EmailIDNAValidator', 'allowEmpty'=>true),
array('active', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('anonymized', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('savetimings', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('datestamp', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('usecookie', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('allowregister', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('allowsave', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('autoredirect', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('allowprev', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('printanswers', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('ipaddr', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('refurl', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('publicstatistics', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('publicgraphs', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('listpublic', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('htmlemail', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('sendconfirmation', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('tokenanswerspersistence', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('assessments', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('usetokens', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('showxquestions', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('shownoanswer', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('showwelcome', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('showprogress', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('questionindex', 'numerical','min' => 0, 'max' => 2, 'allowEmpty'=>false),
array('nokeyboard', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('alloweditaftercompletion', 'in','range'=>array('Y','N'), 'allowEmpty'=>true),
array('bounceprocessing', 'in','range'=>array('L','N','G'), 'allowEmpty'=>true),
array('usecaptcha', 'in','range'=>array('A','B','C','D','X','R','S','N'), 'allowEmpty'=>true),
array('showgroupinfo', 'in','range'=>array('B','N','D','X'), 'allowEmpty'=>true),
array('showqnumcode', 'in','range'=>array('B','N','C','X'), 'allowEmpty'=>true),
return [
['datecreated', 'default','value'=>date("Y-m-d")],
['startdate', 'default','value'=>NULL],
['expires', 'default','value'=>NULL],
['admin,faxto','LSYii_Validators'],
['adminemail','filter', 'filter'=>'trim'],
['bounce_email','LSYii_EmailIDNAValidator', 'allowEmpty'=>true],
['adminemail','filter', 'filter'=>'trim'],
['bounce_email','LSYii_EmailIDNAValidator', 'allowEmpty'=>true],
['active', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['anonymized', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['savetimings', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['datestamp', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['usecookie', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['allowregister', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['allowsave', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['autoredirect', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['allowprev', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['printanswers', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['ipaddr', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['refurl', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['publicstatistics', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['publicgraphs', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['listpublic', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['htmlemail', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['sendconfirmation', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['tokenanswerspersistence', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['assessments', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['showxquestions', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['shownoanswer', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['showwelcome', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['showprogress', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['questionindex', 'numerical','min' => 0, 'max' => 2, 'allowEmpty'=>false],
['nokeyboard', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['alloweditaftercompletion', 'in','range'=>['Y','N'], 'allowEmpty'=>true],
['bounceprocessing', 'in','range'=>['L','N','G'], 'allowEmpty'=>true],
['usecaptcha', 'in','range'=>['A','B','C','D','X','R','S','N'], 'allowEmpty'=>true],
['showgroupinfo', 'in','range'=>['B','N','D','X'], 'allowEmpty'=>true],
['showqnumcode', 'in','range'=>['B','N','C','X'], 'allowEmpty'=>true],
['format', 'in','range' => array_keys($this->formatOptions), 'allowEmpty'=>true],
array('googleanalyticsstyle', 'numerical', 'integerOnly'=>true, 'min'=>'0', 'max'=>'2', 'allowEmpty'=>true),
array('autonumber_start','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('tokenlength','numerical', 'integerOnly'=>true,'allowEmpty'=>true, 'min'=>'5', 'max'=>'36'),
array('bouncetime','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('navigationdelay','numerical', 'integerOnly'=>true,'allowEmpty'=>true),
array('template', 'filter', 'filter'=>array($this,'filterTemplateSave')),
array('language','LSYii_Validators','isLanguage'=>true),
array('language', 'required', 'on' => 'insert'),
// array('language', 'filter', 'filter'=>'trim'),
['googleanalyticsstyle', 'numerical', 'integerOnly'=>true, 'min'=>'0', 'max'=>'2', 'allowEmpty'=>true],
['autonumber_start','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
['tokenlength','numerical', 'integerOnly'=>true,'allowEmpty'=>true, 'min'=>'5', 'max'=>'36'],
['bouncetime','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
['navigationdelay','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
['template', 'filter', 'filter'=>[$this,'filterTemplateSave']],
['language','LSYii_Validators','isLanguage'=>true],
['language', 'required', 'on' => 'insert'],

['additionalLanguages', 'safe'],
// array('additional_languages', 'filter', 'filter'=>'array_filter'),
// array('additional_languages','LSYii_Validators','isLanguageMulti'=>true),
// Date rules currently don't work properly with MSSQL, deactivating for now
// array('expires','date', 'format'=>array('yyyy-MM-dd', 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss',), 'allowEmpty'=>true),
// array('startdate','date', 'format'=>array('yyyy-MM-dd', 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss',), 'allowEmpty'=>true),
// array('datecreated','date', 'format'=>array('yyyy-MM-dd', 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss',), 'allowEmpty'=>true),
['translatedFields', 'safe'],
['use_series', 'boolean'],
['features', 'safe'],
['sid', 'default' , 'value' => randomChars(6, '123456789')],

['bool_listpublic', 'boolean']
);



];
}


Expand Down Expand Up @@ -347,17 +348,19 @@ public function getAllLanguages()
* - expired
*/
public function getStatus() {

if (!$this->isActive) {
$result = 'inactive';
$result = self::STATUS_INACTIVE;
} elseif ($this->isExpired) {
$result = 'expired';
$result = self::STATUS_EXPIRED;
} else {
$result = 'active';
$result = self::STATUS_ACTIVE;
}
return $result;
}

public function getIsActive() {
return $this->active != 'N';
return $this->bool_active;
}
/**
* @return array
Expand Down Expand Up @@ -534,7 +537,7 @@ public function getIsExpired()
{
return !empty($this->expires)
&& (new DateTime($this->expires)) < new DateTime()
&& (new DateTime($this->validfrom)) > new DateTime();
&& (new DateTime($this->startdate)) > new DateTime();
}
/**
* Creates a new survey - does some basic checks of the suppplied data
Expand Down Expand Up @@ -724,7 +727,7 @@ public function activate()
if (Response::createTable($this, $messages)) {

}
if ($this->bool_usetokens) {
if ($this->bool_usetokens && !Token::valid($this->sid)) {
Token::createTable($this->sid);
}
if (Timing::createTable($this, $messages)) {
Expand Down Expand Up @@ -874,6 +877,15 @@ public function __set($name, $value)
}
}

public function __isset($name) {
if (substr($name, 0, 5) == 'bool_') {
$result = parent::__isset(substr($name, 5));
} else {
$result = parent::__isset($name);
}
return $result;
}

public function getTotalSteps() {
switch ($this->format) {
case "A":
Expand Down Expand Up @@ -985,4 +997,6 @@ public function deleteDependent() {
}




}
29 changes: 24 additions & 5 deletions application/models/Token.php
Expand Up @@ -245,14 +245,33 @@ public static function create($surveyId, $scenario = 'insert') {

public function relations()
{
$result = array(
'responses' => [self::HAS_MANY, 'Response_' . $this->dynamicId, ['token' => 'token']],
'survey' => array(self::BELONGS_TO, 'Survey', '', 'on' => "sid = {$this->dynamicId}"),
'surveylink' => array(self::BELONGS_TO, 'SurveyLink', array('participant_id' => 'participant_id'), 'on' => "survey_id = {$this->dynamicId}")
);
$result = [
'survey' => [self::BELONGS_TO, 'Survey', '', 'on' => "sid = {$this->dynamicId}"],
'surveylink' => [self::BELONGS_TO, 'SurveyLink', ['participant_id' => 'participant_id'], 'on' => "survey_id = {$this->dynamicId}"]
];

if (\Response::valid($this->dynamicId)) {
$result['responses'] = [self::HAS_MANY, 'Response_' . $this->dynamicId, ['token' => 'token']];
$result['responseCount'] = [self::STAT, 'Response_' . $this->dynamicId, 'condition' => ["token = '{$this->token}'"]];
}
return $result;
}

/**
* This function is used when the survey is not active and thus the relation above is not added.
* It is protected since you should use ->responses to make sure you get the relation if it is available.
* @return array
*/
protected function getResponses() {
return [];
}

/**
* @return array
*/
protected function getResponseCount() {
return 0;
}
public function rules()
{
return [
Expand Down

0 comments on commit d0bbbed

Please sign in to comment.