/**
  * Fonction qui retourne un fichier texte contenant les évènements exportés d'après une période et les agendas concernés
  * @author Audrey Vassal <*****@*****.**>
  * @since 2006/08/17
  * @param array $pArEvents tableau d'évènements à exporter, classés par ordre croissant de début
  * @return string $content chaine de caractère représentant le contenu du fichier iCal exporté
  */
 public function getFileICal($pArEvents)
 {
     $serviceDate = new DateService();
     $content = 'BEGIN:VCALENDAR' . "\n";
     $content .= 'VERSION:2.0' . "\n";
     $content .= 'X-WR-TIMEZONE;VALUE=TEXT:' . CopixConfig::get('agenda|timeZone') . "\n";
     $content .= 'METHOD:PUBLISH' . "\n";
     foreach ((array) $pArEvents as $jour => $day) {
         if (!isset($day->events)) {
             continue;
         }
         foreach ($day->events as $event) {
             $content .= 'BEGIN:VEVENT' . "\n";
             $content .= 'SUMMARY:' . $event->title_event . "\n";
             if ($event->alldaylong_event == 1) {
                 //si l'évènement se déroule toute la journée
                 $dateCourante = $jour;
                 $content .= 'DTSTART;VALUE=DATE:' . $dateCourante . "\n";
                 $dateFin = $serviceDate->dateBddToDateFr($dateCourante);
                 $dateFin = $serviceDate->addToDate($dateFin, 1, 0, 0);
                 $dateFin = $serviceDate->dateFrToDateBdd($dateFin);
                 $content .= 'DTEND;VALUE=DATE:' . $dateFin . "\n";
             } else {
                 if ($event->endrepeatdate_event != null) {
                     //si c'est un évènement qui se répète
                     $content .= 'DTSTART;TZID=' . CopixConfig::get('agenda|timeZone') . ':' . $jour . 'T' . $serviceDate->heureWithSeparateurToheureWithoutSeparateur($event->heuredeb_event) . '00Z' . "\n";
                     $content .= 'DTEND;TZID=' . CopixConfig::get('agenda|timeZone') . ':' . $jour . 'T' . $serviceDate->heureWithSeparateurToheureWithoutSeparateur($event->heurefin_event) . '00Z' . "\n";
                 } else {
                     //cas d'un évènement classique
                     //var_dump($event);
                     $content .= 'DTSTART;TZID=' . CopixConfig::get('agenda|timeZone') . ':' . $event->datedeb_event . 'T' . $serviceDate->heureWithSeparateurToheureWithoutSeparateur($event->heuredeb_event) . '00Z' . "\n";
                     $content .= 'DTEND;TZID=' . CopixConfig::get('agenda|timeZone') . ':' . $event->datefin_event . 'T' . $serviceDate->heureWithSeparateurToheureWithoutSeparateur($event->heurefin_event) . '00Z' . "\n";
                 }
             }
             if ($event->desc_event != null) {
                 $content .= 'DESCRIPTION:' . $event->desc_event . "\n";
             }
             if ($event->place_event != null) {
                 $content .= 'LOCATION:' . $event->place_event . "\n";
             }
             $content .= 'END:VEVENT' . "\n";
         }
     }
     $content .= 'END:VCALENDAR';
     return $content;
 }
 /**
  * Fonction qui prépare l'affichage de la vue semaine
  */
 public function getVueSemaine()
 {
     CopixHTMLHeader::addCSSLink(_resource("styles/module_agenda.css"));
     CopixHtmlHeader::addJSLink(CopixUrl::get() . 'js/iconito/module_agenda.js');
     $obj = new AgendaService();
     $listAgendas = $obj->getAvailableAgenda();
     $agendaService = new AgendaService();
     $dateService = new DateService();
     if (($params = $this->_getSessionSemaineParams()) == null) {
         $params = new SemaineParams();
         $params->numSemaine = $this->getRequest('numSemaine', $dateService->dateToWeeknum(mktime()), true);
         $params->annee = $this->getRequest('annee', date('Y'), true);
     } else {
         $params->numSemaine = $this->getRequest('numSemaine', $params->numSemaine, true);
         $params->annee = $this->getRequest('annee', $params->annee, true);
     }
     //pour savoir si on a cliqué sur un agenda à afficher
     if (_request('updateAgendaAffiches')) {
         $arIdAgendas = array();
         foreach ($listAgendas as $agenda) {
             if (_request('agendas_' . $agenda->id_agenda)) {
                 $arIdAgendas[$agenda->id_agenda] = $agenda->id_agenda;
             }
         }
         $agendaService->setAgendaAffiches($arIdAgendas);
     }
     //on récupère en session les agendas à afficher
     $params->agendas = $agendaService->getAgendaAffiches();
     //on met à jour la session
     $this->_setSessionSemaineParams($params);
     //on determine la date de début et de fin de la semaine en cours d'affichage
     $dateDebutSemaine = date('Ymd', $dateService->numweekToDate($params->numSemaine, $params->annee, 1));
     //date au format bdd
     $dateFinSemaine = date('Ymd', $dateService->numweekToDate($params->numSemaine, $params->annee, 0));
     //date au format bdd
     $arEventsSemaine = array();
     //on récupère tous les évènements de la semaine en cours de vue
     foreach ((array) $params->agendas as $idAgenda) {
         $arEventsSemaine[$idAgenda] = $agendaService->checkEventOfAgendaInBdd($idAgenda, $dateDebutSemaine, $dateFinSemaine);
     }
     //on classe ces évènements par jour
     $arEventByDay = $agendaService->getEventsByDay($arEventsSemaine, $dateDebutSemaine, $dateFinSemaine);
     //on ordonne les évènements par ordre croissant d'heure de début d'évènement dans la journée
     $arEventByDay = $agendaService->getEventsInOrderByDay($arEventByDay);
     //on détermine l'heure de début et l'heure de fin pour l'affichage du calendrier
     //on travail sur des heures sans séparateur pour pouvoir les comparer
     $heureDeb = CopixConfig::get('agenda|heuredebcal');
     $heureFin = CopixConfig::get('agenda|heurefincal');
     foreach ((array) $arEventByDay as $jours) {
         if (!isset($jours->events)) {
             continue;
         }
         //print_r($jours);
         foreach ((array) $jours->events as $event) {
             if ($event->alldaylong_event == 0) {
                 if ($dateService->heureWithSeparateurToheureWithoutSeparateur($event->heuredeb_event) < $dateService->heureWithSeparateurToheureWithoutSeparateur($heureDeb)) {
                     $heureDeb = $dateService->heureWithSeparateurToheureWithoutSeparateur($event->heuredeb_event);
                 }
                 if ($dateService->heureWithSeparateurToheureWithoutSeparateur($heureFin) < $dateService->heureWithSeparateurToheureWithoutSeparateur($event->heurefin_event)) {
                     $heureFin = $dateService->heureWithSeparateurToheureWithoutSeparateur($event->heurefin_event);
                 }
             }
         }
     }
     //on arrondit à l'heure inférieure pour l'heure de début et à l'heure supérieure pour l'heure de fin
     $heureDeb = substr($heureDeb, 0, 2);
     if (substr($heureFin, 2, 2) == 0) {
         //si les minutes sont à 0, on arrondit à l'heure
         $heureFin = substr($heureFin, 0, 2);
     } else {
         //si les minutes ne sont pas à 0, on arrondit à l'heure supérieure
         $heureFin = substr($heureFin, 0, 2) + 1;
     }
     //on récupère les leçons de la semaine à afficher
     $arLecons = $agendaService->getLeconsByDay((array) $params->agendas, $dateDebutSemaine, $dateFinSemaine);
     // On récupère les travaux de la semaine par agenda
     $travailDAO = _ioDAO('cahierdetextes|cahierdetextestravail');
     $agenda2cahier = array();
     $arTravauxEnClasse = array();
     $arTravauxAFaire = array();
     foreach ($params->agendas as $agendaId) {
         $agendaInfos = Kernel::getModParentInfo('MOD_AGENDA', $agendaId);
         $agendaType = AgendaType::getAgendaTypeForNode($agendaInfos['type'], $agendaInfos['id']);
         $mods = Kernel::getModEnabled($agendaInfos['type'], $agendaInfos['id']);
         $cahierDeTextes = Kernel::filterModuleList($mods, 'MOD_CAHIERDETEXTES');
         $agendaLevel = Kernel::getLevel($agendaInfos['type'], $agendaInfos['id']);
         if (isset($cahierDeTextes[0])) {
             $agenda2cahier[$agendaId] = $cahierDeTextes[0]->module_id;
             $travauxEnClasse = $travailDAO->findTravauxEnClasseByAgendaParJour($agendaId, $dateDebutSemaine, $dateFinSemaine, $agendaInfos['type'], $agendaInfos['id'], $agendaLevel);
             if (!is_null($travauxEnClasse)) {
                 foreach ($travauxEnClasse as $date => $travail) {
                     $arTravauxEnClasse[$date][$agendaId] = $travail;
                 }
             }
             $travauxAFaire = $travailDAO->findTravauxAFaireByAgendaParJour($agendaId, $dateDebutSemaine, $dateFinSemaine, $agendaInfos['type'], $agendaInfos['id'], $agendaLevel);
             if (!is_null($travauxAFaire)) {
                 foreach ($travauxAFaire as $date => $travail) {
                     $arTravauxAFaire[$date][$agendaId] = $travail;
                 }
             }
         }
     }
     //récupération de la liste des agendas affichés
     $listAgendasAffiches = $obj->getAgendaAffiches();
     //template pour agenda
     $tplAgenda = new CopixTpl();
     $tplAgenda->assign('MAIN_AGENDA', CopixZone::process('agenda|agendavuesemaine', array('elementsSemaineAffichee' => $params, 'arEventByDay' => $arEventByDay, 'heureDeb' => $heureDeb, 'heureFin' => $heureFin, 'arLecons' => $arLecons, 'arTravauxEnClasse' => $arTravauxEnClasse, 'arTravauxAFaire' => $arTravauxAFaire, 'agenda2cahier' => $agenda2cahier)));
     $title = $obj->getCurrentTitle();
     //template principal
     $tpl = new CopixTpl();
     $tpl->assign('TITLE_PAGE', $title['title']);
     $menu = $agendaService->getAgendaMenu('week');
     $tpl->assign('MENU', $menu);
     $tpl->assign('MAIN', $tplAgenda->fetch('agenda|main.agenda.tpl'));
     return new CopixActionReturn(COPIX_AR_DISPLAY, $tpl);
 }