Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[CoursePress] - Quiz Color
<?php
/**
* Plugin Name: [CoursePress] - Quiz Color
* Description: [CoursePress] - Quiz Color
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/tho2757
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_cp_quiz_color_func', 100 );
function wpmudev_cp_quiz_color_func() {
if ( class_exists( 'CoursePress_Template_Module' ) ) {
class WPMUDEV_CoursePress_Template_Module_Quiz_Color extends CoursePress_Template_Module{
public function __construct(){
add_filter( 'coursepress_module_template', array( $this, 'quiz_content_color_template' ), 10, 3 );
}
public function quiz_content_color_template($module_elements, $module_type, $module_id){
$response = self::get_response( $module_id, get_current_user_id() );
if( $response ){
$attributes = self::attributes( $module_id );
$quiz_questions = array(
'single' => 1,
'multiple' => 1,
'single1' => 1
);
if ( ! empty( $attributes['questions'] ) ) {
$results = array();
foreach ( $attributes['questions'] as $qi => $question ) {
// pass if not quiz question
if( ! isset( $quiz_questions[ $question['type'] ] ) ) continue;
$correct_answers = $question['options']['checked'];
$total_answers = count( $correct_answers );
$correct_responses = 0;
if ( isset( $response[ $qi ] ) && is_array( $response[ $qi ] ) ) {
foreach ( $response[ $qi ] as $a_key => $answer ) {
if ( $answer === $correct_answers[ $a_key ] ) {
$correct_responses += 1;
}
}
}
$results[$qi]= 1 === $correct_responses / $total_answers;
}
// add js vals
if( ! empty( $results ) ){
$module_elements .=
'<script>
(function($){
var _results = '. json_encode( $results ) .';
$(function(){
var _answer,
_qi,
_status;
for( _qi in _results ){
_answer = $(".module-quiz-question.question-"+ _qi);
if( _answer.length ){
_status = _results[_qi] ? "pass" : "fail";
_answer.addClass("wpmudev-answer-"+ _status);
}
}
})
})(window.jQuery);
</script>
<style>
.wpmudev-answer-pass .question{
background-color: rgba(69,171,19,0.14);
}
.wpmudev-answer-fail .question{
background-color: rgba(244,67,54,0.14);
}
</style>';
}
}
}
return $module_elements;
}
}
$run = new WPMUDEV_CoursePress_Template_Module_Quiz_Color();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.