register_course_widget.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Register course widget.
  4. * Handles user's registration action.
  5. * Display a register to course form if required.
  6. *
  7. * @copyright (c) 2011 University of Geneva
  8. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  9. * @author Laurent Opprecht
  10. */
  11. class RegisterCourseWidget
  12. {
  13. const ACTION_SUBSCRIBE = 'subscribe';
  14. const PARAM_SUBSCRIBE = 'subscribe';
  15. const PARAM_PASSCODE = 'course_registration_code';
  16. /**
  17. * Returns $_POST data for $key is it exists or $default otherwise.
  18. *
  19. * @param string $key
  20. * @param object $default
  21. *
  22. * @return string
  23. */
  24. public static function post($key, $default = '')
  25. {
  26. return isset($_POST[$key]) ? $_POST[$key] : $default;
  27. }
  28. /**
  29. * Returns $_GET data for $key is it exists or $default otherwise.
  30. *
  31. * @param string $key
  32. * @param object $default
  33. *
  34. * @return string
  35. */
  36. public static function get($key, $default = '')
  37. {
  38. return isset($_GET[$key]) ? $_GET[$key] : $default;
  39. }
  40. /**
  41. * @return RegisterCourseWidget
  42. */
  43. public static function factory()
  44. {
  45. return new self();
  46. }
  47. public function run()
  48. {
  49. return $this->action_subscribe_user();
  50. }
  51. /**
  52. * Handle the subscribe action.
  53. *
  54. * @return bool
  55. */
  56. public function action_subscribe_user()
  57. {
  58. $action = self::get('action');
  59. if ($action != self::ACTION_SUBSCRIBE) {
  60. return false;
  61. }
  62. $course_code = self::post(self::PARAM_SUBSCRIBE);
  63. if (empty($course_code)) {
  64. return false;
  65. }
  66. $registration_code = self::post(self::PARAM_PASSCODE);
  67. if ($this->subscribe_user($course_code, $registration_code)) {
  68. echo Display::return_message(get_lang('EnrollToCourseSuccessful'), 'confirmation');
  69. return;
  70. }
  71. if (!empty($registration_code)) {
  72. echo Display::return_message(get_lang('CourseRegistrationCodeIncorrect'), 'error');
  73. }
  74. $this->display_form($course_code);
  75. return true;
  76. }
  77. /**
  78. * Regiser a user to a course.
  79. * Returns true on success, false otherwise.
  80. *
  81. * @param string $course_code
  82. * @param string $registration_code
  83. * @param int $user_id
  84. *
  85. * @return bool
  86. */
  87. public function subscribe_user($course_code, $registration_code = '', $user_id = null)
  88. {
  89. $course = api_get_course_info($course_code);
  90. $course_regisration_code = $course['registration_code'];
  91. if (!empty($course_regisration_code) && $registration_code != $course_regisration_code) {
  92. return false;
  93. }
  94. if (empty($user_id)) {
  95. global $_user;
  96. $user_id = $_user['user_id'];
  97. }
  98. return (bool) CourseManager::subscribeUser($user_id, $course_code);
  99. }
  100. /**
  101. * Display the course registration form.
  102. * Asks for registration code/password.
  103. *
  104. * @param string $course_code
  105. */
  106. public function display_form($course_code)
  107. {
  108. global $stok;
  109. $course = api_get_course_info($course_code);
  110. $self = $_SERVER['REQUEST_URI'];
  111. $course_code = $course['code'];
  112. $course_visual_code = $course['visual_code'];
  113. $course_title = $course['title'];
  114. $submit_registration_code_label = get_lang("SubmitRegistrationCode");
  115. $course_requires_password_label = get_lang('CourseRequiresPassword');
  116. $result = <<<EOT
  117. $course_requires_password_label<br/>
  118. $course_visual_code - $course_title
  119. <form action="$self" method="post">
  120. <input type="hidden" name="sec_token" value="$stok" />
  121. <input type="hidden" name="subscribe" value="$course_code" />
  122. <input type="text" name="course_registration_code" value="$registration_code" />
  123. <input type="Submit" name="submit_course_registration_code" value="OK" alt="$submit_registration_code_label" /></form>
  124. EOT;
  125. echo $result;
  126. }
  127. }