<commit_before><?php

namespace Mobility\PlacementBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Mobility\PlacementBundle\Entity\Placement;
use Mobility\PlacementBundle\Form\PlacementType;
use Mobility\MainBundle\Entity\Year;
use Mobility\StudentBundle\Entity\Student;

/**
 * @Route("/admin/placements")
 */
class AdminController extends Controller
{
    /**
     * @Route("/", defaults={"year" = 0}, name="placement_list")
     * @Route("/list-{year}", requirements={"year" = "\d+"}, name="placement_list_year")
     * @Template()
     */
    public function listAction($year) {
        $repo_years = $this->getDoctrine()->getManager()->getRepository('MobilityMainBundle:Year');
        $years = $repo_years->getYears();
        if (count($years) == 0) return $this->redirect($this->generateUrl('overview'));

        if ($year == 0) $year = $years[0];

    	$repo = $this->getDoctrine()->getManager()->getRepository('MobilityPlacementBundle:Placement');
    	$placements = $repo->getPlacements($year);

        $y = $repo_years->findOneBy(array('year' => $year));
        $public = $y->getPlacementsPublic();
        $unlocked = !$y->getPlacementsLocked();

        return array('year' => $year, 'years' => $years, 'public' => $public, 'placements' => $placements, 'unlocked' => $unlocked);
    }
    
    /**
     * @Route("/auto-{year}", requirements={"year" = "\d+"}, name="placements_auto")
     */
    public function autoAction($year) {
        $em = $this->getDoctrine()->getManager();
        $repo_placements = $em->getRepository('MobilityPlacementBundle:Placement');
        $repo_students = $em->getRepository('MobilityStudentBundle:Student');
        $repo_universities = $em->getRepository('MobilityUniversityBundle:University');

        if ($repo_students->countByState($year, 0) > 0) {
            $this->get('session')->getFlashBag()->add('error', 'Erreur : certains étudiants peuvent encore changer leurs voeux.');
            return $this->redirect($this->generateUrl('placement_list_year', array('year' => $year)));
        } else if ($repo_students->countByNotState($year, 1) > 0) {
            $this->get('session')->getFlashBag()->add('error', 'Erreur : certains étudiants ont un état incohérent.');
            return $this->redirect($this->generateUrl('placement_list_year', array('year' => $year)));
        }

        $placements = $repo_placements->getPlacements($year);

        // On supprime les affectations temporaires
        foreach ($placements as $p) {
            if ($p->getState() == 0) {
                $em->remove($p);
            }
        }
        $em->flush();

        // Liste des affectations fixées
        $placements = $repo_placements->getPlacements($year);

        // Liste des universités
        // cf questions : $universities = $repo_universities->findBy(array('partnershipState' => true));
        $universities = $repo_universities->findAll();

        // Tableau des étudiants déjà affectés dans une université
        $students_placed = array();
        // Tableau contenant le nombre d'étudiants affectés dans chaque université
        $students_count = array();

        foreach ($universities as $u) {
            $students_count[$u->getId()] = 0;
        }
        foreach ($placements as $p) {
            $students_placed[] = $p->getStudent()->getId();
            $students_count[$p->getUniversity()->getId()]++;
        }

        // On récupère la liste des étudiants concernés, triés par leur classement
        $students_4a = $repo_students->getRankedStudents($year, 4);
        $students_3a = $repo_students->getRankedStudents($year, 3);

        $j = 0; $k = 0;
        for ($i=0; $i < count($students_4a) + count($students_3a); $i++) {
            $s = null;

            if ($j >= count($students_4a)) {
                $s = $students_3a[$k];
            } else if ($k >= count($students_3a)) {
                $s = $students_4a[$j];<commit_msg>Fix auto placement<commit_after><?php

namespace Mobility\PlacementBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Mobility\PlacementBundle\Entity\Placement;
use Mobility\PlacementBundle\Form\PlacementType;
use Mobility\MainBundle\Entity\Year;
use Mobility\StudentBundle\Entity\Student;

/**
 * @Route("/admin/placements")
 */
class AdminController extends Controller
{
    /**
     * @Route("/", defaults={"year" = 0}, name="placement_list")
     * @Route("/list-{year}", requirements={"year" = "\d+"}, name="placement_list_year")
     * @Template()
     */
    public function listAction($year) {
        $repo_years = $this->getDoctrine()->getManager()->getRepository('MobilityMainBundle:Year');
        $years = $repo_years->getYears();
        if (count($years) == 0) return $this->redirect($this->generateUrl('overview'));

        if ($year == 0) $year = $years[0];

    	$repo = $this->getDoctrine()->getManager()->getRepository('MobilityPlacementBundle:Placement');
    	$placements = $repo->getPlacements($year);

        $y = $repo_years->findOneBy(array('year' => $year));
        $public = $y->getPlacementsPublic();
        $unlocked = !$y->getPlacementsLocked();

        return array('year' => $year, 'years' => $years, 'public' => $public, 'placements' => $placements, 'unlocked' => $unlocked);
    }
    
    /**
     * @Route("/auto-{year}", requirements={"year" = "\d+"}, name="placements_auto")
     */
    public function autoAction($year) {
        $em = $this->getDoctrine()->getManager();
        $repo_placements = $em->getRepository('MobilityPlacementBundle:Placement');
        $repo_students = $em->getRepository('MobilityStudentBundle:Student');
        $repo_universities = $em->getRepository('MobilityUniversityBundle:University');

        if ($repo_students->countByState($year, 0) > 0) {
            $this->get('session')->getFlashBag()->add('error', 'Erreur : certains étudiants peuvent encore changer leurs voeux.');
            return $this->redirect($this->generateUrl('placement_list_year', array('year' => $year)));
        } else if ($repo_students->countByNotState($year, 1) > 0) {
            $this->get('session')->getFlashBag()->add('error', 'Erreur : certains étudiants ont un état incohérent.');
            return $this->redirect($this->generateUrl('placement_list_year', array('year' => $year)));
        }

        $placements = $repo_placements->getPlacements($year);

        // On supprime les affectations temporaires
        foreach ($placements as $p) {
            if ($p->getState() == 0) {
                $em->remove($p);
            }
        }
        $em->flush();

        // Liste des affectations fixées
        $placements = $repo_placements->getPlacements($year);

        // Liste des universités
        // cf questions : $universities = $repo_universities->findBy(array('partnershipState' => true));
        $universities = $repo_universities->findAll();

        // Tableau des étudiants déjà affectés dans une université
        $students_placed = array();
        // Tableau contenant le nombre d'étudiants affectés dans chaque université
        $students_count = array();

        foreach ($universities as $u) {
            $students_count[$u->getId()] = 0;
        }
        foreach ($placements as $p) {
            $students_placed[] = $p->getStudent()->getId();
            $students_count[$p->getUniversity()->getId()]++;
        }

        // On récupère la liste des étudiants concernés, triés par leur classement
        $students_4a = $repo_students->getRankedStudents($year, 4);
        $students_3a = $repo_students->getRankedStudents($year, 3);

        $i = 0; $j = 0;
        for ($i=0; $i <= (count($students_4a) - 1) + (count($students_3a) - 1); $i++) {
            $s = null;

            if ($j >= count($students_4a)) {
                $s = $students_3a[$k];
            } else if ($k >= count($students_3a)) {
                $s = $students_4a[$j];
{'fraction_non_alphanumeric': 0.16695163654127992, 'fraction_numerical': 0.005862237420615535, 'mean_word_length': 3.100650976464697, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400214', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2464, 'n_words': 453}