PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/opensurvey/lib/opensurvey.lib.php

https://github.com/Dolibarr/dolibarr
PHP | 278 lines | 146 code | 44 blank | 88 comment | 14 complexity | e0aec8c501edf780109dde9a593c6ce2 MD5 | raw file
  1. <?php
  2. /* Copyright (C) 2013 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/opensurvey/fonctions.php
  20. * \ingroup opensurvey
  21. * \brief Functions for module
  22. */
  23. /**
  24. * Returns an array with the tabs for the "Opensurvey poll" section
  25. * It loads tabs from modules looking for the entity Opensurveyso
  26. *
  27. * @param Opensurveysondage $object Current viewing poll
  28. * @return array Tabs for the opensurvey section
  29. */
  30. function opensurvey_prepare_head(Opensurveysondage $object)
  31. {
  32. global $langs, $conf;
  33. $h = 0;
  34. $head = array();
  35. $head[0][0] = 'card.php?id='.$object->id_sondage;
  36. $head[0][1] = $langs->trans("Survey");
  37. $head[0][2] = 'general';
  38. $h++;
  39. $head[1][0] = 'results.php?id='.$object->id_sondage;
  40. $head[1][1] = $langs->trans("SurveyResults");
  41. $nbVotes = $object->countVotes();
  42. if ($nbVotes > 0) {
  43. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbVotes).'</span>';
  44. }
  45. $head[1][2] = 'preview';
  46. $h++;
  47. // Show more tabs from modules
  48. // Entries must be declared in modules descriptor with line
  49. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  50. // $this->tabs = array('entity:-tabname); to remove a tab
  51. complete_head_from_modules($conf, $langs, $object, $head, $h, 'opensurveypoll');
  52. complete_head_from_modules($conf, $langs, $object, $head, $h, 'opensurveypoll', 'remove');
  53. return $head;
  54. }
  55. /**
  56. * Show header for new member
  57. *
  58. * @param string $title Title
  59. * @param string $head Head array
  60. * @param int $disablejs More content into html header
  61. * @param int $disablehead More content into html header
  62. * @param array $arrayofjs Array of complementary js files
  63. * @param array $arrayofcss Array of complementary css files
  64. * @param string $numsondage Num survey
  65. * @return void
  66. */
  67. function llxHeaderSurvey($title, $head = "", $disablejs = 0, $disablehead = 0, $arrayofjs = '', $arrayofcss = '', $numsondage = '')
  68. {
  69. global $conf, $langs, $mysoc;
  70. global $dolibarr_main_url_root;
  71. //$replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
  72. top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss, 0, 1); // Show html headers
  73. print '<body id="mainbody" class="publicnewmemberform">';
  74. print '<span id="dolpaymentspan"></span>'."\n";
  75. print '<div class="center">'."\n";
  76. print '<form name="formulaire" action="studs.php?sondage='.urlencode($numsondage).'#bas" method="POST">'."\n";
  77. print '<input type="hidden" name="sondage" value="'.$numsondage.'"/>';
  78. print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
  79. print "\n";
  80. // Show logo (search order: logo defined by PAYMENT_LOGO_suffix, then PAYMENT_LOGO, then small company logo, large company logo, theme logo, common logo)
  81. // Define logo and logosmall
  82. $logosmall = $mysoc->logo_small;
  83. $logo = $mysoc->logo;
  84. //print '<!-- Show logo (logosmall='.$logosmall.' logo='.$logo.') -->'."\n";
  85. // Define urllogo
  86. $urllogo = '';
  87. $urllogofull = '';
  88. if (!empty($logosmall) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$logosmall)) {
  89. $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/thumbs/'.$logosmall);
  90. $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
  91. } elseif (!empty($logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$logo)) {
  92. $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/'.$logo);
  93. $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
  94. }
  95. // Output html code for logo
  96. if ($urllogo) {
  97. print '<div class="backgreypublicpayment">';
  98. print '<div class="logopublicpayment">';
  99. print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
  100. print '>';
  101. print '</div>';
  102. if (empty($conf->global->MAIN_HIDE_POWERED_BY)) {
  103. print '<div class="poweredbypublicpayment opacitymedium right"><a class="poweredbyhref" href="https://www.dolibarr.org?utm_medium=website&utm_source=poweredby" target="dolibarr" rel="noopener">'.$langs->trans("PoweredBy").'<br><img src="'.DOL_URL_ROOT.'/theme/dolibarr_logo.svg" width="80px"></a></div>';
  104. }
  105. print '</div>';
  106. }
  107. if (!empty($conf->global->OPENSURVEY_IMAGE_PUBLIC_INTERFACE)) {
  108. print '<div class="backimagepublicopensurvey">';
  109. print '<img id="idOPENSURVEY_IMAGE_PUBLIC_INTERFACE" src="'.$conf->global->OPENSURVEY_IMAGE_PUBLIC_INTERFACE.'">';
  110. print '</div>';
  111. }
  112. print '<div style="margin-left: 50px; margin-right: 50px; text-align: start;"><br>';
  113. }
  114. /**
  115. * Show footer for new member
  116. *
  117. * @return void
  118. */
  119. function llxFooterSurvey()
  120. {
  121. print '</div>';
  122. print '</form>';
  123. print '</div>';
  124. printCommonFooter('public');
  125. dol_htmloutput_events();
  126. print "</body>\n";
  127. print "</html>\n";
  128. }
  129. /**
  130. * get_server_name
  131. *
  132. * @return string URL to use
  133. */
  134. function get_server_name()
  135. {
  136. global $dolibarr_main_url_root;
  137. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  138. //$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  139. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  140. $url = $urlwithouturlroot.dol_buildpath('/opensurvey/', 1);
  141. if (!preg_match("|/$|", $url)) {
  142. $url = $url."/";
  143. }
  144. return $url;
  145. }
  146. /**
  147. * Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau
  148. *
  149. * @param string $name La clé à tester
  150. * @param array $tableau Le tableau où rechercher la clé ($_POST par défaut)
  151. * @return bool Vrai si la clé existe et renvoie une valeur non vide
  152. */
  153. function issetAndNoEmpty($name, $tableau = null)
  154. {
  155. if ($tableau === null) {
  156. $tableau = $_POST;
  157. }
  158. return (isset($tableau[$name]) === true && empty($tableau[$name]) === false);
  159. }
  160. /**
  161. * Fonction permettant de générer les URL pour les sondage
  162. *
  163. * @param string $id L'identifiant du sondage
  164. * @param bool $admin True pour générer une URL pour l'administration d'un sondage, False pour un URL publique
  165. * @return string L'url pour le sondage
  166. */
  167. function getUrlSondage($id, $admin = false)
  168. {
  169. if ($admin === true) {
  170. $url = get_server_name().'results.php?id='.$id;
  171. } else {
  172. $url = get_server_name().'/public/studs.php?sondage='.$id;
  173. }
  174. return $url;
  175. }
  176. /**
  177. * Generate a random id
  178. *
  179. * @param int $car Length of string to generate key
  180. * @return string
  181. */
  182. function dol_survey_random($car)
  183. {
  184. $string = "";
  185. $chaine = "abcdefghijklmnopqrstuvwxyz123456789";
  186. mt_srand((double) microtime() * 1000000);
  187. for ($i = 0; $i < $car; $i++) {
  188. $string .= $chaine[mt_rand() % strlen($chaine)];
  189. }
  190. return $string;
  191. }
  192. /**
  193. * Add a poll
  194. *
  195. * @return void
  196. */
  197. function ajouter_sondage()
  198. {
  199. global $db, $user;
  200. require_once DOL_DOCUMENT_ROOT.'/opensurvey/class/opensurveysondage.class.php';
  201. $sondage = dol_survey_random(16);
  202. $allow_comments = empty($_SESSION['allow_comments']) ? 0 : 1;
  203. $allow_spy = empty($_SESSION['allow_spy']) ? 0 : 1;
  204. // Insert survey
  205. $opensurveysondage = new Opensurveysondage($db);
  206. $opensurveysondage->id_sondage = $sondage;
  207. $opensurveysondage->description = $_SESSION['description'];
  208. $opensurveysondage->mail_admin = $_SESSION['adresse'];
  209. $opensurveysondage->nom_admin = $_SESSION['nom'];
  210. $opensurveysondage->title = $_SESSION['title'];
  211. $opensurveysondage->date_fin = $_SESSION['champdatefin'];
  212. $opensurveysondage->format = $_SESSION['formatsondage'];
  213. $opensurveysondage->mailsonde = $_SESSION['mailsonde'];
  214. $opensurveysondage->allow_comments = $allow_comments;
  215. $opensurveysondage->allow_spy = $allow_spy;
  216. $opensurveysondage->sujet = $_SESSION['toutchoix'];
  217. $res = $opensurveysondage->create($user);
  218. if ($res < 0) {
  219. dol_print_error($db);
  220. }
  221. unset($_SESSION["title"]);
  222. unset($_SESSION["nom"]);
  223. unset($_SESSION["adresse"]);
  224. unset($_SESSION["description"]);
  225. unset($_SESSION["mailsonde"]);
  226. unset($_SESSION['allow_comments']);
  227. unset($_SESSION['allow_spy']);
  228. unset($_SESSION['toutchoix']);
  229. unset($_SESSION['totalchoixjour']);
  230. unset($_SESSION['champdatefin']);
  231. $urlback = dol_buildpath('/opensurvey/card.php', 1).'?id='.$sondage;
  232. header("Location: ".$urlback);
  233. exit();
  234. }