<commit_before><?php

namespace WG\AuditBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use WG\AuditBundle\Form\Type\AuditScoreType;
use WG\AuditBundle\Entity\Audit;
use WG\AuditBundle\Entity\AuditScore;

class AuditController extends Controller
{
    /**
     * View created audits
     * 
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction( Request $request )
    {
        $em = $this->getDoctrine()->getEntityManager();
        $repo = $em->getRepository( 'WGAuditBundle:Audit' );
        $auditlist = $repo->findAll();
        return $this->render( 'WGAuditBundle:Audit:index.html.twig', array(
            'audits' => $auditlist,
        ));
    }
    
    /**
     * Create new audit
     * 
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws NotFoundException
     */
    public function addAction( Request $request )
    {
        $em = $this->getDoctrine()->getEntityManager();
        $repo = $em->getRepository( 'WGAuditBundle:AuditForm' );
        $auditform = $repo->find( $request->get( 'id' ) );
        if ( null === $auditform )
        {
            throw $this->createNotFoundException( 'Audit form not found' );
        }
        if ( null !== $scores = $request->get( 'score' ) )
        {
            $fieldRepo = $em->getRepository( 'WGAuditBundle:AuditFormField' );
            $audit = new Audit();
            $audit->setAuditForm( $auditform );
            // TODO: we may want to set a user, depending on configuration
            // $audit->setAuditReference( null );
            // $audit->setAuditingUser( null );
            // ...
            //
            $em->persist( $audit );
            foreach ( $scores as $fieldId => $scoreData )
            {
                $field = $fieldRepo->find( $fieldId );
                $score = new AuditScore();
                $score->setAudit( $audit );
                $score->setField( $field );
                $score->setScore( $scoreData['value'] );
                $score->setComment( $scoreData['comment'] );
                $em->persist( $score );
            }
            // TODO: calculate result and display / send emails / whatever, depending on configuration
            $audit->setFailed( false ); // replace with result of calculation
            $em->flush();
            return $this->redirect( $this->generateUrl( 'wgaudits' ) );
        }
        $scoreform = $this->createForm( new AuditScoreType() );
        return $this->render( 'WGAuditBundle:Audit:add.html.twig', array(
            'auditform' => $auditform,
            'scoreform' => $scoreform->createView(),
        ));
    }

    /**<commit_msg>implemented setting user ID automagically depending on configuration<commit_after><?php

namespace WG\AuditBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use WG\AuditBundle\Form\Type\AuditScoreType;
use WG\AuditBundle\Entity\Audit;
use WG\AuditBundle\Entity\AuditScore;

class AuditController extends Controller
{
    /**
     * View created audits
     * 
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction( Request $request )
    {
        $em = $this->getDoctrine()->getEntityManager();
        $repo = $em->getRepository( 'WGAuditBundle:Audit' );
        $auditlist = $repo->findAll();
        return $this->render( 'WGAuditBundle:Audit:index.html.twig', array(
            'audits' => $auditlist,
        ));
    }
    
    /**
     * Create new audit
     * 
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws NotFoundException
     */
    public function addAction( Request $request )
    {
        $em = $this->getDoctrine()->getEntityManager();
        $repo = $em->getRepository( 'WGAuditBundle:AuditForm' );
        $auditform = $repo->find( $request->get( 'id' ) );
        if ( null === $auditform )
        {
            throw $this->createNotFoundException( 'Audit form not found' );
        }
        if ( null !== $scores = $request->get( 'score' ) )
        {
            $fieldRepo = $em->getRepository( 'WGAuditBundle:AuditFormField' );
            $audit = new Audit();
            $audit->setAuditForm( $auditform );
            // depending on configuration, set a user ID:
            if ( $this->container->getParameter( 'wg.audit.user.class' ) )
            {
                $user = $this->container->get( 'security.context' )->getToken()->getUser();
                $prop = $this->container->getParameter( 'wg.audit.user.property' );
                if ( $user && $prop )
                {
                    $method = 'get' . ucfirst( $prop );
                    $audit->setAuditingUser( $user->$method() );
                }
            }
            // TODO: add input field to form in order to set a reference
            // $audit->setAuditReference( null );
            // ...
            //
            $em->persist( $audit );
            foreach ( $scores as $fieldId => $scoreData )
            {
                $field = $fieldRepo->find( $fieldId );
                $score = new AuditScore();
                $score->setAudit( $audit );
                $score->setField( $field );
                $score->setScore( $scoreData['value'] );
                $score->setComment( $scoreData['comment'] );
                $em->persist( $score );
            }
            // TODO: calculate result and display / send emails / whatever, depending on configuration
            $audit->setFailed( false ); // replace with result of calculation
            $em->flush();
            return $this->redirect( $this->generateUrl( 'wgaudits' ) );
        }
        $scoreform = $this->createForm( new AuditScoreType() );
        return $this->render( 'WGAuditBundle:Audit:add.html.twig', array(
            'auditform' => $auditform,
            'scoreform' => $scoreform->createView(),
        ));
    }

    /**
{'fraction_non_alphanumeric': 0.14079135547433658, 'fraction_numerical': 0.0, 'mean_word_length': 2.028873917228104, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '2336960', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1794, 'n_words': 356}