#!/usr/bin/perl

# Generate questions for the Moodle system. Accelts question numbers
# as command line arguments, and generates random values for the
# question text.

# USAGE:
#   $0 2 3 4 5 6

use strict;
use warnings;

@ARGV = (0) unless @ARGV;

for my $NR (@ARGV) {

    my $span = 200.0; # span of the coordinates in Angstroems

    my $X1 = sprintf ("%8.3f", rand($span) - $span/2);
    my $Y1 = sprintf ("%8.3f", rand($span) - $span/2);
    my $Z1 = sprintf ("%8.3f", rand($span) - $span/2);    

    my $X2 = sprintf ("%8.3f", rand($span) - $span/2);
    my $Y2 = sprintf ("%8.3f", rand($span) - $span/2);
    my $Z2 = sprintf ("%8.3f", rand($span) - $span/2);

    my $q1 = rand(5.0);
    my $Q1 = sprintf ("%4.2f", $q1 > 1 ? 1.0 : $q1);

    my $q2 = rand(5.0);
    my $Q2 = sprintf ("%4.2f", $q2 > 1 ? 1.0 : $q2);
    
    my $B1 = sprintf ("%6.2f", rand(150.0));
    my $B2 = sprintf ("%6.2f", rand(150.0));

    # random numbers of atoms:
    my $A1 = sprintf ("%5d", int(rand(99998)) + 1);
    my $A2 = sprintf ("%5d", $A1 + 1 + int(rand(3)));

    # random residue numbers:
    my $R1 = sprintf ("%4d", int(rand(9998)) + 1);
    my $R2 = sprintf ("%4d", $R1);

    # Generate a random PDB atom:    
    my $PDB_ATOM_LINE_1 = "ATOM  ${A1}  C   ALA A${R1}    ${X1}${Y1}${Z1}  ${Q1}${B1}           C";
    my $PDB_ATOM_LINE_2 = "ATOM  ${A2}  O   ALA A${R2}    ${X2}${Y2}${Z2}  ${Q2}${B2}           O";
    
    my ($x1, $y1, $z1) = (substr($PDB_ATOM_LINE_1, 30, 8), substr($PDB_ATOM_LINE_1, 38, 8), substr($PDB_ATOM_LINE_1, 46, 8));
    my ($x2, $y2, $z2) = (substr($PDB_ATOM_LINE_2, 30, 8), substr($PDB_ATOM_LINE_2, 38, 8), substr($PDB_ATOM_LINE_2, 46, 8));

    my $distance = sqrt (($x1 - $x2)**2 + ($y1 - $y2)**2 + ($z1 - $z2)**2);

    my $ANSWER_3_DIGITS = sprintf "%.3f", $distance;
    my $ANSWER_2_DIGITS = sprintf "%.2f", $distance;

my $template = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 1027817  -->
  <question type="shortanswer">
    <name>
      <text>Atstumas tarp PDB atomų / distance between PDB atoms (${NR})</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p dir="ltr" style="text-align: left;">What is the distance in Ångstrøms between atoms described in the PDB format below? Disregard the B-factor, use only atomic coordinates.<br>Provide the answer with just the significant digits; use decimal point as a separator, e.g.: <strong>9.999</strong><br></p><p dir="ltr" style="text-align: left;">Koks yra atstumas Angstremais tarp dviejų atomų nurodytų PDB formatų (žr. apačioje)? B faktoriaus nepaisykite.<br>Pateikite atsakymą, kuriame nurodyti tik reikšminiai skaitmenys. Naudokite tašką kaip dešimtainės trupmenos skirtuką, pvz. <strong>9.99</strong><br></p><pre>${PDB_ATOM_LINE_1}<br>${PDB_ATOM_LINE_2}</pre><p dir="ltr" style="text-align: left;"><br></p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1.0000000</defaultgrade>
    <penalty>0.3333333</penalty>
    <hidden>0</hidden>
    <idnumber></idnumber>
    <usecase>0</usecase>
    <answer fraction="100" format="moodle_auto_format">
      <text>${ANSWER_3_DIGITS}</text>
      <feedback format="html">
        <text></text>
      </feedback>
    </answer>
    <answer fraction="100" format="moodle_auto_format">
      <text>${ANSWER_2_DIGITS}</text>
      <feedback format="html">
        <text></text>
      </feedback>
    </answer>
  </question>

</quiz>
EOF

    print $template;
}
