register_course_widget.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * @return string
  22. */
  23. public static function post($key, $default = '')
  24. {
  25. return isset($_POST[$key]) ? $_POST[$key] : $default;
  26. }
  27. /**
  28. * Returns $_GET data for $key is it exists or $default otherwise.
  29. *
  30. * @param string $key
  31. * @param object $default
  32. * @return string
  33. */
  34. public static function get($key, $default = '')
  35. {
  36. return isset($_GET[$key]) ? $_GET[$key] : $default;
  37. }
  38. /**
  39. *
  40. * @return RegisterCourseWidget
  41. */
  42. public static function factory()
  43. {
  44. return new self();
  45. }
  46. function run()
  47. {
  48. return $this->action_subscribe_user();
  49. }
  50. /**
  51. * Handle the subscribe action.
  52. *
  53. * @return bool
  54. */
  55. function action_subscribe_user()
  56. {
  57. $action = self::get('action');
  58. if ($action != self::ACTION_SUBSCRIBE)
  59. {
  60. return false;
  61. }
  62. $course_code = self::post(self::PARAM_SUBSCRIBE);
  63. if (empty($course_code))
  64. {
  65. return false;
  66. }
  67. $registration_code = self::post(self::PARAM_PASSCODE);
  68. if ($this->subscribe_user($course_code, $registration_code))
  69. {
  70. echo Display::return_message(get_lang('EnrollToCourseSuccessful'), 'confirmation');
  71. return;
  72. }
  73. if (!empty($registration_code))
  74. {
  75. echo Display::return_message(get_lang('CourseRegistrationCodeIncorrect'), 'error');
  76. }
  77. $this->display_form($course_code);
  78. return true;
  79. }
  80. /**
  81. * Regiser a user to a course.
  82. * Returns true on success, false otherwise.
  83. *
  84. * @param string $course_code
  85. * @param string $registration_code
  86. * @param int $user_id
  87. * @return bool
  88. */
  89. function subscribe_user($course_code, $registration_code = '', $user_id = null)
  90. {
  91. $course = api_get_course_info($course_code);
  92. $course_regisration_code = $course['registration_code'];
  93. if (!empty($course_regisration_code) && $registration_code != $course_regisration_code)
  94. {
  95. return false;
  96. }
  97. if (empty($user_id))
  98. {
  99. global $_user;
  100. $user_id = $_user['user_id'];
  101. }
  102. return (bool) CourseManager::add_user_to_course($user_id, $course_code);
  103. }
  104. /**
  105. * Display the course registration form.
  106. * Asks for registration code/password.
  107. *
  108. * @param string $course_code
  109. */
  110. function display_form($course_code)
  111. {
  112. global $stok;
  113. $course = api_get_course_info($course_code);
  114. $self = $_SERVER['REQUEST_URI'];
  115. $course_code = $course['code'];
  116. $course_visual_code = $course['visual_code'];
  117. $course_title = $course['title'];
  118. $submit_registration_code_label = get_lang("SubmitRegistrationCode");
  119. $course_requires_password_label = get_lang('CourseRequiresPassword');
  120. $result = <<<EOT
  121. $course_requires_password_label<br/>
  122. $course_visual_code - $course_title
  123. <form action="$self" method="post">
  124. <input type="hidden" name="sec_token" value="$stok" />
  125. <input type="hidden" name="subscribe" value="$course_code" />
  126. <input type="text" name="course_registration_code" value="$registration_code" />
  127. <input type="Submit" name="submit_course_registration_code" value="OK" alt="$submit_registration_code_label" /></form>
  128. EOT;
  129. echo $result;
  130. }
  131. }