/**
  * Update the SQL schema if necessary
  */
 function wpvq_update_sql()
 {
     if (get_option('wpvq_update_sql_v1_96') == FALSE) {
         // Create column "quizzes.forceToShare"
         global $wpdb;
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('quizzes') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'forceToShare' ", ARRAY_A);
         if (empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('quizzes') . " ADD forceToShare VARCHAR(100) AFTER askInformations;");
         }
         // Create column "quizzes.randomQuestions" + "quizzes.randomAnswers"
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('quizzes') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'randomQuestions' ", ARRAY_A);
         if (empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('quizzes') . " ADD randomQuestions INT(100) AFTER skin;");
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('quizzes') . " ADD isRandomAnswers smallint(6) AFTER randomQuestions;");
         }
         // Create column "quizzes.meta"
         global $wpdb;
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('quizzes') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'meta' ", ARRAY_A);
         if (empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('quizzes') . " ADD meta TEXT AFTER isRandomAnswers;");
         }
         // Create column "players.meta"
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('players') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'meta' ", ARRAY_A);
         if (empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('players') . " ADD meta TEXT NULL;");
         }
         // Delete column "players.quizName"
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('players') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'quizName' ", ARRAY_A);
         if (!empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('players') . " DROP quizName;");
             // email + nickname can be null
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('players') . " CHANGE `email` `email` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;");
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('players') . " CHANGE `nickname` `nickname` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;");
         }
         // Weight can be NULL (table.answers)
         // + convert old weight schema to new schema (table.multipliers)
         $results = $wpdb->get_results("SHOW TABLES LIKE '" . WPViralQuiz::getTableName('multipliers') . "'");
         if ($wpdb->num_rows == 0) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('players') . " CHANGE `weight` `weight` smallint(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;");
             $wpdb->query("CREATE TABLE IF NOT EXISTS `" . WPViralQuiz::getTableName('multipliers') . "` (\n\t\t\t\t  `id` int(200) NOT NULL AUTO_INCREMENT,\n\t\t\t\t  `quizId` int(200) NOT NULL,\n\t\t\t\t  `questionId` int(200) NOT NULL,\n\t\t\t\t  `answerId` int(200) NOT NULL,\n\t\t\t\t  `appreciationId` int(200) NOT NULL,\n\t\t\t\t  `multiplier` int(200) NOT NULL,\n\t\t\t\t  PRIMARY KEY (`id`)\n\t\t\t\t) CHARSET=utf8 AUTO_INCREMENT=1");
             $results = $wpdb->get_results("SELECT * FROM " . WPViralQuiz::getTableName('answers'));
             foreach ($results as $answer) {
                 $question = new WPVQQuestion();
                 try {
                     $question->load($answer->questionId);
                 } catch (Exception $e) {
                     continue;
                     // ignore error during update
                 }
                 // Update only for personality quiz
                 try {
                     $quizId = $question->getQuizId();
                     if (WPVQGame::getTypeById($quizId) == 'WPVQGamePersonality') {
                         $dataSql = array('quizId' => $quizId, 'questionId' => $answer->questionId, 'answerId' => $answer->ID, 'appreciationId' => $answer->weight, 'multiplier' => 1);
                         $wpdb->insert(WPViralQuiz::getTableName('multipliers'), $dataSql, array('%d', '%d', '%d', '%d', '%d'));
                     }
                 } catch (Exception $e) {
                     continue;
                     // ignore error during update
                 }
             }
         }
         // Create column "questions.pageAfter"
         $row = $wpdb->get_row("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM information_schema.COLUMNS \n\t\t\t\t\tWHERE \n\t\t\t\t\t    TABLE_SCHEMA = '" . DB_NAME . "' \n\t\t\t\t\tAND TABLE_NAME = '" . WPViralQuiz::getTableName('questions') . "' \n\t\t\t\t\tAND COLUMN_NAME = 'pageAfter' ", ARRAY_A);
         if (empty($row)) {
             $wpdb->query("ALTER TABLE " . WPViralQuiz::getTableName('questions') . " ADD `pageAfter` SMALLINT(2) NULL AFTER `content`");
         }
         // New extraOption system update (global settings to single setting)
         $wpdb->query("CREATE TABLE IF NOT EXISTS `" . WPViralQuiz::getTableName('extraoptions') . "` (\n\t\t\t\t\t  `id` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t  `quizId` int(200) NOT NULL,\n\t\t\t\t\t  `optionName` varchar(191) DEFAULT NULL,\n\t\t\t\t\t  `optionValue` longtext,\n\t\t\t\t\t  PRIMARY KEY (`id`)\n\t\t\t\t\t) CHARSET=utf8 AUTO_INCREMENT=1");
         $options = get_option('wpvq_settings');
         if (isset($options['wpvq_checkbox_refresh_page']) && $options['wpvq_checkbox_refresh_page'] == 1) {
             $row = $wpdb->get_results('SELECT id FROM ' . WPViralQuiz::getTableName('quizzes'));
             foreach ($row as $quiz) {
                 WPVQGame::updateExtraOption($quiz->id, 'refreshBrowser', '1');
             }
         }
     }
     // Prevent from mysql update each time
     update_option('wpvq_update_sql_v1_96', '1');
 }
  */
 if ($activeTab == 'questions') {
     /**
      * Delete stuff for update
      */
     $deleteAnswers = array_filter(explode(',', $_POST['deleteAnswers']));
     foreach ($deleteAnswers as $answerId) {
         $answer = new WPVQAnswer();
         try {
             $answer->load(intval($answerId))->delete();
         } catch (Exception $e) {
         }
     }
     $deleteQuestions = array_filter(explode(',', $_POST['deleteQuestions']));
     foreach ($deleteQuestions as $questionId) {
         $question = new WPVQQuestion();
         try {
             $question->load(intval($questionId))->delete();
         } catch (Exception $e) {
         }
     }
     /**
      * Add question and answers
      */
     $i = 0;
     $vqquestions = isset($_POST['vqquestions']) ? $_POST['vqquestions'] : array();
     foreach ($vqquestions as $question) {
         // Empty question
         if ($question['label'] == '' && $question['pictureId'] == 0) {
             continue;
         }
Exemple #3
0
 /**
  * Returns array of Questions
  * @return array of ::Questions
  */
 private function fetchQuestions($gameplayContext = false, $randomAnswers = false)
 {
     global $wpdb;
     // Fetch from DB
     $row = $wpdb->get_results($wpdb->prepare('SELECT id FROM ' . WPViralQuiz::getTableName('questions') . ' WHERE quizId = %d ORDER BY position ASC', $this->id));
     // No questions
     if (empty($row)) {
         return array();
     }
     // Random specificities
     if ($this->isRandomQuestions() && $gameplayContext) {
         if (!session_id()) {
             @session_start();
             // ugly :(
         }
         // Prevent from fetching same question if reloading page
         if (!isset($_SESSION['wpvq_random_quiz_' . $this->id])) {
             $_SESSION['wpvq_random_quiz_' . $this->id] = array();
         }
         // If session contains ALL questions, then reset the algo
         if (count($_SESSION['wpvq_random_quiz_' . $this->id]) == $this->countQuestionsFromDB()) {
             $_SESSION['wpvq_random_quiz_' . $this->id] = array();
         }
     }
     // Fetch questions
     $questions = array();
     $alreadyPlayedQuestions = array();
     // used for random questions
     foreach ($row as $question) {
         $que = new WPVQQuestion();
         $que->load($question->id, $gameplayContext, $randomAnswers);
         // Don't add question already played during previous game
         if ($this->isRandomQuestions() && $gameplayContext) {
             if (!in_array($question->id, $_SESSION['wpvq_random_quiz_' . $this->id])) {
                 $questions[] = $que;
                 // Don't loop anymore if you already have enough questions !
                 if (count($questions) >= $this->randomQuestions) {
                     break;
                 }
             } else {
                 $alreadyPlayedQuestions[] = $que;
             }
         }
         // Not random : we add all questions.
         if (!$this->isRandomQuestions() || !$gameplayContext) {
             $questions[] = $que;
         }
     }
     // Randomize questions
     if ($this->isRandomQuestions() && $gameplayContext) {
         shuffle_seed($questions, $this->getRandomQuestionSeed());
         shuffle_seed($alreadyPlayedQuestions, $this->getRandomQuestionSeed());
         $questions = array_merge($questions, $alreadyPlayedQuestions);
         // complete with more questions if needed
         $questions = array_slice($questions, 0, $this->randomQuestions);
         // truncate the final array
         foreach ($questions as $question) {
             if (!in_array($question->getId(), $_SESSION['wpvq_random_quiz_' . $this->id])) {
                 $_SESSION['wpvq_random_quiz_' . $this->id][] = $question->getId();
             }
         }
     }
     return $questions;
 }
 public function getQuestion()
 {
     $question = new WPVQQuestion();
     $question->load($this->questionId);
     return $question;
 }