public function GetCompetitionQuestionDifficultyNameIdList()
 {
     $modelData = CompetitionQuestionDifficulty::model()->search(true);
     $list = array();
     foreach ($modelData->getData() as $competitionQuestionDifficulty) {
         $competitionQuestionDifficulty['name'] = $competitionQuestionDifficulty->name;
         $list[] = $competitionQuestionDifficulty;
     }
     return $list;
 }
Example #2
0
 public function exportData($competition_id, $competition_category_id = 0)
 {
     // export headers (ID tekmovalca, ime, priimek, spol, šola, mentor, razred, kategorija, vprašanja... (v oklepaju pravilen odgovor), Število točk, Priznanje
     $list = array();
     $superuser = Generic::isSuperAdmin();
     if ($superuser) {
         $conditions = array(':competition_id' => $competition_id);
         if ($competition_category_id != 0) {
             $conditions[':competition_category_id'] = $competition_category_id;
         }
         $data = CompetitionUser::model()->with('school')->with('competitionCategorySchoolMentor.user.profile')->with('competitionCategory')->with('competitionUserQuestions.competitionQuestion.competitionQuestionCategories')->with('competitionUserQuestions')->findAll(array('condition' => 't.competition_id=:competition_id ' . ($competition_category_id != 0 ? 'and t.competition_category_id=:competition_category_id' : '') . ' and t.competition_category_id=competitionQuestionCategories.competition_category_id', 'params' => $conditions, 'order' => 'school.name ASC, profile.last_name ASC, profile.first_name ASC, competitionCategory.name ASC, t.class ASC, t.last_name ASC, t.first_name ASC'));
     } else {
         $cUser = CompetitionUser::model();
         $cUser->competition_id = $competition_id;
         $cUser->competition_category_id = $competition_category_id;
         $dataProvider = $cUser->search(true);
         $data = array();
         foreach ($dataProvider->getData() as $el) {
             $data[] = $el;
         }
     }
     $competition = Competition::model()->findByPk($competition_id);
     $duration = $competition->duration;
     $row_data = array();
     $questions = array();
     $cached_school_mentor_ids = array();
     $cached_school_mentor_emails = array();
     foreach ($data as $el) {
         $row = array();
         $row['id'] = $el->id;
         $row['first_name'] = $el->first_name;
         $row['last_name'] = $el->last_name;
         $row['gender'] = $el->gender == 0 ? 'M' : 'Ž';
         $row['class'] = $el->class;
         $row['competition'] = $el->competition->name;
         $row['school'] = $el->school->name;
         if (self::canShowAwardField($el->competition_id)) {
             $row['award'] = $el->GetAwardName($el->award);
         }
         if (self::canShowAdvancingToNextLevel($competition_id)) {
             $row['advancing_to_next_level'] = $el->advancing_to_next_level;
         }
         // čas tekmovanja
         if ($el->finish_time == null || $el->start_time == null) {
             $row['duration'] = $duration;
         } else {
             $row['duration'] = number_format(round((strtotime($el->finish_time) - strtotime($el->start_time)) / 60 * 100) / 100, 2, ",", ".");
         }
         if ($superuser) {
             $row['user_id'] = isset($el->competitionCategorySchoolMentor) ? $el->competitionCategorySchoolMentor->user_id : '/';
             if ($row['user_id'] != '/') {
                 if (!isset($cached_school_mentor_emails[$row['user_id']])) {
                     $mentorUser = User::model()->findByPk($row['user_id']);
                     if ($mentorUser != null) {
                         $cached_school_mentor_emails[$row['user_id']] = $mentorUser->email;
                         $row['mentor_email'] = $mentorUser->email;
                     } else {
                         $row['mentor_email'] = '/';
                     }
                 } else {
                     $row['mentor_email'] = $cached_school_mentor_emails[$row['user_id']];
                 }
                 $mentor_key = $row['user_id'] . '#' . $el->school_id;
                 if (isset($cached_school_mentor_ids[$mentor_key])) {
                     $row['mentor_id'] = $cached_school_mentor_ids[$mentor_key];
                 } else {
                     $mentor_get = SchoolMentor::model()->find('user_id=:user_id and school_id=:school_id', array(':user_id' => $row['user_id'], ':school_id' => $el->school_id));
                     if ($mentor_get != null) {
                         $cached_school_mentor_ids[$mentor_key] = 'M' . $mentor_get->id;
                     } else {
                         $cached_school_mentor_ids[$mentor_key] = '/';
                     }
                     $row['mentor_id'] = $cached_school_mentor_ids[$mentor_key];
                 }
             } else {
                 $row['mentor_id'] = '/';
             }
             $row['ip_start'] = $el->ip_start;
             $row['ip_stop'] = $el->ip_stop;
         }
         if (isset($el->competitionCategorySchoolMentor) && isset($el->competitionCategorySchoolMentor->user) && isset($el->competitionCategorySchoolMentor->user->profile)) {
             $row['mentor'] = $el->competitionCategorySchoolMentor->user->profile->last_name . ' ' . $el->competitionCategorySchoolMentor->user->profile->first_name;
         } else {
             $row['mentor'] = '/';
         }
         $row['category'] = $el->competitionCategory->name;
         $row['disqualified'] = $el->disqualified;
         $row['questions'] = array();
         if (self::canShowCompetitionResults($el->competition_id)) {
             foreach ($el->competitionUserQuestions as $competitionUserQuestion) {
                 $row['questions'][$competitionUserQuestion->competition_question_id] = $competitionUserQuestion->custom_answer;
                 if (!isset($questions[$competitionUserQuestion->competition_question_id])) {
                     $questionDifficulty = 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" => $el->competition_category_id));
                     $question = $competitionUserQuestion->competitionQuestion->question;
                     $ver_func = unserialize($question->verification_function);
                     $questions[$competitionUserQuestion->competition_question_id] = array('competition_question_id' => $competitionUserQuestion->competition_question_id, 'question_title' => $question->title, 'question_answer' => isset($ver_func[0]) ? $ver_func[0] : '', 'question_difficutlty' => $questionDifficulty->name, 'question_difficutlty_positive' => round(10 * $questionDifficulty->correct_answer_points) / 10, 'question_difficutlty_negative' => round(10 * $questionDifficulty->wrong_answer_points) / 10);
                 }
             }
         }
         $row_data[$row['id']] = $row;
     }
     $question_additional_data = $questions;
     // order questions names
     $this->orderBy($questions, 'order by question_title asc', true, false);
     $header = array('ID', 'Tekmovanje', 'Šola', 'Mentor');
     if ($superuser) {
         $header[] = 'Mentor Uporabnik ID';
         $header[] = 'Mentor ID';
         $header[] = 'Mentor e-pošta';
     }
     $header[] = 'Kategorija';
     $header[] = 'Razred';
     $header[] = 'Priimek';
     $header[] = 'Ime';
     $header[] = 'Spol';
     $header[] = 'Diskvalificiran';
     if (self::canShowCompetitionResults($competition_id)) {
         $question_map = array();
         foreach ($questions as $question) {
             $header[] = $question['question_title'] . ' (' . $question['question_difficutlty'] . ' (' . $question['question_difficutlty_positive'] . '|' . $question['question_difficutlty_negative'] . ')) - ' . $question['question_answer'];
             $question_map[] = $question['competition_question_id'];
         }
         $header[] = 'Število točk';
         $header[] = 'Maksimalno število točk';
     }
     $header[] = 'Čas tekmovanja v minutah';
     if (self::canShowAwardField($competition_id)) {
         $header[] = 'Priznanje';
     }
     if (self::canShowAdvancingToNextLevel($competition_id)) {
         $header[] = 'Napreduje na naslednji nivo';
     }
     if ($superuser) {
         $header[] = 'IP naslov začetek';
         $header[] = 'IP naslov konec';
     }
     $escape_header = array();
     foreach ($header as $h) {
         $escape_header[] = '"' . $h . '"';
     }
     $list[] = implode(';', $escape_header);
     foreach ($row_data as $row) {
         $cols = array($row['id'], $row['competition'], $row['school'], $row['mentor']);
         if ($superuser) {
             $cols[] = $row['user_id'];
             $cols[] = $row['mentor_id'];
             $cols[] = $row['mentor_email'];
         }
         $cols[] = $row['category'];
         $cols[] = $row['class'];
         $cols[] = $row['last_name'];
         $cols[] = $row['first_name'];
         $cols[] = $row['gender'];
         $cols[] = $row['disqualified'] == 1 ? Yii::t('app', 'yes') : Yii::t('app', 'no');
         $total_points = 0;
         $start_points = 0;
         $max_points = 0;
         if (self::canShowCompetitionResults($competition_id)) {
             foreach ($question_map as $q) {
                 $start_points += abs($question_additional_data[$q]['question_difficutlty_negative']);
                 $max_points += $question_additional_data[$q]['question_difficutlty_positive'];
                 if (isset($row['questions'][$q])) {
                     $points = 0;
                     if ($row['questions'][$q] != '' && $row['questions'][$q] != NULL) {
                         if ($row['questions'][$q] == $question_additional_data[$q]['question_answer']) {
                             $points = $question_additional_data[$q]['question_difficutlty_positive'];
                         } else {
                             $points = $question_additional_data[$q]['question_difficutlty_negative'];
                         }
                     }
                     if ($points != 0) {
                         $total_points += $points;
                         $cols[] = $row['questions'][$q] . '|' . $points;
                     } else {
                         $cols[] = '';
                     }
                 } else {
                     $cols[] = '';
                 }
             }
             $cols[] = $start_points + $total_points;
             $cols[] = $start_points + $max_points;
         }
         $cols[] = $row['duration'];
         if (self::canShowAwardField($competition_id)) {
             $cols[] = $row['award'];
         }
         if (self::canShowAdvancingToNextLevel($competition_id)) {
             $cols[] = $row['advancing_to_next_level'] == 1 ? Yii::t('app', 'yes') : Yii::t('app', 'no');
         }
         if ($superuser) {
             $cols[] = $row['ip_start'];
             $cols[] = $row['ip_stop'];
         }
         $escape_cols = array();
         foreach ($cols as $c) {
             $escape_cols[] = '"' . $c . '"';
         }
         $list[] = implode(';', $escape_cols);
     }
     $content = implode("\n", $list);
     //OUPUT HEADERS
     // ob_clean();
     header("Pragma: public");
     header("Expires: 0");
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
     header("Cache-Control: private", false);
     header("Content-Type: application/octet-stream");
     header("Content-Disposition: attachment; filename=\"export-competition-results-" . date('Y-m-d') . ".csv\";");
     header("Content-Transfer-Encoding: binary");
     /* try {
        $export = iconv('utf-8', 'windows-1250', $content);
        } catch(Exception $e) {
        $export = $content;
        }
        die(); */
     $export = $content;
     // header("Content-Length: " . strlen($export));
     echo $export;
     die;
 }
 /**
  * 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 = CompetitionQuestionDifficulty::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
     }
     return $model;
 }