#! /usr/bin/perl

=head1 NAME

room_handout - hand out copies of files to room

=head1 SYNOPSIS

 https://server/schulkonsole/room_handout

=head1 DESCRIPTION

C<room_handout> lets you copy files to members of a selected room
from your directory. The HTML template is room_handout.tt.

=head2 Template variables

Additionally to the variables of Schulkonsole::Session and
Schulkonsole::Room::set_vars()
C<room_handout> provides the following variables:

=over

=cut

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

my $this_file = 'room_handout';
my $transfername = 'handoutcopy_current_room';

my $sk_session = new Schulkonsole::Session($this_file);
my $q = $sk_session->query();
my $d = $sk_session->d();

my $room_session = new Schulkonsole::Room($sk_session);


if (   not $room_session
    or not $room_session->info('is_editing')) {
	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/room/g;
	$sk_session->redirect($url);
}

if ($room_session->param('test_step')) {
	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/room_test/g;
	$sk_session->redirect($url);
}



my $editing_userdata = $room_session->info('editing_userdata');
my $editing_user = $$editing_userdata{uid};

my $workstations
	= Schulkonsole::Config::workstations_room($room_session->info('name'));
my $workstation_users = $room_session->workstation_users();

my $id = $sk_session->userdata('id');
my $password = $sk_session->get_password();

eval {
	SWITCHCOMMAND: {
		$q->param('handoutcopy') and do {
			my @users;
			my $is_all_users = 1;
			foreach my $host (keys %$workstation_users) {
				foreach my $userdata (@{ $$workstation_users{$host} }) {
					my $user = $$userdata{uid};
					next if $user eq $editing_user;
		
					if ($q->param("$user;handout")) {
						push @users, $user;
					} elsif ($is_all_users) {
						$is_all_users = 0;
					}
				}
			}
		
			if (@users) {
				eval {
				Schulkonsole::Sophomorix::handoutcopy_from_room_to_users(
					$id, $password, @users);
		
				if ($is_all_users) {
					$sk_session->set_status($sk_session->d()->get('Ausgeteilt'), 0);
				} else {
					$sk_session->set_status($sk_session->d()->get('Ausgeteilt an')
						. ' ' . join(', ', @users),
					0);
				}
				};
				if ($@) {
					$sk_session->standard_error_handling($this_file, $@);
				}
			} else {
				$sk_session->set_status($sk_session->d()->get(
					'Keine Benutzer ausgewählt'), 1);
			}
			last SWITCHCOMMAND;
		};

		$q->param("upload_$transfername") and do {
			my $file = $q->param("upload_$transfername");
			utf8::decode($file);
			my $tmpfile = "$Schulkonsole::Config::_runtimedir/upload_${transfername}_"
							. $sk_session->session_id() . '_' . time;
			if(! open(DAT,'>'.$tmpfile)) {
				$sk_session->set_status($d->get('Es konnte keine temporäre Datei erzeugt werden.'),1);
				last SWITCHCOMMAND;
			}
			binmode($file);
			binmode(DAT);
			my $buffer = "";
			while( read($file, $buffer, 1024)) {
				print DAT $buffer;
			}
			close(DAT);
			my $filename = basename($file);
			
			Schulkonsole::Sophomorix::add_handoutcopy_current_room(
				$sk_session->userdata('id'), $sk_session->get_password(), $filename, 0, basename($tmpfile));
			$sk_session->set_status($d->get('Datei hochgeladen'),0);
			
			last SWITCHCOMMAND;	
		}; 
		
		foreach my $param ($q->param) {
			if (my ($file, $action)
			    	= $param =~ /^(.+);(delete|download)$/) {
				utf8::decode($file);
				my $teacher_share_files = Schulkonsole::Sophomorix::ls_handoutcopy_current_room(
						$sk_session->userdata('id'), $sk_session->get_password());
				
				SWITCHACTION: {
				$action =~ /delete/ and do {
					if(not defined $$teacher_share_files{$file}) {
						$sk_session->set_status(
							$d->get('Datei ') . $file . $d->get(' nicht als auszuteilende Datei vorhanden.'), 1);
						last SWITCHACTION;
					}
					Schulkonsole::Sophomorix::rm_handoutcopy_current_room(
						$sk_session->userdata('id'), $sk_session->get_password(),
						$file, ($$teacher_share_files{$file}?1:0));
					
					$sk_session->set_status($d->get('Datei gelöscht.'),0);
					
					last SWITCHACTION;
				};
				
				$action =~ /download/ and do {
					if(not defined $$teacher_share_files{$file}) {
						$sk_session->set_status(
							$d->get('Datei ') . $file . $d->get(' nicht als auszuteilende Datei vorhanden.'),	1);
						last SWITCHACTION;
					}
					my $tmpfile = "download_${transfername}_".$sk_session->session_id() . '_' . time;
					
					Schulkonsole::Sophomorix::dl_handoutcopy_current_room(
						$sk_session->userdata('id'), $sk_session->get_password(),
						$file, ($$teacher_share_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 SWITCHCOMMAND;
						}
						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 SWITCHCOMMAND;
						}
						
						if($$teacher_share_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 SWITCHCOMMAND;				
					}
					
					last SWITCHACTION;
				};
				
				} #SWITCHACTION
			}
		} # foreach param

	}; # SWITCHCOMMAND
}; #eval

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


eval {
my $teacher_share_files = Schulkonsole::Sophomorix::ls_handoutcopy_current_room(
	$id, $password);

=item C<teacher_share_files>

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

=cut

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


my @array_overview_workstations;
foreach my $workstation (sort keys %$workstations) {
	my $first_login = 1;
	if (    $$workstation_users{$workstation}
	    and @{ $$workstation_users{$workstation} }) {
		foreach my $userdata (sort { $$a{uid} cmp $$b{uid} }
		                           @{ $$workstation_users{$workstation} }) {
			my $overview_workstation = {
				name => $workstation,
				editing => ($$userdata{uid} eq $editing_user),
				login => $$userdata{uid},
				first => $first_login,
			};
			push @array_overview_workstations, $overview_workstation;

			$first_login = 0;
		}
	}
}

=item C<overview_workstations>

Users logged in on workstations in room as an array of hashes with
the keys

=over

=item C<name>

Name of a workstation

=item C<editing>

True if the logged in user is giving a lesson in the room

=item C<login>

Login of the user

=item C<first>

True if the user is the first of all users on the workstation

=back

=cut

$sk_session->set_var('overview_workstations', \@array_overview_workstations);


$room_session->set_vars($sk_session);

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


=back

=head2 Form fields

=over

=item C<handoutcopy>

Hand out copies to selected users

=item C<${overview_workstations{login}};handout>

Checkboxes created in loop over template variable C<overview_workstations>.
Copying is performed with the selected logins.

=back

