survey_invite.php 9.9 KB

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