ONO::FW::Edu::Apps::Modules::Convert

package ONO::FW::Edu::Apps::Modules::Convert;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2022-2026 Jos Kirps and The Joopita Project [ASBL]
# Copyright (C) 2011-2022 Jos Kirps and Joopita Research ASBL
# Copyright (C) 2006-2011 Jos Kirps and The Joopita Project
# Copyright (C) 2001-2006 Jos Kirps and The WobbleWolf Project
#
# This file, as well as many other parts of the ONO Software Project or
# related elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

#: This module is a front end application module wrapper that has been
#: created for the Morzino Project (morzino.com) and that has been released
#: as Open Source software.
#:
#: Note that this is NOT a self-contained application, it is only a wrapper
#: that makes heavy use of the ONO Perl Framework application engine.

use strict;

use ONO::FW::Edu::Apps::ToolBox;
use ONO::FW::Edu::Apps::Modules::WorksheetMaker::Input;

use ONO::FW::Apps::ToolBox;
use ONO::FW::Apps::Info;

use ONO::Lib::PDF::Draw;
use ONO::Lib::PDF::ToolBox;

use ONO::FW::Apps::Core;
use ONO::FW::Apps::Core::Graphics;

use ONO::Lib::Parser;
use ONO::FW::Apps::PDF;
use ONO::IO;
use ONO::Lib::UI::Check;
use ONO::Lib::UI::Radio;

sub learn {

my (
$self,
$option,
$vars_ref,
$HTTP,
$BASE,
) = @_;

################################################################
# INIT - BEGIN
################################################################

my (
$db,
$community,
$action_url,
$form,
$format,
$canvas,
$pdfkey,
$sec,$min,$hour,
$mday,$mon,$year,
$wday,$yday,$timestamp,
$lang,
$BLK_ref,
$vars_ref,
) = ONO::FW::Apps::Core->init($vars_ref);

my %vars = %$vars_ref;
my %BLK = %$BLK_ref;

my $app = "convert";

ONO::FW::Apps::Core->now("",$app,$vars{'app_lang'});

my ($num_comma,$num_delimiter) = ONO::FW::Apps::Core->local_comma_delimiter(ONO::FW::Apps::Core->detect_local($vars{'app_lang'}),$vars_ref);

my $WEB;

my @txt = (
"Length",
"Area",
"Volume",
"Place value chart",
"Place value charts",
"allow comma numbers",

"Weight",
"Liquid",
"Liquid",
);

my @opts1 = &units_le();
my @opts2 = &units_ar();
my @opts3 = &units_vo();
my @opts4 = &units_we();
my @opts5 = &units_li();

if ($vars{'app_lang'} eq "de") {
@txt = (
"Länge",
"Fläche",
"Volumen",
"Stellentafel",
"Stellentafeln",
"auch Kommazahlen",

"Gewicht",
"Flüssigkeiten",
"Flüssig",
);
}

if ($vars{'app_lang'} eq "lu") {
@txt = (
"Längt",
"Fläch",
"Volume",
"Stellentafel",
"Stellentafelen",
"och Kommazahlen",

"Gewiicht",
"Flëssegkeeten",
"Flësseg",
);
}

if ($vars{'app_lang'} eq "fr") {

@txt = (
"Longueur",
"Surface",
"Volume",
"Tableau des positions",
"Tableaux des positions",
"nombres virgules",

"poids",
"liquide",
"liquide",
);


}

$vars{'app_input_print_options_title_default'} = $BLK{'Convert'};

$WEB .= ONO::FW::Apps::Core->output_paper_save($pdfkey,$BLK_ref,\%vars,$HTTP,$BASE,"$year$mon$mday");

if (ONO::FW::Apps::Core->running(\%vars,$db) && !$vars{'table_generator'}) {

$WEB .= ONO::FW::Apps::Core->blockbackbutton($BASE,$vars_ref);

#############################################################
# WORKSHEET MAKER
#############################################################

if ($vars{'output_worksheet_maker'}) {

$vars_ref = ONO::FW::Edu::Apps::Modules::WorksheetMaker::Input->vars(\%vars);
%vars = %$vars_ref;

}

#############################################################
# ONLINE TRAINING
#############################################################

if ($vars{'output_web'} || $vars{'output_paper'}) {

my $question_number = ONO::FW::Apps::Core->question_number($vars{'output_web_question_number'});

my (
$focus_on,
$page,$pages,%cache,
$whilecounter,$stats_switches,
$PDF_OPTS,
$stat_correct,$stat_wrong,$stat_todo,$stat_todo_total,
);

my $questions_per_page = 1;

if ($vars{'output_paper'}) {

$PDF_OPTS = ONO::FW::Apps::Core->headers_options($BLK_ref,$vars_ref);

ONO::FW::Apps::PDF->new("$BASE/pdf/pdf-$pdfkey.pdf","",$format);

$vars{'app_input_multiple_questions_per_page'}++;
$questions_per_page = 40;

}

# catch bad input data (step 1 of 3)

# if (!$vars{'app_input_convdata_0_0'} && !$vars{'app_input_convdata_1_0'} && !$vars{'app_input_convdata_2_0'} && !$vars{'app_input_convdata_3_0'}) {
# $vars{'app_input_convdata_0_0'}++;
# }
my @test;
for (my $t = 0; $t < 5; $t++) {
for (my $u = 1; $u < 8; $u++) {
if ($vars{"app_input_convdata_${t}_${u}"}) {
$test[$t]++;
$vars{"app_input_convdata_${t}_0"}++;
}
}
if ($vars{"app_input_convdata_${t}_0"} && !$test[$t]) {
if ($t == 3) {
$vars{"app_input_convdata_${t}_4"}++;
$vars{"app_input_convdata_${t}_5"}++;
$vars{"app_input_convdata_${t}_6"}++;
} else {
$vars{"app_input_convdata_${t}_2"}++;
$vars{"app_input_convdata_${t}_3"}++;
$vars{"app_input_convdata_${t}_4"}++;
}
}
}

# run ...

foreach my $copy (ONO::FW::Apps::Core->unique_copies($vars{'app_input_print_options_unique_copies'})) {

my (@num,@res,@unit_1_disp,@unit_2_disp);

my ($pdf_x,$pdf_y) = (0,8);
my @corr_cache;

if ($vars{'output_paper'}) {

if ($copy > 1) {
ONO::FW::Apps::PDF->page("",$format);
}
ONO::FW::Apps::Core->headers(\%vars,$format);
ONO::FW::Apps::PDF->title($vars{'app_input_print_options_title'},$PDF_OPTS,$vars{'app_input_print_options_code'},$format);
ONO::FW::Apps::Core->unique_copies_info($canvas,$copy,$vars_ref);
ONO::FW::Apps::Core::Graphics->pdf_draw_item_numbers($canvas,-2,0,1,4,"HV",$vars_ref);

# ONO::Lib::PDF::ToolBox->canvas_test($canvas);

}

if ($vars{'output_web'}) {

$WEB .= qq~<$form>~;

if ($vars{'app_input_multiple_questions_per_page'}) {

($stats_switches,$questions_per_page,$page,$pages) = ONO::FW::Apps::Core->multi_page_logic($page,\%vars);

$WEB .= ONO::FW::Apps::Core->stats("",$BLK_ref,"ctm$stats_switches");

$WEB .= ONO::FW::Apps::Core->multi_frame_start($vars{'output_web_finish'});

} else {

$WEB .= ONO::FW::Apps::Core->question_progress($vars{'app_input_number_of_questions'},$question_number);

$WEB .= ONO::FW::Apps::Core->stats("",$BLK_ref,"ctmW$stats_switches");

}

}

if (ONO::FW::Apps::Core->question_progress_done($vars{'app_input_number_of_questions'},$question_number) || $vars{'output_web_finish'}) {

$vars{'output_progress_done'}++;

$WEB .= ONO::FW::Apps::Core->question_progress_done_msg($app,$db,$community,$form,$action_url,$vars{'app_input_number_of_questions'},$vars{'output_web_mistakes'},$lang,$BLK_ref,$vars_ref);

} else {

$WEB .= ONO::FW::Apps::Core->timer($vars{'output_web'},$vars{'output_web_timer_start'});

my ($types,@types_available,@units,@units_available);

for (my $t = 0; $t < 5; $t++) {

# has type been selected ?

if ($vars{"app_input_convdata_${t}_0"}) {

# are there at least 2 units seleted ?

my $units2;
for (my $u = 1; $u < 8; $u++) {
if ($vars{"app_input_convdata_${t}_$u"}) {
$units2++;
$units_available[$t] .= "$u:";
}
}

if ($units2 > 1) {
$types_available[$types] = $t;
$units[$t] = $units2;
$types++;
}

}

}

if (!$types) {
$types = 1;
}

for (my $i = 0; $i < $questions_per_page; $i++) {

if ($vars{'output_web_rescramble'}) {
$vars{"output_web_cache_num_$i"} = "";
$vars{"output_web_cache_res_$i"} = "";
$vars{"output_web_cache_unit1_$i"} = "";
$vars{"output_web_cache_unit2_$i"} = "";
}

# CONVERT CUSTOM NUMBER GENERATOR - START

# do we have a cache ? (form already submitted)

if ($vars{"output_web_cache_num_$i"} && $vars{"output_web_cache_res_$i"} && $vars{"output_web_cache_unit1_$i"} && $vars{"output_web_cache_unit2_$i"}) {

$num[$i] = $vars{"output_web_cache_num_$i"};
$res[$i] = $vars{"output_web_cache_res_$i"};

$unit_1_disp[$i] = $vars{"output_web_cache_unit1_$i"};
$unit_2_disp[$i] = $vars{"output_web_cache_unit2_$i"};

} else {

# first we need to select a type (length, area or volume)

my $type = int(rand($types));
my $type_sel = $types_available[$type];
if (!$type_sel) {
$type_sel = 0;
}

# $WEB.="X - $units_available[$type_sel]<br>";

my @up = split(/:/,$units_available[$type_sel]);
my $num = @up;

my $unit_1 = $up[int(rand($num))];
my $unit_2 = $up[int(rand($num))];

# catch bad input data (step 2 of 3)

if (!$unit_1) {
$unit_1 = 1+int(rand(3));
}
if (!$unit_2) {
$unit_2 = 1+int(rand(3));
}

# randomizer (if units are the same -> try something else)

for (my $x = 0; $x < 10; $x++) {
if ($unit_1 == $unit_2) {
$unit_2 = $up[int(rand($num))];
}
}

# catch bad input data (step 3 of 3)

if (!$unit_2) {
$unit_2 = $unit_1 + 1;
}

# unit differences cannot be too big (avoiding very large numbers)

my $unit_diff = abs($unit_1 - $unit_2);

if ($type_sel > 2 && $unit_diff > 2) {
if ($unit_1 > $unit_2) {
$unit_1 = $unit_1 - ($unit_diff-1);
} else {
$unit_1 = $unit_1 + ($unit_diff-1);
}
}

if ($type_sel == 2 && $unit_diff > 3) {
if ($unit_1 > $unit_2) {
$unit_1 = $unit_1 - ($unit_diff-2);
} else {
$unit_1 = $unit_1 + ($unit_diff-2);
}

}

# continue...

my $unit_offset;

my @opts = ('',@opts1);
if ($type_sel == 1) {
@opts = ('',@opts2);
}
if ($type_sel == 2) {
@opts = ('',@opts3);
}
if ($type_sel == 3) {
@opts = ('',@opts4);
$unit_offset = 3;
}
if ($type_sel == 4) {
@opts = ('',@opts5);
}

$unit_1_disp[$i] = $opts[$unit_1-$unit_offset];
$unit_2_disp[$i] = $opts[$unit_2-$unit_offset];

# 0 = mm / 3 = m / 6 = km

my $lowest = $unit_1;
my $highest = $unit_2;

if ($lowest > $highest) {
$lowest = $unit_2;
$highest = $unit_1;
}

my $type_range = $type_sel;
if ($type_range > 2) {
$type_range = 2;
}
if ($type_sel == 4) {
$type_range = 0;
}

my $range = ($highest - $lowest) * ($type_range+1);
my $divider = 10;
for (my $d = 1; $d < $range; $d++) {
$divider = $divider * 10;
}

$num[$i] = 1;
for (my $r = 0; $r < $range+2; $r++) {
$num[$i] .= 0;
}
$num[$i] = int(rand($num[$i]));

# limit number size to 5 digits

$num[$i] = substr($num[$i],0,5);

# calculate the result

$res[$i] = $num[$i]/$divider;
my $op_mode = "div";

if ($unit_1 > $unit_2) {

my $cache = $res[$i];
$res[$i] = $num[$i];
$num[$i] = $cache;
$op_mode = "mul";

}

# don't allow decimals

if (!$vars{'app_input_allow_comma'}) {

my $whilecounter;

while ($num[$i] =~ /\./ && $whilecounter < 16) {
$whilecounter++;
$num[$i] = $num[$i] * 10;
$res[$i] = $res[$i] * 10;
}

while ($res[$i] =~ /\./ && $whilecounter < 32) {
$whilecounter++;
$num[$i] = $num[$i] * 10;
$res[$i] = $res[$i] * 10;
}

while (length $num[$i] > 4 && length $res[$i] > 4 && $whilecounter < 48) {
$whilecounter++;
$num[$i] = int($num[$i]/10);
$res[$i] = int($res[$i]/10);
}

while (((length $num[$i] > 12 && length $res[$i] > 1) || (length $num[$i] > 1 && length $res[$i] > 12)) && $whilecounter < 64) {
$whilecounter++;
$num[$i] = int($num[$i]/10);
$res[$i] = int($res[$i]/10);
}

# look for errors produced by the logic

if ($num[$i] > $res[$i]) {

my $test = $num[$i];
$test =~ s~^$res[$i]~~;

if ($test ne "") {

$num[$i] =~ s~$test$~~;
for (my $c = 0; $c < (length $test); $c++) {
$num[$i] .= 0;
}

}

} else {

my $test = $res[$i];
$test =~ s~^$num[$i]~~;

if ($test ne "") {

$res[$i] =~ s~$test$~~;
for (my $c = 0; $c < (length $test); $c++) {
$res[$i] .= 0;
}

}

}

}

}

# catch error: number too big (example: "30'648 cm = 3,0648e-06 km")

$num[$i] = ONO::FW::Apps::Core->float_to_decimal($num[$i]);
$res[$i] = ONO::FW::Apps::Core->float_to_decimal($res[$i]);

# CONVERT CUSTOM NUMBER GENERATOR - END

if ($vars{'output_web'}) {

$stat_todo++;

$vars{"output_web_answer_$i"} = ONO::FW::Apps::Core->clean_answer_math($vars{"output_web_answer_$i"});

if ($vars{'app_input_multiple_questions_per_page'}) {

if ($vars{'output_web_next_page'}) {

$vars{"output_web_answer_$i"} = "";

} else {

if ($vars{"output_web_cache_num_$i"} && $vars{"output_web_cache_res_$i"} && $vars{"output_web_cache_unit1_$i"} && $vars{"output_web_cache_unit2_$i"}) {

$num[$i] = $vars{"output_web_cache_num_$i"};
$res[$i] = $vars{"output_web_cache_res_$i"};
$unit_1_disp[$i] = $vars{"output_web_cache_unit1_$i"};
$unit_2_disp[$i] = $vars{"output_web_cache_unit2_$i"};

}

}

} else {

if ($vars{"output_web_answer"} ne "") {

if ($vars{"output_web_answer"} != $res[$i]) {

$vars{'output_web_answer_status'} = 2;
$num[$i] = $vars{"output_web_cache_num_$i"};
$res[$i] = $vars{"output_web_cache_res_$i"};
$unit_1_disp[$i] = $vars{"output_web_cache_unit1_$i"};
$unit_2_disp[$i] = $vars{"output_web_cache_unit2_$i"};
$vars{'output_web_mistakes'}++;

} else {

# $question_number++;
$vars{'output_web_answer_status'} = 1;
$WEB .= qq~<input type="hidden" name="output_web_rescramble" value="1">~;
# $vars{'output_web_answer'} = "";

}
}
}
}

# output paper

if ($vars{'output_paper'}) {

$num[$i] = ONO::FW::Apps::Core->make_human_readable($num[$i],$num_comma,$num_delimiter);
$res[$i] = ONO::FW::Apps::Core->make_human_readable($res[$i],$num_comma,$num_delimiter);

$corr_cache[$i] = "$num[$i];$unit_1_disp[$i];$res[$i];$unit_2_disp[$i];$pdf_x;$pdf_y;";

if (length $num[$i] > 10) {
ONO::Lib::PDF::Draw->font('',10);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+25, $pdf_y, $num[$i],"r","","");
ONO::Lib::PDF::Draw->font('',12);
} else {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+25, $pdf_y, $num[$i],"r","","");
}
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+27, $pdf_y, $unit_1_disp[$i],"","","x");
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+37, $pdf_y, "=");
ONO::Lib::PDF::Draw->font('',9);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+42, $pdf_y, ".............................");
ONO::Lib::PDF::Draw->font('',12);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+70, $pdf_y, $unit_2_disp[$i],"","","x");

$pdf_y = $pdf_y + 8;
if (($i+1)%5 == 0) {
$pdf_y = $pdf_y + 8;
}

if ($pdf_y > 190) {
$pdf_x = $pdf_x + 90;
$pdf_y = 8;
}

}

if ($vars{'output_web'}) {

my $qanswer = $res[$i];

$WEB .= qq~<input type="hidden" name="output_web_cache_num_$i" value="$num[$i]">
<input type="hidden" name="output_web_cache_res_$i" value="$res[$i]">
<input type="hidden" name="output_web_cache_unit1_$i" value="$unit_1_disp[$i]">
<input type="hidden" name="output_web_cache_unit2_$i" value="$unit_2_disp[$i]">
~;

if ($vars{'app_input_multiple_questions_per_page'}) {

my ($BG,$is_correct);

my $PH = "?";
if (ONO::IO->devstation) {
$PH = $res[$i];
}

my $FIELD = qq~<input type="text" id="output_web_answer_${i}_id" name="output_web_answer_$i" value="$vars{"output_web_answer_$i"}"
maxlength="24" class="w95 tar" placeholder="$PH">~;

if ($vars{"output_web_answer_$i"} =~ /[A-Z0-9\.]/) {
$BG = " bg_red";
$stat_todo--;
if ($vars{"output_web_answer_$i"} eq $qanswer) {
$BG = " bg_green";
$qanswer = ONO::FW::Apps::Core->make_human_readable($qanswer,$num_comma,$num_delimiter);
$FIELD = qq~<div class="bold green tar">$qanswer</div><input type="hidden" name="output_web_answer_$i" value="$vars{"output_web_answer_$i"}">~;
$stat_correct++;
$is_correct++;
} else {
$stat_wrong++;
$vars{'output_web_mistakes'}++;
}
}

if (!$is_correct && !$focus_on) {
$FIELD .= qq~<script>document.getElementById('output_web_answer_${i}_id').focus();</script>~;
$focus_on++;
}

$num[$i] = ONO::FW::Apps::Core->make_human_readable($num[$i],$num_comma,$num_delimiter);

my $unit1 = ONO::FW::Apps::Core->html_exponent($unit_1_disp[$i]);
my $unit2 = ONO::FW::Apps::Core->html_exponent($unit_2_disp[$i]);

$WEB .= qq~<tr class="bt bb$BG">
<td class="w50 pad5_2 nowrap tar">$num[$i]</td>
<td>$unit1</td>
<td class="pad5_2">=</td>
<td class="w50 pad5_2 nowrap">$FIELD</td>
<td>$unit2</td>
<td><div class="block32"></div></td>
</tr>
~;

}

if ($i == 9) {

$WEB .= ONO::FW::Apps::Core->multi_frame_split;

}

}

}

if ($vars{'output_web'}) {

$WEB .= ONO::FW::Apps::Core->multi_frame_end($vars{'output_web_finish'},$vars{'app_input_multiple_questions_per_page'});

$WEB .= ONO::FW::Apps::Core->input_vars(\%vars);

if ($questions_per_page == 1) {

$num[0] = ONO::FW::Apps::Core->make_human_readable($num[0],$num_comma,$num_delimiter);

my $unit1 = ONO::FW::Apps::Core->html_exponent($unit_1_disp[0]);
my $unit2 = ONO::FW::Apps::Core->html_exponent($unit_2_disp[0]);

$WEB .= ONO::FW::Apps::Core->check_single(
qq~<td class="pad10_2 bold">$num[0] $unit1 = </td>~,
qq~<td class="pad10_2 bold">$unit2</td>~,
200,
24,
$BLK_ref,
\%vars,
"C",
);

if (ONO::IO->devstation) {
$WEB .= qq~<div class="box_paper">debug: res = $res[0]</div>~;
}

} else {

$WEB .= ONO::FW::Apps::Core->check_multi($BLK_ref,$stat_todo+$stat_wrong,$page,$pages,"",\%vars);

}

$WEB .= qq~</form>~;

}

}

if (ONO::FW::Apps::Core->print_corrections("$BASE/pdf/pdf-$pdfkey-corr.pdf","",$format,$vars_ref)) {

ONO::FW::Apps::Core->headers(\%vars,$format,"c");
ONO::FW::Apps::PDF->title($BLK{'Corrections'},$PDF_OPTS,$vars{'app_input_print_options_code'},$format);
ONO::FW::Apps::Core->unique_copies_info($canvas,$copy,$vars_ref);
ONO::FW::Apps::Core::Graphics->pdf_draw_item_numbers($canvas,-2,0,1,4,"CHV",$vars_ref);

# ONO::Lib::PDF::ToolBox->canvas_test($canvas);

for (my $c = 0; $c < $questions_per_page; $c++) {

my ($num,$unit1,$res,$unit2,$pdf_x,$pdf_y) = split(/\;/,$corr_cache[$c]);

if (length $num > 10) {
ONO::Lib::PDF::Draw->font('',10);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+25, $pdf_y, $num,"r","","");
ONO::Lib::PDF::Draw->font('',12);
} else {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+25, $pdf_y, $num,"r","","");
}
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+27, $pdf_y, $unit1,"","","x");
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+37, $pdf_y, "=");
ONO::Lib::PDF::Draw->font('HO');
if (length $res > 10) {
ONO::Lib::PDF::Draw->font('',10);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+68, $pdf_y, $res,"r");
ONO::Lib::PDF::Draw->font('',12);
} else {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+68, $pdf_y, $res,"r");
}
ONO::Lib::PDF::Draw->font('H');
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+70, $pdf_y, $unit2,"","","x");

}

}

}

if ($vars{'output_web'}) {

($stat_correct,$stat_todo_total) = ONO::FW::Apps::Core->stats_multi_pages($vars{'app_input_number_of_questions'},$stat_correct,$stat_wrong,$page,$pages);

if ($questions_per_page == 1) {

$stat_correct = $question_number-1;

$WEB = ONO::FW::Apps::Core->stats_replace($WEB,$stat_correct,$stat_wrong,$stat_todo_total-$stat_correct,$vars{'output_web_mistakes'});

} else {

$WEB = ONO::FW::Apps::Core->stats_replace($WEB,$stat_correct,$stat_wrong,$stat_todo_total,$vars{'output_web_mistakes'},$page,$pages,$vars{'output_web_completed'});

}

}

if ($vars{'output_paper'}) {

ONO::FW::Apps::PDF->end;

$WEB .= ONO::FW::Apps::Core->output_paper($pdfkey,\%BLK,\%vars,$HTTP,$BASE);

}

}

if ($vars{'output_web'}) {
ONO::FW::Apps::Core->varstore($vars{'username'},$vars{'virclass'},$vars{'anonymous'},\%vars);
}

} else {

if ($vars{'mode'} eq "worksheet_download") {

#############################################################
# WORKSHEET DOWNLOAD
#############################################################

if ($vars{'table_generator'} && $vars{'app_input_print_options_unique_copies'}) {

my $PDF_OPTS = ONO::FW::Apps::Core->headers_options($BLK_ref,$vars_ref);

ONO::FW::Apps::PDF->new("$BASE/pdf/pdf-$pdfkey.pdf","",$format);

my @tables;
for (my $i = 1; $i < 11; $i++) {
if ($vars{"app_input_mulrow_$i"}) {
@tables = (@tables,$i);
}
}

foreach my $copy (ONO::FW::Apps::Core->unique_copies($vars{'app_input_print_options_unique_copies'})) {

my ($pdf_x,$pdf_y) = (0,0);

if ($copy > 1) {
ONO::FW::Apps::PDF->page("",$format);
}
ONO::FW::Apps::Core->headers(\%vars,$format);
ONO::FW::Apps::PDF->title($vars{'app_input_print_options_title'},$PDF_OPTS,$vars{'app_input_print_options_code'},$format);
ONO::FW::Apps::Core->unique_copies_info($canvas,$copy,$vars_ref);

# ONO::Lib::PDF::ToolBox->canvas_test($canvas);

my $step = 24;
my $cols = 7;
my @opts = ("",@opts1);

if ($vars{'app_input_type_status'} eq "lengths") {
$step = 14;
$cols = 12;
}
if ($vars{'app_input_type_status'} eq "areas") {
@opts = @opts2;
}
if ($vars{'app_input_type_status'} eq "volumes") {
@opts = @opts3;
}
if ($vars{'app_input_type_status'} eq "weights") {
@opts = ("",@opts4);
}
if ($vars{'app_input_type_status'} eq "liquids") {
@opts = @opts5;
}

for (my $i = 0; $i < $cols; $i++) {
ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,0,$pdf_x+$step,190,1,"","0:250:250:220",2);
ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,0,$pdf_x+$step,12, 1,"","0:220:220:200",2);
$pdf_x = $pdf_x + $step;
}

for (my $i = 21; $i < 185; $i = $i + 8) {
ONO::Lib::PDF::Draw->line($canvas,0,$i,$cols * $step,$i);
}

$pdf_x = $cols * $step - $step/2;
ONO::Lib::PDF::Draw->font("HB",16);
for (my $i = 0; $i < $cols; $i++) {
if ($opts[$i]) {
my $fix_x;
if ($opts[$i] =~ /\^/) {
$fix_x = 5;
}
if ($vars{'app_input_type_status'} eq "weights") {
$fix_x = 9;
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+$fix_x,$pdf_y+7.75,$opts[$i],"r","","x");
} else {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+$fix_x,$pdf_y+7.75,$opts[$i],"c","","x");
}
}
$pdf_x = $pdf_x - $step;
}
ONO::Lib::PDF::Draw->font("H",12);

if ($vars{'app_input_type_status'} eq "areas") {
for (my $i = 0; $i < $cols; $i++) {
ONO::Lib::PDF::Draw->line($canvas,$i*$step+$step/2,16,$i*$step+$step/2,190,"","",1);
}
}

if ($vars{'app_input_type_status'} eq "volumes" || $vars{'app_input_type_status'} eq "weights") {
for (my $i = 0; $i < $cols; $i++) {
ONO::Lib::PDF::Draw->line($canvas,$i*$step+$step/3,16,$i*$step+$step/3,190,"","",1);
ONO::Lib::PDF::Draw->line($canvas,$i*$step+$step/3*2,16,$i*$step+$step/3*2,190,"","",1);
}
}

}

ONO::FW::Apps::PDF->end;

$WEB .= ONO::FW::Apps::Core->output_paper($pdfkey,\%BLK,\%vars,$HTTP,$BASE);

} else {

if (!$vars{'app_input_print_options_title'}) {
$vars{'app_input_print_options_title'} = $txt[3];
}

my $CONF .= ONO::FW::Apps::ToolBox->conf_input("codetitle", $BLK_ref,\%vars,"", "", $vars{'app_input_print_options_code'},$vars{'app_input_print_options_title'});
$CONF .= ONO::FW::Apps::ToolBox->conf_input("spacer", $BLK_ref,\%vars);

if (!$vars{'app_input_type_status'}) {
$vars{'app_input_type_status'} = "lengths";
}

my @radios = ("lengths:G","areas:G","volumes:G","weights:G","liquids:G");
my @RADIO = ONO::Lib::UI::Radio->buttons('app_input_type_status',\@radios,$vars{'app_input_type_status'});

my $CUSTOM .= qq~<table class="default_table">
<tr>
<td>$RADIO[1]</td>
<td>$txt[0]</td>
</tr>
<tr>
<td>$RADIO[2]</td>
<td>$txt[1]</td>
</tr>
<tr>
<td>$RADIO[3]</td>
<td>$txt[2]</td>
</tr>
<tr>
<td>$RADIO[5]</td>
<td>$txt[7]</td>
</tr>
<tr>
<td>$RADIO[4]</td>
<td>$txt[6]</td>
</tr>
</table>
$RADIO[0]
~;

$CONF .= ONO::FW::Apps::ToolBox->conf_input("custom", $BLK_ref,\%vars,"","",$CUSTOM);
$CONF .= ONO::FW::Apps::ToolBox->conf_input("spacer", $BLK_ref,\%vars);

$vars{'conf_box_title'} = $txt[0];
$vars{'conf_box_icon'} = "devices/print_printer";
$vars{'output_paper_custom_text'} = $BLK{'print'};

$CONF .= qq~<input type="hidden" name="mode" value="worksheet_download"><input type="hidden" name="table_generator" value="enabled">~;

$WEB .= ONO::FW::Apps::Core->conf_box(
$db,
$community,
$app,
$form,
"",
$action_url,
$CONF,
$lang,
$BLK_ref,
\%vars,
"p-nd-D-u-Qt",
);
}

} else {

#############################################################
# EXERCISE SETUP
#############################################################

if (!$vars{'app_input_number_of_questions'}) {
$vars{'app_input_number_of_questions'} = 20;
if (ONO::IO->device eq "phone") {
$vars{'app_input_number_of_questions'} = 10;
}
}

my ($CONF,$CUSTOM,$CUSTOM2,%setup,$rows_selected);

for (my $i = 0; $i < 4; $i++) {
for (my $j = 1; $j < 8; $j++) {
if ($vars{"app_input_convdata_${i}_${j}"}) {
$rows_selected++;
}
}
}

if (!$rows_selected && !$vars{"app_input_convdata_1_0"} && !$vars{"app_input_convdata_2_0"} && !$vars{"app_input_convdata_3_0"}) {
$vars{"app_input_convdata_0_0"}++;
$vars{"app_input_convdata_0_2"}++;
$vars{"app_input_convdata_0_3"}++;
$vars{"app_input_convdata_0_4"}++;
}

my $max = 8;

for (my $j = $max; $j > 0; $j--) {

my $TRCLASS = "bg_paper";
my (
$txt0,$txt1,$txt2,
$txt3,$txt4,
) = ("$txt[0] ","$txt[1] ","$txt[2] ","$txt[6] ","$txt[8] ");
if ($j == $max) {
if ($max > 1) {
$TRCLASS = "bb";
} else {
$TRCLASS = "";
}
} else {
(
$txt0,$txt1,$txt2,
$txt3,$txt4,
) = ($opts1[$j-1],$opts2[$j-1],$opts3[$j-1],$opts4[$j-4],$opts5[$j-1]);
}

$txt1 = ONO::FW::Apps::Core->html_exponent($txt1);
$txt2 = ONO::FW::Apps::Core->html_exponent($txt2);

my $k = $j;
if ($k == 8) {
$k = 0;
}

my $SWITCH1 = ONO::Lib::UI::Check->switch("app_input_convdata_0_$k", $vars{"app_input_convdata_0_$k"},"Jm","","select_area_convdata('0');");
my $SWITCH2 = ONO::Lib::UI::Check->switch("app_input_convdata_1_$k", $vars{"app_input_convdata_1_$k"},"Jm","","select_area_convdata('1');");
my $SWITCH3 = ONO::Lib::UI::Check->switch("app_input_convdata_2_$k", $vars{"app_input_convdata_2_$k"},"Jm","","select_area_convdata('2');");
my $SWITCH4;
if ($j > 3) {
$SWITCH4 = ONO::Lib::UI::Check->switch("app_input_convdata_3_$k", $vars{"app_input_convdata_3_$k"},"Jm","","select_area_convdata('3');");
} else {
$txt3 = "";
}
my $SWITCH5 = ONO::Lib::UI::Check->switch("app_input_convdata_4_$k", $vars{"app_input_convdata_4_$k"},"Jm","","select_area_convdata('4');");

if ($j == 8) {

$CUSTOM .= qq~<tr class="$TRCLASS">
<td class="bold br"><div class="ml10">$txt0</div></td>
<td class="bold br"><div class="ml10">$txt1</div></td>
<td class="bold br"><div class="ml10">$txt2</div></td>
<td class="bold br"><div class="ml10">$txt4</div></td>
<td class="bold"><div class="ml10">$txt3</div></td>
</tr>
<tr class="bt">
<td class="br"></td>
<td class="br"></td>
<td class="br"></td>
<td></td>
</tr>
<tr>
<td class="pad0_5 br">
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_all_convdata('0');" class="button button_micro">$BLK{'all'}</a>
</div>
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_none_convdata('0');" class="button button_micro">$BLK{'none'}</a>
</div>
</td>
<td class="pad0_5 br">
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_all_convdata('1');" class="button button_micro">$BLK{'all'}</a>
</div>
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_none_convdata('1');" class="button button_micro">$BLK{'none'}</a>
</div>
</td>
<td class="pad0_5 br">
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_all_convdata('2');" class="button button_micro">$BLK{'all'}</a>
</div>
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_none_convdata('2');" class="button button_micro">$BLK{'none'}</a>
</div>
</td>
<td class="pad0_5 br">
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_all_convdata('4');" class="button button_micro">$BLK{'all'}</a>
</div>
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_none_convdata('4');" class="button button_micro">$BLK{'none'}</a>
</div>
</td>
<td class="pad0_5">
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_all_convdata('3');" class="button button_micro">$BLK{'all'}</a>
</div>
<div class="inline auto">
<a href="javascript:void(0);" onclick="select_none_convdata('3');" class="button button_micro">$BLK{'none'}</a>
</div>
</td>
</tr>
<tr class="bb">
<td class="br"></td>
<td class="br"></td>
<td class="br"></td>
<td></td>
</tr>
~;

} else {

$CUSTOM .= qq~<tr class="$TRCLASS">
<td class="pad5_2 br">
<table class="default_table">
<tr>
<td class="pad5_0">$SWITCH1</td>
<td class="p0">$txt0</td>
</tr>
</table>
</td>
<td class="pad5_2 br">
<table class="default_table">
<tr>
<td class="pad5_0">$SWITCH2</td>
<td class="p0">$txt1</td>
</tr>
</table>
</td>
<td class="pad5_2 br">
<table class="default_table">
<tr>
<td class="pad5_0">$SWITCH3</td>
<td class="p0">$txt2</td>
</tr>
</table>
</td>
<td class="pad5_2 br">
<table class="default_table">
<tr>
<td class="pad5_0">$SWITCH5</td>
<td class="p0">$txt4</td>
</tr>
</table>
</td>
<td class="pad5_2">
<table class="default_table">
<tr>
<td class="pad5_0">$SWITCH4</td>
<td class="p0">$txt3</td>
</tr>
</table>
</td>
</tr>
~;

}

}

$CUSTOM = qq~<div class="bg_fabric radius10 bo" style="overflow:hidden">
<table class="wide_table radius10">
$CUSTOM
</table>
</div>
<script>
function select_all_convdata(id) {
for (i = 0;i < 9;i++){
onojs_inline('app_input_convdata_'+id+'_'+i+'_opt_on');
onojs_hide('app_input_convdata_'+id+'_'+i+'_opt_off');
onojs_setvalue('app_input_convdata_'+id+'_'+i+'_id','1');
}
}
function select_none_convdata(id) {
for (i = 0;i < 9;i++){
onojs_hide('app_input_convdata_'+id+'_'+i+'_opt_on');
onojs_inline('app_input_convdata_'+id+'_'+i+'_opt_off');
onojs_setvalue('app_input_convdata_'+id+'_'+i+'_id','0');
}
}
function select_area_convdata(id) {
onojs_inline('app_input_convdata_'+id+'_0_opt_on');
onojs_hide('app_input_convdata_'+id+'_0_opt_off');
onojs_setvalue('app_input_convdata_'+id+'_0_id','1');
}
</script>
~;

$CONF .= ONO::FW::Apps::ToolBox->conf_input("custom", $BLK_ref,\%vars,"","",$CUSTOM);
if ($vars{'output_paper'} || $vars{'output_web'} || $vars{'output_save'} || $vars{'output_save_now'}) {
$CONF .= ONO::FW::Apps::ToolBox->conf_input("spacer", $BLK_ref,\%vars);
$CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"","","allow_comma",$vars{'app_input_allow_comma'},$txt[5]);
}

$WEB .= ONO::FW::Apps::Core->conf_box(
$db,
$community,
$app,
$form,
"",
$action_url,
$CONF,
$lang,
$BLK_ref,
\%vars,
"op-ncdi-CDI-lsu-NO",
);

my $PDF = ONO::FW::Apps::Info->display_pdf("edu","convert","/images/morzino/apps/conversion_tables.jpg",180,"f");

$WEB .= qq~<div class="abs hide800" style="top:120px;right:20px;width:380px;height:340px">
<a href="${action_url}mode=worksheet_download">
<div class="auto" style="width:180px;height:252px">
$PDF
</div>
</a>
<div class="abs" style="top:260px;width:380px">
<div class="pad10_2 center italic col9 mb5 lh125">$txt[4]</div>
<div class="inline auto"><a href="${action_url}mode=worksheet_download" class="button_green">$BLK{'download'}</a></div>
</div>
</div>
~;

if (!$vars{'exercise'}) {
$WEB .= ONO::FW::Apps::Core->info_display(
ONO::FW::Apps::Core->info("info_online_print",$vars{'app_lang'}),
ONO::FW::Apps::Core->info("info_indiv_diff", $vars{'app_lang'}),
);
}

}
}

my ($status,$WEB) = ONO::Lib::Parser->parse($WEB,$vars_ref);

$WEB .= ONO::FW::Apps::Core->vardump($vars_ref);

$WEB = ONO::FW::Edu::Apps::ToolBox->apps_gate($WEB,$vars_ref);

return $WEB;

}

###################################################################
# SUBS
###################################################################

sub units_le {
return ( "mm", "cm", "dm", "m", "dam", "hm", "km" );
}
sub units_ar {
return ( "mm^2", "cm^2", "dm^2", "m^2", "a", "ha", "km^2" );
}
sub units_vo {
return ( "mm^3", "cm^3", "dm^3", "m^3", "dam^3", "hm^3", "km^3" );
}
sub units_we {
return ( "mg", "g", "kg", "t" );
}
sub units_li {
return ( "ml", "cl", "dl", "l", "dal", "hl", "F" );
}

###################################################################
#### THAT'S IT :-D
###################################################################

1;

__END__