Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* moved most of the options to a config file
  * added getopt to determine what config/test to build
  * added parsing the answers and setting the correct answer in umlessons
  * changed parsing the response id to avoid nasty regex
  • Loading branch information
schelcj committed Oct 5, 2012
1 parent bb404d7 commit a3b098c
Showing 1 changed file with 69 additions and 37 deletions.
106 changes: 69 additions & 37 deletions create.pl
@@ -1,9 +1,5 @@
#!/usr/bin/env perl #!/usr/bin/env perl


# TODO - add getopt for latex file and possbily more
# TODO - answers are in a separate file and need to be imported
# TODO - get directions and summary from template file
#
# FIXME - question 11 is a parsing issue with % signs in the question # FIXME - question 11 is a parsing issue with % signs in the question
# FIXME - questions 24,25 use images, will have to figure this out # FIXME - questions 24,25 use images, will have to figure this out
# FIXME - parsing the latex results is giving an undef in the array, no idea why though # FIXME - parsing the latex results is giving an undef in the array, no idea why though
Expand All @@ -21,43 +17,47 @@
use English qw(-no_match_vars); use English qw(-no_match_vars);
use List::MoreUtils qw(apply); use List::MoreUtils qw(apply);
use Text::Roman; use Text::Roman;
use Data::Dumper; use Config::Tiny;
use Getopt::Compact;
use URI;


Readonly::Scalar my $EMPTY => q{}; Readonly::Scalar my $EMPTY => q{};
Readonly::Scalar my $BANG => q{!}; Readonly::Scalar my $BANG => q{!};
Readonly::Scalar my $SPACE => q{ };
Readonly::Scalar my $WEBLOGIN_URL => q{https://weblogin.umich.edu}; Readonly::Scalar my $WEBLOGIN_URL => q{https://weblogin.umich.edu};
Readonly::Scalar my $COSIGN_CGI => q{cosign-bin/cosign.cgi}; Readonly::Scalar my $COSIGN_CGI => q{cosign-bin/cosign.cgi};
Readonly::Scalar my $UMLESSONS_URL => q{https://lessons.ummu.umich.edu}; Readonly::Scalar my $UMLESSONS_URL => q{https://lessons.ummu.umich.edu};
Readonly::Scalar my $UNIT_URL_NAME => q{sph_algebra_assesment}; Readonly::Scalar my $UNIT_URL_NAME => q{sph_algebra_assesment};
Readonly::Scalar my $DIRECTIONS => q{Hello World};
Readonly::Scalar my $SUMMARY => q{Goodbye World};


my $latex = $ARGV[0]; ## no tidy
my $lesson_name = q{2013}; my $opts = Getopt::Compact->new(
my $title = q{SPH Algebra Assesment for 2013}; struct => [
my $agent = get_login_agent(); [[qw(c config)], q(Config file), q(=s)],
my $parsed_ref = parse_latex($latex); [[qw(t test)], q(Test name to build), q(=s)],
]
)->opts();
## use tidy
my $config = Config::Tiny->read($opts->{config});
my $test = $config->{$opts->{test}};
my $agent = get_login_agent();
my $answer_ref = parse_answers($test->{answers});
my $parsed_ref = parse_latex($test->{test}, $answer_ref);


create_lesson($lesson_name, $title); create_lesson($test);


my $resource_id = create_resource($lesson_name); my $resource_id = create_resource($test->{lesson_name});


foreach my $question (@{$parsed_ref}) { foreach my $question (@{$parsed_ref}) {
next if not $question; next if not $question;


my $question_id = create_question($resource_id, $lesson_name, $question); my $question_id = create_question($resource_id, $test->{lesson_name}, $question);
say "Created question #$question->{number} - $question_id"; say "Created question #$question->{number} - $question_id";


add_answers($question_id, $lesson_name, $question->{answers}); add_answers($question_id, $test->{lesson_name}, $question->{correct}, $question->{answers});
my $answer_count = scalar keys %{$question->{answers}}; my $answer_count = scalar keys %{$question->{answers}};
say "Added $answer_count to question #$question->{number}"; say "Added $answer_count to question #$question->{number}";
} }


sub format_latex_for_mathjax {
my ($latex) = @_;
return sprintf(qq{<!-- html -->\n\\( %s \\)\n<!-- html -->\n}, $latex);
}

sub get_login_agent { sub get_login_agent {
my $mach = Net::Netrc->lookup('cosign.umich.edu'); my $mach = Net::Netrc->lookup('cosign.umich.edu');
my $www = WWW::Mechanize->new(); my $www = WWW::Mechanize->new();
Expand All @@ -82,11 +82,11 @@ sub get_login_agent {
} }


sub parse_latex { sub parse_latex {
my ($test) = @_; my ($file,$answers) = @_;
my @questions = (); my @questions = ();
my $question_ref = []; my $question_ref = [];
my $contents = read_file($test); my $contents = read_file($file);
my $temp_fh = File::Temp->new(); my $temp_fh = File::Temp->new();


$contents =~ s/^(?:(.*)?\\begin{document})|(?:\\end{document})$//gs; $contents =~ s/^(?:(.*)?\\begin{document})|(?:\\end{document})$//gs;


Expand All @@ -101,7 +101,8 @@ sub parse_latex {


if ($question =~ /^(\d+)/) { if ($question =~ /^(\d+)/) {
$number = $1; $number = $1;
$question_ref->[$number]->{number} = $number; $question_ref->[$number]->{number} = $number;
$question_ref->[$number]->{correct} = $answers->{$number};
} else { } else {
next; next;
} }
Expand All @@ -125,7 +126,7 @@ sub parse_latex {
when ($line =~ /^$number([A-D])\n/) { when ($line =~ /^$number([A-D])\n/) {
my $answer = $1; my $answer = $1;
$line =~ s/^${number}${answer}\n(.*)(?:(?:\s+[\\]+\s+[\n%]+)|\n+$)/$1/g; $line =~ s/^${number}${answer}\n(.*)(?:(?:\s+[\\]+\s+[\n%]+)|\n+$)/$1/g;
$question_ref->[$number]->{answers}->{$answer} = $line; $question_ref->[$number]->{answers}->{lc($answer)} = $line;
} }
default { default {
} }
Expand All @@ -137,8 +138,40 @@ sub parse_latex {
return $question_ref; return $question_ref;
} }


sub parse_answers {
my ($file) = @_;

my $answers = {};
foreach my $line (read_file($file)) {
chomp($line);
next if $line !~ /^\d+/;
my ($number, $answer) = split(/$SPACE/, $line);
$answers->{$number} = $answer;
}

return $answers;
}

sub format_latex_for_mathjax {
my ($latex) = @_;
return sprintf(qq{<!-- html -->\n\\( %s \\)\n<!-- html -->\n}, $latex);
}

sub get_response_id {
my ($url) = @_;

my $uri = URI->new($url);
my ($path, $id) = split(/\$/, $uri->path);

return $id;
}

sub create_lesson { sub create_lesson {
my ($name, $lesson_title) = @_; my ($test_conf) = @_;
my $directions = read_file($test_conf->{directions});
my $summary = read_file($test_conf->{summary});
my $name = $test_conf->{lesson_name};
my $title = $test_conf->{lesson_title};


$agent->post( $agent->post(
qq{$UMLESSONS_URL/2k/manage/lesson/setup/$UNIT_URL_NAME}, { qq{$UMLESSONS_URL/2k/manage/lesson/setup/$UNIT_URL_NAME}, {
Expand Down Expand Up @@ -169,21 +202,21 @@ sub create_lesson {
showFooter => 'TRUE', showFooter => 'TRUE',
showLinks => 'TRUE', showLinks => 'TRUE',
style => 'quiz', style => 'quiz',
title => $lesson_title, title => $title,
} }
); );


$agent->post( $agent->post(
qq{$UMLESSONS_URL/2k/manage/lesson/update_content/$UNIT_URL_NAME/$name#directions}, { qq{$UMLESSONS_URL/2k/manage/lesson/update_content/$UNIT_URL_NAME/$name#directions}, {
directionsText => $DIRECTIONS, directionsText => $directions,
op => 'save', op => 'save',
section => 'directions', section => 'directions',
} }
); );


$agent->post( $agent->post(
qq{$UMLESSONS_URL/2k/manage/lesson/update_content/$UNIT_URL_NAME/$name#summary}, { qq{$UMLESSONS_URL/2k/manage/lesson/update_content/$UNIT_URL_NAME/$name#summary}, {
summaryText => $SUMMARY, summaryText => $summary,
op => 'save', op => 'save',
section => 'summary', section => 'summary',
} }
Expand Down Expand Up @@ -226,7 +259,7 @@ sub create_resource {
} }
); );


(my $id = $agent->response->previous->header('location')) =~ s/^.*\$(.*)$/$1/g; my $id = get_response_id($agent->response->previous->header('location'));


if ($agent->success) { if ($agent->success) {
say qq{Created resource ($resource_title - $id) successfully}; say qq{Created resource ($resource_title - $id) successfully};
Expand All @@ -253,8 +286,7 @@ sub create_question {
} }
); );


(my $id = $agent->response->previous->header('location')) =~ s/^.*\$([\w]+)(?:\?.*)?$/$1/g; my $id = get_response_id($agent->response->previous->header('location'));

$agent->post( $agent->post(
qq{$UMLESSONS_URL/2k/manage/multiple_choice/update_settings/unit_4697/quiz_001\$${id}#}, { qq{$UMLESSONS_URL/2k/manage/multiple_choice/update_settings/unit_4697/quiz_001\$${id}#}, {
correctCaption => 'Correct!', correctCaption => 'Correct!',
Expand All @@ -281,7 +313,7 @@ sub create_question {
} }


sub add_answers { sub add_answers {
my ($id, $name, $answers) = @_; my ($id, $name, $correct_answer, $answers) = @_;


my $answer_number = 1; my $answer_number = 1;
foreach my $answer (sort keys %{$answers}) { foreach my $answer (sort keys %{$answers}) {
Expand All @@ -303,7 +335,7 @@ sub add_answers {
qq{order.$order} => $order, qq{order.$order} => $order,
response => format_latex_for_mathjax($answer_text), response => format_latex_for_mathjax($answer_text),
section => qq{answers.c$roman}, section => qq{answers.c$roman},
correct => 'FALSE', correct => ($answer eq $correct_answer) ? 'TRUE' : 'FALSE',
feedback => $EMPTY, feedback => $EMPTY,
'response/align' => 'LEFT', 'response/align' => 'LEFT',
'response/resource' => 'none', 'response/resource' => 'none',
Expand Down

0 comments on commit a3b098c

Please sign in to comment.