PageRenderTime 26ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/handle_signups_ajax.php

https://github.com/cwarren/sus
PHP | 305 lines | 262 code | 23 blank | 20 comment | 17 complexity | c6b5b2b2801635185c17200eef30c6f6 MD5 | raw file
  1. <?php
  2. // this script processes a signup addition or removal request. If the
  3. // process fails for some reason it responds with "FAILURE: reason". On
  4. // success it responds with "SUCCESS<data>" where <data> is the updated
  5. // HTML for the opening to which the signup was attached.
  6. require_once('../../config.php');
  7. require_once($CFG->libdir.'/moodlelib.php');
  8. /////////////////////////////////////////////////////////////////////////////////////////
  9. // locally defined functions
  10. // This function returns true. It is called by various included files
  11. // to make sure they're actually in the signup sheet world - i.e. to
  12. // prevent them from being (usefully) used directly from the web. E.g.
  13. // if (! verify_in_signup_sheets()) { die("not in signup_sheets"); }
  14. function verify_in_signup_sheets() {
  15. return true;
  16. }
  17. require_login();
  18. include_once 'sus_lib.php';
  19. $DEBUG=0;
  20. if ($DEBUG > 0)
  21. {
  22. echo 'FAILURE: ';
  23. }
  24. /////////////////////////////////////////////////////////////////////////////////////////
  25. // input validation
  26. log_debug_r(4,$_REQUEST);
  27. $contextid = clean_param($_REQUEST['contextid'],PARAM_INT); // determines what course
  28. $access_id = clean_param($_REQUEST['access'],PARAM_CLEAN);
  29. $sheet_id = clean_param($_REQUEST['sheet'],PARAM_CLEAN);
  30. $opening_id = clean_param($_REQUEST['opening'],PARAM_CLEAN);
  31. $action = clean_param($_REQUEST['action'],PARAM_CLEAN);
  32. $actionsource = clean_param($_REQUEST['actionsource'],PARAM_CLEAN);
  33. $username = clean_param($_REQUEST['username'],PARAM_CLEAN);
  34. $admincomment = clean_param($_REQUEST['admincomment'],PARAM_CLEAN);
  35. $signup_id = clean_param($_REQUEST['signup'],PARAM_CLEAN);
  36. log_debug(1,"
  37. contextid is $contextid
  38. access_id is $access_id
  39. sheet_id is $sheet_id
  40. opening_id is $opening_id
  41. action is $action
  42. actionsource is $actionsource
  43. username is $username
  44. signup_id is $signup_id
  45. ");
  46. /////////////////////////////////////////////////////////////////////////////////////////
  47. // processing
  48. $user_is_sheet_admin = userHasAdminAccess($USER->id,$sheet_id);
  49. if ($action == 'addsignup')
  50. {
  51. $user_has_signup_access = userHasSignupAccess($USER->id,$sheet_id,$access_id);
  52. if ((! $user_has_signup_access) && (! $user_is_sheet_admin) )
  53. {
  54. echo "FAILURE: you do not have signup access to this sheet and you are not a sheet admin";
  55. exit;
  56. }
  57. $for_user = $USER->id;
  58. if ($username)
  59. {
  60. if (! $user_is_sheet_admin)
  61. {
  62. echo "FAILURE: you are not a sheet admin and so cannot sign up other users";
  63. exit;
  64. }
  65. $for_user = $username;
  66. }
  67. $su = newSignup($opening_id,$for_user);
  68. if (! $su)
  69. {
  70. echo "FAILURE: could not create signup for user $for_user";
  71. exit;
  72. }
  73. // add signup
  74. $signup = newSignup($opening_id,$for_user);
  75. if ($admincomment)
  76. {
  77. $signup->admin_comment = $admincomment;
  78. }
  79. $ns_id = addSignup($signup);
  80. // if user is not admin
  81. // get info for that opening
  82. // check that max sign-ups is not exceeded
  83. // if it is
  84. // remove the just added signup and alert user
  85. if (! $user_is_sheet_admin)
  86. {
  87. $op = getOpenings($sheet_id,$opening_id);
  88. if ($op->o_num_signups > $op->o_max_signups)
  89. {
  90. removeSignup($ns_id);
  91. echo "FAILURE: opening is fully-booked";
  92. exit;
  93. }
  94. }
  95. $sheet_complex = getStructuredSheetData($sheet_id,0,$opening_id);
  96. $su_user_data = $sheet_complex->openings[0]->signups_by_user[$signup->signup_user_id]->user;
  97. log_debug_r(2,$su_user_data);
  98. $confirmation_message = "
  99. You are signed up for {$sheet_complex->s_name}.\n\n ".ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-');
  100. $admin_message = "{$su_user_data->usr_firstname} {$su_user_data->usr_lastname} has signed up for the ".
  101. ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-')." opening on ".$sheet_complex->s_name.".";
  102. if ($sheet_complex->openings[0]->o_name)
  103. {
  104. $confirmation_message .= "\n ".$sheet_complex->openings[0]->o_name;
  105. $admin_message .= "\n ".$sheet_complex->openings[0]->o_name;
  106. }
  107. if ($sheet_complex->openings[0]->o_description)
  108. {
  109. $confirmation_message .= "\n ".$sheet_complex->openings[0]->o_description;
  110. $admin_message .= "\n ".$sheet_complex->openings[0]->o_description;
  111. }
  112. if ($sheet_complex->openings[0]->o_location)
  113. {
  114. $confirmation_message .= "\n\nYou're meeting at ".$sheet_complex->openings[0]->o_location;
  115. $admin_message .= "\n\nThe meeting is at ".$sheet_complex->openings[0]->o_location;
  116. }
  117. $mail_error = mailAlerts($sheet_complex,$signup->signup_user_id,
  118. sus_block_name()." - {$sheet_complex->s_name} at ".ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-'),
  119. $confirmation_message,
  120. "Glow SUS- {$su_user_data->usr_firstname} {$su_user_data->usr_lastname} signed up for ".$sheet_complex->s_name,
  121. $admin_message);
  122. if (! $mail_error)
  123. {
  124. include_once 'cal_lib.php'; // for openingDisplay below
  125. $new_opening_html = openingDisplay($sheet_complex->openings[0],
  126. ($user_is_sheet_admin && ($actionsource != 'do_signup')),
  127. $sheet_complex->s_flag_private_signups);
  128. echo 'SUCCESS';
  129. if ($actionsource == 'sheet_edit_opening_form')
  130. {
  131. $sep = '___|||---';
  132. echo $ns_id . $sep;
  133. echo $sheet_complex->openings[0]->signups_by_id[$ns_id]->user->usr_firstname . $sep
  134. . $sheet_complex->openings[0]->signups_by_id[$ns_id]->user->usr_lastname . $sep;
  135. echo $admincomment . $sep . mktime();
  136. } else
  137. {
  138. echo $new_opening_html;
  139. }
  140. }
  141. exit;
  142. } else if ($action == 'removesignup')
  143. {
  144. if (! $signup_id)
  145. {
  146. echo 'FAILURE: no signup specified';
  147. exit;
  148. }
  149. $sheet_complex = getStructuredSheetData($sheet_id,0,$opening_id,$signup_id);
  150. $su = $sheet_complex->openings[0]->signups_by_id[$signup_id];
  151. $su_user_data = $su->user;
  152. // log_debug_r(-1,$su);
  153. // user either has to own the given signup (i.e. be the one signed up) or be an admin of the sheet for that signup
  154. $removal_success = 0;
  155. if ($user_is_sheet_admin)
  156. {
  157. $removal_success = removeSignup($signup_id);
  158. } else
  159. {
  160. if ($su->su_signup_user_id == $USER->id)
  161. {
  162. $removal_success = removeSignup($signup_id);
  163. }
  164. }
  165. if ($removal_success)
  166. {
  167. $confirmation_message = "\nSignup cancelled for {$sheet_complex->s_name}.\n\n ".ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-');
  168. $admin_message = "Cancelled {$su_user_data->usr_firstname} {$su_user_data->usr_lastname} signup for the ".
  169. ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-')." opening on ".$sheet_complex->s_name.".\n";
  170. if ($sheet_complex->openings[0]->o_name)
  171. {
  172. $confirmation_message .= "\n ".$sheet_complex->openings[0]->o_name;
  173. $admin_message .= "\n ".$sheet_complex->openings[0]->o_name;
  174. }
  175. if ($sheet_complex->openings[0]->o_description)
  176. {
  177. $confirmation_message .= "\n ".$sheet_complex->openings[0]->o_description;
  178. $admin_message .= "\n ".$sheet_complex->openings[0]->o_description;
  179. }
  180. $mail_error = mailAlerts($sheet_complex,$su->su_signup_user_id,
  181. sus_block_name()." - signup cancelled on {$sheet_complex->s_name} at ".ymd_hm_a($sheet_complex->openings[0]->o_begin_datetime,'-'),
  182. $confirmation_message,
  183. sus_block_name()." - cancelled {$su_user_data->usr_firstname} {$su_user_data->usr_lastname} signup for ".$sheet_complex->s_name,
  184. $admin_message);
  185. include_once 'cal_lib.php'; // for openingDisplay below
  186. $sheet_complex = getStructuredSheetData($sheet_id,0,$opening_id);
  187. log_debug_r(4,$sheet_complex);
  188. $new_opening_html = openingDisplay($sheet_complex->openings[0],($user_is_sheet_admin && ($actionsource != 'do_signup')),$sheet_complex->s_flag_private_signups);
  189. echo 'SUCCESS';
  190. echo $new_opening_html;
  191. exit;
  192. } else
  193. {
  194. echo 'FAILURE: could not remove';
  195. exit;
  196. }
  197. } else
  198. {
  199. echo 'FAILURE: unknown action $action';
  200. exit;
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////////////////////
  203. # takes:
  204. # a sheet complex
  205. # subject for signee alert
  206. # body for signee alert
  207. # subject for admin alert
  208. # body for admin alert
  209. # does: send alert email to the signee and admin as appropriate
  210. # returns: true on all success, false on 1 or more failures
  211. function mailAlerts($sheet_complex,$signup_user_id,$sign_subj,$sign_body,$admin_subj,$admin_body)
  212. {
  213. $mail_error = false;
  214. $su_user_data = $sheet_complex->openings[0]->signups_by_user[$signup_user_id]->user;
  215. // log_debug_r(-2,$su_user_data);
  216. if (! simpleEmail($su_user_data->usr_email, $sign_subj, $sign_body)) {
  217. echo 'FAILURE: signup handled, but no confirmation email sent';
  218. $mail_error = true;
  219. }
  220. if ($sheet_complex->s_flag_alert_owner_signup || $sheet_complex->s_flag_alert_admin_signup)
  221. {
  222. $users_to_alert = getSheetOwnerAndAdmins($sheet_complex->s_id);
  223. $owner = $users_to_alert[0];
  224. // trim off owner xor admins as necessary
  225. if (! $sheet_complex->s_flag_alert_owner_signup)
  226. {
  227. array_shift($users_to_alert);
  228. } else if (! $sheet_complex->s_flag_alert_admin_signup)
  229. {
  230. $users_to_alert = array(array_shift($users_to_alert));
  231. }
  232. // CSW 200/02/24 : modify owner info to make mail message clearer
  233. $owner->firstname = sus_block_name();
  234. $owner->lastname = 'Admin';
  235. // NOTE: ideally the message would be from a noreply address,
  236. // any change to the owner email address causes a silent and
  237. // inexplicable mail deliver failure
  238. $mailresult = true;
  239. foreach ($users_to_alert as $u)
  240. {
  241. $mailresult = $mailresult && email_to_user($u, $owner, $admin_subj, $admin_body);
  242. }
  243. if (! $mailresult)
  244. {
  245. echo 'FAILURE: signup handled, but admin alerts failed';
  246. $mail_error = true;
  247. }
  248. }
  249. return $mail_error;
  250. }
  251. ?>