summaryrefslogtreecommitdiffstats
path: root/guideme.module
blob: 13443e1a446d7eb9ee70f3d2ff5f57ef205cac63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php

/**
 * @file
 * Defines module hooks and logic.
 */

/**
 * Implements hook_menu().
 */
function guideme_menu() {
  return array(
    'guideme/mark-done/%' => array(
      'page callback' => 'guideme_ajax_mark_done',
      'page arguments' => array(2),
      'access callback' => 'guideme_access',
      'access arguments' => array(2),
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Implements hook_hook_info().
 */
function guideme_hook_info() {
  return array(
    'guideme_path' => array('group' => 'guideme'),
    'guideme_path_alter' => array('group' => 'guideme'),
  );
}

/**
 * Implements hook_permission().
 */
function guideme_permission() {
  $guide_paths = guideme_get_guide_paths();
  $permission = array();
  if (!empty($guide_paths)) {
    foreach ($guide_paths as $key => $value) {
      $permission["use $key guided path"] = array(
        'title' => t("Use the %path guided path", array('%path' => $key)),
        'description' => t("Allow a user to follow the %path guided step by step instructional path.", array('%path' => $key)),
      );
    }
  }
  return $permission;
}

/**
 * Implements hook_init().
 */
function guideme_init() {
  global $user;

  $map = guideme_get_map();
  $url = drupal_is_front_page() ? '<front>' : current_path();
  list($guide_path_id, $step_url_key) = guidemap_fetch_appropriate_guide_path($map, $url);

  // Check with the path alias.
  if (empty($guide_path_id) && $url !== '<front>') {
    $url = request_path();
    list($guide_path_id, $step_url_key) = guidemap_fetch_appropriate_guide_path($map, $url);
  }

  if (!empty($guide_path_id)) {
    if (user_access("use $guide_path_id guided path") && !guideme_has_user_completed_guide_path($user->uid, $guide_path_id)) {
      guideme_initialize_guide_path($guide_path_id, $step_url_key);
    }
  }
}

/**
 * Access callback: check if user has access to guide path.
 *
 * @param string $guide_path_id
 *
 * @return bool
 */
function guideme_access($guide_path_id) {
  return user_access("use $guide_path_id guided path");
}

/**
 * Page callback: AJAX, mark guide path as done.
 *
 * @param string $guide_path_id
 */
function guideme_ajax_mark_done($guide_path_id) {
  global $user;
  if ($_POST['token'] === $_SESSION['guideme']['token']) {
    guideme_user_completed_guide_path($user->uid, $guide_path_id);
    return '';
  }
  else {
    drupal_access_denied();
  }
}

/**
 * Helper function to get the registered guide paths.
 * Uses both static and persistent caching.
 *
 * @return array
 */
function guideme_get_guide_paths() {
  $guide_paths = &drupal_static(__FUNCTION__);
  
  if (!isset($guide_paths)) {
    $cache = cache_get('guideme:guide_paths');
    if ($cache) {
      $guide_paths = $cache->data;
    }
    else {
      $guide_paths = module_invoke_all('guideme_path');
      drupal_alter('guideme_path', $guide_paths);
      $guide_paths = guideme_add_defaults($guide_paths);

      cache_set('guideme:guide_paths', $guide_paths, 'cache', CACHE_TEMPORARY);
    }
  }
  
  return $guide_paths;
}

/**
 * Helper function to get a single guide path.
 *
 * @param string $guide_path_id
 *
 * @return array|null
 */
function guideme_get_guide_path($guide_path_id) {
  $guide_paths = guideme_get_guide_paths();
  return !empty($guide_paths[$guide_path_id]) ? $guide_paths[$guide_path_id] : NULL;
}

/**
 * Helper function to get the registered paths map.
 * Uses both static and persistent caching.
 *
 * @return array
 */
function guideme_get_map() {
  $map = &drupal_static(__FUNCTION__);
  
  if (!isset($map)) {
    $cache = cache_get('guideme:map');
    if ($cache) {
      $map = $cache->data;
    }
    else {
      $guide_paths = guideme_get_guide_paths();
      $map = guideme_map_guide_paths($guide_paths);
      cache_set('guideme:map', $map, 'cache', CACHE_TEMPORARY);
    }
  }
  
  return $map;
}

/**
 * Extract URI mapping from the registered paths for quick retreival and
 * to easily initiate a guide path.
 *
 * @param array $guide_paths
 *
 * @return array
 */
function guideme_map_guide_paths($guide_paths) {
  $map = array();

  // First, sort by weight.
  uasort($guide_paths, 'drupal_sort_weight');

  // Extract the URIs.
  foreach ($guide_paths as $key => $info) {
    $map[$key] = array_keys($info['steps']);
  }

  return $map;
}

/**
 * Set default values on guide paths that left out optional parameters,
 * like weight.
 *
 * @param array $guide_paths
 *
 * @return array
 */
function guideme_add_defaults($guide_paths) {
  foreach ($guide_paths as $key => $info) {
    if (!isset($info['weight'])) {
      $guide_paths[$key]['weight'] = 0;
    }
  }
  return $guide_paths;
}

/**
 * Check if the current path is registered for a guided path.
 *
 * @param array $map
 * @param string $current_path
 *
 * @return string|null
 */
function guidemap_fetch_appropriate_guide_path($map, $current_path) {
  foreach ($map as $key => $paths) {
    if (in_array($current_path, $paths)) {
      return array($key, $current_path);
    } else {
      foreach ($paths as $path) {
        if (strpos($path, '/*')) {
          $regex = str_replace(array('/', '*'), array('\\/', '[a-zA-Z0-9\-_]+'), $path);
          if (preg_match("/$regex/", $current_path)) {
            return array($key, $path);
          }
        }
      }
    }
  }
}

/**
 * Check if user has completed the path.
 *
 * @param int $uid
 * @param string $guide_path
 *
 * @return bool
 */
function guideme_has_user_completed_guide_path($uid, $guide_path) {
  $result = db_select('guideme_user_completion', 'gc')
    ->fields('gc', array('uid'))
    ->condition('gc.uid', $uid)
    ->condition('gc.guide_path', $guide_path)
    ->execute();

  if (!empty($result)) {
    return !!$result->fetchField();
  }
  else {
    return FALSE;
  }
}

/**
 * Set user has completed the path.
 *
 * @param int $uid
 * @param string $guide_path
 */
function guideme_user_completed_guide_path($uid, $guide_path) {
  db_merge('guideme_user_completion')
    ->key(array('uid' => $uid, 'guide_path' => $guide_path))
    ->fields(array('uid' => $uid, 'guide_path' => $guide_path))
    ->execute();
}

/**
 * Initialize the guide path.
 *
 * @param string $guide_path_id
 * @param string $url
 */
function guideme_initialize_guide_path($guide_path_id, $url) {
  $guide_path = guideme_get_guide_path($guide_path_id);
  $page_steps = $guide_path['steps'][$url];
  $url_clean = str_replace(array('*'), '_', $url);
  $token = uniqid(TRUE);
  $_SESSION['guideme']['token'] = $token;

  guideme_add_assets();
  drupal_add_js(array('guideMe' => array(
    'id' => $guide_path_id,
    'cookie_id' => "guideMe:$guide_path_id:$url_clean",
    'steps' => $page_steps,
    'token' => $token,
  )), 'setting');
}

/**
 * Helper function to add all Guideme assets.
 */
function guideme_add_assets() {
  static $added = FALSE;
  if (!$added) {
    $path = drupal_get_path('module', 'guideme');
    drupal_add_js("$path/js/vendor/joyride/jquery.joyride-2.1.js");
    drupal_add_js("$path/js/guideme.js");
    drupal_add_css("$path/css/vendor/joyride/joyride-2.1.css");
    drupal_add_css("$path/css/guideme.css");
    $added = TRUE;
  }
}