#! /usr/bin/perl

=head1 NAME

room_test_password - set the workstations password

=head1 SYNOPSIS

 https://server/schulkonsole/room_test_password

=head1 DESCRIPTION

C<room_test_password> lets you set a new workstations password during an exam.
The HTML template is room_test.tt.

=head2 Template variables

There are no other template variables but the variables of
Schulkonsole::Session and Schulkonsole::Room::set_vars().

=cut

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

my $this_file = 'room_test_password';


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

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 ($q->param('passwordtest')) {
	my $workstation_password = $q->param('workstationpassword');
	if (not $workstation_password) {
		$sk_session->set_status(
			 $sk_session->d()->get(
				 "Kein Workstationpasswort angegeben."),
				 1);
	} else {
		eval {
			my $id = $sk_session->userdata('id');
			my $password = $sk_session->get_password();

			$room_session->change_workstation_passwords(
				$id, $password, $workstation_password);
			$sk_session->set_status_redirect(
				$sk_session->d()->get("Workstationpasswort geändert."), 0);


			$room_session->param('test_step',
			                     Schulkonsole::Config::TEST_COLLECT)
				if (  $room_session->param('test_step')
				    < Schulkonsole::Config::TEST_COLLECT);


			my $url = $q->url( -absolute => 1 );
			$url =~ s/$this_file$/room_test_collect/g;
			$sk_session->redirect($url);
		};
		if ($@) {
			$sk_session->standard_error_handling($this_file, $@);
		}
	}
}


$q->param('workstationpassword', Schulkonsole::Room::random_password(10))
	unless $q->param('workstationpassword');


$room_session->set_vars($sk_session);

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


=head2 Form fields

=over

=item C<passwordtest>

Set the password

=item C<workstationpassword>

The password to be set for the workstation accounts

=back

