survey_invite.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // Language file that needs to be included
  15. $language_file = 'survey';
  16. // Including the global initialization file
  17. require_once '../inc/global.inc.php';
  18. // Including additional libraries
  19. require_once 'survey.lib.php';
  20. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  21. $this_section = SECTION_COURSES;
  22. if (!api_is_allowed_to_edit(false, true)) {
  23. Display :: display_header(get_lang('ToolSurvey'));
  24. Display :: display_error_message(get_lang('NotAllowed'), false);
  25. Display :: display_footer();
  26. exit;
  27. }
  28. // Database table definitions
  29. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  30. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  31. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  32. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  33. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  34. $course_id = api_get_course_int_id();
  35. // Getting the survey information
  36. $survey_id = Security::remove_XSS($_GET['survey_id']);
  37. $survey_data = survey_manager::get_survey($survey_id);
  38. if (empty($survey_data)) {
  39. Display :: display_header(get_lang('ToolSurvey'));
  40. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  41. Display :: display_footer();
  42. exit;
  43. }
  44. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  45. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  46. $urlname .= '...';
  47. }
  48. // Breadcrumbs
  49. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
  50. if (api_is_course_admin()) {
  51. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id, 'name' => $urlname);
  52. } else {
  53. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?survey_id='.$survey_id, 'name' => $urlname);
  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").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 WHERE c_id = $course_id AND code='".Database::escape_string($survey_data['code'])."'";
  68. $result = Database::query($sql);
  69. if (Database::num_rows($result) > 1) {
  70. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  71. }
  72. // Invited / answered message
  73. if ($survey_data['invited'] > 0 && !isset($_POST['submit'])) {
  74. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  75. $message .= get_lang('HaveAnswered').' ';
  76. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  77. $message .= get_lang('WereInvited');
  78. Display::display_normal_message($message, false);
  79. }
  80. // Building the form for publishing the survey
  81. $form = new FormValidator('publish_form', 'post', api_get_self().'?survey_id='.$survey_id.'&'.api_get_cidReq());
  82. $form->addElement('header', '', $tool_name);
  83. // Course users
  84. $complete_user_list = CourseManager::get_user_list_from_course_code(
  85. api_get_course_id(),
  86. api_get_session_id(),
  87. '',
  88. api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname'
  89. );
  90. $possible_users = array();
  91. foreach ($complete_user_list as & $user) {
  92. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  93. }
  94. $users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $possible_users, 'style="width: 250px; height: 200px;"');
  95. $users->setElementTemplate('
  96. {javascript}
  97. <table{class}>
  98. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  99. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  100. <tr>
  101. <td valign="top">{unselected}</td>
  102. <td align="center">{add}<br /><br />{remove}</td>
  103. <td valign="top">{selected}</td>
  104. </tr>
  105. </table>
  106. ');
  107. $users->setButtonAttributes('add', array('class' => 'btn arrowr'));
  108. $users->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  109. // Additional users
  110. $form->addElement('textarea', 'additional_users', array(get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')), array('class' => 'span6', 'rows' => 5));
  111. $form->addElement('html', '<div id="check_mail">');
  112. $form->addElement('checkbox', 'send_mail','', get_lang('SendMail'));
  113. $form->addElement('html', '</div>');
  114. $form->addElement('html', '<div id="mail_text">');
  115. // The title of the mail
  116. $form->addElement('text', 'mail_title', get_lang('MailTitle'), array('class' => 'span6'));
  117. // The text of the mail
  118. $form->addElement('html_editor', 'mail_text', array(get_lang('MailText'), get_lang('UseLinkSyntax')), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '150'));
  119. $form->addElement('html', '</div>');
  120. // You cab send a reminder to unanswered people if the survey is not anonymous
  121. if ($survey_data['anonymous'] != 1) {
  122. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  123. }
  124. // Allow resending to all selected users
  125. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  126. // Submit button
  127. $form->addElement('style_submit_button', 'submit', get_lang('PublishSurvey'), 'class="save"');
  128. // The rules (required fields)
  129. /*if ($survey_data['send_mail'] == 0) {
  130. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  131. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  132. }*/
  133. $portal_url = api_get_path(WEB_PATH);
  134. if (api_is_multiple_url_enabled()) {
  135. $access_url_id = api_get_current_access_url_id();
  136. if ($access_url_id != -1) {
  137. $url = api_get_access_url($access_url_id);
  138. $portal_url = $url['url'];
  139. }
  140. }
  141. // Show the URL that can be used by users to fill a survey without invitation
  142. $auto_survey_link = $portal_url.'main/survey/fillsurvey.php?course='.$_course['sysCode'].'&invitationcode=auto&scode='.$survey_data['survey_code'];
  143. $form->addElement('label', null, get_lang('AutoInviteLink'));
  144. $form->addElement('label', null, $auto_survey_link);
  145. if ($form->validate()) {
  146. $values = $form->exportValues();
  147. if ($values['send_mail'] == 1) {
  148. if (empty($values['mail_title']) || empty($values['mail_text'])) {
  149. Display :: display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  150. // Getting the invited users
  151. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  152. // Getting the survey mail text
  153. if (!empty($survey_data['reminder_mail'])) {
  154. $defaults['mail_text'] = $survey_data['reminder_mail'];
  155. } else {
  156. $defaults['mail_text'] = $survey_data['invite_mail'];
  157. }
  158. $defaults['mail_title'] = $survey_data['mail_subject'];
  159. $defaults['send_mail'] = 1;
  160. $form->setDefaults($defaults);
  161. $form->display();
  162. return;
  163. }
  164. }
  165. // Save the invitation mail
  166. SurveyUtil::save_invite_mail($values['mail_text'], $values['mail_title'], !empty($survey_data['invite_mail']));
  167. // Saving the invitations for the course users
  168. $count_course_users = SurveyUtil::save_invitations($values['course_users'], $values['mail_title'],
  169. $values['mail_text'], $values['resend_to_all'], $values['send_mail'], $values['remindUnAnswered']);
  170. // Saving the invitations for the additional users
  171. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  172. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  173. $additional_users = explode(';', $temp);
  174. for ($i = 0; $i < count($additional_users); $i++) {
  175. $additional_users[$i] = trim($additional_users[$i]);
  176. }
  177. $counter_additional_users = SurveyUtil::save_invitations(
  178. $additional_users,
  179. $values['mail_title'],
  180. $values['mail_text'],
  181. $values['resend_to_all'],
  182. $values['send_mail'],
  183. $values['remindUnAnswered']
  184. );
  185. // Updating the invited field in the survey table
  186. SurveyUtil::update_count_invited($survey_data['code']);
  187. $total_count = $count_course_users + $counter_additional_users;
  188. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  189. // Counting the number of people that are invited
  190. $sql = "SELECT * FROM $table_survey
  191. WHERE c_id = $course_id AND code = '".Database::escape_string($survey_data['code'])."'";
  192. $result = Database::query($sql);
  193. $row = Database::fetch_array($result);
  194. $total_invited = $row['invited'];
  195. if ($total_invited > 0) {
  196. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  197. $message .= get_lang('HaveAnswered').' ';
  198. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$total_invited.'</a> ';
  199. $message .= get_lang('WereInvited');
  200. Display::display_normal_message($message, false);
  201. Display::display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  202. }
  203. } else {
  204. // Getting the invited users
  205. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  206. // Getting the survey mail text
  207. if (!empty($survey_data['reminder_mail'])) {
  208. $defaults['mail_text'] = $survey_data['reminder_mail'];
  209. } else {
  210. $defaults['mail_text'] = $survey_data['invite_mail'];
  211. }
  212. $defaults['mail_title'] = $survey_data['mail_subject'];
  213. $defaults['send_mail'] = 1;
  214. $form->setDefaults($defaults);
  215. $form->display();
  216. }
  217. Display :: display_footer();