studs.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. <?php
  2. /* Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/public/opensurvey/studs.php
  20. * \ingroup opensurvey
  21. * \brief Page to list surveys
  22. */
  23. if (!defined('NOLOGIN')) {
  24. define("NOLOGIN", 1); // This means this output page does not require to be logged.
  25. }
  26. if (!defined('NOCSRFCHECK')) {
  27. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  28. }
  29. if (!defined('NOBROWSERNOTIF')) {
  30. define('NOBROWSERNOTIF', '1');
  31. }
  32. if (!defined('NOIPCHECK')) {
  33. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  34. }
  35. // Load Dolibarr environment
  36. require '../../main.inc.php';
  37. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  38. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  39. require_once DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php";
  40. require_once DOL_DOCUMENT_ROOT."/opensurvey/lib/opensurvey.lib.php";
  41. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  42. // Init vars
  43. $action = GETPOST('action', 'aZ09');
  44. $numsondage = '';
  45. if (GETPOST('sondage')) {
  46. $numsondage = GETPOST('sondage', 'alpha');
  47. }
  48. $object = new Opensurveysondage($db);
  49. $result = $object->fetch(0, $numsondage);
  50. $nblines = $object->fetch_lines();
  51. //If the survey has not yet finished, then it can be modified
  52. $canbemodified = ((empty($object->date_fin) || $object->date_fin > dol_now()) && $object->status != Opensurveysondage::STATUS_CLOSED);
  53. // Security check
  54. if (empty($conf->opensurvey->enabled)) {
  55. httponly_accessforbidden('Module Survey not enabled');
  56. }
  57. /*
  58. * Actions
  59. */
  60. $nbcolonnes = substr_count($object->sujet, ',') + 1;
  61. $listofvoters = explode(',', $_SESSION["savevoter"]);
  62. // Add comment
  63. if (GETPOST('ajoutcomment', 'alpha')) {
  64. if (!$canbemodified) {
  65. httponly_accessforbidden('ErrorForbidden');
  66. }
  67. $error = 0;
  68. $comment = GETPOST("comment", 'alphanohtml');
  69. $comment_user = GETPOST('commentuser', 'alphanohtml');
  70. if (!$comment) {
  71. $error++;
  72. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Comment")), null, 'errors');
  73. }
  74. if (!$comment_user) {
  75. $error++;
  76. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
  77. }
  78. if (!in_array($comment_user, $listofvoters)) {
  79. setEventMessages($langs->trans("UserMustBeSameThanUserUsedToVote"), null, 'errors');
  80. $error++;
  81. }
  82. $user_ip = getUserRemoteIP();
  83. $nb_post_max = getDolGlobalInt("MAIN_SECURITY_MAX_POST_ON_PUBLIC_PAGES_BY_IP_ADDRESS", 200);
  84. $now = dol_now();
  85. $minmonthpost = dol_time_plus_duree($now, -1, "m");
  86. // Calculate nb of post for IP
  87. $nb_post_ip = 0;
  88. if ($nb_post_max > 0) { // Calculate only if there is a limit to check
  89. $sql = "SELECT COUNT(id_comment) as nb_comments";
  90. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_comments";
  91. $sql .= " WHERE ip = '".$db->escape($user_ip)."'";
  92. $sql .= " AND date_creation > '".$db->idate($minmonthpost)."'";
  93. $resql = $db->query($sql);
  94. if ($resql) {
  95. $num = $db->num_rows($resql);
  96. $i = 0;
  97. while ($i < $num) {
  98. $i++;
  99. $obj = $db->fetch_object($resql);
  100. $nb_post_ip = $obj->nb_comments;
  101. }
  102. }
  103. }
  104. if ($nb_post_max > 0 && $nb_post_ip >= $nb_post_max) {
  105. setEventMessages($langs->trans("AlreadyTooMuchPostOnThisIPAdress"), null, 'errors');
  106. $error++;
  107. }
  108. if (!$error) {
  109. $resql = $object->addComment($comment, $comment_user, $user_ip);
  110. if (!$resql) {
  111. dol_print_error($db);
  112. }
  113. }
  114. }
  115. // Add vote
  116. if (GETPOST("boutonp") || GETPOST("boutonp.x") || GETPOST("boutonp_x")) { // boutonp for chrome, boutonp_x for firefox
  117. if (!$canbemodified) {
  118. httponly_accessforbidden('ErrorForbidden');
  119. }
  120. //Si le nom est bien entré
  121. if (GETPOST('nom', 'alphanohtml')) {
  122. $nouveauchoix = '';
  123. for ($i = 0; $i < $nbcolonnes; $i++) {
  124. if (GETPOSTISSET("choix".$i) && GETPOST("choix".$i) == '1') {
  125. $nouveauchoix .= "1";
  126. } elseif (GETPOSTISSET("choix".$i) && GETPOST("choix".$i) == '2') {
  127. $nouveauchoix .= "2";
  128. } else {
  129. $nouveauchoix .= "0";
  130. }
  131. }
  132. $user_ip = getUserRemoteIP();
  133. $nb_post_max = getDolGlobalInt("MAIN_SECURITY_MAX_POST_ON_PUBLIC_PAGES_BY_IP_ADDRESS", 200);
  134. $now = dol_now();
  135. $minmonthpost = dol_time_plus_duree($now, -1, "m");
  136. // Calculate nb of post for IP
  137. $nb_post_ip = 0;
  138. if ($nb_post_max > 0) { // Calculate only if there is a limit to check
  139. $sql = "SELECT COUNT(id_users) as nb_records";
  140. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  141. $sql .= " WHERE ip = '".$db->escape($user_ip)."'";
  142. $sql .= " AND date_creation > '".$db->idate($minmonthpost)."'";
  143. $resql = $db->query($sql);
  144. if ($resql) {
  145. $num = $db->num_rows($resql);
  146. $i = 0;
  147. while ($i < $num) {
  148. $i++;
  149. $obj = $db->fetch_object($resql);
  150. $nb_post_ip = $obj->nb_records;
  151. }
  152. }
  153. }
  154. $nom = substr(GETPOST("nom", 'alphanohtml'), 0, 64);
  155. // Check if vote already exists
  156. $sql = 'SELECT id_users, nom as name';
  157. $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_user_studs';
  158. $sql .= " WHERE id_sondage='".$db->escape($numsondage)."' AND nom = '".$db->escape($nom)."' ORDER BY id_users";
  159. $resql = $db->query($sql);
  160. if (!$resql) {
  161. dol_print_error($db);
  162. }
  163. $num_rows = $db->num_rows($resql);
  164. if ($num_rows > 0) {
  165. setEventMessages($langs->trans("VoteNameAlreadyExists"), null, 'errors');
  166. $error++;
  167. } elseif ($nb_post_max > 0 && $nb_post_ip >= $nb_post_max) {
  168. setEventMessages($langs->trans("AlreadyTooMuchPostOnThisIPAdress"), null, 'errors');
  169. $error++;
  170. } else {
  171. $now = dol_now();
  172. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'opensurvey_user_studs (nom, id_sondage, reponses, ip, date_creation)';
  173. $sql .= " VALUES ('".$db->escape($nom)."', '".$db->escape($numsondage)."','".$db->escape($nouveauchoix)."', '".$db->escape($user_ip)."', '".$db->idate($now)."')";
  174. $resql = $db->query($sql);
  175. if ($resql) {
  176. // Add voter to session
  177. $_SESSION["savevoter"] = $nom.','.(empty($_SESSION["savevoter"]) ? '' : $_SESSION["savevoter"]); // Save voter
  178. $listofvoters = explode(',', $_SESSION["savevoter"]);
  179. if ($object->mailsonde) {
  180. if ($object->fk_user_creat) {
  181. $userstatic = new User($db);
  182. $userstatic->fetch($object->fk_user_creat);
  183. $email = $userstatic->email;
  184. } else {
  185. $email = $object->mail_admin;
  186. }
  187. //Linked user may not have an email set
  188. if ($email) {
  189. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  190. $application = ($conf->global->MAIN_APPLICATION_TITLE ? $conf->global->MAIN_APPLICATION_TITLE : 'Dolibarr ERP/CRM');
  191. $link = getUrlSondage($numsondage, true);
  192. $link = '<a href="'.$link.'">'.$link.'</a>';
  193. $body = str_replace('\n', '<br>', $langs->transnoentities('EmailSomeoneVoted', $nom, $link));
  194. //var_dump($body);exit;
  195. $cmailfile = new CMailFile("[".$application."] ".$langs->trans("Poll").': '.$object->title, $email, $conf->global->MAIN_MAIL_EMAIL_FROM, $body, null, null, null, '', '', 0, -1);
  196. $result = $cmailfile->sendfile();
  197. }
  198. }
  199. } else {
  200. dol_print_error($db);
  201. }
  202. }
  203. } else {
  204. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Name")), null, 'errors');
  205. }
  206. }
  207. // Update vote
  208. $testmodifier = false;
  209. $testligneamodifier = false;
  210. $ligneamodifier = -1;
  211. for ($i = 0; $i < $nblines; $i++) {
  212. if (GETPOSTISSET('modifierligne'.$i)) {
  213. $ligneamodifier = $i;
  214. $testligneamodifier = true;
  215. }
  216. //test to see if a line is to be modified
  217. if (GETPOSTISSET('validermodifier'.$i)) {
  218. $modifier = $i;
  219. $testmodifier = true;
  220. }
  221. }
  222. if ($testmodifier) {
  223. $nouveauchoix = '';
  224. for ($i = 0; $i < $nbcolonnes; $i++) {
  225. if (GETPOSTISSET("choix".$i) && GETPOST("choix".$i) == '1') {
  226. $nouveauchoix .= "1";
  227. } elseif (GETPOSTISSET("choix".$i) && GETPOST("choix".$i) == '2') {
  228. $nouveauchoix .= "2";
  229. } else {
  230. $nouveauchoix .= "0";
  231. }
  232. }
  233. if (!$canbemodified) {
  234. httponly_accessforbidden('ErrorForbidden');
  235. }
  236. $idtomodify = GETPOST("idtomodify".$modifier);
  237. $sql = 'UPDATE '.MAIN_DB_PREFIX."opensurvey_user_studs";
  238. $sql .= " SET reponses = '".$db->escape($nouveauchoix)."'";
  239. $sql .= " WHERE id_users = '".$db->escape($idtomodify)."'";
  240. $resql = $db->query($sql);
  241. if (!$resql) {
  242. dol_print_error($db);
  243. }
  244. }
  245. // Delete comment
  246. $idcomment = GETPOST('deletecomment', 'int');
  247. if ($idcomment) {
  248. if (!$canbemodified) {
  249. httponly_accessforbidden('ErrorForbidden');
  250. }
  251. $resql = $object->deleteComment($idcomment);
  252. }
  253. /*
  254. * View
  255. */
  256. $form = new Form($db);
  257. $arrayofjs = array();
  258. $arrayofcss = array('/opensurvey/css/style.css');
  259. llxHeaderSurvey($object->title, "", 0, 0, $arrayofjs, $arrayofcss, $numsondage);
  260. if (empty($object->ref)) { // For survey, id is a hex string
  261. $langs->load("errors");
  262. print $langs->trans("ErrorRecordNotFound");
  263. llxFooterSurvey();
  264. $db->close();
  265. exit();
  266. }
  267. // Define format of choices
  268. $toutsujet = explode(",", $object->sujet);
  269. $listofanswers = array();
  270. foreach ($toutsujet as $value) {
  271. $tmp = explode('@', $value);
  272. $listofanswers[] = array('label'=>$tmp[0], 'format'=>(!empty($tmp[1]) ? $tmp[1] : 'checkbox'));
  273. }
  274. $toutsujet = str_replace("°", "'", $toutsujet);
  275. print '<div class="survey_invitation">'.$langs->trans("YouAreInivitedToVote").'</div>';
  276. print $langs->trans("OpenSurveyHowTo").'<br>';
  277. if (empty($object->allow_spy)) {
  278. print '<span class="opacitymedium">'.$langs->trans("YourVoteIsPrivate").'</span><br>';
  279. } else {
  280. print $form->textwithpicto('<span class="opacitymedium">'.$langs->trans("YourVoteIsPublic").'</span>', $langs->trans("CanSeeOthersVote")).'<br>';
  281. }
  282. print '<br>';
  283. print '<div class="corps"> '."\n";
  284. // show title of survey
  285. $titre = str_replace("\\", "", $object->title);
  286. print '<strong>'.dol_htmlentities($titre).'</strong>';
  287. // show description of survey
  288. if ($object->description) {
  289. print '<br><br>'."\n";
  290. print dol_htmlentitiesbr($object->description);
  291. print '<br>'."\n";
  292. }
  293. print '</div>'."\n";
  294. //The survey has expired, users can't vote or do any action
  295. if (!$canbemodified) {
  296. print '<br><center><div class="quatrevingtpercent center warning">'.$langs->trans('SurveyExpiredInfo').'</div></center>';
  297. llxFooterSurvey();
  298. $db->close();
  299. exit;
  300. }
  301. print '<div class="cadre"> '."\n";
  302. print '<br><br>'."\n";
  303. // Start to show survey result
  304. print '<table class="resultats">'."\n";
  305. // Show choice titles
  306. if ($object->format == "D") {
  307. //display of survey topics
  308. print '<tr>'."\n";
  309. print '<td></td>'."\n";
  310. //display of years
  311. $colspan = 1;
  312. $nbofsujet = count($toutsujet);
  313. for ($i = 0; $i < $nbofsujet; $i++) {
  314. if (isset($toutsujet[$i + 1]) && date('Y', intval($toutsujet[$i])) == date('Y', intval($toutsujet[$i + 1]))) {
  315. $colspan++;
  316. } else {
  317. print '<td colspan='.$colspan.' class="annee">'.date('Y', intval($toutsujet[$i])).'</td>'."\n";
  318. $colspan = 1;
  319. }
  320. }
  321. print '</tr>'."\n";
  322. print '<tr>'."\n";
  323. print '<td></td>'."\n";
  324. //display of months
  325. $colspan = 1;
  326. for ($i = 0; $i < $nbofsujet; $i++) {
  327. $cur = intval($toutsujet[$i]); // intval() est utiliser pour supprimer le suffixe @* qui déplaît logiquement à strftime()
  328. if (isset($toutsujet[$i + 1]) === false) {
  329. $next = false;
  330. } else {
  331. $next = intval($toutsujet[$i + 1]);
  332. }
  333. if ($next && dol_print_date($cur, "%B") == dol_print_date($next, "%B") && dol_print_date($cur, "%Y") == dol_print_date($next, "%Y")) {
  334. $colspan++;
  335. } else {
  336. print '<td colspan='.$colspan.' class="mois">'.dol_print_date($cur, "%B").'</td>'."\n";
  337. $colspan = 1;
  338. }
  339. }
  340. print '</tr>'."\n";
  341. print '<tr>'."\n";
  342. print '<td></td>'."\n";
  343. //display of days
  344. $colspan = 1;
  345. for ($i = 0; $i < $nbofsujet; $i++) {
  346. $cur = intval($toutsujet[$i]);
  347. if (isset($toutsujet[$i + 1]) === false) {
  348. $next = false;
  349. } else {
  350. $next = intval($toutsujet[$i + 1]);
  351. }
  352. if ($next && dol_print_date($cur, "%a %e") == dol_print_date($next, "%a %e") && dol_print_date($cur, "%B") == dol_print_date($next, "%B")) {
  353. $colspan++;
  354. } else {
  355. print '<td colspan="'.$colspan.'" class="jour">'.dol_print_date($cur, "%a %e").'</td>'."\n";
  356. $colspan = 1;
  357. }
  358. }
  359. print '</tr>'."\n";
  360. //Display schedules
  361. if (strpos($object->sujet, '@') !== false) {
  362. print '<tr>'."\n";
  363. print '<td></td>'."\n";
  364. for ($i = 0; isset($toutsujet[$i]); $i++) {
  365. $heures = explode('@', $toutsujet[$i]);
  366. if (isset($heures[1])) {
  367. print '<td class="heure">'.dol_htmlentities($heures[1]).'</td>'."\n";
  368. } else {
  369. print '<td class="heure"></td>'."\n";
  370. }
  371. }
  372. print '</tr>'."\n";
  373. }
  374. } else {
  375. //display of survey topics
  376. print '<tr>'."\n";
  377. print '<td></td>'."\n";
  378. for ($i = 0; isset($toutsujet[$i]); $i++) {
  379. $tmp = explode('@', $toutsujet[$i]);
  380. print '<td class="sujet">'.dol_escape_htmltag($tmp[0]).'</td>'."\n";
  381. }
  382. print '</tr>'."\n";
  383. }
  384. // Loop on each answer
  385. $sumfor = array();
  386. $sumagainst = array();
  387. $compteur = 0;
  388. $sql = "SELECT id_users, nom as name, id_sondage, reponses";
  389. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  390. $sql .= " WHERE id_sondage = '".$db->escape($numsondage)."'";
  391. $resql = $db->query($sql);
  392. if (!$resql) {
  393. dol_print_error($db);
  394. exit;
  395. }
  396. $num = $db->num_rows($resql);
  397. while ($compteur < $num) {
  398. $obj = $db->fetch_object($resql);
  399. $ensemblereponses = $obj->reponses;
  400. // ligne d'un usager pré-authentifié
  401. $mod_ok = (in_array($obj->name, $listofvoters));
  402. if (!$mod_ok && !$object->allow_spy) {
  403. $compteur++;
  404. continue;
  405. }
  406. print '<tr>'."\n";
  407. // Name
  408. print '<td class="nom">'.dol_htmlentities($obj->name).'</td>'."\n";
  409. // si la ligne n'est pas a changer, on affiche les données
  410. if (!$testligneamodifier) {
  411. for ($i = 0; $i < $nbcolonnes; $i++) {
  412. $car = substr($ensemblereponses, $i, 1);
  413. //print 'xx'.$i."-".$car.'-'.$listofanswers[$i]['format'].'zz';
  414. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  415. if (((string) $car) == "1") {
  416. print '<td class="ok">OK</td>'."\n";
  417. } else {
  418. print '<td class="non">KO</td>'."\n";
  419. }
  420. // Total
  421. if (!isset($sumfor[$i])) {
  422. $sumfor[$i] = 0;
  423. }
  424. if (((string) $car) == "1") {
  425. $sumfor[$i]++;
  426. }
  427. }
  428. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  429. if (((string) $car) == "1") {
  430. print '<td class="ok">'.$langs->trans("Yes").'</td>'."\n";
  431. } elseif (((string) $car) == "0") {
  432. print '<td class="non">'.$langs->trans("No").'</td>'."\n";
  433. } else {
  434. print '<td class="vide">&nbsp;</td>'."\n";
  435. }
  436. // Total
  437. if (!isset($sumfor[$i])) {
  438. $sumfor[$i] = 0;
  439. }
  440. if (!isset($sumagainst[$i])) {
  441. $sumagainst[$i] = 0;
  442. }
  443. if (((string) $car) == "1") {
  444. $sumfor[$i]++;
  445. }
  446. if (((string) $car) == "0") {
  447. $sumagainst[$i]++;
  448. }
  449. }
  450. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  451. if (((string) $car) == "1") {
  452. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  453. } elseif (((string) $car) == "0") {
  454. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  455. } else {
  456. print '<td class="vide">&nbsp;</td>'."\n";
  457. }
  458. // Total
  459. if (!isset($sumfor[$i])) {
  460. $sumfor[$i] = 0;
  461. }
  462. if (!isset($sumagainst[$i])) {
  463. $sumagainst[$i] = 0;
  464. }
  465. if (((string) $car) == "1") {
  466. $sumfor[$i]++;
  467. }
  468. if (((string) $car) == "0") {
  469. $sumagainst[$i]++;
  470. }
  471. }
  472. }
  473. } else {
  474. //sinon on remplace les choix de l'utilisateur par une ligne de checkbox pour recuperer de nouvelles valeurs
  475. if ($compteur == $ligneamodifier) {
  476. for ($i = 0; $i < $nbcolonnes; $i++) {
  477. $car = substr($ensemblereponses, $i, 1);
  478. print '<td class="vide">';
  479. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  480. print '<input type="checkbox" name="choix'.$i.'" value="1" ';
  481. if ($car == '1') {
  482. print 'checked';
  483. }
  484. print '>';
  485. }
  486. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  487. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("No"), '1'=>$langs->trans("Yes"));
  488. print $form->selectarray("choix".$i, $arraychoice, $car);
  489. }
  490. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  491. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("Against"), '1'=>$langs->trans("For"));
  492. print $form->selectarray("choix".$i, $arraychoice, $car);
  493. }
  494. print '</td>'."\n";
  495. }
  496. } else {
  497. for ($i = 0; $i < $nbcolonnes; $i++) {
  498. $car = substr($ensemblereponses, $i, 1);
  499. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  500. if (((string) $car) == "1") {
  501. print '<td class="ok">OK</td>'."\n";
  502. } else {
  503. print '<td class="non">KO</td>'."\n";
  504. }
  505. // Total
  506. if (!isset($sumfor[$i])) {
  507. $sumfor[$i] = 0;
  508. }
  509. if (((string) $car) == "1") {
  510. $sumfor[$i]++;
  511. }
  512. }
  513. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  514. if (((string) $car) == "1") {
  515. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  516. } elseif (((string) $car) == "0") {
  517. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  518. } else {
  519. print '<td class="vide">&nbsp;</td>'."\n";
  520. }
  521. // Total
  522. if (!isset($sumfor[$i])) {
  523. $sumfor[$i] = 0;
  524. }
  525. if (!isset($sumagainst[$i])) {
  526. $sumagainst[$i] = 0;
  527. }
  528. if (((string) $car) == "1") {
  529. $sumfor[$i]++;
  530. }
  531. if (((string) $car) == "0") {
  532. $sumagainst[$i]++;
  533. }
  534. }
  535. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  536. if (((string) $car) == "1") {
  537. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  538. } elseif (((string) $car) == "0") {
  539. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  540. } else {
  541. print '<td class="vide">&nbsp;</td>'."\n";
  542. }
  543. // Total
  544. if (!isset($sumfor[$i])) {
  545. $sumfor[$i] = 0;
  546. }
  547. if (!isset($sumagainst[$i])) {
  548. $sumagainst[$i] = 0;
  549. }
  550. if (((string) $car) == "1") {
  551. $sumfor[$i]++;
  552. }
  553. if (((string) $car) == "0") {
  554. $sumagainst[$i]++;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. // Button edit at end of line
  561. if ($compteur != $ligneamodifier && $mod_ok) {
  562. print '<td class="casevide"><input type="submit" class="button smallpaddingimp" name="modifierligne'.$compteur.'" value="'.dol_escape_htmltag($langs->trans("Edit")).'"></td>'."\n";
  563. }
  564. //demande de confirmation pour modification de ligne
  565. for ($i = 0; $i < $nblines; $i++) {
  566. if (GETPOSTISSET("modifierligne".$i)) {
  567. if ($compteur == $i) {
  568. print '<td class="casevide">';
  569. print '<input type="hidden" name="idtomodify'.$compteur.'" value="'.$obj->id_users.'">';
  570. print '<input type="submit" class="button button-save" name="validermodifier'.$compteur.'" value="'.dol_escape_htmltag($langs->trans("Save")).'">';
  571. print '</td>'."\n";
  572. }
  573. }
  574. }
  575. $compteur++;
  576. print '</tr>'."\n";
  577. }
  578. // Add line to add new record
  579. if ($ligneamodifier < 0 && (!isset($_SESSION['nom']))) {
  580. print '<tr>'."\n";
  581. print '<td class="nom">'."\n";
  582. if (isset($_SESSION['nom'])) {
  583. print '<input type=hidden name="nom" value="'.$_SESSION['nom'].'">'.$_SESSION['nom']."\n";
  584. } else {
  585. print '<input type="text" name="nom" placeholder="'.dol_escape_htmltag($langs->trans("Name")).'" maxlength="64" class=" minwidth175">'."\n";
  586. }
  587. print '</td>'."\n";
  588. // affichage des cases de formulaire checkbox pour un nouveau choix
  589. for ($i = 0; $i < $nbcolonnes; $i++) {
  590. print '<td class="vide">';
  591. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  592. print '<input type="checkbox" name="choix'.$i.'" value="1"';
  593. if (GETPOSTISSET('choix'.$i) && GETPOST('choix'.$i) == '1') {
  594. print ' checked';
  595. }
  596. print '>';
  597. }
  598. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  599. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("No"), '1'=>$langs->trans("Yes"));
  600. print $form->selectarray("choix".$i, $arraychoice, GETPOST('choix'.$i));
  601. }
  602. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  603. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("Against"), '1'=>$langs->trans("For"));
  604. print $form->selectarray("choix".$i, $arraychoice, GETPOST('choix'.$i));
  605. }
  606. print '</td>'."\n";
  607. }
  608. // Affichage du bouton de formulaire pour inscrire un nouvel utilisateur dans la base
  609. print '<td><input type="image" class="borderimp" name="boutonp" value="'.$langs->trans("Vote").'" src="'.img_picto('', 'edit_add', '', false, 1).'"></td>'."\n";
  610. print '</tr>'."\n";
  611. }
  612. // Select value of best choice (for checkbox columns only)
  613. $nbofcheckbox = 0;
  614. for ($i = 0; $i < $nbcolonnes; $i++) {
  615. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  616. $nbofcheckbox++;
  617. }
  618. if (isset($sumfor[$i])) {
  619. if ($i == 0) {
  620. $meilleurecolonne = $sumfor[$i];
  621. }
  622. if (!isset($meilleurecolonne) || $sumfor[$i] > $meilleurecolonne) {
  623. $meilleurecolonne = $sumfor[$i];
  624. }
  625. }
  626. }
  627. if ($object->allow_spy) {
  628. // Show line total
  629. print '<tr>'."\n";
  630. print '<td class="center">'.$langs->trans("Total").'</td>'."\n";
  631. for ($i = 0; $i < $nbcolonnes; $i++) {
  632. $showsumfor = isset($sumfor[$i]) ? $sumfor[$i] : '';
  633. $showsumagainst = isset($sumagainst[$i]) ? $sumagainst[$i] : '';
  634. if (empty($showsumfor)) {
  635. $showsumfor = 0;
  636. }
  637. if (empty($showsumagainst)) {
  638. $showsumagainst = 0;
  639. }
  640. print '<td>';
  641. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  642. print $showsumfor;
  643. }
  644. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  645. print $langs->trans("Yes").': '.$showsumfor.'<br>'.$langs->trans("No").': '.$showsumagainst;
  646. }
  647. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  648. print $langs->trans("For").': '.$showsumfor.'<br>'.$langs->trans("Against").': '.$showsumagainst;
  649. }
  650. print '</td>'."\n";
  651. }
  652. print '</tr>';
  653. // Show picto winner
  654. if ($nbofcheckbox >= 2) {
  655. print '<tr>'."\n";
  656. print '<td class="somme"></td>'."\n";
  657. for ($i = 0; $i < $nbcolonnes; $i++) {
  658. //print 'xx'.(!empty($listofanswers[$i]['format'])).'-'.$sumfor[$i].'-'.$meilleurecolonne;
  659. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst')) && isset($sumfor[$i]) && isset($meilleurecolonne) && $sumfor[$i] == $meilleurecolonne) {
  660. print '<td class="somme"><img src="'.dol_buildpath('/opensurvey/img/medaille.png', 1).'"></td>'."\n";
  661. } else {
  662. print '<td class="somme"></td>'."\n";
  663. }
  664. }
  665. print '</tr>'."\n";
  666. }
  667. }
  668. print '</table>'."\n";
  669. print '</div>'."\n";
  670. if ($object->allow_spy) {
  671. $toutsujet = explode(",", $object->sujet);
  672. $toutsujet = str_replace("°", "'", $toutsujet);
  673. $compteursujet = 0;
  674. $meilleursujet = '';
  675. for ($i = 0; $i < $nbcolonnes; $i++) {
  676. if (isset($sumfor[$i]) && isset($meilleurecolonne) && $sumfor[$i] == $meilleurecolonne) {
  677. $meilleursujet .= ($meilleursujet ? ", " : "");
  678. if ($object->format == "D") {
  679. $meilleursujetexport = $toutsujet[$i];
  680. if (strpos($toutsujet[$i], '@') !== false) {
  681. $toutsujetdate = explode("@", $toutsujet[$i]);
  682. $meilleursujet .= dol_print_date($toutsujetdate[0], 'daytext').' ('.dol_print_date($toutsujetdate[0], '%A').') - '.$toutsujetdate[1];
  683. } else {
  684. $meilleursujet .= dol_print_date((empty($toutsujet[$i]) ? 0 : $toutsujet[$i]), 'daytext').' ('.dol_print_date((empty($toutsujet[$i]) ? 0 : $toutsujet[$i]), '%A').')';
  685. }
  686. } else {
  687. $tmps = explode('@', $toutsujet[$i]);
  688. $meilleursujet .= dol_htmlentities($tmps[0]);
  689. }
  690. $compteursujet++;
  691. }
  692. }
  693. $meilleursujet = substr("$meilleursujet", 1);
  694. $meilleursujet = str_replace("°", "'", $meilleursujet);
  695. // Show best choice
  696. if ($nbofcheckbox >= 2) {
  697. $vote_str = $langs->trans('votes');
  698. print '<p class="affichageresultats">'."\n";
  699. if (isset($meilleurecolonne) && $compteursujet == "1") {
  700. print '<img src="'.dol_buildpath('/opensurvey/img/medaille.png', 1).'"> '.$langs->trans('TheBestChoice').": <b>".$meilleursujet."</b> ".$langs->trans('with')." <b>".$meilleurecolonne."</b>".$vote_str.".\n";
  701. } elseif (isset($meilleurecolonne)) {
  702. print '<img src="'.dol_buildpath('/opensurvey/img/medaille.png', 1).'"> '.$langs->trans('TheBestChoices').": <b>".$meilleursujet."</b> ".$langs->trans('with')." <b>".$meilleurecolonne."</b>".$vote_str.".\n";
  703. }
  704. print '</p><br>'."\n";
  705. }
  706. }
  707. print '<br>';
  708. // Comment list
  709. $comments = $object->getComments();
  710. if ($comments) {
  711. print '<br><u><span class="bold opacitymedium">'.$langs->trans("CommentsOfVoters").':</span></u><br>'."\n";
  712. foreach ($comments as $obj) {
  713. // ligne d'un usager pré-authentifié
  714. //$mod_ok = (in_array($obj->name, $listofvoters));
  715. print '<div class="comment"><span class="usercomment">';
  716. if (in_array($obj->usercomment, $listofvoters)) {
  717. print '<a href="'.$_SERVER["PHP_SELF"].'?deletecomment='.$obj->id_comment.'&sondage='.$numsondage.'"> '.img_picto('', 'delete.png', '', false, 0, 0, '', 'nomarginleft').'</a> ';
  718. }
  719. //else print img_picto('', 'ellipsis-h', '', false, 0, 0, '', 'nomarginleft').' ';
  720. print dol_htmlentities($obj->usercomment).':</span> <span class="comment">'.dol_nl2br(dol_htmlentities($obj->comment))."</span></div>";
  721. }
  722. }
  723. // Form to add comment
  724. if ($object->allow_comments) {
  725. print '<br><div class="addcomment"><span class="opacitymedium">'.$langs->trans("AddACommentForPoll")."</span><br>\n";
  726. print '<textarea name="comment" rows="'.ROWS_2.'" class="quatrevingtpercent">'.dol_escape_htmltag(GETPOST('comment', 'alphanohtml'), 0, 1).'</textarea><br>'."\n";
  727. print $langs->trans("Name").': ';
  728. print '<input type="text" name="commentuser" maxlength="64" value="'.dol_escape_htmltag(GETPOST('commentuser', 'alphanohtml')).'"> &nbsp; '."\n";
  729. print '<input type="submit" class="button" name="ajoutcomment" value="'.dol_escape_htmltag($langs->trans("AddComment")).'"><br>'."\n";
  730. print '</form>'."\n";
  731. print '</div>'."\n"; // div add comment
  732. }
  733. print '<br><br>';
  734. print '<a name="bas"></a>'."\n";
  735. llxFooterSurvey();
  736. $db->close();