Skip to content

Commit

Permalink
Adds wpautop around the leaderboard system to auto-generate the p ele…
Browse files Browse the repository at this point in the history
…ments for new lines
  • Loading branch information
fpcorso committed Jul 20, 2018
1 parent 48dc994 commit e727344
Showing 1 changed file with 23 additions and 19 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 );
}
?>

0 comments on commit e727344

Please sign in to comment.