survey_invite.php 10 KB

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