#! /usr/bin/perl

=head1 NAME

myclass_handedout - files handed out to a student

=head1 SYNOPSIS

 https://server/schulkonsole/myclass_handedout

=head1 DESCRIPTION

C<myclass_handedout> lets a student download files handed out to him.
The HTML template is myclass_handedout.tt.

=head2 Template variables

Additionally to the variables of Schulkonsole::Session C<myclass_handedout>
provides the following variables:

=over

=cut

use strict;
use utf8;
use lib '/usr/share/schulkonsole';
use Schulkonsole::Session;
use Schulkonsole::Info;
use Schulkonsole::Sophomorix;
use File::Basename;

my $this_file = 'myclass_handedout';
my $transfername = 'handedout_myclass';

my $sk_session = new Schulkonsole::Session($this_file);
if (not $sk_session->get_password()) {
	my $q = new CGI;
	my $url = $q->url( -full => 1 );

	# we send cookies over secure connections only
	if ($url =~ s/^http:/https:/g) {
		$sk_session->redirect($url);
	} else {
		$sk_session->exit_with_login_page($this_file);
	}
}

my $q = $sk_session->query();
my $d = $sk_session->d();

my $classs = Schulkonsole::Info::groups_classes($sk_session->groups());
my $myclass = $sk_session->param('myclass');
if (   not $myclass  ) {
	$sk_session->param('myclass',$$classs{$sk_session->userdata('gid')});
	$myclass = $sk_session->param('myclass');
}

if (   not $myclass  ) {
	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/myclass/g;
	$sk_session->redirect($url);
}

eval {
	foreach my $param ($q->param) {
		if (my ($teacher, $file, $action)
		    	= $param =~ /^(.+);(.+);(download)$/) {
			my $student_files = Schulkonsole::Sophomorix::ls_handedout_myclass(
					$sk_session->userdata('id'), $sk_session->get_password());
			
			SWITCHACTION: {
			$action =~ /download/ and do {
				if(not defined $$student_files{$teacher}{$file}) {
					$sk_session->set_status(
						$d->get('Lehrer-Datei ') . $teacher . '-' . $file 
								. $d->get(' nicht als bereitgestellte Datei vorhanden.'),	1);
					last SWITCHACTION;
				}
				my $tmpfile = "dl_handedout_myclass_".$sk_session->session_id() . '_' . time;
				
				Schulkonsole::Sophomorix::dl_handedout_myclass(
					$sk_session->userdata('id'), $sk_session->get_password(),
					$teacher, $file, ($$student_files{$file}?1:0), $tmpfile);
				
				if( -e "$Schulkonsole::Config::_runtimedir/$tmpfile") {
					print $q->header( -type           => "application/x-download",
							          -attachment     => $file,
									   -Content_length => -s "$Schulkonsole::Config::_runtimedir/$tmpfile");
								
					if(! open(FILE, "<$Schulkonsole::Config::_runtimedir/$tmpfile")) {
						$sk_session->set_status($d->get('Die temporäre Datei konnte nicht geöffnet werden.'), 1);
						last SWITCHACTION;
					}
					binmode(FILE);
					binmode(STDOUT);
					print while <FILE>;
					if(!close(FILE)) {
						$sk_session->set_status($d->get('Die temporäre Datei konnte nicht geschlossen werden.'), 1);
						last SWITCHACTION;
					}
					
					if($$student_files{$file}){
						system("rm -rf $Schulkonsole::Config::_runtimedir/$tmpfile");
					} else {
						system("rm -f $Schulkonsole::Config::_runtimedir/$tmpfile");
					}
	
					$sk_session->set_status($d->get('Die Datei wurde heruntergeladen.'), 0);
					exit 0;
					
				} else {
					$sk_session->set_status($d->get('Es konnte keine temporäre Datei erzeugt werden.'), 1);
					last SWITCHACTION;				
				}
				
				last SWITCHACTION;
			};
			
			} #SWITCHACTION
		}
	} # foreach param
}; # eval

if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}






eval {
my $student_files = Schulkonsole::Sophomorix::ls_handedout_myclass(
	$sk_session->userdata('id'), $sk_session->get_password());

=item C<student_files>

Available files as returned by C<Schulkonsole::Sophomorix::create_teacher_file_list()>

=cut

$sk_session->set_var('student_files',
	Schulkonsole::Sophomorix::create_category_file_list($student_files));
};
if ($@) {
	if (    ref $@
	    and $@->{code} == Schulkonsole::Error::SophomorixError::WRAPPER_NO_SUCH_DIRECTORY) {
		$sk_session->set_status(
			$sk_session->d()->get(
				'Verzeichnis zum Bereitstellen nicht gefunden'),
				1);
	} else {
		$sk_session->standard_error_handling($this_file, $@);
	}
}

=item C<myclass>

Students class' hash

=cut

$sk_session->set_var('myclass', $myclass);

$sk_session->print_page("$this_file.tt", $this_file);
