public function getCompetitionResults()
 {
     if (!self::canShowCompetitionResults($this->competition_id)) {
         return Yii::t('app', 'Competition results are not available yet.');
     }
     $points = 0;
     $wrong = array();
     $not = array();
     $startpoints = 0;
     $maxpoints = 0;
     foreach ($this->competitionUserQuestions as $competitionUserQuestion) {
         $cache_key = "CQAnswer-" . $competitionUserQuestion->competition_question_id . '-' . $this->competition_category_id;
         $cache = Yii::app()->cache->get($cache_key);
         if ($cache == null) {
             $right_answer = unserialize($competitionUserQuestion->competitionQuestion->question->verification_function);
             $correct_points = 1;
             $wrong_points = 0;
             $competitonQuestionDifficulty = CompetitionQuestionDifficulty::model()->with("competitionQuestionCategories")->find("competitionQuestionCategories.competition_question_id=:competition_question_id and competitionQuestionCategories.competition_category_id=:competition_category_id", array(":competition_question_id" => $competitionUserQuestion->competition_question_id, ":competition_category_id" => $this->competition_category_id));
             if ($competitonQuestionDifficulty != null) {
                 $correct_points = $competitonQuestionDifficulty->correct_answer_points;
                 $wrong_points = $competitonQuestionDifficulty->wrong_answer_points;
             }
             $cache = array('right_answer' => $right_answer, 'correct_points' => $correct_points, 'wrong_points' => $wrong_points);
             Yii::app()->cache->set($cache_key, serialize($cache), 60);
         } else {
             $cache = unserialize($cache);
         }
         $startpoints += abs($cache['wrong_points']);
         $maxpoints += $cache['correct_points'];
         if ($competitionUserQuestion->custom_answer != '' && $competitionUserQuestion->custom_answer != null) {
             if (in_array($competitionUserQuestion->custom_answer, $cache['right_answer'])) {
                 $points += $cache['correct_points'];
             } else {
                 $points += $cache['wrong_points'];
                 $wrong[] = CompetitionQuestion::getQuestionTitle($competitionUserQuestion->competition_question_id);
             }
         } else {
             $not[] = CompetitionQuestion::getQuestionTitle($competitionUserQuestion->competition_question_id);
         }
     }
     sort($wrong);
     sort($not);
     return $startpoints + $points . '/' . ($startpoints + $maxpoints) . (count($wrong) > 0 ? ' | napačni: ' . implode(', ', $wrong) : '') . (count($not) > 0 ? ' | neodgov.: ' . implode(',', $not) : '');
 }
Exemple #2
0
?>
 <span class="required">*</span> <?php 
echo Yii::t('app', 'are_required');
?>
.</p>
    
	<?php 
echo $form->errorSummary($model);
?>

        <div class="row">
		<?php 
echo $form->labelEx($model, 'competition_id');
?>
		<?php 
$data = CompetitionQuestion::model()->GetCompetitionNameIdList();
echo CHtml::activeDropDownList($model, 'competition_id', CHtml::listData($data, 'id', 'name'), array('empty' => Yii::t('app', 'choose')));
?>
		<?php 
echo $form->error($model, 'competition_id');
?>
	</div>

        <div class="row">
                <?php 
echo $form->labelEx($model, 'question_id');
?>
                <?php 
$data = QuestionResource::model()->GetQuestionTitleIdList();
echo CHtml::activeDropDownList($model, 'question_id', CHtml::listData($data, 'id', 'title'), array('empty' => Yii::t('app', 'choose')));
?>
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = CompetitionQuestion::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
     }
     return $model;
 }
 public static function getQuestionTitle($competitionQuestionId)
 {
     $cache_key = 'CompetitionQuestionTitle#' . $competitionQuestionId;
     $cached = Yii::app()->cache->get($cache_key);
     if ($cached == null) {
         $competitionQuestion = CompetitionQuestion::model()->findByPk($competitionQuestionId);
         if ($competitionQuestion != null) {
             $cached = $competitionQuestion->question->title;
         } else {
             $cached = '';
         }
         Yii::app()->cache->set($cache_key, $cached, 600);
     }
     return $cached;
 }
 public function actionGetQuestions()
 {
     $session = Yii::app()->session;
     $competition_user_id = $session['competition_user_id'] ? $session['competition_user_id'] : 0;
     if ($competition_user_id == 0) {
         self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'User is not authenticated.')));
     }
     $competitionUser = CompetitionUser::model()->findByPk($competition_user_id);
     if ($competitionUser != null) {
         // we need to get that info somewhere
         /* $GetLanguageIds = QuestionResource::model()->with('question')->with('question.competitionQuestions')->findAll(array(
                'select' => 't.language_id',
                'distinct' => true,
                'condition' => 'competitionQuestions.competition_id=:competition_id',
                'params' => array(':competition_id' => $competitionUser->competition_id)
            ));
            $language_id = 1;
            if ($GetLanguageIds != null) {
                foreach ($GetLanguageIds as $record) {
                    $language_id = $record->language_id;
                    // echo 'Found Language ID: ', $record->language_id, '<br />';
                    // die();
                    break;
                }
            }
            $language = Language::model()->findByPk($language_id); 
            if ($language == null) {
                die('Language invalid');
            }
            $language_code = $language->short;*/
         // how about the session?
         if ($competitionUser->finished == 1 || $competitionUser->finish_time != null) {
             self::reponseJSON(array('success' => false, 'errorCode' => 999, 'error' => Yii::t('app', 'You have already finished competition.')));
         }
         if ($competitionUser->start_time != null) {
             $duration = $competitionUser->competition->duration * 60;
             $starttime = strtotime($competitionUser->start_time);
             $endtime = $starttime + $duration;
             if ($endtime < time()) {
                 $competitionUser->finished = 1;
                 $competitionUser->finish_time = $endtime;
                 $competitionUser->save(true, array('finished', 'finish_time'));
                 self::reponseJSON(array('success' => false, 'errorCode' => 999, 'error' => Yii::t('app', 'Your time is up. Questions won\'t be loaded!')));
             }
         }
         /*
                     $count = CompetitionUserQuestion::model()->count('competition_user_id=:competition_user_id', array(':competition_user_id' => $competition_user_id));*/
         $language_code = $session['preferred_language'];
         // error_log("Lang:".$language_code);
         $language = Language::model()->findByAttributes(array('short' => $language_code));
         $language_id = $language->id;
         if (!$competitionUser->questions_prepared) {
             $connection = Yii::app()->db;
             $transaction = $connection->beginTransaction();
             try {
                 // error_log("Language code:".$language_code."id:".$language_id);
                 // echo 'Adding new Competiton User Questions';
                 // generate questions order for user
                 $criteria = new CDbCriteria();
                 $criteria->together = true;
                 $criteria->with = array('competitionQuestionCategories');
                 $criteria->condition = 't.competition_id=:competition_id and competitionQuestionCategories.competition_category_id=:competition_category_id';
                 $criteria->params = array(':competition_id' => $competitionUser->competition_id, ':competition_category_id' => $competitionUser->competition_category_id);
                 $criteria->order = 'RAND()';
                 $competitionQuestions = CompetitionQuestion::model()->findAll($criteria);
                 $order = 1;
                 foreach ($competitionQuestions as $competitionQuestion) {
                     $competitionUserQuestion = new CompetitionUserQuestion();
                     $competitionUserQuestion->competition_question_id = $competitionQuestion->id;
                     $competitionUserQuestion->competition_user_id = $competition_user_id;
                     $competitionUserQuestion->ordering = $order;
                     $random_seed = number_format(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 9, '.', '');
                     $competitionUserQuestion->random_seed = $random_seed;
                     if (!$competitionUserQuestion->save()) {
                         var_dump($competitionUserQuestion->getErrors());
                         die;
                     }
                     $order++;
                 }
                 $competitionUser->questions_prepared = 1;
                 $competitionUser->save(true, array('questions_prepared'));
                 $transaction->commit();
             } catch (Exception $e) {
                 error_log($e);
                 $transaction->rollback();
             }
         }
         // $count = CompetitionUserQuestion::model()->count('competition_user_id=:competition_user_id', array(':competition_user_id' => $competition_user_id));
         $competitionUserQuestions = CompetitionUserQuestion::model()->findAll(array('condition' => 'competition_user_id=:competition_user_id', 'params' => array(':competition_user_id' => $competition_user_id), 'order' => 'ordering ASC'));
         if ($competitionUserQuestions != null) {
             $questions = array();
             foreach ($competitionUserQuestions as $competitionUserQuestion) {
                 $question_id = $competitionUserQuestion->competitionQuestion->question_id;
                 $startup_file = QuestionResource::model()->find('question_id=:question_id and language_id=:language_id and type=:type and start_up=:start_up', array(':question_id' => $question_id, ':language_id' => $language_id, ':type' => 1, ':start_up' => 1));
                 if ($startup_file != null) {
                     $questions[] = array('id' => $question_id, 'title' => $competitionUserQuestion->competitionQuestion->question->title, 'country' => $competitionUserQuestion->competitionQuestion->question->country_of_origin, 'link' => $question_id . '/' . $language_code . '/' . $startup_file->path . $startup_file->filename, 'custom_answer' => $competitionUserQuestion->custom_answer == null ? '' : $competitionUserQuestion->custom_answer, 'random_seed' => $competitionUserQuestion->random_seed, 'css' => $competitionUserQuestion->competitionQuestion->question->css);
                 } else {
                     echo "Question: ", $question_id, "\n";
                     die('missing startup file for language id: ' . $language_id . ' question id: ' . $question_id);
                 }
             }
             $response = array('success' => true, 'questions' => $questions, 'competition_title' => $competitionUser->competition->name, 'competition_length' => $competitionUser->competition->duration, 'seconds_to_end' => $competitionUser->start_time == null ? -1 : $competitionUser->competition->duration * 60 - time() - strtotime($competitionUser->start_time));
             self::reponseJSON($response);
         } else {
             self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'No questions defined for user.')));
         }
     } else {
         self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'User is not authenticated.')));
     }
 }
 public function GetCompetitionQuestionIdList()
 {
     $modelData = CompetitionQuestion::model()->search(true);
     $list = array();
     foreach ($modelData->getData() as $competitionQuestion) {
         $competitionQuestion['competition_id'] = $competitionQuestion->id;
         // var_dump($competitionQuestion->competition);
         //echo"<br><BR><br><BR><br><BR><br><BR>";
         $competitionQuestion['id'] = $competitionQuestion->competition->name . ' - ' . $competitionQuestion->question->title;
         // $competitionQuestion['id'] = $competitionQuestion->competition->name . ' - ' . $competitionQuestion->question->title;
         $list[] = $competitionQuestion;
     }
     return $list;
 }