summaryrefslogtreecommitdiff
blob: 6ab6e1d14677afd1921f4f7583b35b163a7382c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
( function( $ ) {
	$.fn.shuffleQuiz = function() {
		var allElems = this.get(),
			getRandom = function( max ) {
				return Math.floor( Math.random() * max );
			},
			shuffled = $.map( allElems, function() {
				var random = getRandom( allElems.length ),
					randEl = $( allElems[ random ] ).clone( true )[ 0 ];
				allElems.splice( random, 1 );
				return randEl;
			} );

		this.each( function( i ) {
			$( this ).replaceWith( $( shuffled[ i ] ) );
		} );

		return $( shuffled );
	};
} )( jQuery );

jQuery( function( $ ) {
	$( '.jetpack-quiz' ).each( function() {
		var quiz = $( this );
		quiz.find( 'div.jetpack-quiz-answer' ).shuffleQuiz();
		quiz
			.find( 'div[data-correct]' )
			.removeAttr( 'data-correct' )
			.data( 'correct', 1 );
		quiz.find( 'div.jetpack-quiz-answer:last' ).addClass( 'last' );
	} );

	$( 'div.jetpack-quiz' ).on( 'click', 'div.jetpack-quiz-answer', function() {
		var trackid,
			answer = $( this ),
			quiz = answer.closest( 'div.jetpack-quiz' );

		if ( quiz.data( 'a8ctraining' ) ) {
			new Image().src =
				'//pixel.wp.com/b.gif?v=wpcom-no-pv&x_trainingchaos-' +
				quiz.data( 'username' ) +
				'=' +
				quiz.data( 'a8ctraining' ) +
				'&rand=' +
				Math.random();
			quiz.data( 'a8ctraining', false );
			quiz.data( 'trackid', false );
		}

		trackid = quiz.data( 'trackid' );
		if ( answer.data( 'correct' ) ) {
			answer.addClass( 'correct' );
			if ( trackid ) {
				new Image().src =
					'//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=correct&rand=' + Math.random();
			}
		} else {
			answer.addClass( 'wrong' );
			if ( trackid ) {
				new Image().src =
					'//pixel.wp.com/b.gif?v=wpcom-no-pv&x_quiz-' + trackid + '=wrong&rand=' + Math.random();
			}
		}
		// only track the first answer
		quiz.data( 'trackid', false );
	} );
} );