Skip to content

Commit

Permalink
Entering the correct answers
Browse files Browse the repository at this point in the history
  • Loading branch information
mnbv09870987 committed Jan 9, 2020
1 parent 7766160 commit 8df54ac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/MyApp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ sub startup {
$r->get('/start')->to('bennet#registration_get') -> name( 'registration' );
$r->post('/start')->to('bennet#registration_post');

$r->get('/init')->to('bennet#vvod_pravelnyh_otvetov') ->name( 'init' );

$r->get( '/next_question' )->to( 'bennet#next_question' )->name( 'nq' );
$r->post('/next_question')->to('bennet#saving_answers') ->name( 'nq_post' );
$r->get('/miss_question')->to('bennet#saving_answers') ->name( 'nq_get' );
Expand Down
60 changes: 49 additions & 11 deletions lib/MyApp/Controller/Bennet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use warnings;
# use parent 'MyApp::Controller::User';


sub registration_get {
sub registration_get {
my $c = shift;

$c->render( "registration_list" );
Expand All @@ -29,7 +29,6 @@ sub registration_post {
return;
}


my $u = $c->model( 'User' )->create({ name => $name, age => $age });
$c->cookie( user_id => $u->id );
$c->cookie( time => time );
Expand All @@ -41,14 +40,14 @@ sub registration_post {

# use DBIx::Class::Report;
# my $qrNextQuestion = <<SQL;
# select * from answer where user_id = 0 and question <= 10 and question not in (
# select * from answer where user_id = 0 and question <= 10 and question not in (
# select question from answer where user_id = ? and answer >= 0
# ) limit 1
# SQL

use DBIx::Class::Report;
my $qrNextQuestion = <<SQL;
select * from answer where user_id = 0 and question not in (
select * from answer where user_id = 0 and question not in (
select question from answer where user_id = ? and answer >= 0
) limit 1
SQL
Expand All @@ -72,15 +71,15 @@ sub next_question {
$c->redirect_to( "finish" );
return;
}

my $report = DBIx::Class::Report->new(
schema => $c->db,
sql => $qrNextQuestion,
columns => [qw/ id question answer user_id /],
);

my $rs = $report->fetch( $uid );

if( !$rs->count ) {
$dsAnswer = $c->model( 'Answer' )->search({ user_id => $uid, answer => 0 });
$dsAnswer->update ({ answer => -1 });
Expand All @@ -95,7 +94,7 @@ sub next_question {

my $nextQuestion = $rs->first;
$c->cookie( num => $nextQuestion->question );

$c->render( "ask_question", user_id => $uid, number => $nextQuestion->question );
}

Expand Down Expand Up @@ -147,10 +146,10 @@ sub saving_answers {

sub show_results {
my( $c ) = @_;

my $uid = $c->cookie( 'user_id' ) or do{
$c->redirect_to( 'registration' );
return;
return;
};


Expand All @@ -171,7 +170,7 @@ sub output_results {

my @user = $c->model( 'User' )->all;
my @otvety = ();

for my $u ( @user ) {
my %otv = ();

Expand All @@ -186,7 +185,7 @@ sub output_results {
my $min_total = int ( $time_total / 60 );
my $sec_total = int ((( $time_total / 60 ) - $min_total) * 60);


my( $kol, $size ) = $c->total_calc( $u->id );
$otv{ name } = $u->name;
$otv{ age } = $u->age;
Expand Down Expand Up @@ -232,6 +231,45 @@ sub delete {

$c->redirect_to ( 'out' );



sub vvod_pravelnyh_otvetov {

my( $c ) = @_;

my @otvety = ( 2, 2, 1, 3, 2, 2, 3, 3, 2, 3,
2, 2, 2, 3, 2, 2, 2, 3, 2, 3,
2, 1, 3, 3, 2, 2, 1, 3, 2, 1,
3, 2, 1, 3, 1, 3, 2, 3, 1, 2,
1, 2, 2, 1, 3, 1, 1, 1, 2, 3,
2, 1, 2, 1, 1, 2, 1, 2, 2, 1,
2, 1, 3, 2, 1, 2, 3, 1, 2, 1
);

my $question = 1;
for my $answer ( @otvety ) {

my $o = $c->model( 'Answer' )->search({
question => $question,
answer => $answer,
user_id => 0,
})-> first;

if ( !$o ) {
$c->model( 'Answer' )->create({
question => $question,
answer => $answer,
user_id => 0,
});
}

$question++;
}

}



}

1;

0 comments on commit 8df54ac

Please sign in to comment.