#! /usr/bin/perl

=head1 NAME

user_teachers - administration of teacher accounts

=head1 SYNOPSIS

 https://server/schulkonsole/user_teachers

=head1 DESCRIPTION

C<user_teachers> lets you set teachers' passwords.
The HTML templates are user_teacherpasswords.tt and showpassword.tt.

=head2 Template variables

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

=cut

use strict;
use utf8;
use lib '/usr/share/schulkonsole';
use Schulkonsole::Session;
use Schulkonsole::DB;
use Schulkonsole::Sophomorix;

my $this_file = 'user_teacherpasswords';


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();



my $path = $q->path_info();
$path =~ s:^/+:: if defined $path;
if ($path) {
	eval {
	TYPE: {
	$q->param('passwords_print_pdf') and do {
		my $pdf_data = Schulkonsole::Sophomorix::print_teachers(
			$id, $password, 0);

		if ($pdf_data =~ /^\%PDF/) {
			binmode STDOUT;
			
			print $q->header( -type => 'application/pdf' ), $pdf_data;

			exit;
		}

		$sk_session->set_status_redirect($sk_session->d()->get(
			'Fehler bei der Erzeugung des Dokuments'), 1);
		last TYPE;
	};

	$q->param('passwords_print_csv') and do {
		my $csv_data = Schulkonsole::Sophomorix::print_teachers(
			$id, $password, 1);

		if ($csv_data =~ /^.*;.*;.*;/) {
			binmode STDOUT, ':utf8';

			print $q->header( -type => 'text/comma-separated-values' ),
			      $csv_data;

			exit;
		}

		$sk_session->set_status_redirect($sk_session->d()->get(
			'Fehler bei der Erzeugung des Dokuments'), 1);
		last TYPE;
	};

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

	# in case of error, re-direct to URL without PATH_INFO
	my $url = $q->url( -absolute => 1 );
	$sk_session->redirect($url);
}








my $teachers = Schulkonsole::DB::get_teachers();

eval {
COMMANDS: {

(   $q->param('passwords_reset')
 or $q->param('passwords_random')
 or $q->param('passwords_set')) and do {

 	my $is_selected = 0;
	my @user_selects;
	foreach my $param ($q->param) {
		if (my ($user) = $param =~ /^(.+);select$/) {
			if ($$teachers{$user}) {
				push @user_selects, $user;
				$is_selected = 1;
			}
		}
	}

	if (not @user_selects) {
		$sk_session->set_status(
			$sk_session->d()->get('Keine Benutzer ausgewählt'), 1);

		last COMMANDS;
	}

	PASSWORDACTION: {
	$q->param('passwords_reset') and do {
		Schulkonsole::Sophomorix::passwords_reset(
			$id, $password, @user_selects);

		$sk_session->set_status(
			$sk_session->d()->get('Passwörter zurückgesetzt'), 0);

		last PASSWORDACTION;
	};
	$q->param('passwords_random') and do {
		Schulkonsole::Sophomorix::passwords_random(
			$id, $password, @user_selects);

		$sk_session->set_status(
			$sk_session->d()->get('Zufallspasswörter gesetzt'), 0);

		last PASSWORDACTION;
	};
	$q->param('passwords_set') and do {
		if (my $user_password = $q->param('userpassword')) {
			Schulkonsole::Sophomorix::passwords_set(
				$id, $password, $user_password, @user_selects);

			$sk_session->set_status(
				$sk_session->d()->get('Passwörter gesetzt'), 0);
		} else {
			$sk_session->set_status(
				$sk_session->d()->get('Kein Passwort eingegeben'), 1);
		}

		last PASSWORDACTION;
	};

	}


	last COMMANDS;
};


foreach my $param ($q->param) {
	if (my ($user) = $param =~ /^(.+)_showpassword$/) {
		if ($$teachers{$user}) {

=head3 Template variables specific to showpassword.tt

=over

=item C<showfirstname>

Displayed user's first name

=cut

			$sk_session->set_var('showfirstname', $$teachers{$user}{firstname});

=item C<showsurname>

Displayed user's surname

=cut

			$sk_session->set_var('showsurname', $$teachers{$user}{surname});

=item C<showusername>

Displayed user's login

=cut

			$sk_session->set_var('showusername', $user);

=item C<showpassword>

Displayed user's password

=cut

			$sk_session->set_var('showpassword', $$teachers{$user}{firstpassword});


=item C<isteachers>

Indicates that the user is displayed by user_teacherpasswords

=back

=cut

			$sk_session->set_var('isteachers', 1);

			$sk_session->print_page("showpassword.tt", $this_file);
			exit;
		} else {
			$sk_session->set_status(sprintf(
					$sk_session->d()->get('%s ist kein Lehrer'),
					$user),
				1);
		}
	}
}

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






my @teachers;
my $lfdnr = 0;
foreach my $teacher (sort keys %$teachers) {
        $lfdnr++;
	push @teachers, {
                        lfdnr => $lfdnr,
			login => $$teachers{$teacher}{uid},
			surname => $$teachers{$teacher}{surname},
			firstname => $$teachers{$teacher}{firstname},
		};
}

=head3 Template variables specific to user_teacherpasswords.tt

=over

=item C<teachers>

The teacher account as an array of hashes with the keys

=over

=item C<lfdnr>

The line nr of the user

=over

=item C<login>

The user's login

=item C<firstname>

User's firstname

=item C<surname>

User's surname

=item C<birthdate>

User's birthdate

=item C<short>

User's sign

=back

=cut

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



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


=back

=head2 Form fields

=over

=item C<${teachers{login}};select>

True if the user is selected for password change.
Created in loop over template variable C<teachers>

=item C<passwords_reset>

Reset passwords of selected users

=item C<passwords_random>

Set password of selected users to random password

=item C<passwords_set>

Set password of selected users to C<userpassword>

=item C<userpassword>

New password to set for selected users

=item C<${teachers{login}}_showpassword>

Show password of teacher.
Created in loop over template variable C<teachers>

=back
