Added template code for showing rank.
[ccbs] / html / do-edit-scores.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 my $dbh = ccbs::db_connect();
8 my $cgi = new CGI;
9
10 my $tournament = $cgi->param('tournament');
11 my $round = $cgi->param('round');
12 my $group = $cgi->param('group');
13
14 $dbh->{AutoCommit} = 0;
15
16 my %checked_songs = ();
17
18 # Loop through all parameters and see what parameters differ between old- and current
19 # versions
20 for my $p ($cgi->param()) {
21         next if ($p =~ /^old-(.*?)$/);
22         next unless (defined($cgi->param('old-' . $p)));
23         next if ($cgi->param($p) eq $cgi->param('old-' . $p));
24
25         my $val = $cgi->param($p);
26         undef $val if ($val =~ /^\s*$/);
27
28         # _Before_ we do changes to the database, check if this is possibly something
29         # that will trigger a foreign key error. There will be an error if the tuple
30         # (song,playmode,difficulty) is set (ie. all members are set) and the combination
31         # doesn't exist in songratings, so check that. However, only check once per
32         # song (or else we'll be doing lots of unneeded database calls).
33         if ($p =~ /^(?:playmode|difficulty|song)(\d+)-(\d+)$/ && !defined($checked_songs{"$1-$2"})) {
34                 # check that the entire triple is set
35                 if (defined($cgi->param("playmode$1-$2"))   && $cgi->param("playmode$1-$2") !~ /^\s*$/ &&
36                     defined($cgi->param("difficulty$1-$2")) && $cgi->param("difficulty$1-$2") !~ /^\s*$/ &&
37                     defined($cgi->param("song$1-$2"))       && $cgi->param("song$1-$2") !~ /^\s*$/) {
38                         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_songs FROM songratings WHERE song=? AND playmode=? AND difficulty=?',
39                                 undef, $cgi->param("song$1-$2"), $cgi->param("playmode$1-$2"),
40                                 $cgi->param("difficulty$1-$2"));
41                         if ($ref->{'num_songs'} != 1) {
42                                 # Fetch the name of the song if we can
43                                 $ref = $dbh->selectrow_hashref('SELECT title FROM songs WHERE song=?',
44                                         undef, $cgi->param("song$1-$2"));
45                                 ccbs::user_error("Sangen $ref->{'title'} har ikke steps for " .
46                                         $cgi->param("playmode$1-$2") . " " . $cgi->param("difficulty$1-$2") . ".");
47                         }
48                         $checked_songs{"$1-$2"} = 1;
49                 }
50         }
51
52         if ($p =~ /^score(\d+)-(\d+)/) {
53                 if (defined($val) && ($val < 0 || $val > 10000)) {
54                         ccbs::user_error("Alle poengsummer må være mellom 0 og 10000 (inklusive).");
55                 }
56         
57                 $dbh->do('UPDATE scores SET score=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
58                         $val, $tournament, $round, $group, $1, $2);
59         } elsif ($p =~ /^playmode(\d+)-(\d+)/) {
60                 $dbh->do('UPDATE scores SET playmode=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
61                         $val, $tournament, $round, $group, $1, $2);
62         } elsif ($p =~ /^difficulty(\d+)-(\d+)/) {
63                 $dbh->do('UPDATE scores SET difficulty=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
64                         $val, $tournament, $round, $group, $1, $2);
65         } elsif ($p =~ /^song(\d+)-(\d+)/) {
66                 $dbh->do('UPDATE scores SET song=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=? AND chosen=\'t\'', undef,
67                         $val, $tournament, $round, $group, $1, $2);
68         }
69 }
70
71 $dbh->commit;
72 $dbh->disconnect;
73
74 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
75