campagne.php 15.9 KB
Newer Older
Ayolo's avatar
Ayolo committed
1 2 3
<?php
/* Copyright 2007
 * - Simon Lopez <simon POINT lopez CHEZ ayolo POINT org>
BenC's avatar
BenC committed
4 5 6
 * - Rémy Burney <rburney POINT utbm CHEZ gmail POINT com>
 * - Maxime Petazzoni <maxime POINT petazzoni CHEZ bulix POINT org>
 * - Benjamin Collet <bcollet CHEZ oxynux POINT org>
Ayolo's avatar
Ayolo committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
$topdir = "../";

require_once($topdir. "include/site.inc.php");
require_once($topdir. "include/cts/sqltable.inc.php");
require_once($topdir. "include/entities/asso.inc.php");
BenC's avatar
BenC committed
31
require_once($topdir."include/cts/board.inc.php");
Ayolo's avatar
Ayolo committed
32 33 34 35 36 37
$site = new site ();

$asso = new asso($site->db,$site->dbrw);
$asso->load_by_id($_REQUEST["id_asso"]);
if ( $asso->id < 1 )
{
38
  $site->error_not_found();
Ayolo's avatar
Ayolo committed
39 40 41
  exit();
}
if ( !$site->user->is_in_group("gestion_ae") && !$asso->is_member_role($site->user->id,ROLEASSO_MEMBREBUREAU) )
42
  $site->error_forbidden();
Ayolo's avatar
Ayolo committed
43 44 45 46 47 48 49 50


$site->start_page("campagne","Campagnes de recrutement");

$cts = new contents($asso->get_html_path());
$cts->add(new tabshead($asso->get_tabs($site->user),"cpg"));
$site->add_contents($cts);

Ayolo's avatar
bleh  
Ayolo committed
51
$cpg = new campagne($site->db,$site->dbrw);
Ayolo's avatar
bleh  
Ayolo committed
52 53
if(isset($_REQUEST["id_campagne"]))
  $cpg->load_by_id($_REQUEST["id_campagne"]);
Ayolo's avatar
bleh  
Ayolo committed
54

Ayolo's avatar
bleh  
Ayolo committed
55
if(!is_null($cpg->id) && isset($_REQUEST["action"]) && $_REQUEST["action"]=="delete")
Ayolo's avatar
bleh  
Ayolo committed
56
{
Ayolo's avatar
bleh  
Ayolo committed
57
  if($cpg->asso==$_REQUEST["id_asso"])
Ayolo's avatar
bleh  
Ayolo committed
58 59 60 61 62 63 64
  {
    new delete($site->dbrw,"cpg_campagne",array("id_campagne"=>$_REQUEST["id_campagne"]));
    new delete($site->dbrw,"cpg_participe",array("id_campagne"=>$_REQUEST["id_campagne"]));
    new delete($site->dbrw,"cpg_question",array("id_campagne"=>$_REQUEST["id_campagne"]));
    new delete($site->dbrw,"cpg_reponse",array("id_campagne"=>$_REQUEST["id_campagne"]));
  }
  unset($_REQUEST["action"]);
Ayolo's avatar
bleh  
Ayolo committed
65
  unset($_REQUEST['id_campagne']);
Ayolo's avatar
bleh  
Ayolo committed
66 67
}

Ayolo's avatar
Ayolo committed
68 69 70
if (isset($_REQUEST["addcpg"]) && isset($_REQUEST["nom"]) && !empty($_REQUEST["nom"]) && isset($_REQUEST["end_date"]) && isset($_REQUEST["description"]) && isset($_REQUEST["questions"]) )
{
  $cts = new contents("Campagne ajoutée avec succès");
Ayolo's avatar
Ayolo committed
71
  $cpg->new_campagne($_REQUEST["nom"], $_REQUEST["description"], $_REQUEST["end_date"], $_REQUEST["id_groupe"], $asso->id);
Ayolo's avatar
Ayolo committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  foreach ( $_REQUEST["questions"] as $rep )
  {
    if ( isset($rep['nom_question']) && !empty($rep['nom_question']) && isset($rep['type_question']))
    {
      if(empty($rep['description_question']))
        $rep['description_question']==$rep['nom_question'];

      if (($rep['type_question'] == "list" || $rep['type_question'] =="radio") && !empty($rep['reponses_question']))
      {
        $reponses=$rep['reponses_question'];
        $values=explode(";",$reponses,2);
        foreach($values as $value)
        {
          $value=explode("|", $value, 2);
          $c=count($value);
          if( $c!= 2 || empty($value[0]) || empty($value[1]))
          {
            $rep['type_question']="text";
            $reponses="";
          }
        }
        $cpg->add_question($rep['nom_question'],$rep['description_question'],$rep['type_question'],$reponses);
      }
      elseif ($rep['type_question'] == "text" || $rep['type_question'] == "checkbox" )
      {
        $cpg->add_question($rep['nom_question'],$rep['description_question'],$rep['type_question']);
      }
    }
  }
Ayolo's avatar
Ayolo committed
101
  $cts->add_paragraph("<img src=\"".$topdir."images/actions/done.png\">&nbsp;La campagne \"".$cpg->nom."\" a bien été ajoutée.");
Ayolo's avatar
Ayolo committed
102 103 104 105 106 107 108 109
  $site->add_contents($cts,true);
  unset($_REQUEST["nom"]);
  unset($_REQUEST["description"]);
  unset($_REQUEST["questions"]);
  unset($_REQUEST["end_date"]);

}

Ayolo's avatar
bleh  
Ayolo committed
110 111 112
if($_REQUEST["action"]=="add")
{
  $frm = new form ("nvcampagne","campagne.php?id_asso=".$asso->id,false,"POST","Nouvelle campagne");
Ayolo's avatar
Ayolo committed
113

Ayolo's avatar
bleh  
Ayolo committed
114
  /* Duree de validite d'une campagne = 15 jours par defaut */
Ayolo's avatar
Ayolo committed
115

Ayolo's avatar
bleh  
Ayolo committed
116
  $default_valid = time() + (15 * 24 * 60 * 60);
Ayolo's avatar
Ayolo committed
117

Ayolo's avatar
bleh  
Ayolo committed
118 119 120 121
  if ($_REQUEST["end_date"])
    $frm->add_date_field("end_date", "Date de fin de validite : ",$_REQUEST["end_date"],true);
  else
    $frm->add_date_field("end_date", "Date de fin de validite : ",$default_valid,true);
Ayolo's avatar
Ayolo committed
122

123
  $frm->add_text_field("nom", "Nom de la campagne",$_REQUEST["nom"],true,80);
Ayolo's avatar
Ayolo committed
124

Ayolo's avatar
bleh  
Ayolo committed
125 126
  $frm->add_text_area("description", "Description de la campagne",$_REQUEST["description"]);
  $frm->add_entity_smartselect("id_groupe","Groupe",new group($site->db));
Ayolo's avatar
Ayolo committed
127

Ayolo's avatar
bleh  
Ayolo committed
128 129 130
  $frm->add_info("Pour supprimer une question, il suffit de laisser son nom vide !<br />");
  $frm->add_info("Pour une question de type liste ou bouton radio, complétez impérativement le champ \"Réponses possibles\".");
  $frm->add_info("Formatage du champ \"Réponses possibles\" : valeur_1|La valeur 1;valeur_2|La valeur 2;...;valeur_z|La dernière valeur");
Ayolo's avatar
Ayolo committed
131

Ayolo's avatar
bleh  
Ayolo committed
132
  if (isset($_REQUEST["questions"]))
Ayolo's avatar
Ayolo committed
133
  {
Ayolo's avatar
bleh  
Ayolo committed
134 135
    $n = 1;
    foreach ( $_REQUEST["questions"] as $num=>$question )
Ayolo's avatar
Ayolo committed
136
    {
Ayolo's avatar
bleh  
Ayolo committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      if ( !empty($question) )
      {
        $subfrm = new form("questions".$num,null,null,null,"Question $n");
        $subfrm->add_text_field("questions[$num][nom_question]", "Nom question",$question["nom_question"],false,80);
        $subfrm->add_text_area("questions[$num][description_question]", "Description",$question["description_question"]);
        if(isset($question["type_question"]))
          $type = $question["type_question"];
        else
          $type="text";
        $subfrm->add_select_field("questions[$num][type_question]","Type de question",array("text"=>"Texte","checkbox"=>"Boite à cocher","list"=>"Liste", "radio"=>"Bouton radio"),$type);
        $subfrm->add_text_field("questions[$num][reponses_question]", "Réponses possibles",$question["reponses_question"],false,80);
        $frm->add ( $subfrm, false, false, false, false, false, false, true );
        $n++;
      }
    }
    if (isset($_REQUEST["newques"]))
    {
      $i=$n-1;
      $subfrm = new form("questions".$i,null,null,null,"Question $n");
      $subfrm->add_text_field("questions[$i][nom_question]", "Nom question","",false,80);
      $subfrm->add_text_area("questions[$i][description_question]", "Description");
      $subfrm->add_select_field("questions[$i][type_question]","Type de question",array("text"=>"Texte","checkbox"=>"Boite à cocher","list"=>"Liste", "radio"=>"Bouton radio"),"text");
      $subfrm->add_text_field("questions[$i][reponses_question]", "Réponses possibles","",false,80);
Ayolo's avatar
Ayolo committed
160 161 162
      $frm->add ( $subfrm, false, false, false, false, false, false, true );
    }
  }
Ayolo's avatar
bleh  
Ayolo committed
163
  else
Ayolo's avatar
Ayolo committed
164
  {
Ayolo's avatar
bleh  
Ayolo committed
165 166 167 168 169 170 171 172 173 174 175
    $n=1;
    for($i=0;$i<6;$i++)
    {
      $subfrm = new form("questions".$i,null,null,null,"Question $n");
      $subfrm->add_text_field("questions[$i][nom_question]", "Nom question","",false,80);
      $subfrm->add_text_area("questions[$i][description_question]", "Description");
      $subfrm->add_select_field("questions[$i][type_question]","Type de question",array("text"=>"Texte","checkbox"=>"Boite à cocher","list"=>"Liste", "radio"=>"Bouton radio"),"text");
      $subfrm->add_text_field("questions[$i][reponses_question]", "Réponses possibles","",false,80);
      $frm->add ( $subfrm, false, false, false, false, false, false, true );
      $n++;
    }
Ayolo's avatar
Ayolo committed
176 177
  }

Ayolo's avatar
bleh  
Ayolo committed
178 179
  $frm->add_hidden("text_submit",$text_submit);
  $frm->add_hidden("date_campagne",$_REQUEST['date_campagne']);
Ayolo's avatar
Ayolo committed
180

Ayolo's avatar
bleh  
Ayolo committed
181
  $frm->add_submit("newques","Question supplémentaire");
Ayolo's avatar
Ayolo committed
182

Feu's avatar
Feu committed
183
  $frm->add_submit("addcpg","Ajouter");
Ayolo's avatar
Ayolo committed
184

Ayolo's avatar
bleh  
Ayolo committed
185 186
  $site->add_contents($frm);
}
Ayolo's avatar
bleh  
Ayolo committed
187
elseif(!is_null($cpg->id) && $_REQUEST["action"]=="results" && $cpg->asso==$_REQUEST["id_asso"])
Ayolo's avatar
bleh  
Ayolo committed
188 189 190 191 192 193 194 195 196
{
  $questions = $cpg->get_questions();
  /* c'est porc mais on va créer un array(array(field=>value)) au lieu d'un $req pour le sqltable) */
  $req = new requete ( $site->db,
                       "SELECT `utilisateurs`.`id_utilisateur`, ".
                       "CONCAT(`utilisateurs`.`prenom_utl`,' ',`utilisateurs`.`nom_utl`) as `nom_utilisateur` " .
                       "FROM `cpg_reponse` ".
                       "INNER JOIN `utilisateurs` USING(`id_utilisateur`) ".
                       "WHERE `id_campagne`='".$cpg->id."' ".
197 198
                       "GROUP BY `cpg_reponse`.`id_utilisateur` ".
                       "ORDER BY `nom_utl`");
Ayolo's avatar
bleh  
Ayolo committed
199 200 201
  $answers=array();
  while(list($id_utl,$nom)=$req->get_row())
  {
Ayolo's avatar
bleh  
Ayolo committed
202
    $_answers_utl=$cpg->get_user_results($id_utl);
Ayolo's avatar
bleh  
Ayolo committed
203 204 205 206
    $_answers=array("id_utilisateur"=>$id_utl,"nom_utilisateur"=>$nom);
    foreach($questions as $id => $question)
    {
      if(isset($_answers_utl[$id]))
Ayolo's avatar
bleh  
Ayolo committed
207
        $_answers[$id]=$_answers_utl[$id];
Ayolo's avatar
bleh  
Ayolo committed
208
      else
Ayolo's avatar
bleh  
Ayolo committed
209
        $_answers[$id]="";
Ayolo's avatar
bleh  
Ayolo committed
210 211 212 213 214 215
    }
    $answers[]=$_answers;
  }
  $answers_champs=array("nom_utilisateur"=>"Utilisateur");
  foreach($questions as $id => $question)
  {
furtif's avatar
furtif committed
216
    $answers_champs[$id]=$question["nom"];
Ayolo's avatar
bleh  
Ayolo committed
217
  }
218 219 220 221

  $cts=new contents("Résultats : " . $cpg->nom .
    " (" . $req->lines . " r&eacute;ponse(s))");

Ayolo's avatar
bleh  
Ayolo committed
222 223 224 225
  $tbl = new sqltable("results",
                      "Résultats",
                      $answers,
                      "campagne.php",
Ayolo's avatar
bleh  
Ayolo committed
226
                      "id_utilisateur",
Ayolo's avatar
bleh  
Ayolo committed
227 228 229 230 231
                      $answers_champs,
                      array(),
                      array(),
                      array() );
  $cts->add($tbl);
BenC's avatar
BenC committed
232 233 234 235 236 237 238


  $req = new requete($site->db,
                     "SELECT COUNT(`valeur_reponse`) AS `nombre_reponse`,
                      `id_question`, `nom_question`, `valeur_reponse`
                      FROM `cpg_reponse`
                      INNER JOIN `cpg_question` USING(`id_question`)
239 240
                      WHERE `id_campagne`='".$cpg->id."'
                        AND (`type_question`=\"radio\"
BenC's avatar
BenC committed
241 242
                          OR `type_question`=\"checkbox\"
                          OR `type_question`=\"list\")
BenC's avatar
BenC committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                      GROUP BY `valeur_reponse`
                      ORDER BY `id_question`");

  $id_question_precedente = "";

  if($req->lines > 0)
  {
    $board = new board();
    while(list($nombre_reponses, $id_question, $nom_question, $valeur_reponse) = $req->get_row())
    {
      if($id_question != $id_question_precedente)
      {
        if($id_question_precdente != "")
        {
          $board->add($list,true);
        }

        $list = new itemlist($nom_question);
      }
      $list->add($valeur_reponse." : ".$nombre_reponses);
BenC's avatar
BenC committed
263
      $id_question_precedente = $id_question;
BenC's avatar
BenC committed
264 265 266 267 268
    }
    $board->add($list,true);
    $cts->add($board);
  }

Ayolo's avatar
bleh  
Ayolo committed
269 270
  $site->add_contents($cts);
}
furtif's avatar
furtif committed
271 272

/* modification d'une campagne */
273
elseif(!is_null($cpg->id) && $_REQUEST["action"]=="edit" && $cpg->asso==$_REQUEST["id_asso"] )
furtif's avatar
furtif committed
274 275 276
{
  $cts=new contents();

furtif's avatar
furtif committed
277
  $frm = new form("editcpg","./campagne.php",true,"POST","Edition campagne ".$cpg->id);
furtif's avatar
furtif committed
278
  $frm->add_hidden("id_campagne",$cpg->id);
279
  $frm->add_date_field("end_date", "Date de fin de validite : ",$cpg->end_date,true);
280
  $frm->add_text_field("nom", "Nom de la campagne",$cpg->nom,true,80);
281
  $frm->add_text_area("description", "Description de la campagne",$cpg->description);
furtif's avatar
furtif committed
282 283
  $frm->add_entity_select("id_groupe", "Groupe concern&eacute;", $site->db, "group",$cpg->group,true);
  $frm->add_entity_select("id_asso", "Association", $site->db, "asso",$cpg->asso,true);
284 285


furtif's avatar
furtif committed
286 287 288 289 290 291 292 293 294
  $frm->add_info("Pour supprimer une question, il suffit de laisser son nom vide !<br />");
  $frm->add_info("Pour une question de type liste ou bouton radio, complétez impérativement le champ \"Réponses possibles\".");
  $frm->add_info("Formatage du champ \"Réponses possibles\" : valeur_1|La valeur 1;valeur_2|La valeur 2;...;valeur_z|La dernière valeur");

  $req = new requete($site->db,
		     "SELECT * FROM `cpg_question` WHERE `id_campagne` =".$cpg->id);
  $n = 0;
  while ( $row = $req->get_row() )
    {
furtif's avatar
furtif committed
295
      $subfrm = new subform("questions".$n,"Question $n",true);
furtif's avatar
furtif committed
296 297 298 299
      $subfrm->add_text_field("questions[".$row['id_question']."][nom_question]", "Nom question",$row["nom_question"],true,80);
      $subfrm->add_text_area("questions[".$row['id_question']."][description_question]", "Description",$row["description_question"]);
      $subfrm->add_select_field("questions[".$row['id_question']."][type_question]","Type de question",array("text"=>"Texte","checkbox"=>"Boite à cocher","list"=>"Liste", "radio"=>"Bouton radio"),$row["type_question"]);
      $subfrm->add_text_field("questions[".$row['id_question']."][reponses_question]", "Réponses possibles",$row["reponses_question"],true,80);
furtif's avatar
furtif committed
300
      $frm->addsub ( $subfrm, true, false );
furtif's avatar
furtif committed
301
      $n++;
302

furtif's avatar
furtif committed
303 304
    }

furtif's avatar
furtif committed
305
  $frm->add_submit("editcpg","Enregistrer");
furtif's avatar
furtif committed
306
  $cts->add($frm);
307
  $site->add_contents($cts);
furtif's avatar
furtif committed
308 309


310
}elseif(isset($_REQUEST["editcpg"]) &&
furtif's avatar
furtif committed
311
	isset($_REQUEST["nom"]) && !empty($_REQUEST["nom"]) &&
312 313
	isset($_REQUEST["end_date"]) &&
	isset($_REQUEST["description"]) &&
314 315 316 317 318
	isset($_REQUEST["id_asso"]) &&
	isset($_REQUEST["id_groupe"]) &&
	isset($_REQUEST["questions"])
	)
{
furtif's avatar
furtif committed
319
  $cts=new contents();
320 321 322 323 324 325 326 327 328 329
  $nb_remove_question = 0;

  $cpg->load_by_id($_REQUEST["id_campagne"]);
  if( $cpg->id == NULL){
    $site->error_not_found();
    exit();
  }
  $cpg->update_campagne($_REQUEST["nom"],$_REQUEST["description"],$cpg->date,$_REQUEST["end_date"]);


furtif's avatar
furtif committed
330
  foreach ( $_REQUEST["questions"] as $id_question=>$rep )
331 332
  {
    if ( isset($rep['nom_question']) && !empty($rep['nom_question']) &&
furtif's avatar
furtif committed
333 334
         isset($id_question) &&
         isset($rep['type_question']))
335 336 337 338
    {
      if(empty($rep['description_question']))
        $rep['description_question']=$rep['nom_question'];

furtif's avatar
furtif committed
339
      if (($rep['type_question'] == "list" || $rep['type_question'] =="radio")
furtif's avatar
furtif committed
340
          && !empty($rep['reponses_question']))
341 342 343 344 345 346 347 348 349 350 351 352 353
      {
        $reponses=$rep['reponses_question'];
        $values=explode(";",$reponses,2);
        foreach($values as $value)
        {
          $value=explode("|", $value, 2);
          $c=count($value);
          if( $c!= 2 || empty($value[0]) || empty($value[1]))
          {
            $rep['type_question']="text";
            $reponses="";
          }
        }
furtif's avatar
furtif committed
354
        $cpg->update_question($id_question,$rep['nom_question'],$rep['description_question'],$rep['type_question'],$reponses);
355
      }
356
      elseif ($rep['type_question'] == "text" ||
furtif's avatar
furtif committed
357
              $rep['type_question'] == "checkbox" )
358
      {
furtif's avatar
furtif committed
359
        $cpg->update_question($id_question,$rep['nom_question'],$rep['description_question'],$rep['type_question'],NULL);
360 361
      }
    }else{
furtif's avatar
furtif committed
362
      if( isset($id_question) ){
furtif's avatar
furtif committed
363 364
        $cpg->remove_question($id_question);
        $nb_remove_question += 1;
furtif's avatar
furtif committed
365
      }
366
    }
furtif's avatar
furtif committed
367
  }
368

369

furtif's avatar
furtif committed
370
  $cts->add_paragraph("<a href=\"./campagne.php?id_asso=".$asso->id."&action=add\">Ajouter une campagne</a>");
furtif's avatar
furtif committed
371
  if($nb_remove_question == 1){
372 373 374 375
    $cts->add_paragraph($nb_remove_question." question a &eacute;t&eacute; supprim&eacute;e ! ");
  }elseif($nb_remove_question > 1){
    $cts->add_paragraph($nb_remove_question." questions ont &eacute;t&eacute; supprim&eacute;es ! ");
  }
furtif's avatar
furtif committed
376
  $cts->add_paragraph("Campagne modifi&eacute;e avec succ&egrave ! ");
377
  $site->add_contents($cts);
furtif's avatar
furtif committed
378 379 380


} /* fin modification d'une campagne */
furtif's avatar
furtif committed
381
else
Ayolo's avatar
bleh  
Ayolo committed
382 383 384 385 386 387 388 389 390
{
  $cts=new contents();
  $cts->add_paragraph("<a href=\"./campagne.php?id_asso=".$asso->id."&action=add\">Ajouter une campagne</a>");
  $req = new requete ( $site->db,
                       "SELECT `id_campagne`, `nom_campagne`, `date_debut_campagne`, `date_fin_campagne` ".
                       "FROM `cpg_campagne` WHERE `id_asso`='".$asso->id."'" );
  $tbl = new sqltable("listcampagne",
                      "Campagnes",
                      $req,
Ayolo's avatar
bleh  
Ayolo committed
391
                      "campagne.php?id_asso=".$asso->id,
Ayolo's avatar
bleh  
Ayolo committed
392 393
                      "id_campagne",
                      array("nom_campagne"=>"Intitulé","date_debut_campagne"=>"Début","date_fin_campagne"=>"Fin"),
furtif's avatar
furtif committed
394
                      array("results"=>"Résultats","edit"=>"Editer","delete"=>"Supprimer"),
Ayolo's avatar
bleh  
Ayolo committed
395 396 397 398 399
                      array(),
                      array() );
  $cts->add($tbl);
  $site->add_contents($cts);
}
Ayolo's avatar
Ayolo committed
400 401 402 403

$site->end_page ();

?>