| 1 |
import { select } from '@wordpress/data'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import { default as formatDuration } from '../duration'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Displays list of all attempt from a quiz. |
| 7 |
*/ |
| 8 |
const Attempts = () => { |
| 9 |
const attempts = select( 'learnpress/quiz' ).getData( 'attempts' ) || []; |
| 10 |
|
| 11 |
const hasAttempts = attempts && !! attempts.length; |
| 12 |
return ( |
| 13 |
! hasAttempts ? false : <> |
| 14 |
<div className="quiz-attempts"> |
| 15 |
<h4 className="attempts-heading">{ __( 'Last Attempt', 'learnpress' ) }</h4> |
| 16 |
|
| 17 |
{ hasAttempts && ( |
| 18 |
<table> |
| 19 |
<thead> |
| 20 |
<tr> |
| 21 |
<th className="quiz-attempts__questions">{ __( 'Questions', 'learnpress' ) }</th> |
| 22 |
<th className="quiz-attempts__spend">{ __( 'Time spent', 'learnpress' ) }</th> |
| 23 |
<th className="quiz-attempts__marks">{ __( 'Marks', 'learnpress' ) }</th> |
| 24 |
<th className="quiz-attempts__grade">{ __( 'Passing grade', 'learnpress' ) }</th> |
| 25 |
<th className="quiz-attempts__result">{ __( 'Result', 'learnpress' ) }</th> |
| 26 |
</tr> |
| 27 |
</thead> |
| 28 |
<tbody> |
| 29 |
{ attempts.map( ( row, key ) => { |
| 30 |
// Re-write value to attempts.timeSpend |
| 31 |
/*if ( lpQuizSettings.checkNorequizenroll === 1 ) { |
| 32 |
const timespendStart = window.localStorage.getItem( 'quiz_start_' + lpQuizSettings.id ), |
| 33 |
timespendEnd = window.localStorage.getItem( 'quiz_end_' + lpQuizSettings.id ); |
| 34 |
if ( timespendStart && timespendEnd ) { |
| 35 |
row.timeSpend = timeDifference( timespendStart, timespendEnd ).duration; |
| 36 |
} |
| 37 |
}*/ |
| 38 |
return ( |
| 39 |
<tr key={ `attempt-${ key }` }> |
| 40 |
<td className="quiz-attempts__questions">{ `${ row.questionCorrect } / ${ row.questionCount }` }</td> |
| 41 |
<td className="quiz-attempts__spend">{ row.timeSpend || '--' }</td> |
| 42 |
<td className="quiz-attempts__marks">{ `${ row.userMark } / ${ row.mark }` }</td> |
| 43 |
<td className="quiz-attempts__grade">{ row.passingGrade || '-' }</td> |
| 44 |
<td className="quiz-attempts__result">{ `${ parseFloat( row.result ).toFixed( 2 ) }%` } <span>{ row.graduationText }</span></td> |
| 45 |
</tr> |
| 46 |
); |
| 47 |
} ) } |
| 48 |
</tbody> |
| 49 |
</table> |
| 50 |
) } |
| 51 |
</div> |
| 52 |
</> |
| 53 |
); |
| 54 |
}; |
| 55 |
function timeDifference( earlierDate, laterDate ) { |
| 56 |
const oDiff = new Object(); |
| 57 |
|
| 58 |
// Calculate Differences |
| 59 |
// ------------------------------------------------------------------- // |
| 60 |
let nTotalDiff = laterDate - earlierDate; |
| 61 |
|
| 62 |
oDiff.days = Math.floor( nTotalDiff / 1000 / 60 / 60 / 24 ); |
| 63 |
nTotalDiff -= oDiff.days * 1000 * 60 * 60 * 24; |
| 64 |
|
| 65 |
oDiff.hours = Math.floor( nTotalDiff / 1000 / 60 / 60 ); |
| 66 |
nTotalDiff -= oDiff.hours * 1000 * 60 * 60; |
| 67 |
|
| 68 |
oDiff.minutes = Math.floor( nTotalDiff / 1000 / 60 ); |
| 69 |
nTotalDiff -= oDiff.minutes * 1000 * 60; |
| 70 |
|
| 71 |
oDiff.seconds = Math.floor( nTotalDiff / 1000 ); |
| 72 |
// ------------------------------------------------------------------- // |
| 73 |
|
| 74 |
// Format Duration |
| 75 |
// ------------------------------------------------------------------- // |
| 76 |
// Format Hours |
| 77 |
let hourtext = '00'; |
| 78 |
if ( oDiff.days > 0 ) { |
| 79 |
hourtext = String( oDiff.days ); |
| 80 |
} |
| 81 |
if ( hourtext.length == 1 ) { |
| 82 |
hourtext = '0' + hourtext; |
| 83 |
} |
| 84 |
|
| 85 |
// Format Minutes |
| 86 |
let mintext = '00'; |
| 87 |
if ( oDiff.minutes > 0 ) { |
| 88 |
mintext = String( oDiff.minutes ); |
| 89 |
} |
| 90 |
if ( mintext.length == 1 ) { |
| 91 |
mintext = '0' + mintext; |
| 92 |
} |
| 93 |
|
| 94 |
// Format Seconds |
| 95 |
let sectext = '00'; |
| 96 |
if ( oDiff.seconds > 0 ) { |
| 97 |
sectext = String( oDiff.seconds ); |
| 98 |
} |
| 99 |
if ( sectext.length == 1 ) { |
| 100 |
sectext = '0' + sectext; |
| 101 |
} |
| 102 |
// Set Duration |
| 103 |
const sDuration = hourtext + ':' + mintext + ':' + sectext; |
| 104 |
oDiff.duration = sDuration; |
| 105 |
// ------------------------------------------------------------------- // |
| 106 |
|
| 107 |
return oDiff; |
| 108 |
} |
| 109 |
export default Attempts; |
| 110 |
|