<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Training\ModuleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\JoinColumn;
use App\Traits\Actions;
use Doctrine\ORM\Mapping as ORM;
#[Entity(repositoryClass: ModuleRepository::class)]
#[ORM\Table(name: 'hs_tc_training_module')]
#[ApiResource]
class Module implements \Stringable
{
use Actions;
#[ORM\Column(name: "ref", type: "string", length: 255)]
private ?string $ref = null;
#[ORM\Column(name: "name", type: "string", length: 255)]
private $name;
#[ManyToOne(targetEntity: "Training", inversedBy: "modules")]
#[JoinColumn(nullable: false)]
private ?Training $training = null;
#[ORM\Column(name: "coefficient", type: "integer", length: 255)]
private ?int $coefficient = null;
#[ORM\Column(name: "semester", type: "string", length: 255)]
private ?string $semester = null;
#[ORM\OneToMany(mappedBy: "module", targetEntity: "Assignation")]
private ?Collection $assignations = null;
#[ORM\OneToMany(mappedBy: "module", targetEntity: Notemodule::class, cascade: ["all"])]
private ?Collection $notes = null;
#[ORM\Column()]
private ?int $hourlyVolume = null;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=false;
$this->notes=new ArrayCollection();
$note3=new Notemodule();
$note3->setGenre('controle')->setName('c1');
$this->addNote($note3);
$note4=new Notemodule();
$note4->setGenre('controle')->setName('c2');
$this->addNote($note4);
$note1=new Notemodule();
$note1->setGenre('examen')->setName('ep');
$this->addNote($note1);
$note2=new Notemodule();
$note2->setGenre('examen')->setName('et');
$this->addNote($note2);
}
/**
* @return string
*/
public function getRef(): ?string
{
return $this->ref;
}
/**
* @param string $ref
*/
public function setRef(?string $ref): Module
{
$this->ref = $ref;
return $this;
}
/**
* Set nom
*
* @param string $name
* @return Module
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
function __toString(): string
{
return (string) $this->name;
}
/**
* @return Training
*/
public function getTraining(): ?Training
{
return $this->training;
}
/**
* @param Training $training
*/
public function setTraining(?Training $training): Module
{
$this->training = $training;
return $this;
}
/**
* @return integer
*/
public function getCoefficient(): ?int
{
return $this->coefficient;
}
/**
* @param integer $coefficient
*/
public function setCoefficient(?int $coefficient): Module
{
$this->coefficient = $coefficient;
return $this;
}
/**
* @return string
*/
public function getSemester(): ?string
{
return $this->semester;
}
/**
* @param string $semester
*/
public function setSemester(?string $semester): Module
{
$this->semester = $semester;
return $this;
}
/**
* Add assignation
*
*
* @return Module
*/
public function addAssignation(Assignation $assignation)
{
$assignation->setModule($this);
$this->assignations[] = $assignation;
return $this;
}
/**
* Remove assignation
*/
public function removeAssignation(Assignation $assignation)
{
$this->assignations->removeElement($assignation);
}
/**
* Get assignations
*
* @return Collection
*/
public function getAssignations()
{
return $this->assignations;
}
/**
* Add note
*
*
* @return Module
*/
public function addNote(Notemodule $note)
{
$note->setModule($this);
$this->notes->add($note);
return $this;
}
/**
* Remove note
*/
public function removeNote(Notemodule $note)
{
$this->notes->removeElement($note);
}
/**
* Get notes
*
* @return ArrayCollection
*/
public function getNotes()
{
return $this->notes;
}
public function getHourlyVolume(): ?int
{
return $this->hourlyVolume;
}
public function setHourlyVolume(?int $hourlyVolume): void
{
$this->hourlyVolume = $hourlyVolume;
}
public function getFormerGroupe(Groupe $groupe): ?\App\Entity\Training\Former{
$former=null;
/** @var Assignation $assignation */
foreach ($this->getAssignations() as $assignation){
if($assignation->getGroupe()===$groupe) $former=$assignation->getFormer();
}
return $former;
}
public function getPCInput(Groupe $groupe){
$sm=0;
$nbr=0;
/** @var Assignation $assign */
foreach ($this->assignations as $assign){
if($assign->getGroupe()===$groupe){
foreach ($assign->getModule()->getNotes() as $note){
$sm+=$note->getPCInput($groupe);
$nbr++;
}
}
}
if($nbr==0) return 0;
return floor($sm/$nbr);
}
public function getNbrCC(){
$i=0;
/** @var Notemodule $note */
foreach ($this->getNotes() as $note){
if($note->getGenre()=='controle') $i++;
}
return $i;
}
public function getLevel(){
return match ($this->semester) {
'Semestre 2', 'Semestre 1' => 'first_year',
'Semestre 3', 'Semestre 4' => 'second_year',
'Semestre 5', 'Semestre 6' => 'third_year',
default => '',
};
}
public function getSchoolyearGroupe(Groupe $groupe){
return match ($this->getTrainingYear()) {
'first_year' => $groupe->getSchoolyear1(),
'second_year' => $groupe->getSchoolyear2(),
'third_year' => $groupe->getSchoolyear3(),
default => null,
};
}
public function hasCC(){
$rep=false;
/** @var Notemodule $note */
foreach ($this->getNotes() as $note){
if($note->getGenre()=='controle') $rep=true;
}
return $rep;
}
}