| 1 | #!perl | ||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | # PODNAME: mcp | ||||||||||
| 6 | # ABSTRACT: pick multiple choice questions randomly | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | binmode STDOUT, ":utf8"; | |||||||||
| 10 | 4 | ▶ | use YAML::Tiny 'LoadFile'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use HTML::Entities; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use Getopt::Args; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 143 | ▶ | sub say { print "@_\n" } # say for perl below 5.10 | ||||||||
|
Subroutine
say: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | arg quantity => ( | |||||||||
| 17 | isa => 'Int', | ||||||||||
| 18 | required => 1, | ||||||||||
| 19 | comment => 'Number of questions to pic', | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 4 | arg file => ( | |||||||||
| 23 | isa => 'Str', | ||||||||||
| 24 | required => 1, | ||||||||||
| 25 | comment => 'Path to YAML file, containg multiple choice questions', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 4 | my $o = optargs; | |||||||||
| 29 | |||||||||||
| 30 | 4 | my @doc = LoadFile( $o->{file} ); | |||||||||
| 31 | 4 | my @q = @{$doc[0]}; | |||||||||
| 32 | |||||||||||
| 33 | 4 | my $question_count = scalar @q; | |||||||||
| 34 | |||||||||||
| 35 | # Choose questions randomly | ||||||||||
| 36 | |||||||||||
| 37 | 4 | my $index; | |||||||||
| 38 | 4 | my @choosen = (); | |||||||||
| 39 | |||||||||||
| 40 | 4 | for (1..$o->{quantity}) { | |||||||||
| 41 | 4 | $index = int(rand(scalar @q)); | |||||||||
| 42 | 4 | my $question = $q[$index]; | |||||||||
| 43 | |||||||||||
| 44 | # if multiple subquestions, choose subquestion and override | ||||||||||
| 45 | 4 | ▶ | if (ref($question) eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | my $index = int(rand(scalar @{$question})); | |||||||||
| 47 | 2 | $question = $question->[$index]; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 4 | push @choosen, $question; | |||||||||
| 51 | #say $question->{q}; | ||||||||||
| 52 | |||||||||||
| 53 | # delete index from array | ||||||||||
| 54 | 4 | splice(@q, $index, 1); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | # print choosen questions | ||||||||||
| 58 | |||||||||||
| 59 | 4 | say '<!DOCTYPE html>'; | |||||||||
| 60 | 4 | say '<html>'; | |||||||||
| 61 | 4 | say '<head>'; | |||||||||
| 62 | 4 | say '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">'; | |||||||||
| 63 | 4 | say '<style>td { text-align: center; vertical-align: middle; }</style>'; | |||||||||
| 64 | 4 | say '<title>mc survey</title>'; | |||||||||
| 65 | 4 | say '</head>'; | |||||||||
| 66 | 4 | say '<body>'; | |||||||||
| 67 | 4 | say '<h1>Questions</h1>'; | |||||||||
| 68 | 4 | say $o->{quantity} . " randomly picked questions out of $question_count."; | |||||||||
| 69 | 4 | say '<table border="1">'; | |||||||||
| 70 | 4 | say "<tr><th>Answer</th><th>Claim</th></tr>"; | |||||||||
| 71 | |||||||||||
| 72 | 4 | my $difficulty_sum = 0; | |||||||||
| 73 | 4 | my $difficulty_avg = 0; | |||||||||
| 74 | 4 | my $themen_count = {}; | |||||||||
| 75 | |||||||||||
| 76 | 4 | for my $question (@choosen) { | |||||||||
| 77 | |||||||||||
| 78 | 4 | $difficulty_sum += $question->{difficulty}; | |||||||||
| 79 | 4 | ▶ | if (exists $themen_count->{$question->{chapter}} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | $themen_count->{$question->{chapter}}++; | |||||||||
| 81 | } | ||||||||||
| 82 | else { | ||||||||||
| 83 | 4 | $themen_count->{$question->{chapter}} = 1; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 4 | say '<tr>'; | |||||||||
| 87 | 4 | say '<td>'; | |||||||||
| 88 | 4 | ▶ | if (ref($question->{answer}) eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | ▶ | while (my ($answer, $bool) = each(%{$question->{answer}})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 4 | ▶ | say "\x{2610} $answer" if $bool =~ /false/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 4 | ▶ | say "\x{2611} $answer" if $bool =~ /true/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | else { | ||||||||||
| 95 | 3 | ▶ | say "\x{2610} true \x{2611} false" if $question->{answer} =~ /false/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 3 | ▶ | say "\x{2611} true \x{2610} false" if $question->{answer} =~ /true/i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | } | ||||||||||
| 98 | 4 | say "\x{2610} don't know"; | |||||||||
| 99 | 4 | say '</td>'; | |||||||||
| 100 | 4 | say '<td>'; | |||||||||
| 101 | 4 | say encode_entities($question->{q}); | |||||||||
| 102 | 4 | say '</td>'; | |||||||||
| 103 | 4 | say '</tr>'; | |||||||||
| 104 | |||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 4 | $difficulty_avg = $difficulty_sum / int scalar @choosen; | |||||||||
| 108 | |||||||||||
| 109 | 4 | say '</table>'; | |||||||||
| 110 | |||||||||||
| 111 | 4 | say '<h1>Statistics</h1>'; | |||||||||
| 112 | |||||||||||
| 113 | 4 | say '<p>'; | |||||||||
| 114 | 4 | say "Average Difficulty $difficulty_avg (1=simplest; 6=most difficult)"; | |||||||||
| 115 | 4 | say '</p>'; | |||||||||
| 116 | 4 | say '<p>'; | |||||||||
| 117 | 4 | say 'Occurence of Chapters<br />'; | |||||||||
| 118 | |||||||||||
| 119 | 4 | for my $chap (keys %{$themen_count}) { | |||||||||
| 120 | 4 | say "Chapter $chap"; | |||||||||
| 121 | 4 | say ': '; | |||||||||
| 122 | 4 | say $themen_count->{$chap}; | |||||||||
| 123 | 4 | say '<br />'; | |||||||||
| 124 | } | ||||||||||
| 125 | 4 | say '</p>'; | |||||||||
| 126 | |||||||||||
| 127 | 4 | say '</body>'; | |||||||||
| 128 | 4 | say '</html>'; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||