SessionActivityController.php 12.7 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19 20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */
Ghislain Loas's avatar
Ghislain Loas committed
21
class Admin_SessionActivityController extends ZendAfi_Controller_Action {
22
  public function getPlugins() {
Ghislain Loas's avatar
Ghislain Loas committed
23 24
    return ['ZendAfi_Controller_Plugin_ResourceDefinition_SessionActivity',
            'ZendAfi_Controller_Plugin_Manager_SessionActivity'];
Patrick Barroca's avatar
Patrick Barroca committed
25
  }
26 27


Patrick Barroca's avatar
Patrick Barroca committed
28 29 30
  public function indexAction() {
    $this->_redirectToIndex();
  }
31 32


Patrick Barroca's avatar
Patrick Barroca committed
33 34 35 36 37
  protected function _redirect($url, array $options = []) {
    $year = $this->_getParam('year');
    $url .= ($year ? '/year/'.$year : '');
    return parent::_redirect($url, $options);
  }
Laurent's avatar
Laurent committed
38 39


40
  protected function _redirectToIndex() {
Ghislain Loas's avatar
Ghislain Loas committed
41
    $this->_redirect('/admin/activity/index');
Patrick Barroca's avatar
Patrick Barroca committed
42
  }
43 44


Patrick Barroca's avatar
Patrick Barroca committed
45
  public function inscriptionsAction() {
Ghislain Loas's avatar
Ghislain Loas committed
46
    $session = Class_SessionActivity::getLoader()->find((int)$this->_getParam('id'));
47

Patrick Barroca's avatar
Patrick Barroca committed
48
    if ($id_user_to_delete = $this->_getParam('delete')) {
49 50
      $subscription_to_delete = Class_SessionActivityInscription::deleteBy(['stagiaire_id' => $id_user_to_delete,
                                                                            'session_activity_id' => $session->getId()]);
51

Ghislain Loas's avatar
Ghislain Loas committed
52
      $redirect_url = '/admin/session-activity/inscriptions/id/'.$session->getId();
Patrick Barroca's avatar
Patrick Barroca committed
53 54
      if ($_GET)
        $redirect_url .= '?'.http_build_query($_GET);
55

Patrick Barroca's avatar
Patrick Barroca committed
56 57 58
      $this->_redirect($redirect_url);
      return;
    }
59

Patrick Barroca's avatar
Patrick Barroca committed
60 61 62 63 64 65
    if ($this->_request->isPost()
        && ($ids_users_to_subscribe = $this->_request->getPost('users'))) {
      foreach($ids_users_to_subscribe as $id)
        $session->addStagiaire(Class_Users::getLoader()->find($id));
      $session->save();
    }
66

Patrick Barroca's avatar
Patrick Barroca committed
67 68 69 70
    $this->view->session = $session;
    $this->view->page = $this->_request->getParam('page');
    $this->view->search = $this->_getParam('search');
    $this->view->titre = sprintf(
71
      'Liste des participants à la session du %s de l\'activité "%s"',
Patrick Barroca's avatar
Patrick Barroca committed
72
      $this->view->humanDate($session->getDateDebut(), 'd MMMM YYYY'),
Ghislain Loas's avatar
Ghislain Loas committed
73
      $session->getActivity()->getLibelle());
Patrick Barroca's avatar
Patrick Barroca committed
74
  }
75 76 77



Patrick Barroca's avatar
Patrick Barroca committed
78
  public function exportinscriptionsAction() {
Ghislain Loas's avatar
Ghislain Loas committed
79
    $session = Class_SessionActivity::getLoader()->find((int)$this->_getParam('id'));
80

Patrick Barroca's avatar
Patrick Barroca committed
81
    $this->_helper->getHelper('ViewRenderer')->setNoRender(true);
82

Patrick Barroca's avatar
Patrick Barroca committed
83 84 85 86 87 88 89 90 91 92
    // on va devoir modifier les entetes HTTP
    $response = Zend_Controller_Front::getInstance()->getResponse();
    $response->canSendHeaders(true);
    $response->setHeader('Content-Type',
                         'text/csv;charset=utf-8',
                         //                        sprintf('text/csv; name="session_%d.csv"', $session->getId()),
                         true);
    $response->setHeader('Content-Disposition',
                         sprintf('attachment; filename="session_%d.csv"', $session->getId()),
                         true);
93

Ghislain Loas's avatar
Ghislain Loas committed
94
    $content = sprintf("Activité;%s;\n".
Patrick Barroca's avatar
Patrick Barroca committed
95 96 97
                       "Session;%s;\n".
                       "Durée;%dh;\n".
                       "Effectif;%d-%d;\n",
Ghislain Loas's avatar
Ghislain Loas committed
98
                       $session->getLibelleActivity(),
Patrick Barroca's avatar
Patrick Barroca committed
99 100 101 102
                       $this->view->humanDate($session->getDateDebut(), 'd MMMM YYYY'),
                       $session->getDuree(),
                       $session->getEffectifMin(),
                       $session->getEffectifMax());
103

Patrick Barroca's avatar
Patrick Barroca committed
104
    $content .= sprintf("\nNom;Prénom;Identifiant\n");
105

Patrick Barroca's avatar
Patrick Barroca committed
106 107 108 109 110 111
    foreach ($session->getStagiairesSortedByNom() as $stagiaire) {
      $content .= sprintf("%s;%s;%s\n",
                          $stagiaire->getNom(),
                          $stagiaire->getPrenom(),
                          $stagiaire->getLogin());
    }
112

Patrick Barroca's avatar
Patrick Barroca committed
113 114
    $response->setBody($content);
  }
115 116 117



Patrick Barroca's avatar
Patrick Barroca committed
118
  public function impressionsAction() {
Ghislain Loas's avatar
Ghislain Loas committed
119
    $session = Class_SessionActivity::find((int)$this->_getParam('id'));
Patrick Barroca's avatar
Patrick Barroca committed
120
    if(!$session){
Ghislain Loas's avatar
Ghislain Loas committed
121
      $this->_redirect('admin/activity');
Patrick Barroca's avatar
Patrick Barroca committed
122 123
      return;
    }
124

125
    $this->view->titre = $this->_('Impressions pour la session du %s de l\'activité "%s"',
Patrick Barroca's avatar
Patrick Barroca committed
126
                                 $this->view->humanDate($session->getDateDebut(), 'd MMMM YYYY'),
Ghislain Loas's avatar
Ghislain Loas committed
127
                                 $session->getLibelleActivity());
Patrick Barroca's avatar
Patrick Barroca committed
128
    $this->view->session = $session;
129

Patrick Barroca's avatar
Patrick Barroca committed
130 131
    $this->view->impressions = array(
      'Liste des stagiaires' => array('action' => 'liste_stagiaires',
132
                                      'form' => $this->_modeleFusionForm('ACTIVITY_LISTE_STAGIAIRES',
Patrick Barroca's avatar
Patrick Barroca committed
133
                                                                         $session)),
134

Patrick Barroca's avatar
Patrick Barroca committed
135
      'Fiche d\'émargement' => array('action' => 'fiche_emargement',
136
                                     'form' => $this->_modeleFusionForm('ACTIVITY_EMARGEMENT',
Patrick Barroca's avatar
Patrick Barroca committed
137 138
                                                                        $session)),
      'Convocations' => array('action' => 'convocations',
139
                              'form' => $this->_modeleFusionForm('ACTIVITY_CONVOCATION',
Patrick Barroca's avatar
Patrick Barroca committed
140
                                                                 $session)),
141

Patrick Barroca's avatar
Patrick Barroca committed
142
      'Attestations' => array('action' => 'attestations',
143
                              'form' => $this->_modeleFusionForm('ACTIVITY_ATTESTATION',
Patrick Barroca's avatar
Patrick Barroca committed
144
                                                                 $session)),
145

Patrick Barroca's avatar
Patrick Barroca committed
146
      'Refus' => array('action' => 'refus',
147
                       'form' => $this->_modeleFusionForm('ACTIVITY_REFUS',
Patrick Barroca's avatar
Patrick Barroca committed
148 149
                                                          $session)));
  }
150 151


Patrick Barroca's avatar
Patrick Barroca committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  protected function _modeleFusionForm($modele_fusion_name, $session) {
    $modele_fusion = Class_ModeleFusion::get($modele_fusion_name);
    return $this->view
      ->newForm(array('id' => $modele_fusion->getNom().'_FORM'))
      ->setMethod('post')
      ->setAction($this->view->url(array('action' => 'modele_impression',
                                         'id' => $modele_fusion->getId(),
                                         'session_id' => $session->getId())))
      ->addElement('ckeditor',
                   $modele_fusion->getNom(),
                   array(
                     'label' => '',
                     'value' => $modele_fusion->getContenu(),
                     'required' => true,
                     'allowEmpty' => false));
  }
168 169 170



Patrick Barroca's avatar
Patrick Barroca committed
171 172 173 174 175
  public function modeleimpressionAction() {
    $modele = Class_ModeleFusion::getLoader()->find($this->_getParam('id'));
    $modele
      ->setContenu($this->_request->getPost($modele->getNom()))
      ->save();
176
    $this->_helper->notify($this->_('Modèle sauvegardé'));
Ghislain Loas's avatar
Ghislain Loas committed
177
    $this->_redirect('admin/session-activity/impressions/id/'.$this->_getParam('session_id'));
Patrick Barroca's avatar
Patrick Barroca committed
178
  }
179 180


Patrick Barroca's avatar
Patrick Barroca committed
181
  public function ficheemargementAction() {
182
    $this->_renderLettreFusion(new SessionOneLetterPerDayFusionStrategy(Class_ModeleFusion::getTrainingTemplateFor('EMARGEMENT')));
Patrick Barroca's avatar
Patrick Barroca committed
183
  }
184 185


Patrick Barroca's avatar
Patrick Barroca committed
186
  public function listestagiairesAction() {
187
    $this->_renderLettreFusion(new SessionFusionStrategy(Class_ModeleFusion::getTrainingTemplateFor('STAGIAIRES')));
Patrick Barroca's avatar
Patrick Barroca committed
188
  }
189 190


Patrick Barroca's avatar
Patrick Barroca committed
191
  public function convocationsAction() {
192
    $this->_renderLettreFusion(new SessionStagiairesFusionStrategy(Class_ModeleFusion::getTrainingTemplateFor('CONVOCATION')));
Patrick Barroca's avatar
Patrick Barroca committed
193
  }
194 195


Patrick Barroca's avatar
Patrick Barroca committed
196
  public function attestationsAction() {
197
    $this->_renderLettreFusion(new SessionStagiairesPresentsFusionStrategy(Class_ModeleFusion::getTrainingTemplateFor('ATTESTATION')));
Patrick Barroca's avatar
Patrick Barroca committed
198
  }
199 200


Patrick Barroca's avatar
Patrick Barroca committed
201
  public function refusAction() {
202
    $this->_renderLettreFusion(new SessionStagiairesFusionStrategy(Class_ModeleFusion::getTrainingTemplateFor('REFUS')));
Patrick Barroca's avatar
Patrick Barroca committed
203
  }
204 205


Patrick Barroca's avatar
Patrick Barroca committed
206 207
  public function _renderLettreFusion($fusion_strategy) {
    $this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
Ghislain Loas's avatar
Ghislain Loas committed
208
    $session_activity = Class_SessionActivity::getLoader()->find($this->_getParam('id'));
209

Ghislain Loas's avatar
Ghislain Loas committed
210
    $this->view->lettre = $fusion_strategy->getContenuFusionne($session_activity);
211

Ghislain Loas's avatar
Ghislain Loas committed
212
    $this->renderScript('activity/lettre-fusion.phtml');
Patrick Barroca's avatar
Patrick Barroca committed
213
  }
214

215

Patrick Barroca's avatar
Patrick Barroca committed
216 217 218
  public function showAction() {
    $this->_sessionDoAndNotify('show', 'La session "%s" est visible');
  }
219

Patrick Barroca's avatar
Patrick Barroca committed
220 221 222
  public function hideAction() {
    $this->_sessionDoAndNotify('hide', 'La session "%s" n\'est plus visible');
  }
223 224


Patrick Barroca's avatar
Patrick Barroca committed
225
  protected function _sessionDoAndNotify($action, $message) {
Ghislain Loas's avatar
Ghislain Loas committed
226
    $session = Class_SessionActivity::find($this->_getParam('id'));
Patrick Barroca's avatar
Patrick Barroca committed
227 228
    call_user_func([$session, $action]);
    $session->save();
229

Patrick Barroca's avatar
Patrick Barroca committed
230 231 232
    $this->_redirectToIndex();
    $this->_helper->notify($this->_($message, $session->getLibelle()));
  }
233 234


235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  public function linkedArticleAction() {
    if (!$session = Class_SessionActivity::find((int)$this->_getParam('id'))) {
      $this->_redirect('admin/activity');
      return;
    }

    if (!$article = $session->getArticle()) {
      $session->save();
      $article = $session->getArticle();
    }

    $this->view->session_activity = $session;
    $this->view->titre = $this->_('Article lié à "%s"', $session->getLabel());
    $this->view->article = $article;
  }


  public function linkedArticleRegenerateAction() {
    if (!$session = Class_SessionActivity::find((int)$this->_getParam('id'))) {
      $this->_redirect('admin/activity');
      return;
    }

    $session->regenerateArticle();

    $this->_helper->notify($this->_('Article lié à "%s" regénéré.', $session->getLabel()));
    $this->_redirect('admin/session-activity/linked-article/id/' . $session->getId());
  }
263
}
264 265 266 267 268




class AbstractSessionFusionStrategy {
Patrick Barroca's avatar
Patrick Barroca committed
269
  protected $_modele_fusion;
270

Patrick Barroca's avatar
Patrick Barroca committed
271 272 273
  public function __construct($nom) {
    $this->_modele_fusion = Class_ModeleFusion::get($nom);
  }
274 275 276 277
}


class SessionFusionStrategy extends AbstractSessionFusionStrategy{
Ghislain Loas's avatar
Ghislain Loas committed
278
  public function getContenuFusionne($session_activity) {
Patrick Barroca's avatar
Patrick Barroca committed
279
    return $this->_modele_fusion
Ghislain Loas's avatar
Ghislain Loas committed
280
      ->setDataSource(array("session_activity" => $session_activity,
Patrick Barroca's avatar
Patrick Barroca committed
281 282 283
                            "date_jour" => new FusionDateContext()))
      ->getContenuFusionne();
  }
284 285 286 287
}


class SessionOneLetterPerDayFusionStrategy extends AbstractSessionFusionStrategy{
Ghislain Loas's avatar
Ghislain Loas committed
288 289
  public function getContenuFusionne($session_activity) {
    $date_context = new FusionDateContext($session_activity->getDateDebut());
Patrick Barroca's avatar
Patrick Barroca committed
290

Ghislain Loas's avatar
Ghislain Loas committed
291
    $nb_jours = $date_context->numberOfDaysTo($session_activity->getDateFin());
Patrick Barroca's avatar
Patrick Barroca committed
292 293 294 295

    $lettres = array();
    for ($i=0; $i<=$nb_jours; $i++) {
      $lettres []= $this->_modele_fusion
Ghislain Loas's avatar
Ghislain Loas committed
296
        ->setDataSource(array("session_activity" => $session_activity,
Patrick Barroca's avatar
Patrick Barroca committed
297 298 299 300 301 302 303 304
                              "date_context" => $date_context,
                              "date_jour" => new FusionDateContext()))
        ->getContenuFusionne();
      $date_context->forwardOneDay();
    }

    return implode('<div style="page-break-after: always"></div>', $lettres);
  }
305 306 307 308 309 310
}




class FusionDateContext {
Patrick Barroca's avatar
Patrick Barroca committed
311
  protected $_current_date;
312

Patrick Barroca's avatar
Patrick Barroca committed
313 314 315
  public function __construct($datestr=null) {
    $this->_current_date = $this->dateStringToZendDate($datestr);
  }
316 317


Patrick Barroca's avatar
Patrick Barroca committed
318 319 320
  public function dateStringToZendDate($datestr) {
    return new Zend_Date($datestr, null, Zend_Registry::get('locale'));
  }
321 322


Patrick Barroca's avatar
Patrick Barroca committed
323 324 325 326
  public function forwardOneDay() {
    $this->_current_date->add(1, Zend_Date::DAY);
    return $this;
  }
327 328


Patrick Barroca's avatar
Patrick Barroca committed
329 330 331
  public function getTexte() {
    return $this->_current_date->toString('d MMMM yyyy');
  }
332 333


Patrick Barroca's avatar
Patrick Barroca committed
334 335 336
  public function numberOfDaysTo($other_datestr) {
    if (!$other_datestr)
      return 0;
337

Patrick Barroca's avatar
Patrick Barroca committed
338 339 340 341
    $other_date = $this->dateStringToZendDate($other_datestr);
    $other_date->sub($this->_current_date);
    return ($other_date->toValue(Zend_Date::DAY)-1);
  }
342 343


Patrick Barroca's avatar
Patrick Barroca committed
344 345 346
  public function callGetterByAttributeName($attribute) {
    return call_user_func(array($this, 'get'.Storm_Inflector::camelize($attribute)));
  }
347 348 349 350 351 352
}




class SessionStagiairesFusionStrategy extends AbstractSessionFusionStrategy{
Ghislain Loas's avatar
Ghislain Loas committed
353
  protected $_session_activity;
354

Ghislain Loas's avatar
Ghislain Loas committed
355 356
  public function getContenuFusionne($session_activity) {
    $this->_session_activity = $session_activity;
Patrick Barroca's avatar
Patrick Barroca committed
357
    $lettres = [];
358
    $stagiaires = $this->_getStagiaires();
Patrick Barroca's avatar
Patrick Barroca committed
359 360
    foreach($stagiaires as $stagiaire)
      $lettres []= $this->_modele_fusion
Ghislain Loas's avatar
Ghislain Loas committed
361
      ->setDataSource(array('session_activity' => $session_activity,
Patrick Barroca's avatar
Patrick Barroca committed
362 363 364 365 366 367 368
                            'stagiaire' => $stagiaire,
                            "date_jour" => new FusionDateContext()))
      ->getContenuFusionne();


    return implode('<div style="page-break-after: always"></div>', $lettres);
  }
369 370 371


  protected function _getStagiaires() {
Ghislain Loas's avatar
Ghislain Loas committed
372
    return $this->_session_activity->getStagiaires();
373
  }
374 375 376
}


377 378 379 380


class SessionStagiairesPresentsFusionStrategy extends SessionStagiairesFusionStrategy {
  protected function _getStagiaires() {
Ghislain Loas's avatar
Ghislain Loas committed
381
    return $this->_session_activity->getPresentStagiaires();
382 383
  }
}