/** * Shortcode display Quiz's Results in front */ public static function viralQuizResults() { global $wpdata; // Data helpful to debug : // $array = array( // 'quizId' => 98, // 'resultValue' => 1, // ); // echo base64_encode(json_encode($array)); // No URL parameters if (!isset($_GET['wpvqdataresults'])) { return; } $results = json_decode(base64_decode($_GET['wpvqdataresults']), true); if (!is_array($results) || !isset($results['quizId']) || !isset($results['resultValue'])) { return; } // Load quizz $id = intval($results['quizId']); try { $type = WPVQGame::getTypeById($id); $quiz = new $type(); $q = $quiz->load($id, false); } catch (Exception $e) { echo "ERROR : Quiz #{$param['id']} doesn't exist."; die; } // Useful to load JS script self::$isShortcodeLoaded = true; self::$quiz = $q; $wpdata['quiz'] = $q; $wpdata['type'] = $type; // Fetch results if ($type == 'WPVQGameTrueFalse') { $wpdata['results'] = json_decode(WPVQShortcode::getTrueFalseAppreciation($id, $results['resultValue']), true); } else { if ($type == 'WPVQGamePersonality') { $wpdata['results'] = json_decode(WPVQShortcode::choosePersonality($results['resultValue']), true); } } // Also keep the main value (score, personality, ...) $wpdata['results']['resultValue'] = $results['resultValue']; $shortCode = ob_start(); include dirname(__FILE__) . '/../views/WPVQShortcodeResults.php'; $output = ob_get_contents(); ob_end_clean(); return $output; }
/** * Shortcode display Quiz's Results in front */ public static function viralQuizAnalytics($param) { global $wpdata; // Bad ID if (!is_numeric($param['id'])) { return; } // Load quizz $id = intval($param['id']); try { $type = WPVQGame::getTypeById($id); $quiz = new $type(); $q = $quiz->load($id, false); } catch (Exception $e) { return __("ERROR : Quiz #{$param['id']} doesn't exist.", 'wpvq'); } // Add a fake count ? $countPlayers = $q->countPlayersFromDB(); if (isset($param['fake']) && is_numeric($param['fake'])) { $countPlayers += $param['fake']; } return $countPlayers; }
/** * Returns an appreciation object for a $quizId and a $score * @param [type] $score [description] * @return [type] [description] */ public static function getAppreciationByScore($quizId, $score) { if (!is_numeric($quizId) || !is_numeric($score)) { return NULL; } $type = WPVQGame::getTypeById($quizId); $quiz = new $type(); $quiz->load(intval($quizId)); $appreciations = $quiz->getAppreciations(); if (empty($appreciations) || !is_numeric($score)) { return NULL; } // Force DESC order for scoreCondition appreciation items // Anonymous functions not supported by php < 5.3 :( // See snippets.php usort($appreciations, 'wpvq_usort_callback_function'); $currentAppreciation = NULL; foreach ($appreciations as $appreciation) { if ($appreciation->getScoreCondition() > $score) { $currentAppreciation = $appreciation; } } return $currentAppreciation; }
/** * 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'); }
<?php // DATA global $vqData; // Fetch TYPE if (isset($_GET['type'])) { $vqData['type'] = htmlentities($_GET['type']); } elseif (isset($_GET['id']) && is_numeric($_GET['id'])) { $vqData['type'] = WPVQGame::getTypeById($_GET['id']); } // Global option $options = get_option('wpvq_settings'); $vqData['showMiniature'] = isset($options['wpvq_checkbox_backoffice_miniature']) ? true : false; // Get ID (or NULL) // Useful for redirection after submit, and other stuff. $vqData['quizId'] = NULL; $vqData['quizName'] = ''; $vqData['showSharing'] = 1; $vqData['showCopyright'] = 0; $vqData['skin'] = 'buzzfeed'; $vqData['askInformations'] = array(); $vqData['forceToShare'] = array(); $vqData['isRandomQuestions'] = false; $vqData['randomQuestions'] = -1; $vqData['isRandomAnswers'] = false; $vqData['meta'] = null; $vqData['extraOptions'] = null; $referer = 'create'; /** * Prepare the controller for EDIT MODE */
/** * Load a question by ID * @param $loadPlayers Permet de ne pas perdre de temps à charger les joueurs si pas besoin. * @return [type] [description] */ public function load($quizId, $loadPlayers = true, $page = 1) { global $wpdb; if (!is_numeric($quizId)) { throw new Exception("Need numeric quizID on players load ({$quizId})."); } $firstPos = ($page - 1) * WPVQ_PLAYERS_PER_PAGE; $lastPos = $page * WPVQ_PLAYERS_PER_PAGE; $playersCount = $wpdb->get_row($wpdb->prepare('SELECT COUNT(*) as count FROM ' . WPViralQuiz::getTableName('players') . ' WHERE quizId = %d', array(intval($quizId)))); $this->playersCount = $playersCount->count; $this->pagesCount = round($this->playersCount / WPVQ_PLAYERS_PER_PAGE); // Fetch quiz name $type = WPVQGame::getTypeById($quizId); $quiz = new $type(); $quiz->load(intval($quizId)); $this->quizName = $quiz->getName(); // Fetch from DB $this->quizId = $quizId; if ($loadPlayers) { if ($page == '*') { $row = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . WPViralQuiz::getTableName('players') . ' WHERE quizId = %d ORDER BY id DESC', array($quizId))); } else { $row = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . WPViralQuiz::getTableName('players') . ' WHERE quizId = %d ORDER BY id DESC LIMIT ' . $firstPos . ',' . WPVQ_PLAYERS_PER_PAGE, array($quizId))); } foreach ($row as $player) { $newPlayer = array('id' => $player->id, 'email' => $player->email, 'quizName' => $this->quizName, 'nickname' => $player->nickname, 'result' => $player->result, 'date' => $player->date, 'meta' => array()); // Count results if (!isset($this->resultsCounter[$player->result])) { $this->resultsCounter[$player->result] = 1; } else { $this->resultsCounter[$player->result]++; } // Fetch meta (serialized in DB) if ($player->meta != NULL && $player->meta != '') { $metaRaw = $player->meta; $meta = unserialize($metaRaw); if (!is_array($meta)) { continue; // malformed array } foreach ($meta as $key => $value) { $newPlayer['meta'][$key] = $value; } } $this->players[] = $newPlayer; } // sort array arsort($this->resultsCounter); } return $this; }
<?php global $vqData; try { if (isset($_GET['id']) && is_numeric($_GET['id'])) { $type = WPVQGame::getTypeById($_GET['id']); $quiz = new $type(); $quiz->load(intval($_GET['id'])); if (!isset($_GET['wp_nonce']) || !wp_verify_nonce($_GET['wp_nonce'], 'delete_wpvquizz_' . $_GET['id'])) { throw new Exception(__("You can't do that ! Sorry.", 'wpvq')); } else { if ($quiz->delete() > 0) { $url_redirect = esc_url_raw(remove_query_arg(array('id', 'element', 'action', 'wp_nonce', 'noheader'), add_query_arg(array('referer' => 'remove')))); wp_redirect(url_origin($_SERVER) . $url_redirect); } else { throw new Exception(__("Error during quiz deletion.", 'wpvq')); } } } else { throw new Exception(__("This quiz doesn't exist.", 'wpvq')); } } catch (Exception $e) { echo $e->getMessage(); } //include dirname(__FILE__) . '/../views/WPVQAddQuizz.php';
/** * Triggered when player submit information (mail, nickname, ...) * @return [type] [description] */ public static function submitInformations() { if (!isset($_POST['data'])) { die('nodata'); } // WP options disable players logs $options = get_option('wpvq_settings'); $disablePlayersLogs = isset($options['wpvq_checkbox_disable_playersLogs']) ? true : false; // Parse jquery data $post = array(); parse_str($_POST['data'], $post); // isset($post['beforeResults']) on natural tigger (not form validation, but quiz end) $nickname = sanitize_text_field(isset($post['wpvq_askNickname']) ? $post['wpvq_askNickname'] : ''); $email = sanitize_text_field(isset($post['wpvq_askEmail']) ? $post['wpvq_askEmail'] : ''); $result = sanitize_text_field(isset($post['wpvq_ask_result']) ? $post['wpvq_ask_result'] : ''); $quizId = intval($post['wpvq_quizId']); // Destroy seed (for random quiz only) if (!session_id()) { session_start(); } $_SESSION['wpvq_randomQuestionSeed_quiz' . $quizId] = 0; // If disable logs AND no nickname/email catching, don't save any logs if ($disablePlayersLogs && $nickname == '' && $email == '') { die('nolog'); } $type = WPVQGame::getTypeById($quizId); $quiz = new $type(); $quiz = $quiz->load($quizId); $meta = $quiz->getMeta(); // Don't save Anonymous Player if we save firstname/email on the next step $getInformations = $quiz->getAskInformations(); if (!empty($getInformations) && isset($post['beforeResults'])) { return; } $players = new WPVQPlayers(); $players = $players->load($quizId, false); $playerData = array('nickname' => $nickname, 'email' => $email, 'quizName' => $quiz->getName(), 'result' => $result); $playerId = $players->addPlayers($playerData); // Meta for Mailchimp if (isset($meta['mailchimp'])) { $mailingApi = new WPVQMailingAPI('mailchimp', $meta, $playerData); } // Meta for Aweber if (isset($meta['aweber'])) { $mailingApi = new WPVQMailingAPI('aweber', $meta, $playerData); } // Hook do_action('wpvq_add_player', $playerId, $playerData, $quizId, $post); }