<?php

namespace Cadoles\PortalBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Filesystem\Filesystem;

class FeedController extends Controller
{
    public function limesurveyAction($id)
    {

        $directory=$this->get('kernel')->getRootDir()."/../uploads/limesurvey/".$id;
        $feeds=[];
        $fs = new Filesystem();

        if($fs->exists($directory."/invitation.json")) {
            $file=file_get_contents($directory."/invitation.json");
            $json = substr($file, 0, -2);  // sup dernier ,
            $surveys = json_decode("{".$json."}", true);
            foreach($surveys as $id => $survey) {
                array_push($feeds,[
                    "id"=>$id,
                    "title"=>$survey["title"],
                    "url"=>$survey["url"],
                    "date"=>$survey["date"],
                    "description"=>$survey["description"],
                    "category"=>"limesurvey"]);
            }
        }

        $response = new Response($this->renderView('CadolesPortalBundle:Feed:index.xml.twig',array(
            "feeds" => $feeds,
        )));

        $response->headers->set('Content-Type', 'application/xml; charset=utf-8');
        return $response;
    }

    public function moodleAction($id)
    {

        $directory=$this->get('kernel')->getRootDir()."/../uploads/moodle/".$id;
        $feeds=[];
        $fs = new Filesystem();

        if($fs->exists($directory."/invitation.json")) {
            $file=file_get_contents($directory."/invitation.json");
            $json = substr($file, 0, -2);  // sup dernier ,
            $surveys = json_decode("{".$json."}", true);
            foreach($surveys as $id => $survey) {
                array_push($feeds,[
                    "id"=>$id,
                    "title"=>$survey["title"],
                    "url"=>$survey["url"],
                    "date"=>$survey["date"],
                    "description"=>$survey["description"],
                    "category"=>"moodle"]);
            }
        }

        $response = new Response($this->renderView('CadolesPortalBundle:Feed:index.xml.twig',array(
            "feeds" => $feeds,
        )));

        $response->headers->set('Content-Type', 'application/xml; charset=utf-8');
        return $response;
    }    
}
