#! /usr/bin/perl
#
# Copyright (C) 2012-2016 Alexis Bienvenue <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

package AMC::Substitute;

use AMC::Basic;

use utf8;

sub new {
    my (%o)=@_;
    my $self={'names'=>'',
	      'scoring'=>'',
	      'assoc'=>'',
	      'name'=>'','chsign'=>4,
	      'lk'=>'',
	  };

    for (keys %o) {
	$self->{$_}=$o{$_} if(defined($self->{$_}));
    }

    bless $self;
    return($self);
}

sub format_note {
  my ($self,$mark)=@_;

  if($self->{'chsign'}) {
    $mark=sprintf("%.*g",$self->{'chsign'},$mark);
  }
  return($mark);
}

# Affiche le niveau acquis pour chaque compétence entrée comme groupe
# <30%: non maîtrisé - <70%: maîtrise incomplète - <100%: maîtrise satisfaisante - =100%: maîtrise experte
sub format_skills {
  my ($self,$student,$copy)=@_;
  my $sth = $self->{scoring}->statement('studentSkillLevel');
  $sth->execute($student, $copy);
  my $stext = "\t\t\t\t\t\t\t\t\t";
  while(my @row=$sth->fetchrow_array) { # retrieve one row
    if (scalar(split '',$stext) > 6) {$stext = $stext . "\n\t";}
    $stext = $stext . @row[0] . " : ";
    if (@row[1]>0.9) {
      $stext = $stext . "maîtrise experte 😄";
    } elsif (@row[1]>0.5) {
      $stext = $stext . "maîtrise satisfaisante 😊";
    } elsif (@row[1]>0) {
      $stext = $stext . "maîtrise incomplète 😕";
    } else {
      $stext = $stext . "non maîtrisé 😟";
    }
  }
  return($stext);
}

# Affiche les pourcentages de réussite par groupe, arrondi à l'entier, le minimum étant fixé à zéro
sub format_knowledge {
  my ($self,$student,$copy)=@_;
  my $sth = $self->{scoring}->statement('studentSkillLevel');
  $sth->execute($student, $copy);
  my $stext = "\n";
  while(my @row=$sth->fetchrow_array) { # retrieve one row
    if (scalar(split '',$stext) > 6) {$stext = $stext . "  —  ";}
    my $knowledge=int(@row[1]*100+.5);
    $stext = $stext . @row[0] . " : " . (0, $knowledge)[$knowledge>0] . " %";
  }
  return($stext);
}

sub substitute {
  my ($self,$text,$student,$copy)=@_;

  if($self->{'scoring'}) {
    my $student_mark=$self->{'scoring'}->student_global($student,$copy);

    if($student_mark) {
$text =~ s/\%[M]/$self->format_note($student_mark->{'max'})/ge;
       $text =~ s/\%[s]/$self->format_note($student_mark->{'mark'})/ge;
       $text =~ s/\%[m]/$self->format_note($self->{'scoring'}->variable('mark_max'))/ge;
       $text =~ s/\%[C]/$self->format_skills($student, $copy)/ge;
       $text =~ s/\%[c]/$self->format_knowledge($student, $copy)/ge;
    } else {
      debug "No marks found ! Copy=".studentids_string($student,$copy);
    }
  }

  $text =~ s/\%[n]/$self->{'name'}/ge;

  if($self->{'assoc'} && $self->{'names'}) {
    $self->{'lk'}=$self->{'assoc'}->variable('key_in_list')
      if(!$self->{'lk'});

    my $i=$self->{'assoc'}->get_real($student,$copy);
    my $n;

    debug "Association -> ID=$i";

    if(defined($i)) {
      ($n)=$self->{'names'}->data($self->{'lk'},$i,test_numeric=>1);
      if($n) {
	$text=$self->{'names'}->substitute($n,$text,'prefix'=>'%');
      }
    }
  }

  return($text);
}

1;
