ONO::FW::Edu::Apps::Modules::Drawing

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

use ONO::Render;

use ONO::ToolBox::Auth;
use ONO::Lib::Lang::LangKitEdu;

use ONO::Lib::DateTime::ToolBox;

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

use ONO::FW::Edu::Apps::ToolBox;
use ONO::FW::User::Init;

use ONO::Lib::Image::Drawing;
use ONO::IO;
use ONO::DB;
use ONO::Lib::Basic;

sub play {

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 ($HEAD,$WEB);

my (
$vpath,
$lang,$domain,$community,
$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$timestamp,
$script_url,$action_url,$form
) = ONO::FW::User::Init->getvar(\%vars);

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

my $action_url = qq~$ENV{'SCRIPT_NAME'}?app_lang=$vars{'app_lang'}&~;

###################################################################
# DRAW
###################################################################

if ($vars{'username'} && $vars{'ono_lib_image_drawing_target_folder'} && $vars{'ono_lib_image_drawing_data'} =~ s~^data:image/png;base64,~~) {

my $FILE;

if ($vars{'ono_lib_image_drawing_target_folder'} eq "user_documents_images") {
$FILE = "media/​users/".ONO::IO->deepdir($vars{'username'})."/​$vars{'username'}/​documents/images";
}
if ($vars{'ono_lib_image_drawing_target_folder'} =~ m~^virclass_(.*?)_class$~) {
my $virclass = $1;
$virclass =~ s~[^0-9]~~gi;
foreach my $line (ONO::DB->select($db,"${community}_school_virclass_relationships","username = '$vars{'username'}' AND virclass = '$virclass'")) {
my @col = ONO::DB->readcols($line);
$FILE = "media/virclass/$virclass/class/$vars{'username'}/documents/images";
}
}
if ($FILE) {
$vars{'ono_lib_image_drawing_save_success'} = ONO::Lib::Image::Drawing->save("$FILE/$vars{'ono_lib_image_drawing_filename'}.png​",$vars{'ono_lib_image_drawing_data'});
}
}

my $DIRS = &get_dirs("",$db,$community,$vars{'username'});

my $CSSJS = ONO::Render->cssjs($BASE);
$CSSJS .= ONO::Lib::Image::Drawing->cssjs($BASE);
my $DRAW = ONO::Lib::Image::Drawing->editor(840,420,$vars{'app_lang'},$BLK_ref,\%vars,"",$BASE,$DIRS);

print qq~Content-Type: text/html; charset=UTF-8\n\n
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="Generator" content="ONO App Engine">
$CSSJS
</head>
<body style="margin:0px;padding:0px">
<form id="ono_lib_image_drawing_form" method="post">
<input type="hidden" name="app_lang" value="$vars{'app_lang'}">
$DRAW
</form>
</body>
</html>
~;

}

sub get_dirs {

my (
$self,
$db,
$community,
$username,
) = @_;

my $DIRS;

if ($username) {

my (@VIRS,%found);

foreach my $line (ONO::DB->select($db,"${community}_school_virclass_relationships","username = '$username'")) {
my @col = ONO::DB->readcols($line);

if (!$found{"$col[1]_$col[2]"}) {
$found{"$col[1]_$col[2]"}++;

foreach my $line2 (ONO::DB->select($db,"${community}_school_virclass","id = '$col[2]'")) {
my @col2 = ONO::DB->readcols($line2);

my $sort = ONO::Lib::Basic->sortstring($col2[6]);

@VIRS = (@VIRS,qq~<!-- sort:$sort --><option value="virclass_$col[2]_class">$col2[6]: /class/$username/documents/images/</option>~);

}
}
}

foreach my $VIR (sort @VIRS) {
$DIRS .= $VIR;
}

}

if ($DIRS) {
$DIRS = qq~<option value="">----</option>$DIRS~;
}

return $DIRS;

}

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

1;

__END__