survey_invite.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  7. * @author Julio Montoya Chamilo: cleanup, refactoring, security improvements
  8. * @version $Id: survey_invite.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo checking if the additional emails are valid (or add a rule for this)
  11. * @todo check if the mailtext contains the **link** part, if not, add the link to the end
  12. * @todo add rules: title and text cannot be empty
  13. */
  14. require_once '../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. if (!api_is_allowed_to_edit(false, true)) {
  17. Display :: display_header(get_lang('ToolSurvey'));
  18. Display :: display_error_message(get_lang('NotAllowed'), false);
  19. Display :: display_footer();
  20. exit;
  21. }
  22. // Database table definitions
  23. $table_survey = Database:: get_course_table(TABLE_SURVEY);
  24. $table_survey_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
  25. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  26. $table_course = Database:: get_main_table(TABLE_MAIN_COURSE);
  27. $table_user = Database:: get_main_table(TABLE_MAIN_USER);
  28. $course_id = api_get_course_int_id();
  29. // Getting the survey information
  30. $survey_id = Security::remove_XSS($_GET['survey_id']);
  31. $survey_data = SurveyManager::get_survey($survey_id);
  32. if (empty($survey_data)) {
  33. Display :: display_header(get_lang('ToolSurvey'));
  34. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  35. Display :: display_footer();
  36. exit;
  37. }
  38. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  39. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  40. $urlname .= '...';
  41. }
  42. // Breadcrumbs
  43. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
  44. if (api_is_course_admin()) {
  45. $interbreadcrumb[] = array(
  46. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  47. 'name' => $urlname,
  48. );
  49. } else {
  50. $interbreadcrumb[] = array(
  51. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  52. 'name' => $urlname,
  53. );
  54. }
  55. $tool_name = get_lang('SurveyPublication');
  56. // Displaying the header
  57. Display::display_header($tool_name,'Survey');
  58. echo '<script>
  59. $(function() {
  60. $("#check_mail").change(function() {
  61. $("#mail_text_wrapper").toggle();
  62. });
  63. });
  64. </script>';
  65. // Checking if there is another survey with this code.
  66. // If this is the case there will be a language choice
  67. $sql = "SELECT * FROM $table_survey
  68. WHERE c_id = $course_id AND code='".Database::escape_string($survey_data['code'])."'";
  69. $result = Database::query($sql);
  70. if (Database::num_rows($result) > 1) {
  71. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  72. }
  73. // Invited / answered message
  74. if ($survey_data['invited'] > 0 && !isset($_POST['submit'])) {
  75. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  76. $message .= get_lang('HaveAnswered').' ';
  77. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  78. $message .= get_lang('WereInvited');
  79. Display::display_normal_message($message, false);
  80. }
  81. // Building the form for publishing the survey
  82. $form = new FormValidator(
  83. 'publish_form',
  84. 'post',
  85. api_get_self().'?survey_id='.$survey_id.'&'.api_get_cidReq()
  86. );
  87. $form->addElement('header', '', $tool_name);
  88. // Course users
  89. $complete_user_list = CourseManager::get_user_list_from_course_code(
  90. api_get_course_id(),
  91. api_get_session_id(),
  92. '',
  93. api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname'
  94. );
  95. $possible_users = array();
  96. foreach ($complete_user_list as & $user) {
  97. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  98. }
  99. CourseManager::addUserGroupMultiSelect($form, array());
  100. /*$form->addElement(
  101. 'advmultiselect',
  102. 'course_users',
  103. get_lang('CourseUsers'),
  104. $possible_users,
  105. 'style="width: 250px; height: 200px;"'
  106. );*/
  107. // Additional users
  108. $form->addElement(
  109. 'textarea',
  110. 'additional_users',
  111. array(get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')),
  112. array('rows' => 5)
  113. );
  114. $form->addElement('html', '<div id="check_mail">');
  115. $form->addElement('checkbox', 'send_mail','', get_lang('SendMail'));
  116. $form->addElement('html', '</div>');
  117. $form->addElement('html', '<div id="mail_text_wrapper">');
  118. // The title of the mail
  119. $form->addText('mail_title', get_lang('MailTitle'), false);
  120. // The text of the mail
  121. $form->addHtmlEditor(
  122. 'mail_text',
  123. array(get_lang('MailText'), get_lang('UseLinkSyntax')),
  124. false,
  125. array('ToolbarSet' => 'Survey', 'Height' => '150')
  126. );
  127. $form->addElement('html', '</div>');
  128. // You cab send a reminder to unanswered people if the survey is not anonymous
  129. if ($survey_data['anonymous'] != 1) {
  130. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  131. }
  132. // Allow resending to all selected users
  133. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  134. // Submit button
  135. $form->addButtonSave(get_lang('PublishSurvey'));
  136. // The rules (required fields)
  137. /*if ($survey_data['send_mail'] == 0) {
  138. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  139. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  140. }*/
  141. $portal_url = api_get_path(WEB_PATH);
  142. if (api_is_multiple_url_enabled()) {
  143. $access_url_id = api_get_current_access_url_id();
  144. if ($access_url_id != -1) {
  145. $url = api_get_access_url($access_url_id);
  146. $portal_url = $url['url'];
  147. }
  148. }
  149. // Show the URL that can be used by users to fill a survey without invitation
  150. $auto_survey_link = $portal_url.'main/survey/fillsurvey.php?course='.$_course['sysCode'].'&invitationcode=auto&scode='.$survey_data['survey_code'];
  151. $form->addElement('label', null, get_lang('AutoInviteLink'));
  152. $form->addElement('label', null, $auto_survey_link);
  153. if ($form->validate()) {
  154. $values = $form->exportValues();
  155. if (isset($values['send_mail']) && $values['send_mail'] == 1) {
  156. if (empty($values['mail_title']) || empty($values['mail_text'])) {
  157. Display :: display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  158. // Getting the invited users
  159. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  160. // Getting the survey mail text
  161. if (!empty($survey_data['reminder_mail'])) {
  162. $defaults['mail_text'] = $survey_data['reminder_mail'];
  163. } else {
  164. $defaults['mail_text'] = $survey_data['invite_mail'];
  165. }
  166. $defaults['mail_title'] = $survey_data['mail_subject'];
  167. $defaults['send_mail'] = 1;
  168. $form->setDefaults($defaults);
  169. $form->display();
  170. return;
  171. }
  172. }
  173. // Save the invitation mail
  174. SurveyUtil::save_invite_mail(
  175. $values['mail_text'],
  176. $values['mail_title'],
  177. !empty($survey_data['invite_mail'])
  178. );
  179. $resendAll = isset($values['resend_to_all']) ? $values['resend_to_all'] : '';
  180. $sendMail = isset($values['send_mail']) ? $values['send_mail'] : '';
  181. $remindUnAnswered = isset($values['remindUnAnswered']) ? $values['remindUnAnswered'] : '';
  182. // Saving the invitations for the course users
  183. $count_course_users = SurveyUtil::saveInvitations(
  184. $values['users'],
  185. $values['mail_title'],
  186. $values['mail_text'],
  187. $resendAll,
  188. $sendMail,
  189. $remindUnAnswered
  190. );
  191. // Saving the invitations for the additional users
  192. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  193. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  194. $additional_users = explode(';', $temp);
  195. for ($i = 0; $i < count($additional_users); $i++) {
  196. $additional_users[$i] = trim($additional_users[$i]);
  197. }
  198. $counter_additional_users = SurveyUtil::saveInvitations(
  199. $additional_users,
  200. $values['mail_title'],
  201. $values['mail_text'],
  202. $resendAll,
  203. $sendMail,
  204. $remindUnAnswered
  205. );
  206. // Updating the invited field in the survey table
  207. SurveyUtil::update_count_invited($survey_data['code']);
  208. $total_count = $count_course_users + $counter_additional_users;
  209. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  210. // Counting the number of people that are invited
  211. $sql = "SELECT * FROM $table_survey
  212. WHERE
  213. c_id = $course_id AND
  214. code = '".Database::escape_string($survey_data['code'])."'
  215. ";
  216. $result = Database::query($sql);
  217. $row = Database::fetch_array($result);
  218. $total_invited = $row['invited'];
  219. if ($total_invited > 0) {
  220. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey_data['survey_id'].'">'.
  221. $survey_data['answered'].'</a> ';
  222. $message .= get_lang('HaveAnswered').' ';
  223. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey_data['survey_id'].'">'.
  224. $total_invited.'</a> ';
  225. $message .= get_lang('WereInvited');
  226. Display::display_normal_message($message, false);
  227. Display::display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  228. }
  229. } else {
  230. // Getting the invited users
  231. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  232. // Getting the survey mail text
  233. if (!empty($survey_data['reminder_mail'])) {
  234. $defaults['mail_text'] = $survey_data['reminder_mail'];
  235. } else {
  236. $defaults['mail_text'] = $survey_data['invite_mail'];
  237. }
  238. $defaults['mail_title'] = $survey_data['mail_subject'];
  239. $defaults['send_mail'] = 1;
  240. $form->setDefaults($defaults);
  241. $form->display();
  242. }
  243. Display :: display_footer();