cis_service_activation_codes.module

  1. cis7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  2. cle7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  3. ecd7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  4. elmsmedia7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  5. harmony7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  6. icor7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  7. meedjum_blog7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
  8. mooc7 sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module

Activation code integration with CIS.

Functions

Namesort descending Description
cis_service_activation_codes_form Activation code entry block / form.
cis_service_activation_codes_form_submit Activation code entry form submission.
cis_service_activation_codes_form_validate Acitvation Code entry form validation.
cis_service_activation_codes_init Implements hook_init().
cis_service_activation_codes_menu Implements hook_menu().
cis_service_activation_codes_page_build Implements hook_page_build().
cis_service_activation_codes_user_login Implements hook_user_login().
_cis_service_activation_codes_verify Helper to determine if someone requires a code
_cis_service_activation_code_remaining_time Return the time remaining for a given user before locking access.

Constants

File

sites/all/modules/elmsln_contrib/cis_connector/modules/cis_service_activation_codes/cis_service_activation_codes.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Activation code integration with CIS.
  5. */
  6. define('CIS_SERVICE_ACTIVATION_CODES_WINDOW', 1209600);
  7. define('ACTIVATION_CODE_ACCESS_DENY', 0);
  8. define('ACTIVATION_CODE_ACCESS_GRANT', 1);
  9. /**
  10. * Implements hook_init().
  11. */
  12. function cis_service_activation_codes_init() {
  13. // if we aren't verified, check the count down as to whether to block access
  14. if (!in_array(arg(0), array('activation-blocked', 'syllabus')) && !_cis_service_activation_codes_verify()) {
  15. // verify time remaining is positive, if 0 or negative, block access
  16. if (_cis_service_activation_code_remaining_time() < 1) {
  17. drupal_goto('activation-blocked');
  18. }
  19. }
  20. }
  21. /**
  22. * Implements hook_menu().
  23. */
  24. function cis_service_activation_codes_menu() {
  25. $items = array();
  26. $path = drupal_get_path('module', 'cis_service_activation_codes');
  27. // admin settings for activation codes
  28. $items['admin/config/system/cis-service-activation-codes'] = array(
  29. 'title' => 'CIS service activation codes',
  30. 'description' => 'Options for implementation of activation codes in this site',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('cis_service_activation_codes_admin_settings'),
  33. 'access arguments' => array('administer site configuration'),
  34. 'type' => MENU_NORMAL_ITEM,
  35. 'file' => 'cis_service_activation_codes.admin.inc',
  36. 'file path' => $path,
  37. );
  38. $items['activation-blocked'] = array(
  39. 'title' => 'Activation code required',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('cis_service_activation_codes_form'),
  42. 'access arguments' => array('access content'),
  43. 'type' => MENU_NORMAL_ITEM,
  44. );
  45. return $items;
  46. }
  47. /**
  48. * Implements hook_page_build().
  49. */
  50. function cis_service_activation_codes_page_build(&$page) {
  51. // don't do this on admin pages
  52. if (arg(0) != 'admin' && arg(0) != 'activation-blocked') {
  53. // if they aren't verified, present activation code form
  54. if (!_cis_service_activation_codes_verify()) {
  55. drupal_add_css(drupal_get_path('module', 'cis_service_activation_codes') . '/cis_service_activation_codes.css');
  56. drupal_add_js(drupal_get_path('module', 'cis_service_activation_codes') . '/cis_service_activation_codes.js');
  57. $page['content']['cis_service_activation_codes'] = array(
  58. '#markup' => drupal_render(drupal_get_form('cis_service_activation_codes_form')),
  59. '#weight' => 100,
  60. );
  61. }
  62. }
  63. }
  64. /**
  65. * Return the time remaining for a given user before locking access.
  66. */
  67. function _cis_service_activation_code_remaining_time($account = NULL) {
  68. if (empty($account)) {
  69. $account = $GLOBALS['user'];
  70. }
  71. // look up dates we care about
  72. $dates = _cis_connector_transaction('section_dates');
  73. // set the current access time to now
  74. $access = REQUEST_TIME;
  75. // pull the access window allowed
  76. $window = variable_get('cis_service_activation_codes_window', CIS_SERVICE_ACTIVATION_CODES_WINDOW);
  77. // class has started, begin timer relative to first login
  78. // or start of class, which ever is last
  79. if ($account->data['mac_start'] < $dates['class_begin']) {
  80. $count_down = ($dates['class_begin'] + $window) - $access;
  81. }
  82. else {
  83. $count_down = ($account->data['mac_start'] + $window) - $access;
  84. }
  85. return $count_down;
  86. }
  87. /**
  88. * Activation code entry block / form.
  89. */
  90. function cis_service_activation_codes_form() {
  91. // convert timestamp into days remaining
  92. $window = ceil(_cis_service_activation_code_remaining_time() / 60 / 60 / 24);
  93. // format window day(s)
  94. $win_text = format_plural($window, '1 day', '@count days');
  95. // check if they've run out of time
  96. if ($window < 1) {
  97. $title = t('Access denied');
  98. $text = t('An activation code is required to gain access to these materials. You will not be able to access materials until you enter a valid activation code.');
  99. }
  100. else {
  101. $title = t('@win remaining', array('@win' => $win_text));
  102. $text = t('An activation code is required to maintain access to these materials. You have @window left until you will be required to enter a code. At this time, you will not be able to access materials until you enter a valid activation code.', array('@window' => $win_text));
  103. }
  104. // apend the output to the form so it looks nice
  105. $form['cis_service_activation_code_prefix'] = array(
  106. '#prefix' => '<div id="activation_code_form">
  107. <div class="mac_top"></div>
  108. <div class="mac_courselock">
  109. <div class="mac_courselocked"></div>
  110. </div>
  111. <div class="mac_main"><h3>' . $title . '</h3>
  112. <div class="content">',
  113. '#markup' => $text,
  114. '#suffix' => '</div>',
  115. );
  116. // add the entry field
  117. $form['cis_service_activation_code_entry'] = array(
  118. '#type' => 'textfield',
  119. '#size' => '30',
  120. '#default_value' => '',
  121. '#required' => TRUE,
  122. );
  123. // add submit button to send form data
  124. $form['submit'] = array(
  125. '#type' => 'submit',
  126. '#value' => t('Activate'),
  127. '#suffix' => '</div><div class="mac_bottom"></div></div>',
  128. );
  129. return $form;
  130. }
  131. /**
  132. * Acitvation Code entry form validation.
  133. */
  134. function cis_service_activation_codes_form_validate($form, &$form_state) {
  135. // verify an entry was made
  136. if ($form_state['values']['cis_service_activation_code_entry'] == '') {
  137. form_set_error('cis_service_activation_code_entry', t('Please enter an activation code'));
  138. }
  139. else {
  140. // attempt to validate code after filtering input
  141. $code = check_plain($form_state['values']['cis_service_activation_code_entry']);
  142. $code = filter_xss($code);
  143. // make request
  144. $response = _cis_connector_transaction('activation_code', 'default', array('code' => $code));
  145. if ($response == ACTIVATION_CODE_ACCESS_DENY) {
  146. form_set_error('cis_service_activation_code_entry', t('Please enter a valid activation code'));
  147. drupal_set_message(t('Please enter a valid activation code'), 'error');
  148. }
  149. }
  150. }
  151. /**
  152. * Activation code entry form submission.
  153. */
  154. function cis_service_activation_codes_form_submit($form, &$form_state) {
  155. // access checks passed, give the user the role
  156. user_multiple_role_edit(array($GLOBALS['user']->uid), 'add_role', variable_get('cis_service_activation_codes_role_grant', 2));
  157. drupal_set_message(t('Activation code accepted!'), 'status');
  158. }
  159. /**
  160. * Implements hook_user_login().
  161. */
  162. function cis_service_activation_codes_user_login(&$edit, $account) {
  163. // first time access save when they first logged in to compare
  164. if (isset($account->uid) && !isset($account->data['mac_start'])) {
  165. $new_data = $account->data;
  166. $new_data['mac_start'] = REQUEST_TIME;
  167. $user = user_save($account, array('data' => $new_data));
  168. }
  169. }
  170. /**
  171. * Helper to determine if someone requires a code
  172. *
  173. * TRUE implies that the user has verified access
  174. * FALSE implies the user requires an activation code still
  175. */
  176. function _cis_service_activation_codes_verify($account = NULL) {
  177. // allow for verification of other accounts
  178. if (is_null($account)) {
  179. $account = $GLOBALS['user'];
  180. }
  181. // verify they have a role which requires a code
  182. $intersect = array_intersect_key(variable_get('cis_service_activation_codes_role_check', array(2)), $account->roles);
  183. // if there was no intersection then we don't care
  184. // this implies they don't need an activation code
  185. if (empty($intersect)) {
  186. return TRUE;
  187. }
  188. // they have a role we care about, now verify if they have the code role
  189. // if they have the code role, we return TRUE, meaning they have access
  190. if (array_key_exists(variable_get('cis_service_activation_codes_role_grant', 2), $account->roles)) {
  191. return TRUE;
  192. }
  193. return FALSE;
  194. }