#! /usr/bin/perl

=head1 NAME

project_shares - activate/deactivate shares

=head1 SYNOPSIS

 https://server/schulkonsole/project_shares

=head1 DESCRIPTION

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

=head2 Template variables

Additionally to the variables of Schulkonsole::Session C<project_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 = 'project_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 $projects = Schulkonsole::Info::groups_projects($sk_session->groups());



my $project = $q->param('projects');
if (    $project
    and $$projects{$project}) {
	$sk_session->param('project', $project);
}
$project = $sk_session->param('project');

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

if (not Schulkonsole::DB::is_project_admin($$projects{$project}{id},
        	$sk_session->userdata('uidnumber'))) {
	$sk_session->set_status_redirect($sk_session->d()->get(
		'Sie sind kein Projektleiter'),
		1);

	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/project_members/g;
	$sk_session->redirect($url);
}






my $member_userdatas
	= Schulkonsole::DB::project_user_members($$projects{$project}{gidnumber});

my @login_ids;
foreach my $user (keys %$member_userdatas) {
	push @login_ids, $$member_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 %$member_userdatas) {
		next if $$member_userdatas{$user}{gid} eq 'teachers';

		my $user_id = $$member_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 {
	$$member_userdatas{$a}{surname} cmp $$member_userdatas{$b}{surname} }
	keys %$member_userdatas) {
	next if $$member_userdatas{$user}{gid} eq 'teachers';

	my $student = {
		login => $user,
		firstname => $$member_userdatas{$user}{firstname},
		surname => $$member_userdatas{$user}{surname},
		shares => $$share_states{$$member_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 @projects;
foreach my $project (sort {
	$$projects{$a}{displayname} cmp $$projects{$b}{displayname} }
	keys %$projects) {
	push @projects, { gid => $project,
	                  name => $$projects{$project}{displayname},
	                };
}

=item C<projects>

Projects of the current user as an array of hashes with the keys

=over

=item C<gid>

GID of the project

=item C<name>

Name of the project

=back

=cut

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


=item C<isadmin>

Indicates that the current user is an administrator of the selected project

=cut

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

=item C<project_gid>

GID of the selected project

=cut

$sk_session->set_var('project_gid', $project);

=item C<project>

Name of the selected project

=cut

$sk_session->set_var('project', $$projects{$project}{displayname});



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


=back

=head2 Form fields

=over

=item C<projects>

Select the project with this GID.
Possible values read in loop over template variable C<projects>.

=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

