studs.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 '<span class="opacitymedium">'.$langs->trans("OpenSurveyHowTo").'</span><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. if (empty($object->description)) {
  284. print '<div class="corps"> '."\n";
  285. }
  286. // show title of survey
  287. $titre = str_replace("\\", "", $object->title);
  288. print '<strong>'.dol_htmlentities($titre).'</strong><br>';
  289. if (!empty($object->description)) {
  290. print '<br><div class="corps"> '."\n";
  291. }
  292. // show description of survey
  293. if ($object->description) {
  294. print dol_htmlentitiesbr($object->description);
  295. }
  296. print '</div>'."\n";
  297. //The survey has expired, users can't vote or do any action
  298. if (!$canbemodified) {
  299. print '<br><center><div class="quatrevingtpercent center warning">'.$langs->trans('SurveyExpiredInfo').'</div></center>';
  300. llxFooterSurvey();
  301. $db->close();
  302. exit;
  303. }
  304. print '<div class="cadre"> '."\n";
  305. print '<br><br>'."\n";
  306. // Start to show survey result
  307. print '<table class="resultats">'."\n";
  308. // Show choice titles
  309. if ($object->format == "D") {
  310. //display of survey topics
  311. print '<tr>'."\n";
  312. print '<td></td>'."\n";
  313. //display of years
  314. $colspan = 1;
  315. $nbofsujet = count($toutsujet);
  316. for ($i = 0; $i < $nbofsujet; $i++) {
  317. if (isset($toutsujet[$i + 1]) && date('Y', intval($toutsujet[$i])) == date('Y', intval($toutsujet[$i + 1]))) {
  318. $colspan++;
  319. } else {
  320. print '<td colspan='.$colspan.' class="annee">'.date('Y', intval($toutsujet[$i])).'</td>'."\n";
  321. $colspan = 1;
  322. }
  323. }
  324. print '</tr>'."\n";
  325. print '<tr>'."\n";
  326. print '<td></td>'."\n";
  327. //display of months
  328. $colspan = 1;
  329. for ($i = 0; $i < $nbofsujet; $i++) {
  330. $cur = intval($toutsujet[$i]); // intval() est utiliser pour supprimer le suffixe @* qui déplaît logiquement à strftime()
  331. if (isset($toutsujet[$i + 1]) === false) {
  332. $next = false;
  333. } else {
  334. $next = intval($toutsujet[$i + 1]);
  335. }
  336. if ($next && dol_print_date($cur, "%B") == dol_print_date($next, "%B") && dol_print_date($cur, "%Y") == dol_print_date($next, "%Y")) {
  337. $colspan++;
  338. } else {
  339. print '<td colspan='.$colspan.' class="mois">'.dol_print_date($cur, "%B").'</td>'."\n";
  340. $colspan = 1;
  341. }
  342. }
  343. print '</tr>'."\n";
  344. print '<tr>'."\n";
  345. print '<td></td>'."\n";
  346. //display of days
  347. $colspan = 1;
  348. for ($i = 0; $i < $nbofsujet; $i++) {
  349. $cur = intval($toutsujet[$i]);
  350. if (isset($toutsujet[$i + 1]) === false) {
  351. $next = false;
  352. } else {
  353. $next = intval($toutsujet[$i + 1]);
  354. }
  355. if ($next && dol_print_date($cur, "%a %d") == dol_print_date($next, "%a %d") && dol_print_date($cur, "%B") == dol_print_date($next, "%B")) {
  356. $colspan++;
  357. } else {
  358. print '<td colspan="'.$colspan.'" class="jour">'.dol_print_date($cur, "%a %d").'</td>'."\n";
  359. $colspan = 1;
  360. }
  361. }
  362. print '</tr>'."\n";
  363. //Display schedules
  364. if (strpos($object->sujet, '@') !== false) {
  365. print '<tr>'."\n";
  366. print '<td></td>'."\n";
  367. for ($i = 0; isset($toutsujet[$i]); $i++) {
  368. $heures = explode('@', $toutsujet[$i]);
  369. if (isset($heures[1])) {
  370. print '<td class="heure">'.dol_htmlentities($heures[1]).'</td>'."\n";
  371. } else {
  372. print '<td class="heure"></td>'."\n";
  373. }
  374. }
  375. print '</tr>'."\n";
  376. }
  377. } else {
  378. //display of survey topics
  379. print '<tr>'."\n";
  380. print '<td></td>'."\n";
  381. for ($i = 0; isset($toutsujet[$i]); $i++) {
  382. $tmp = explode('@', $toutsujet[$i]);
  383. print '<td class="sujet">'.dol_escape_htmltag($tmp[0]).'</td>'."\n";
  384. }
  385. print '</tr>'."\n";
  386. }
  387. // Loop on each answer
  388. $currentusername = '';
  389. $sumfor = array();
  390. $sumagainst = array();
  391. $compteur = 0;
  392. $sql = "SELECT id_users, nom as name, id_sondage, reponses";
  393. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  394. $sql .= " WHERE id_sondage = '".$db->escape($numsondage)."'";
  395. $resql = $db->query($sql);
  396. if (!$resql) {
  397. dol_print_error($db);
  398. exit;
  399. }
  400. $num = $db->num_rows($resql);
  401. while ($compteur < $num) {
  402. $obj = $db->fetch_object($resql);
  403. $ensemblereponses = $obj->reponses;
  404. // ligne d'un usager pré-authentifié
  405. $mod_ok = (in_array($obj->name, $listofvoters));
  406. if (!$mod_ok && !$object->allow_spy) {
  407. $compteur++;
  408. continue;
  409. }
  410. print '<tr>'."\n";
  411. // Name
  412. print '<td class="nom">'.dol_htmlentities($obj->name).'</td>'."\n";
  413. // si la ligne n'est pas a changer, on affiche les données
  414. if (!$testligneamodifier) {
  415. for ($i = 0; $i < $nbcolonnes; $i++) {
  416. $car = substr($ensemblereponses, $i, 1);
  417. //print 'xx'.$i."-".$car.'-'.$listofanswers[$i]['format'].'zz';
  418. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  419. if (((string) $car) == "1") {
  420. print '<td class="ok">OK</td>'."\n";
  421. } else {
  422. print '<td class="non">KO</td>'."\n";
  423. }
  424. // Total
  425. if (!isset($sumfor[$i])) {
  426. $sumfor[$i] = 0;
  427. }
  428. if (((string) $car) == "1") {
  429. $sumfor[$i]++;
  430. }
  431. }
  432. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  433. if (((string) $car) == "1") {
  434. print '<td class="ok">'.$langs->trans("Yes").'</td>'."\n";
  435. } elseif (((string) $car) == "0") {
  436. print '<td class="non">'.$langs->trans("No").'</td>'."\n";
  437. } else {
  438. print '<td class="vide">&nbsp;</td>'."\n";
  439. }
  440. // Total
  441. if (!isset($sumfor[$i])) {
  442. $sumfor[$i] = 0;
  443. }
  444. if (!isset($sumagainst[$i])) {
  445. $sumagainst[$i] = 0;
  446. }
  447. if (((string) $car) == "1") {
  448. $sumfor[$i]++;
  449. }
  450. if (((string) $car) == "0") {
  451. $sumagainst[$i]++;
  452. }
  453. }
  454. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  455. if (((string) $car) == "1") {
  456. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  457. } elseif (((string) $car) == "0") {
  458. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  459. } else {
  460. print '<td class="vide">&nbsp;</td>'."\n";
  461. }
  462. // Total
  463. if (!isset($sumfor[$i])) {
  464. $sumfor[$i] = 0;
  465. }
  466. if (!isset($sumagainst[$i])) {
  467. $sumagainst[$i] = 0;
  468. }
  469. if (((string) $car) == "1") {
  470. $sumfor[$i]++;
  471. }
  472. if (((string) $car) == "0") {
  473. $sumagainst[$i]++;
  474. }
  475. }
  476. }
  477. } else {
  478. //sinon on remplace les choix de l'utilisateur par une ligne de checkbox pour saisie
  479. if ($compteur == $ligneamodifier) {
  480. for ($i = 0; $i < $nbcolonnes; $i++) {
  481. $car = substr($ensemblereponses, $i, 1);
  482. print '<td class="vide">';
  483. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  484. print '<input type="checkbox" name="choix'.$i.'" value="1" ';
  485. if ($car == '1') {
  486. print 'checked';
  487. }
  488. print '>';
  489. }
  490. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  491. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("No"), '1'=>$langs->trans("Yes"));
  492. print $form->selectarray("choix".$i, $arraychoice, $car);
  493. }
  494. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  495. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("Against"), '1'=>$langs->trans("For"));
  496. print $form->selectarray("choix".$i, $arraychoice, $car);
  497. }
  498. print '</td>'."\n";
  499. }
  500. } else {
  501. for ($i = 0; $i < $nbcolonnes; $i++) {
  502. $car = substr($ensemblereponses, $i, 1);
  503. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  504. if (((string) $car) == "1") {
  505. print '<td class="ok">OK</td>'."\n";
  506. } else {
  507. print '<td class="non">KO</td>'."\n";
  508. }
  509. // Total
  510. if (!isset($sumfor[$i])) {
  511. $sumfor[$i] = 0;
  512. }
  513. if (((string) $car) == "1") {
  514. $sumfor[$i]++;
  515. }
  516. }
  517. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  518. if (((string) $car) == "1") {
  519. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  520. } elseif (((string) $car) == "0") {
  521. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  522. } else {
  523. print '<td class="vide">&nbsp;</td>'."\n";
  524. }
  525. // Total
  526. if (!isset($sumfor[$i])) {
  527. $sumfor[$i] = 0;
  528. }
  529. if (!isset($sumagainst[$i])) {
  530. $sumagainst[$i] = 0;
  531. }
  532. if (((string) $car) == "1") {
  533. $sumfor[$i]++;
  534. }
  535. if (((string) $car) == "0") {
  536. $sumagainst[$i]++;
  537. }
  538. }
  539. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  540. if (((string) $car) == "1") {
  541. print '<td class="ok">'.$langs->trans("For").'</td>'."\n";
  542. } elseif (((string) $car) == "0") {
  543. print '<td class="non">'.$langs->trans("Against").'</td>'."\n";
  544. } else {
  545. print '<td class="vide">&nbsp;</td>'."\n";
  546. }
  547. // Total
  548. if (!isset($sumfor[$i])) {
  549. $sumfor[$i] = 0;
  550. }
  551. if (!isset($sumagainst[$i])) {
  552. $sumagainst[$i] = 0;
  553. }
  554. if (((string) $car) == "1") {
  555. $sumfor[$i]++;
  556. }
  557. if (((string) $car) == "0") {
  558. $sumagainst[$i]++;
  559. }
  560. }
  561. }
  562. }
  563. }
  564. // Button edit at end of line
  565. if ($compteur != $ligneamodifier && $mod_ok) {
  566. $currentusername = $obj->name;
  567. print '<td class="casevide"><input type="submit" class="button small" name="modifierligne'.$compteur.'" value="'.dol_escape_htmltag($langs->trans("Edit")).'"></td>'."\n";
  568. }
  569. //demande de confirmation pour modification de ligne
  570. for ($i = 0; $i < $nblines; $i++) {
  571. if (GETPOSTISSET("modifierligne".$i)) {
  572. if ($compteur == $i) {
  573. print '<td class="casevide">';
  574. print '<input type="hidden" name="idtomodify'.$compteur.'" value="'.$obj->id_users.'">';
  575. print '<input type="submit" class="button button-save small" name="validermodifier'.$compteur.'" value="'.dol_escape_htmltag($langs->trans("Save")).'">';
  576. print '</td>'."\n";
  577. }
  578. }
  579. }
  580. $compteur++;
  581. print '</tr>'."\n";
  582. }
  583. // Add line to add new record
  584. if ($ligneamodifier < 0 && (!isset($_SESSION['nom']))) {
  585. print '<tr>'."\n";
  586. print '<td class="nom">'."\n";
  587. if (isset($_SESSION['nom'])) {
  588. print '<input type=hidden name="nom" value="'.$_SESSION['nom'].'">'.$_SESSION['nom']."\n";
  589. } else {
  590. print '<input type="text" name="nom" placeholder="'.dol_escape_htmltag($langs->trans("Name")).'" maxlength="64" class=" minwidth175" value="">'."\n";
  591. }
  592. print '</td>'."\n";
  593. // affichage des cases de formulaire checkbox pour un nouveau choix
  594. for ($i = 0; $i < $nbcolonnes; $i++) {
  595. print '<td class="vide">';
  596. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  597. print '<input type="checkbox" name="choix'.$i.'" value="1"';
  598. if (GETPOSTISSET('choix'.$i) && GETPOST('choix'.$i) == '1') {
  599. print ' checked';
  600. }
  601. print '>';
  602. }
  603. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  604. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("No"), '1'=>$langs->trans("Yes"));
  605. print $form->selectarray("choix".$i, $arraychoice, GETPOST('choix'.$i));
  606. }
  607. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  608. $arraychoice = array('2'=>'&nbsp;', '0'=>$langs->trans("Against"), '1'=>$langs->trans("For"));
  609. print $form->selectarray("choix".$i, $arraychoice, GETPOST('choix'.$i));
  610. }
  611. print '</td>'."\n";
  612. }
  613. // Show button to add a new line into database
  614. print '<td><input type="image" class="borderimp" name="boutonp" value="'.$langs->trans("Vote").'" src="'.img_picto('', 'edit_add', '', false, 1).'"></td>'."\n";
  615. print '</tr>'."\n";
  616. }
  617. // Select value of best choice (for checkbox columns only)
  618. $nbofcheckbox = 0;
  619. for ($i = 0; $i < $nbcolonnes; $i++) {
  620. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  621. $nbofcheckbox++;
  622. }
  623. if (isset($sumfor[$i])) {
  624. if ($i == 0) {
  625. $meilleurecolonne = $sumfor[$i];
  626. }
  627. if (!isset($meilleurecolonne) || $sumfor[$i] > $meilleurecolonne) {
  628. $meilleurecolonne = $sumfor[$i];
  629. }
  630. }
  631. }
  632. if ($object->allow_spy) {
  633. // Show line total
  634. print '<tr>'."\n";
  635. print '<td class="center">'.$langs->trans("Total").'</td>'."\n";
  636. for ($i = 0; $i < $nbcolonnes; $i++) {
  637. $showsumfor = isset($sumfor[$i]) ? $sumfor[$i] : '';
  638. $showsumagainst = isset($sumagainst[$i]) ? $sumagainst[$i] : '';
  639. if (empty($showsumfor)) {
  640. $showsumfor = 0;
  641. }
  642. if (empty($showsumagainst)) {
  643. $showsumagainst = 0;
  644. }
  645. print '<td>';
  646. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst'))) {
  647. print $showsumfor;
  648. }
  649. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'yesno') {
  650. print $langs->trans("Yes").': '.$showsumfor.'<br>'.$langs->trans("No").': '.$showsumagainst;
  651. }
  652. if (!empty($listofanswers[$i]['format']) && $listofanswers[$i]['format'] == 'foragainst') {
  653. print $langs->trans("For").': '.$showsumfor.'<br>'.$langs->trans("Against").': '.$showsumagainst;
  654. }
  655. print '</td>'."\n";
  656. }
  657. print '</tr>';
  658. // Show picto winner
  659. if ($nbofcheckbox >= 2) {
  660. print '<tr>'."\n";
  661. print '<td class="somme"></td>'."\n";
  662. for ($i = 0; $i < $nbcolonnes; $i++) {
  663. //print 'xx'.(!empty($listofanswers[$i]['format'])).'-'.$sumfor[$i].'-'.$meilleurecolonne;
  664. if (empty($listofanswers[$i]['format']) || !in_array($listofanswers[$i]['format'], array('yesno', 'foragainst')) && isset($sumfor[$i]) && isset($meilleurecolonne) && $sumfor[$i] == $meilleurecolonne) {
  665. print '<td class="somme"><img src="'.dol_buildpath('/opensurvey/img/medaille.png', 1).'"></td>'."\n";
  666. } else {
  667. print '<td class="somme"></td>'."\n";
  668. }
  669. }
  670. print '</tr>'."\n";
  671. }
  672. }
  673. print '</table>'."\n";
  674. print '</div>'."\n";
  675. if ($object->allow_spy) {
  676. $toutsujet = explode(",", $object->sujet);
  677. $toutsujet = str_replace("°", "'", $toutsujet);
  678. $compteursujet = 0;
  679. $meilleursujet = '';
  680. for ($i = 0; $i < $nbcolonnes; $i++) {
  681. if (isset($sumfor[$i]) && isset($meilleurecolonne) && $sumfor[$i] == $meilleurecolonne) {
  682. $meilleursujet .= ($meilleursujet ? ", " : "");
  683. if ($object->format == "D") {
  684. $meilleursujetexport = $toutsujet[$i];
  685. if (strpos($toutsujet[$i], '@') !== false) {
  686. $toutsujetdate = explode("@", $toutsujet[$i]);
  687. $meilleursujet .= dol_print_date($toutsujetdate[0], 'daytext').' ('.dol_print_date($toutsujetdate[0], '%A').') - '.$toutsujetdate[1];
  688. } else {
  689. $meilleursujet .= dol_print_date((empty($toutsujet[$i]) ? 0 : $toutsujet[$i]), 'daytext').' ('.dol_print_date((empty($toutsujet[$i]) ? 0 : $toutsujet[$i]), '%A').')';
  690. }
  691. } else {
  692. $tmps = explode('@', $toutsujet[$i]);
  693. $meilleursujet .= dol_htmlentities($tmps[0]);
  694. }
  695. $compteursujet++;
  696. }
  697. }
  698. $meilleursujet = substr("$meilleursujet", 1);
  699. $meilleursujet = str_replace("°", "'", $meilleursujet);
  700. // Show best choice
  701. if ($nbofcheckbox >= 2) {
  702. $vote_str = $langs->trans('votes');
  703. print '<p class="affichageresultats">'."\n";
  704. if (isset($meilleurecolonne) && $compteursujet == "1") {
  705. 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";
  706. } elseif (isset($meilleurecolonne)) {
  707. 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";
  708. }
  709. print '</p><br>'."\n";
  710. }
  711. }
  712. print '<br>';
  713. // Comment list
  714. $comments = $object->getComments();
  715. if ($comments) {
  716. print '<br><u><span class="bold opacitymedium">'.$langs->trans("CommentsOfVoters").':</span></u><br>'."\n";
  717. foreach ($comments as $obj) {
  718. // ligne d'un usager pré-authentifié
  719. //$mod_ok = (in_array($obj->name, $listofvoters));
  720. print '<div class="comment"><span class="usercomment">';
  721. if (in_array($obj->usercomment, $listofvoters)) {
  722. print '<a href="'.$_SERVER["PHP_SELF"].'?deletecomment='.$obj->id_comment.'&sondage='.$numsondage.'"> '.img_picto('', 'delete.png', '', false, 0, 0, '', 'nomarginleft').'</a> ';
  723. }
  724. //else print img_picto('', 'ellipsis-h', '', false, 0, 0, '', 'nomarginleft').' ';
  725. print dol_htmlentities($obj->usercomment).':</span> <span class="comment">'.dol_nl2br(dol_htmlentities($obj->comment))."</span></div>";
  726. }
  727. }
  728. // Form to add comment
  729. if ($object->allow_comments && $currentusername) {
  730. print '<br><div class="addcomment"><span class="opacitymedium">'.$langs->trans("AddACommentForPoll")."</span><br>\n";
  731. print '<textarea name="comment" rows="'.ROWS_2.'" class="quatrevingtpercent">'.dol_escape_htmltag(GETPOST('comment', 'alphanohtml'), 0, 1).'</textarea><br>'."\n";
  732. print $langs->trans("Name").': ';
  733. print '<input type="text" name="commentuser" maxlength="64" value="'.dol_escape_htmltag(GETPOSTISSET('commentuser') ? GETPOST('commentuser', 'alphanohtml') : (empty($_SESSION['nom']) ? $currentusername : $_SESSION['nom'])).'"> &nbsp; '."\n";
  734. print '<input type="submit" class="button smallpaddingimp" name="ajoutcomment" value="'.dol_escape_htmltag($langs->trans("AddComment")).'"><br>'."\n";
  735. print '</form>'."\n";
  736. print '</div>'."\n"; // div add comment
  737. }
  738. print '<br><br>';
  739. print '<a name="bas"></a>'."\n";
  740. llxFooterSurvey();
  741. $db->close();