+ # _Before_ we do changes to the database, check if this is possibly something
+ # that will trigger a foreign key error. There will be an error if the tuple
+ # (song,playmode,difficulty) is set (ie. all members are set) and the combination
+ # doesn't exist in songratings, so check that. However, only check once per
+ # song (or else we'll be doing lots of unneeded database calls).
+ if ($p =~ /^(?:playmode|difficulty|song)(\d+)-(\d+)$/ && !defined($checked_songs{"$1-$2"})) {
+ # check that the entire triple is set
+ if (defined($cgi->param("playmode$1-$2")) && $cgi->param("playmode$1-$2") !~ /^\s*$/ &&
+ defined($cgi->param("difficulty$1-$2")) && $cgi->param("difficulty$1-$2") !~ /^\s*$/ &&
+ defined($cgi->param("song$1-$2")) && $cgi->param("song$1-$2") !~ /^\s*$/) {
+ my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_songs FROM songratings WHERE song=? AND playmode=? AND difficulty=? AND machine=?',
+ undef, $cgi->param("song$1-$2"), $cgi->param("playmode$1-$2"),
+ $cgi->param("difficulty$1-$2"), $tournamentdata->{'machine'});
+ if ($ref->{'num_songs'} != 1) {
+ # Fetch the name of the song if we can
+ $ref = $dbh->selectrow_hashref('SELECT title FROM songs WHERE song=?',
+ undef, $cgi->param("song$1-$2"));
+ ccbs::user_error(_("The song") . $ref->{'title'} _("doesn't have steps for ") .
+ $cgi->param("playmode$1-$2") . " " . $cgi->param("difficulty$1-$2") . _(" on ").
+ $tournamentdata->{'machinename'} . ".");
+ }
+ $checked_songs{"$1-$2"} = 1;
+ }
+ }
+