#!/usr/bin/perl

=head1 NAME

class_passwords - allow to show and change class member's passwords

=head1 SYNOPSIS

 https://server/schulkonsole/class_passwords

=head1 DESCRIPTION

C<class_passwords> allows to show and display the passwords of all or single
members of a selected class.
The HTML template is class_passwords.tt and showpassword.tt to display
single passwords.

=head2 Template variables

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

=over

=cut

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

my $this_file = 'class_passwords';


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 $classs = Schulkonsole::Info::groups_classes($sk_session->groups());
my $class = $q->param('classes');
if (    $class
    and $$classs{$class}) {
    $sk_session->param('class', $class);
}
$class = $sk_session->param('class');

if (   not $class
    or not $$classs{$class}) {
	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/class/g;
	$sk_session->param('requested_page',$this_file);
	$sk_session->redirect($url);
}


my @classs;
foreach my $class (sort {
    $$classs{$a}{displayname} cmp $$classs{$b}{displayname} } keys %$classs) {
	push @classs, { gid => $class,
	                name => $$classs{$class}{displayname} };
}

=item C<classes>

An array of hashes of the classes of which the current user is a member
with the keys

=over

=item C<gid>

The GID of the class

=item C<name>

The name of the class

=back

=cut

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


=item C<class_name>

The name of the class

=cut

$sk_session->set_var('class_name', $$classs{$class}{displayname});

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_class(
			$sk_session->userdata('id'),
			$sk_session->get_password(),
			$class, 0);

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

			exit;
		} else {
			$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_class(
			$sk_session->userdata('id'),
			$sk_session->get_password(),
			$class, 1);

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

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

			exit;
		} else {
			$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 $class_userdatas = Schulkonsole::DB::get_class_userdatas($class);


=back

=head3 Template variables specific to showpassword.tt

=over

=cut

foreach my $user (keys %$class_userdatas) {
	if ($q->param("${user}_showpassword")) {

=item C<showfirstname>

The first name of the user of the displayed password

=cut

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

=item C<showsurname>

The surname of the user of the displayed password

=cut

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

=item C<showusername>

The username of the user of the displayed password

=cut

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

=item C<showpassword>

The initial password of the user of the displayed password

=back

=cut

		$sk_session->set_var('showpassword',
			$$class_userdatas{$user}{firstpassword});
	
		$sk_session->print_page("showpassword.tt", $this_file);
		exit;
	}
}


=head3 Template variables specific to class_passwords.tt

=over

=cut

my @students;
my @user_selects;
my $lfdnr = 0;
foreach my $user (sort {
	$$class_userdatas{$a}{surname} cmp $$class_userdatas{$b}{surname} }
	keys %$class_userdatas) {

	$lfdnr++;
	my $is_selected = 0;
	if ($q->param("${user};select")) {
		push @user_selects, $user;
		$is_selected = 1;
	}

	my $student = {
                lfdnr => $lfdnr,
		login => $user,
		firstname => $$class_userdatas{$user}{firstname},
		surname => $$class_userdatas{$user}{surname},
		selected => $is_selected,
	};
	push @students, $student;
}

=item C<students>

An array of hashes with the keys

=over

=item C<lfdnr>

The line nr of the user

=over

=item C<login>

The login of a user

=item C<firstname>

The user's first name

=item C<surname>

The user's surname

=item C<selected>

True if the user was previously selected

=back

=cut

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


eval {
COMMANDS: {
$q->param('passwords_reset') and do {
	if (@user_selects) {
		Schulkonsole::Sophomorix::passwords_reset(
			$sk_session->userdata('id'),
			$sk_session->get_password(),
			@user_selects);

		$sk_session->set_status(
			$sk_session->d()->get('Passwörter zurückgesetzt'), 0);
	} else {
		$sk_session->set_status(
			$sk_session->d()->get('Keine Benutzer ausgewählt'), 1);
	}
	last COMMANDS;
};
$q->param('passwords_random') and do {
	if (@user_selects) {
		Schulkonsole::Sophomorix::passwords_random(
			$sk_session->userdata('id'),
			$sk_session->get_password(),
			@user_selects);

		$sk_session->set_status(
			$sk_session->d()->get('Zufallspasswörter gesetzt'), 0);
	} else {
		$sk_session->set_status(
			$sk_session->d()->get('Keine Benutzer ausgewählt'), 1);
	}
	last COMMANDS;
};
$q->param('passwords_set') and do {
	if (@user_selects) {
		if (my $user_password = $q->param('userpassword')) {
			Schulkonsole::Sophomorix::passwords_set(
				$sk_session->userdata('id'),
				$sk_session->get_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);
		}

	} else {
		$sk_session->set_status(
			$sk_session->d()->get('Keine Benutzer ausgewählt'), 1);
	}
	last COMMANDS;
};
}
};
if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}



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

=back

=head2 Form fields

=over

=item C<passwords_print>

Write PDF-document with passwords to the file name in the PATHINFO

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

Show password of user with login.
Created in loop over template variable C<students>.

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

Checkboxes created in loop over template variable C<students>. The selected
action (passwords_reset/passwords_random/passwords_set) is performed with
the selected logins.

=item C<passwords_reset>

Reset passwords of selected users

=item C<passwords_random>

Set random password for selected users

=item C<passwords_set>

Set password in C<userpassword> for selected users

=item C<userpassword>

Password to be set by passwords_set

=back
