Skip to content

Commit

Permalink
Merge pull request #2 from QuizandSurveyMaster/dev
Browse files Browse the repository at this point in the history
1.0.2 Release
  • Loading branch information
Frank Corso authored Jul 25, 2018
2 parents 2587970 + e727344 commit 813ee63
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 84 deletions.
42 changes: 23 additions & 19 deletions php/leaderboard-function.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
<?php
/**
* Handles the actual generation of the leaderboards.
*/

if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Generates the leaderboads
*
* @since 1.0.0
* @param $quiz_id int The ID of the quiz
* @param int $quiz_id int The ID of the quiz.
* @return string The HTML of the leaderboard
*/
function qsm_addon_leaderboards_generate( $quiz_id ) {

// Globals
global $wpdb;
global $mlwQuizMasterNext;
$quiz_id = intval( $quiz_id );

// Retrieve template, grading system, and name of quiz
// Retrieves template, grading system, and name of quiz.
$mlwQuizMasterNext->pluginHelper->prepare_quiz( $quiz_id );
$template = $mlwQuizMasterNext->pluginHelper->get_section_setting( 'quiz_leaderboards', 'template' );
$grade_system = $mlwQuizMasterNext->pluginHelper->get_section_setting( 'quiz_options', 'system' );
$quiz_name = $wpdb->get_var( $wpdb->prepare( "SELECT quiz_name FROM {$wpdb->prefix}mlw_quizzes WHERE deleted='0' AND quiz_id=%d", $quiz_id ) );

// Prepare SQL for results, then retrieve results
// Prepares SQL for results, then retrieve results.
$sql = "SELECT * FROM {$wpdb->prefix}mlw_results WHERE quiz_id=%d AND deleted='0'";
if ( $grade_system == 0 ) {
$sql .= " ORDER BY correct_score DESC";
if ( 0 == $grade_system ) {
$sql .= ' ORDER BY correct_score DESC';
}
if ( $grade_system == 1 ) {
$sql .= " ORDER BY point_score DESC";
if ( 1 == $grade_system ) {
$sql .= ' ORDER BY point_score DESC';
}
$sql .= " LIMIT 10";
$sql .= ' LIMIT 10';
$results = $wpdb->get_results( $wpdb->prepare( $sql, $quiz_id ) );

// Change variable to quiz name
$template = str_replace( "%QUIZ_NAME%" , $quiz_name, $template);
// Changes variable to quiz name.
$template = str_replace( '%QUIZ_NAME%' , $quiz_name, $template );

// Cycle through each result and use name/points for entry in leaderboard
// Cycles through each result and use name/points for entry in leaderboard.
$leader_count = 0;
foreach( $results as $result ) {
foreach ( $results as $result ) {
$leader_count++;

// Change name to quiz taker's name
if ($leader_count == 1) {$template = str_replace( "%FIRST_PLACE_NAME%" , $result->name, $template);}
// Changes name to quiz taker's name.
if ( $leader_count == 1 ) {$template = str_replace( "%FIRST_PLACE_NAME%" , $result->name, $template);}
if ($leader_count == 2) {$template = str_replace( "%SECOND_PLACE_NAME%" , $result->name, $template);}
if ($leader_count == 3) {$template = str_replace( "%THIRD_PLACE_NAME%" , $result->name, $template);}
if ($leader_count == 4) {$template = str_replace( "%FOURTH_PLACE_NAME%" , $result->name, $template);}
if ($leader_count == 5) {$template = str_replace( "%FIFTH_PLACE_NAME%" , $result->name, $template);}

// Depending on grading system, use either score or points
// Depending on grading system, use either score or points.
if ( $grade_system == 0 ) {
if ($leader_count == 1) {$template = str_replace( "%FIRST_PLACE_SCORE%" , $result->correct_score . "%", $template);}
if ($leader_count == 2) {$template = str_replace( "%SECOND_PLACE_SCORE%" , $result->correct_score . "%", $template);}
Expand All @@ -65,7 +69,7 @@ function qsm_addon_leaderboards_generate( $quiz_id ) {
}
}

// Remove all variables in case any were missed
// Removes all variables in case any were missed.
$template = str_replace( "%QUIZ_NAME%", " ", $template );
$template = str_replace( "%FIRST_PLACE_NAME%", " ", $template );
$template = str_replace( "%SECOND_PLACE_NAME%", " ", $template );
Expand All @@ -79,6 +83,6 @@ function qsm_addon_leaderboards_generate( $quiz_id ) {
$template = str_replace( "%FIFTH_PLACE_SCORE%", " ", $template );

// Return template
return $template;
return wpautop( $template );
}
?>
133 changes: 68 additions & 65 deletions qsm-leaderboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* Description: Adds some basic leaderboards to Quiz And Survey Master
* Author: Frank Corso
* Author URI: https://quizandsurveymaster.com
* Version: 1.0.1
* Version: 1.0.2
*
* @author Frank Corso
* @version 1.0.1
* @version 1.0.2
*/

if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

// If the PHP version is less then our minimum requirements, end.
if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
return;
}


/**
Expand All @@ -20,16 +27,16 @@
* When loaded, it loads the included plugin files and add functions to hooks or filters. The class also handles the admin menu
*
* @since 1.0.0
*/
*/
class QSM_Leaderboards {

/**
* Version Number
*
* @var string
* @since 1.0.0
*/
public $version = '1.0.1';
*/
public $version = '1.0.2';

/**
* Main Construct Function
Expand All @@ -40,8 +47,8 @@ class QSM_Leaderboards {
* @uses QSM_Leaderboards::load_dependencies() Loads required filed
* @uses QSM_Leaderboards::add_hooks() Adds actions to hooks and filters
* @return void
*/
function __construct() {
*/
public function __construct() {
$this->load_dependencies();
$this->add_hooks();
$this->check_license();
Expand All @@ -52,13 +59,13 @@ function __construct() {
*
* @since 1.0.0
* @return void
*/
*/
public function load_dependencies() {
include( "php/addon-settings-tab-content.php" );
include( "php/quiz-settings-tab-content.php" );
include( "php/leaderboard-function.php" );
include( "php/shortcodes.php" );
include( "php/widgets.php" );
include 'php/addon-settings-tab-content.php';
include 'php/quiz-settings-tab-content.php';
include 'php/leaderboard-function.php';
include 'php/shortcodes.php';
include 'php/widgets.php';
}

/**
Expand All @@ -68,14 +75,16 @@ public function load_dependencies() {
*
* @since 1.0.0
* @return void
*/
*/
public function add_hooks() {
add_action( 'plugins_loaded', array( $this, 'register_fields' ) );
add_action( 'init', array( $this, 'backwards_compatibility' ) );
add_action( 'init', array( $this, 'register_fields' ) );
add_action( 'init', array( $this, 'backwards_compatibility' ) );
add_action( 'admin_init', 'qsm_addon_leaderboards_register_quiz_settings_tabs' );
add_action( 'admin_init', 'qsm_addon_leaderboards_register_addon_settings_tabs' );
add_shortcode( 'qsm_leaderboard', 'qsm_addon_leaderboards_shortcode' );
add_action('widgets_init', create_function('', 'return register_widget("QSM_Leaderboards_Widget");') );
add_action( 'widgets_init', function() {
return register_widget( 'QSM_Leaderboards_Widget' );
});
}

/**
Expand All @@ -96,29 +105,26 @@ public function backwards_compatibility() {
public function register_fields() {
global $mlwQuizMasterNext;

// Registers template setting if it doesn't already exist
if ( ! $mlwQuizMasterNext->pluginHelper->get_section_setting( 'quiz_leaderboards', 'template' ) ) {
$field_array = array(
'id' => 'template',
'label' => 'Leaderboard Template',
'type' => 'editor',
'variables' => array(
'%QUIZ_NAME%',
'%FIRST_PLACE_NAME%',
'%FIRST_PLACE_SCORE%',
'%SECOND_PLACE_NAME%',
'%SECOND_PLACE_SCORE%',
'%THIRD_PLACE_NAME%',
'%THIRD_PLACE_SCORE%',
'%FOURTH_PLACE_NAME%',
'%FOURTH_PLACE_SCORE%',
'%FIFTH_PLACE_NAME%',
'%FIFTH_PLACE_SCORE%'
),
'default' => 0
);
$mlwQuizMasterNext->pluginHelper->register_quiz_setting( $field_array, 'quiz_leaderboards' );
}
$field_array = array(
'id' => 'template',
'label' => 'Leaderboard Template',
'type' => 'editor',
'variables' => array(
'%QUIZ_NAME%',
'%FIRST_PLACE_NAME%',
'%FIRST_PLACE_SCORE%',
'%SECOND_PLACE_NAME%',
'%SECOND_PLACE_SCORE%',
'%THIRD_PLACE_NAME%',
'%THIRD_PLACE_SCORE%',
'%FOURTH_PLACE_NAME%',
'%FOURTH_PLACE_SCORE%',
'%FIFTH_PLACE_NAME%',
'%FIFTH_PLACE_SCORE%',
),
'default' => 0,
);
$mlwQuizMasterNext->pluginHelper->register_quiz_setting( $field_array, 'quiz_leaderboards' );
}

/**
Expand All @@ -128,30 +134,29 @@ public function register_fields() {
*
* @since 1.0.0
* @return void
*/
*/
public function check_license() {

if( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
// load our custom updater
include( 'php/EDD_SL_Plugin_Updater.php' );
}
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
// Loads our custom updater.
include 'php/EDD_SL_Plugin_Updater.php';
}

// retrieve our license key from the DB
$leaderboard_data = get_option( 'qsm_addon_leaderboard_settings', '' );
if ( isset( $leaderboard_data["license_key"] ) ) {
$license_key = trim( $leaderboard_data["license_key"] );
} else {
$license_key = '';
}
// Retrieves our license key from the DB.
$leaderboard_data = get_option( 'qsm_addon_leaderboard_settings', '' );
if ( isset( $leaderboard_data['license_key'] ) ) {
$license_key = trim( $leaderboard_data['license_key'] );
} else {
$license_key = '';
}

// setup the updater
// Sets up the updater.
$edd_updater = new EDD_SL_Plugin_Updater( 'https://quizandsurveymaster.com', __FILE__, array(
'version' => $this->version, // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_name' => 'Leaderboards', // name of this plugin
'author' => 'Frank Corso' // author of this plugin
)
);
'version' => $this->version,
'license' => $license_key,
'item_name' => 'Leaderboards',
'author' => 'Frank Corso',
));
}
}

Expand All @@ -162,9 +167,9 @@ public function check_license() {
* @return void
*/
function qsm_addon_leaderboard_load() {
// Make sure QSM is active
// Makes sure QSM is active.
if ( class_exists( 'MLWQuizMasterNext' ) ) {
$plugin_name = new QSM_Leaderboards();
$qsm_leaderboards = new QSM_Leaderboards();
} else {
add_action( 'admin_notices', 'qsm_addon_leaderboard_missing_qsm' );
}
Expand All @@ -175,9 +180,7 @@ function qsm_addon_leaderboard_load() {
* Display notice if Quiz And Survey Master isn't installed
*
* @since 1.0.0
* @return string The notice to display
*/
function qsm_addon_leaderboard_missing_qsm() {
echo '<div class="error"><p>QSM - Leaderboards requires Quiz And Survey Master. Please install and activate the Quiz And Survey Master plugin.</p></div>';
echo '<div class="error"><p>QSM - Leaderboards requires Quiz And Survey Master. Please install and activate the Quiz And Survey Master plugin.</p></div>';
}
?>

0 comments on commit 813ee63

Please sign in to comment.