#! /usr/bin/perl

=head1 NAME

class_shares - activate/deactivate shares

=head1 SYNOPSIS

 https://server/schulkonsole/class_shares

=head1 DESCRIPTION

C<class_shares> lets you activate and deactivate the shares of the members
of a class.
The HTML template is class_shares.tt.

=head2 Template variables

Additionally to the variables of Schulkonsole::Session C<class_shares>
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_shares';


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 $class_userdatas = Schulkonsole::DB::get_class_userdatas($class);
my @login_ids;
foreach my $user (keys %$class_userdatas) {
	push @login_ids, $$class_userdatas{$user}{id};
}



my $id = $sk_session->userdata('id');
my $password = $sk_session->get_password();
if ($q->param('acceptshares')) {
	eval {
	my $share_states = Schulkonsole::Sophomorix::share_states(
		$id, $password, @login_ids);

	my @activate_shares;
	my @deactivate_shares;
	foreach my $user (keys %$class_userdatas) {
		my $user_id = $$class_userdatas{$user}{id};

		if ($q->param("$user;shares")) {
			push @activate_shares, $user unless $$share_states{$user_id};
		} else {
			push @deactivate_shares, $user if $$share_states{$user_id};
		}
	}

	if (   @activate_shares
	    or @deactivate_shares) {
		Schulkonsole::Sophomorix::shares_on($id, $password, @activate_shares)
			if @activate_shares;
		Schulkonsole::Sophomorix::shares_off($id, $password, @deactivate_shares)
			if @deactivate_shares;

		$sk_session->set_status(
			$sk_session->d()->get("Änderungen übernommen."), 0);
	} else {
		$sk_session->set_status(
			$sk_session->d()->get("Keine Änderungen."), 1);
	}
	};
	if ($@) {
		$sk_session->standard_error_handling($this_file, $@);
	}
}




eval {
my $share_states = Schulkonsole::Sophomorix::share_states(
	$id, $password, @login_ids);


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

	my $student = {
		login => $user,
		firstname => $$class_userdatas{$user}{firstname},
		surname => $$class_userdatas{$user}{surname},
		shares => $$share_states{$$class_userdatas{$user}{id}},
	};
	push @students, $student;

}

=item C<students>

An array of hashes with the keys

=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<shares>

True if the user's share is activated

=back

=cut

$sk_session->set_var('students', \@students);
};
if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}


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

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


=back

=head2 Form fields

=over

=item C<acceptshares>

Accept new shares settings

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

Checkboxes created in loop over template variable C<students>. If true
the share of the user is activated

=back

