survey_invite.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. 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. $user_info = Database::get_main_table(TABLE_MAIN_SURVEY_REMINDER); // TODO: To be checked. TABLE_MAIN_SURVEY_REMINDER has not been defined.
  35. $course_id = api_get_course_int_id();
  36. // Getting the survey information
  37. $survey_id = Security::remove_XSS($_GET['survey_id']);
  38. $survey_data = survey_manager::get_survey($survey_id);
  39. if (empty($survey_data)) {
  40. Display :: display_header(get_lang('ToolSurvey'));
  41. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  42. Display :: display_footer();
  43. exit;
  44. }
  45. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  46. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  47. $urlname .= '...';
  48. }
  49. // Breadcrumbs
  50. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  51. if (api_is_course_admin()) {
  52. $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname);
  53. } else {
  54. $interbreadcrumb[] = array('url' => 'survey_invite.php?survey_id='.$survey_id, 'name' => $urlname);
  55. }
  56. $tool_name = get_lang('SurveyPublication');
  57. // Displaying the header
  58. Display::display_header($tool_name,'Survey');
  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 WHERE c_id = $course_id AND code='".Database::escape_string($survey_data['code'])."'";
  62. $result = Database::query($sql);
  63. if (Database::num_rows($result) > 1) {
  64. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  65. }
  66. // Invited / answered message
  67. if ($survey_data['invited'] > 0) {
  68. $message = '<a href="survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  69. $message .= get_lang('HaveAnswered').' ';
  70. $message .= '<a href="survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  71. $message .= get_lang('WereInvited');
  72. Display::display_normal_message($message, false);
  73. }
  74. // Building the form for publishing the survey
  75. $form = new FormValidator('publish_form', 'post', api_get_self().'?survey_id='.$survey_id);
  76. $form->addElement('header', '', $tool_name);
  77. // Course users
  78. $complete_user_list = CourseManager :: get_user_list_from_course_code($_course['id'], $_SESSION['id_session'], '', api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname');
  79. $possible_users = array();
  80. foreach ($complete_user_list as $index => & $user) {
  81. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  82. }
  83. $users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $possible_users, 'style="width: 250px; height: 200px;"');
  84. $users->setElementTemplate('
  85. {javascript}
  86. <table{class}>
  87. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  88. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  89. <tr>
  90. <td valign="top">{unselected}</td>
  91. <td align="center">{add}<br /><br />{remove}</td>
  92. <td valign="top">{selected}</td>
  93. </tr>
  94. </table>
  95. ');
  96. $users->setButtonAttributes('add', array('class' => 'btn arrowr'));
  97. $users->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  98. // Additional users
  99. $form->addElement('textarea', 'additional_users', array(get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')), array('class' => 'span6', 'rows' => 2));
  100. // The title of the mail
  101. $form->addElement('text', 'mail_title', get_lang('MailTitle'), array('class' => 'span6'));
  102. // The text of the mail
  103. $form->addElement('html_editor', 'mail_text', array(get_lang('MailText'), get_lang('UseLinkSyntax')), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '150'));
  104. $form->addElement('checkbox', 'send_mail', '', get_lang('SendMail'));
  105. // You cab send a reminder to unanswered people if the survey is not anonymous
  106. if ($survey_data['anonymous'] != 1) {
  107. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  108. }
  109. // Allow resending to all selected users
  110. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  111. // Submit button
  112. $form->addElement('style_submit_button', 'submit', get_lang('PublishSurvey'), 'class="save"');
  113. // The rules (required fields)
  114. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  115. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  116. $portal_url = api_get_path(WEB_PATH);
  117. if ($_configuration['multiple_access_urls']) {
  118. $access_url_id = api_get_current_access_url_id();
  119. if ($access_url_id != -1) {
  120. $url = api_get_access_url($access_url_id);
  121. $portal_url = $url['url'];
  122. }
  123. }
  124. // Show the URL that can be used by users to fill a survey without invitation
  125. $auto_survey_link = $portal_url.'main/survey/fillsurvey.php?course='.$_course['sysCode'].'&invitationcode=auto&scode='.$survey_data['survey_code'];
  126. $form->addElement('label', null, get_lang('AutoInviteLink'));
  127. $form->addElement('label', null, $auto_survey_link);
  128. if ($form->validate()) {
  129. $values = $form->exportValues();
  130. // Save the invitation mail
  131. SurveyUtil::save_invite_mail($values['mail_text'], $values['mail_title'], !empty($survey_data['invite_mail']));
  132. // Saving the invitations for the course users
  133. $count_course_users = SurveyUtil::save_invitations($values['course_users'], $values['mail_title'],
  134. $values['mail_text'], $values['resend_to_all'], $values['send_mail'], $values['remindUnAnswered']);
  135. // Saving the invitations for the additional users
  136. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  137. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  138. $additional_users = explode(';', $temp);
  139. for ($i = 0; $i < count($additional_users); $i++) {
  140. $additional_users[$i] = trim($additional_users[$i]);
  141. }
  142. $counter_additional_users = SurveyUtil::save_invitations($additional_users, $values['mail_title'],
  143. $values['mail_text'], $values['resend_to_all'], $values['send_mail'], $values['remindUnAnswered']);
  144. // Updating the invited field in the survey table
  145. SurveyUtil::update_count_invited($survey_data['code']);
  146. $total_count = $count_course_users + $counter_additional_users;
  147. Display :: display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  148. } else {
  149. // Getting the invited users
  150. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  151. // Getting the survey mail text
  152. if (!empty($survey_data['reminder_mail'])) {
  153. $defaults['mail_text'] = $survey_data['reminder_mail'];
  154. } else {
  155. $defaults['mail_text'] = $survey_data['invite_mail'];
  156. }
  157. $defaults['mail_title'] = $survey_data['mail_subject'];
  158. $defaults['send_mail'] = 1;
  159. $form->setDefaults($defaults);
  160. $form->display();
  161. }
  162. // Footer
  163. Display :: display_footer();