<commit_before><?php

namespace davidjeddy\yii2poll;

use yii;

class PollDb {

    public function isPollExist($pollName) {
        $db = Yii::$app->db;
        $command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')->
        bindParam(':pollName',$pollName);

        $pollData = $command->queryOne();

        if ($pollData==null) {

            return false;
        } else {

            return true;
        }
    }

    /**
     * [setVoicesData description]
     *
     * @deprecated 2.0.2 Use pollAnswerOptions()
     * @param [type] $pollName      [description]
     * @param [type] $answerOptions [description]
     */
    public function setVoicesData($pollName, $answerOptions) {
        $db = Yii::$app->db;
        $answersList = array();

        for($i=0; $i<count($answerOptions); $i++){
            $command = $db->createCommand()->insert('poll_response', [
                'answers'   => $answerOptions[$i],
                'poll_name' => $pollName,
                'value'     => 0,
            ])->execute(); 
        }
    }

    /**
     * poll_response TBO logic
     * ADDS new answers dynamically.
     * REMOVES answers that are not part of $pollObj->answerOptionsData
     * 
     * @param  [type] $pollObj [description]
     * @return [type]          [description]
     */
    public function pollAnswerOptions($pollObj)
    {
        foreach ($pollObj->answerOptions as $key => $value) {
            $db = Yii::$app->db;

            $answer = $db->createCommand('
                SELECT `answers`
                FROM `poll_response`
                WHERE `poll_name` = "'.$pollObj->pollName.'"
                    AND `answers` = "'.$value.'"
            ')->queryOne();

            if (!$answer) {
                $db->createCommand()->insert('poll_response', [
                    'answers'   => $value,
                    'poll_name' => $pollObj->pollName,
                    'value'     => 0,
                ])->execute(); 
            }
        }

        // remove answers that are no longer a part of the poll answer_options
        $db->createCommand('
            DELETE FROM `poll_response`
            WHERE `poll_name` = "'.$pollObj->pollName.'"<commit_msg>FIXED issue where new poll answer options would not save due to missing $db being defined inside a foreach() instead of at method scope<commit_after><?php

namespace davidjeddy\yii2poll;

use yii;

class PollDb {

    public function isPollExist($pollName) {
        $db = Yii::$app->db;
        $command = $db->createCommand('SELECT * FROM poll_question WHERE poll_name=:pollName')->
        bindParam(':pollName',$pollName);

        $pollData = $command->queryOne();

        if ($pollData==null) {

            return false;
        } else {

            return true;
        }
    }

    /**
     * [setVoicesData description]
     *
     * @deprecated 2.0.2 Use pollAnswerOptions()
     * @param [type] $pollName      [description]
     * @param [type] $answerOptions [description]
     */
    public function setVoicesData($pollName, $answerOptions) {
        $db = Yii::$app->db;
        $answersList = array();

        for($i=0; $i<count($answerOptions); $i++){
            $command = $db->createCommand()->insert('poll_response', [
                'answers'   => $answerOptions[$i],
                'poll_name' => $pollName,
                'value'     => 0,
            ])->execute(); 
        }
    }

    /**
     * poll_response TBO logic
     * ADDS new answers dynamically.
     * REMOVES answers that are not part of $pollObj->answerOptionsData
     * 
     * @param  [type] $pollObj [description]
     * @return [type]          [description]
     */
    public function pollAnswerOptions($pollObj)
    {
        $db = Yii::$app->db;

        foreach ($pollObj->answerOptions as $key => $value) {

            $answer = $db->createCommand('
                SELECT `answers`
                FROM `poll_response`
                WHERE `poll_name` = "'.$pollObj->pollName.'"
                    AND `answers` = "'.$value.'"
            ')->queryOne();

            if (!$answer) {
                $db->createCommand()->insert('poll_response', [
                    'answers'   => $value,
                    'poll_name' => $pollObj->pollName,
                    'value'     => 0,
                ])->execute(); 
            }
        }

        // remove answers that are no longer a part of the poll answer_options
        $db->createCommand('
            DELETE FROM `poll_response`
            WHERE `poll_name` = "'.$pollObj->pollName.'"
{'fraction_non_alphanumeric': 0.141240478781284, 'fraction_numerical': 0.003046789989118607, 'mean_word_length': 1.7537447573397245, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '161771', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1294, 'n_words': 294}