package acroweb_eng;

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&genrandom &filtruj &permutemyfield);

#### We read the rows of the file with all questions. Then name of the file 
#### is the first papameter. The number of incorrect answers for each question 
#### is in the second parameter.
#### The last parameter is flag (see the genrandom function below).
sub filtruj{
    my ($jmenosouboru,$variantspatne,$flag)=@_;
    open(HLAVA,$jmenosouboru);
    my @predfiltrem=<HLAVA>;
    push(@predfiltrem,"konec");
    my @pofiltru=();
    my @spatne=(); # tady jsou pro kazde zadani radky zacinajici minusem
    my @nabidkaodpovedi=(); # spatne odpovedi vybrane do testu
    my @nabidkaperm=();

    # nasledujici promenne jsou pouzite pri zpracovavani zadani do
    # formy citelne acrotexem
    my $spravne="";
    my $bylominus=0;
    my $pocetspatne=0; # pocet spatnych variant pro kazdou otazku
    my $radekvystup="";
    foreach $radek (@predfiltrem)
    {
	chomp($radek);
	# radek nezacina plusem ani minusem
	if (not(substr($radek,0,1) eq "-") and not(substr($radek,0,1) eq "+")) 
	{
	    if ($bylominus==1) 
	    {
		@nabidkaodpovedi=();
		if ($flag==2)
		{
		    chomp($spravne);
		    chomp($spatne[0]);
		    $spravne=~ s/^\+//;
		    $spatne[0]=~ s/^-//;
		    $radekvystup=$radekvystup."\\interval{$spatne[0]}";
		    $radekvystup=$radekvystup."\\correctanswer{$spravne}";
		    # do pole @pofiltru ukladame zpracovana zadani
		    push (@pofiltru,$radekvystup."\n\n");

		}
		else
		{
		    # nahodne vybereme spatne odpovedi
		    for ($cislo=($pocetspatne-1);$cislo>=($pocetspatne-$variantspatne);$cislo--)
		    {
			$randcislo = int(rand($cislo+1));
			push(@nabidkaodpovedi,$spatne[$randcislo]);
			splice(@spatne,$randcislo,1);
		    }
		    
		    # pridame spravnou odpoved
		    push(@nabidkaodpovedi,$spravne);
		    
		    # promichame @nabidkaodpovedi a ulozime do @nabidkaperm
		    for ($cislo=$variantspatne;$cislo>=0;$cislo--)
		    {
			$randcislo = int(rand($cislo+1));
			push(@nabidkaperm,$nabidkaodpovedi[$randcislo]);
			splice(@nabidkaodpovedi,$randcislo,1);
		    }
		    
		    
		    if ($flag==1)
		    {
			# zmenime zneni posledni odpovedi, spravnost nechame
			$plusminus=substr($nabidkaperm[$variantspatne],0,1);
			$nabidkaperm[$variantspatne]=$plusminus."another answer";
		    }
		    $cislo=0;
		    
		    # zmenime tak, aby to proslo v AcroTeXu
		    for ($cislo=0; $cislo<=($variantspatne); $cislo++)
		    {
			$odpoved=$nabidkaperm[$cislo];
			$odpoved=~ s/^\+/\\Ans1 /;
			$odpoved=~ s/^-/\\Ans0 /;
			if (not($cislo==$variantspatne))
			{
			    # oddelovac variant odpovedi
			    chomp $odpoved;
			    $radekvystup=$radekvystup.$odpoved."\\\\[5pt]\n";
			}
			else
			{
			    # konec nabizenych odpovedi
			    $radekvystup=$radekvystup.$odpoved;
			}
		    }
		    # do pole @pofiltru ukladame zpracovana zadani
		    push (@pofiltru,"$radekvystup\\end{answers}\n\n\n");
		}


		# nova inicializace promennych pro cteni dalsiho zadani
		$radekvystup="";
		$pocetspatne=0;
		$spravne="";
		@spatne=();
		@nabidkaperm=();
		$bylominus=0;
	    }
	    if ($radek eq "")
	    {
		$radekvystup=$radekvystup.$radek;
	    }
	    else
	    {
		$radekvystup=$radekvystup."\\problem{".$radek."}\n";
		if ($flag != 2)
		{
		    $radekvystup=$radekvystup."\\begin{answers}{1}\n";
		}
	    }
	}
	else
	{
	    if (substr($radek,0,1) eq "-")
	    {
		$bylominus=1;
		$pocetspatne=$pocetspatne+1;
		push(@spatne,$radek."\n");
	    }
	    else
	    {
		$spravne=$radek."\n";
	    }
	}
    }


return(@pofiltru)
}



sub genrandom
# Parameters: 
# ----------
# 1. filename, 
# 2. the number of questions in the test
# 3. the number of incorrect answers for each question
# 4. flag: 0 = keep answers  
#          1 = replace the last answer with words "another question"
#          2 = the questions are fill-in questions
{
    my ($jmeno,$pocetzadani,$variantspatne,$flag)=@_;
    my @hotovezadani;
    my @ktisku;

# We read all questions from the file, the answers are already randomized
    @ktisku=filtruj($jmeno,$variantspatne,$flag);
    
# We find the number of questions in the file (database)
    my $celkemzadani=@ktisku;

# We select random questions and put to the field @hotovezadani
    for ($cislo=$celkemzadani;$cislo>($celkemzadani-$pocetzadani);$cislo--)
    {
	$randcislo = int(rand($cislo));
	push(@hotovezadani, $ktisku[$randcislo]);
	splice(@ktisku,$randcislo,1);
    }

# We return the result stored in @hotovezadani
    return @hotovezadani;
}

sub permutemyfield
{
# reads a field and permutes this field randomly
    my @initfield=@_;
    my $NumberOfProblems=@initfield;
    my @permutedfield=();

    for ($cislo=$NumberOfProblems;$cislo>0;$cislo--)
    {
	$randcislo = int(rand($cislo));
	push(@permutedfield, $initfield[$randcislo]);
	splice(@initfield,$randcislo,1);
    }
    
    return @permutedfield;
}

1;

