ONO::FW::Edu::Apps::Modules::Measure

package ONO::FW::Edu::Apps::Modules::Measure;
################################################################################
# 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::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::IO;

use ONO::FW::Apps::PDF;
use ONO::FW::Apps::ToolBox;

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 = "measure";

my @txt = (
"measure",
"draw",
"snakes",
"precision",
"grid",
"align on grid",

"also diagonals",
"show where to start",
);

if ($vars{'app_lang'} eq "de") {
@txt = (
"messen",
"zeichnen",
"Schlangen",
"Präzision",
"Raster",
"am Raster ausrichten",

"auch diagonal",
"Anfang anzeigen",
);
}

if ($vars{'app_lang'} eq "lu") {
@txt = (
"moossen",
"zeechnen",
"Schlaangen",
"Präzisioun",
"Raster",
"um Raster ausriichten",

"och diagonal",
"Ufank uweisen",
);
}

if ($vars{'app_lang'} eq "fr") {
@txt = (
"mesurer",
"dessiner",
"serpents",
"précision",
"grille",
"aligner sur la grille",

"aussi diagonales",
"afficher début",
);
}

if (!$vars{'app_input_print_options_title'}) {
$vars{'app_input_print_options_title'} = ucfirst $txt[0]." & ".ucfirst $txt[1];
if ($vars{'app_input_learn_measure'} || $vars{'app_input_learn_draw'}) {
if ($vars{'app_input_learn_measure'} < 1) {
$vars{'app_input_print_options_title'} = ucfirst $txt[1];
}
if ($vars{'app_input_learn_draw'} < 1) {
$vars{'app_input_print_options_title'} = ucfirst $txt[0];
}
}
}

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

my $WEB;

$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)) {

$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 $PDF_OPTS;

my $questions_per_page = 12;

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

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);

}

my $range_from = $vars{'app_input_range_from'};
my $range_to = $vars{'app_input_range_to'};

if ($vars{'output_worksheet_maker'}) {
$questions_per_page = 2;
}

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

my ($pdf_x,$pdf_y) = (0,10);
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);

if ($vars{'app_input_opt_grid'}) {
my @grid = (0,0,170,190);
if ($vars{'output_worksheet_maker'}) {
@grid = (0,5,170,40);
$pdf_y = 15;
}
if ($vars{'app_input_select_units'} =~ /mm/) {
ONO::Lib::PDF::Draw->grid($canvas,@grid,1,0.1,"","","","lL");
ONO::Lib::PDF::Draw->grid($canvas,@grid,5,0.1,"","","","l");
} else {
ONO::Lib::PDF::Draw->grid($canvas,@grid,5,0.1,"","","","L");
}
}

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

}

my $last_length_cache;

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

# output paper

if ($vars{'output_paper'}) {

my $length = ONO::FW::Apps::Core->random_number(1,$range_to*10,1,9999999,\%vars);
my $whilecounter;
if ($whilecounter < 255 && ($length > 160 || $length == $last_length_cache || ($vars{'app_input_select_precision'} > 1 && ($length > $last_length_cache-12 && $length < $last_length_cache+12)))) {
$whilecounter++;
$length = ONO::FW::Apps::Core->random_number(1,$range_to*10,1,9999999,\%vars);
}
if ($length < 5 || $length > 160 || ($length > $last_length_cache-12 && $length < $last_length_cache+12)) {
$length = 10;
}
if ($length > $last_length_cache-12 && $length < $last_length_cache+12) {
$length = 20;
}

$last_length_cache = $length;

my @units = ("cm","mm");

my $unit = $units[0];
if ($vars{'app_input_select_units'} =~ /mm/) {
$unit = $units[1];
if ($vars{'app_input_select_units'} =~ /cm/ && int(rand(100)) > 50) {
$unit = $units[0];
}
}

my $type = "measure";
if ($vars{'app_input_learn_draw'}) {
$type = "draw";
if ($vars{'app_input_learn_measure'} && int(rand(100)) > 50) {
$type = "measure";
}
}

ONO::FW::Apps::Core::Graphics->pdf_draw_item_number($canvas,-2,$pdf_y-8,$i+1,"W",$vars_ref);

my $lines = 1;
my $off_x = 0;

if ($length < 111) {
$lines = 2;
}

for (my $l = 0; $l < $lines; $l++) {

if ($l) {
$off_x = $length + 10 + 5*int(rand(3));
if ($off_x < 40) {
$off_x = 40;
}
$length = 160 - $length;
}

my $length_cm = $length/10;

if ($vars{'app_input_select_precision'} == 10 || $vars{'app_input_select_precision'} == 5 || $vars{'app_input_select_precision'} == 1) {
$length = 1+int($length/10);
$length_cm = $length;
$length = $length*10;
if ($vars{'app_input_select_precision'} == 5 && int(rand(100)) > 50) {
$length = $length+5;
$length_cm = $length_cm.$num_comma."5";
}
if ($vars{'app_input_select_precision'} == 1 && int(rand(100)) > 25) {
my $add = int(rand(10));
$length = $length+$add;
$length_cm = $length_cm.$num_comma.$add;
}
}

if ($l) {
my $whilecounter;
while ($whilecounter < 255 && ($off_x + $length) > 160) {
$whilecounter++;
$length = $length - 10;
$length_cm = $length_cm - 1;
}
}

my $length_display = $length;
if ($unit eq "cm") {
$length_display = $length_cm;
}

$corr_cache[$i] .= "$unit\^$length_display\^$length\^$off_x\^";

my ($c1,$c2,$c3) = (230+int(rand(20)),230+int(rand(20)),230+int(rand(20)));

ONO::Lib::PDF::Draw->rect($canvas,$pdf_x+9+$off_x,$pdf_y-6,$pdf_x+37+$off_x,$pdf_y+1,1,"0:25:25:25","0:$c1:$c2:$c3","2");

ONO::Lib::PDF::Draw->text($canvas,$pdf_x+27+$off_x,$pdf_y-1,$unit);

my $debug;
if (ONO::IO->devstation) {
# $debug = 1;
}

if ($type eq "measure" || $debug) {
ONO::Lib::PDF::Draw->line($canvas,$pdf_x+5+$off_x,$pdf_y+5,$pdf_x+5+$length+$off_x,$pdf_y+5,2,"","","s:4","s:4");
ONO::Lib::PDF::Draw->font('',9);
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+26+$off_x,$pdf_y-1,"...............","r");
ONO::Lib::PDF::Draw->font('',12);
}

if ($type eq "draw" || $debug) {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+26+$off_x,$pdf_y-1,$length_display,"r");
if ($vars{'app_input_learn_draw_start'}) {
ONO::Lib::PDF::Draw->line($canvas,$pdf_x+5+$off_x,$pdf_y+5,$pdf_x+10+$off_x,$pdf_y+5,1,"",1,"s:4");
}
}

}

$pdf_y = $pdf_y + 15;

}

}

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);

if ($vars{'app_input_opt_grid'}) {

my @grid = (0,0,170,190);
if ($vars{'output_worksheet_maker'}) {
@grid = (0,5,170,40);
$pdf_y = 15;
}

if ($vars{'app_input_select_units'} =~ /mm/) {
ONO::Lib::PDF::Draw->grid($canvas,@grid,1,0.1,"","","","lL");
ONO::Lib::PDF::Draw->grid($canvas,@grid,5,0.1,"","","","l");
} else {
ONO::Lib::PDF::Draw->grid($canvas,@grid,5,0.1,"","","","L");
}

}

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

$pdf_y = 10;
if ($vars{'output_worksheet_maker'}) {
$pdf_y = 15;
}

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

for (my $l = 0; $l < 2; $l++) {

my (
$unit,$length_display,$length,$off_x,
$unit2,$length_display2,$length2,$off_x2
) = split(/\^/,$corr_cache[$c]);

if ($l) {
($unit,$length_display,$length,$off_x) = ($unit2,$length_display2,$length2,$off_x2);
}

if ($length) {

ONO::FW::Apps::Core::Graphics->pdf_draw_item_number($canvas,-2,$pdf_y-8,$c+1,"CW",$vars_ref);

ONO::Lib::PDF::Draw->rect($canvas,$pdf_x+9+$off_x,$pdf_y-6,$pdf_x+37+$off_x,$pdf_y+1,1,"0:25:25:25","0:240:240:240","2");

ONO::Lib::PDF::Draw->text($canvas,$pdf_x+27+$off_x,$pdf_y-1,$unit);

ONO::Lib::PDF::Draw->text($canvas,$pdf_x+26+$off_x,$pdf_y-1,$length_display,"r");

ONO::Lib::PDF::Draw->line($canvas,$pdf_x+5+$off_x,$pdf_y+5,$pdf_x+5+$length+$off_x,$pdf_y+5,2,"","","s:4","s:4");

}

}

$pdf_y = $pdf_y + 15;

}
}

}

if ($vars{'output_paper'}) {

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

$WEB .= ONO::FW::Apps::PDF->please_wait($pdfkey,\%BLK,\%vars,"",$HTTP,$BASE,"S");

}

}

} 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;
}
}

# "screen:320:50:50::arithmetic_screenshot_1.jpg",

$WEB .= ONO::FW::Apps::Core->infopics(
"sheet:168:180:50:15:measure_sheet_1.jpg",
"sheet:168:40:80:-10:measure_sheet_2.jpg",
);

if (!$vars{'app_input_learn_measure'} && !$vars{'app_input_learn_draw'}) {
$vars{'app_input_learn_measure'}++;
$vars{'app_input_learn_draw'}++;
$vars{'app_input_learn_draw_start'}++;
$vars{'app_input_opt_grid'}++;
# $vars{'app_input_select_opt_align'}++;
}
if (!$vars{'app_input_select_units'}) {
$vars{'app_input_select_units'} = "cm";
}

my $CONF .= ONO::FW::Apps::ToolBox->conf_input("rangesel", $BLK_ref,\%vars,"",1,16,16,$BLK{'length'},"cm");

$CONF .= ONO::FW::Apps::ToolBox->conf_input("select", $BLK_ref,\%vars,"","10=$txt[3]: 1 cm;05=$txt[3]: 0.5 cm;01=$txt[3]: 1 mm","",$vars{'app_input_select_precision'},"","precision");
$CONF .= ONO::FW::Apps::ToolBox->conf_input("select", $BLK_ref,\%vars,"","cmmm=$BLK{'units'}: cm + mm;cm=$BLK{'unit'}: cm;mm=$BLK{'unit'}: mm","",$vars{'app_input_select_units'},"","units");
$CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"","","opt_grid",$vars{'app_input_opt_grid'},"$txt[4] (5 mm)");

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

$CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"S","","learn_measure",$vars{'app_input_learn_measure'},$txt[0]);
$CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"S","","learn_draw",$vars{'app_input_learn_draw'},$txt[1]);
$CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"M","","learn_draw_start",$vars{'app_input_learn_draw_start'},$txt[7]);

# $CONF .= ONO::FW::Apps::ToolBox->conf_input("select", $BLK_ref,\%vars,"","snap=$txt[5] ($BLK{'if_possible'});diagonal=$txt[6]","",$vars{'app_input_select_opt_align'},"opt_align");
# $CONF .= ONO::FW::Apps::ToolBox->conf_input("check", $BLK_ref,\%vars,"M","","learn_measure_snakes",$vars{'app_input_learn_measure_snakes'},$txt[2]);

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

if (!$vars{'exercise'}) {
$WEB .= ONO::FW::Apps::Core->info_display(
ONO::FW::Apps::Core->info("info_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
###################################################################

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

1;

__END__