Skip to content

Commit

Permalink
dev: speed up browsing responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mennodekker committed Dec 13, 2012
1 parent c475c4d commit 49772b7
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions application/models/Answers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Answers extends CActiveRecord
* @static
* @access public
* @param string $class
* @return CActiveRecord
* @return Answers
*/
public static function model($class = __CLASS__)
{
Expand Down Expand Up @@ -78,17 +78,39 @@ function getAnswers($qid)
->query();
}

/**
* Return the key=>value answer for a given $qid
*
* @staticvar array $answerCache
* @param type $qid
* @param type $code
* @param type $lang
* @param type $iScaleID
* @return array
*/
function getAnswerFromCode($qid, $code, $lang, $iScaleID=0)
{
return Yii::app()->db->cache(6)->createCommand()
static $answerCache = array();

if (array_key_exists($qid, $answerCache)
&& array_key_exists($code, $answerCache[$qid])
&& array_key_exists($lang, $answerCache[$qid][$code])
&& array_key_exists($iScaleID, $answerCache[$qid][$code][$lang])) {
// We have a hit :)
return $answerCache[$qid][$code][$lang][$iScaleID];
} else {
$answerCache[$qid][$code][$lang][$iScaleID] = Yii::app()->db->cache(6)->createCommand()
->select('answer')
->from(self::tableName())
->where(array('and', 'qid=:qid', 'code=:code', 'scale_id=:scale_id', 'language=:lang'))
->bindParam(":qid", $qid, PDO::PARAM_INT)
->bindParam(":code", $code, PDO::PARAM_STR)
->bindParam(":lang", $lang, PDO::PARAM_STR)
->bindParam(":scale_id", $iScaleID, PDO::PARAM_INT)
->query();
->query()->readAll();

return $answerCache[$qid][$code][$lang][$iScaleID];
}
}

public function oldNewInsertansTags($newsid,$oldsid)
Expand Down

8 comments on commit 49772b7

@c-schmitz
Copy link
Contributor

Choose a reason for hiding this comment

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

Menno, getAnswerFromCode now returns a completely different data type (array instead of CActiveRecord) - but it looks like you did not convert the places from where this is called. Response browsing seems to be partially broken now - as example try the survey from http://bugs.limesurvey.org/view.php?id=7112
Can you please have a look at this asap?

@mennodekker
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I read something in forum abouth answer code L but don't understand... returned value is only ever used as array so that should not make a difference. Query returns CDbDataReader and that is interator like array . Are you sure about the linked bug? it is about a different subject. Will have a look later today. Probably the 'or die' should be removed in common helper but that could only occur when the answer we are looking for is not in db and that is an error.

@c-schmitz
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I mean only the survey from that linked bug. Just acitvate it,answer it once and try to view the responses. You will see an error above the response grid resulting from this. I rolled back until this patch und it works in the revision prior to this so something does not work as it should with this patch.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

@mennodekker : replacing ->query()->readAll(); by ->query(); lauch sameobject than before, no ?

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

Quick fix: 05cd63d

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, reverted: 5629add

Had to find beter solution :)

@mennodekker
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok fixed: 87cacbf

feel free to revert if you feel it still is not ok and you want to release

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

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

@mennodekker : CDbDataReader cannot rewind. It is a forward-only reader. Then already reverted :)

Think a new getAnswerFromCode have to use findbypk : but if i don't make error : here primaryKey of answers are array('qid', 'code','language','scale_id'); and not array('qid', 'code'); only

Denis

Please sign in to comment.