#! /usr/bin/perl

=head1 NAME

user_students - administration of student accounts

=head1 SYNOPSIS

 https://server/schulkonsole/user_students

=head1 DESCRIPTION

C<user_students> lets you edit the user lists for students.
The HTML template is user_students.tt.

=head2 Template variables

There are no other template variables but the variables of
Schulkonsole::Session.

=cut

use strict;
use utf8;
use lib '/usr/share/schulkonsole';
use Schulkonsole::Session;
use Schulkonsole::Sophomorix;
use Sophomorix::SophomorixConfig;
# --encoding-*-*
my %supported_encodings = qw(
    ascii    ascii
    8859-1   iso-8859-1
    8859-15  iso-8859-15
    win1252  cp1252
    utf8     utf8
);

my $this_file = 'user_students';


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 $id = $sk_session->userdata('id');
my $password = $sk_session->get_password();


eval {
COMMANDS: {
$q->param('studentsupload') and do {
	my $filename = $q->param('studentsfilename');
	if ($filename) {
		my @lines;
		my $encoding = 'utf8';
		if (defined $Conf::encoding_students) {
			$encoding = $Conf::encoding_students;
		} elsif (defined $DevelConf::encoding_students) {
			$encoding = $DevelConf::encoding_students;
		}
		if (not exists $supported_encodings{$encoding}){
			$encoding = "utf8";
		}
		else {
			$encoding = $supported_encodings{$encoding};
		}
		
		binmode $filename, ":encoding($encoding)";
		while(<$filename>){
			push @lines,$_;
		}
		
		Schulkonsole::Sophomorix::write_students_file($id, $password, \@lines);

		$sk_session->set_status($sk_session->d()->get(
			'Die Datei wurde hochgeladen. Die Änderungen müssen noch in das System übernommen werden.'), 0);

	} else {
		$sk_session->set_status($sk_session->d()->get(
			'Die Datei kann nicht gelesen werden'), 1);
	}

	last COMMANDS;
};

$q->param('studentsedit') and do {
	my @lines;
	foreach my $line (split "\n", $q->param('students')) {
		$line =~ s/\r$//;
		push @lines, "$line\n";
	}

	Schulkonsole::Sophomorix::write_students_file($id, $password, \@lines);

	$sk_session->set_status($sk_session->d()->get(
		'Die Datei wurde geschrieben. Die Änderungen müssen noch in das System übernommen werden.'), 0);

	last COMMANDS;
};

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



eval {
my $students_lines
	= Schulkonsole::Sophomorix::read_students_file($id, $password);
$q->param('students', join('', @$students_lines)) unless $q->param('students');
};
if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}





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


=head2 Form fields

=over

=item C<studentsupload>

Upload file C<studentsfilename>

=item C<studentsfilename>

Filename of a user list to upload

=item C<studentsedit>

Write C<students> to user list

=item C<students>

Content of the user list

=back
