ONO::FW::Edu::Apps::Modules::Loader

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

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

sub load {

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

# this is the dynamic app loader that's required in the following case(s):
#
# - /loader.pl?exercise=ABC123... will load an entire exercise dataset, as required by the web publisher's "app" paragraph type

my %vars = %$vars_ref;

my $WEB = qq~<div class="p10">please wait...</div>~;

if ($vars{'exercise'}) {

my $db = ONO::FW::Edu::Apps::ToolBox->connect();
my $community = "school";
my $found;
my $exercise = $vars{'exercise'};
$exercise =~ s~[^A-Za-z0-9]~~g;

$WEB .= qq~<div class="p10">looking for exercise ID '$vars{'exercise'}' ...</div>~;

foreach my $line (ONO::DB->select($db,"${community}_school_apps_storage","exercise = '$exercise'")) {
my @row = ONO::DB->readcols($line);

foreach my $line2 (ONO::DB->select($db,"${community}_apps_apps","app = '$row[3]'")) {
my @row2 = ONO::DB->readcols($line2);

$found++;

my $exercise_id = $row[1];
$exercise_id =~ s~[^A-Za-z0-9]~~g;

my $VARS = qq~&exercise=$exercise&exercise_id=$exercise_id~;

foreach my $line (ONO::IO->list("var/community/${community}/apps/storage/".ONO::IO->deepdir($exercise_id)."/$exercise_id/config.txt")) {

$line =~ s~(\n|\r|\t)~~g;
my @lp = split(/:/,$line);
$VARS .= qq~&$lp[0]=$lp[1]~;

}

my $LINK = "$row2[1]?lang=$vars{'lang'}$VARS";
$WEB .= qq~<div class="p10"><a href="$LINK" class="button_green button_small">click here if the exercise is not loading...</a></div>~;

$LINK =~ s~^/oli/(.*?)/~/~;

$WEB .= ONO::IO->refresh($LINK);

}

}

if (!$found) {

$WEB .= qq~<div class="p10">error: no such exercise!</div>~;

}

}

return $WEB;

}

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

1;

__END__