| 1 |
<?php
|
| 2 |
// $Id: uts.analysis.inc,v 1.21 2009/01/15 07:21:55 boombatower Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Provide data analysis pages for the Usability Testing Suite.
|
| 6 |
*
|
| 7 |
* Copyright 2008 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Menu callback: List the studies that can be analyzed.
|
| 12 |
*
|
| 13 |
* @return string HTML Output.
|
| 14 |
*/
|
| 15 |
function uts_analysis_list() {
|
| 16 |
$studies = array_merge(uts_studies_load(UTS_STUDY_STATUS_ACTIVE), uts_studies_load(UTS_STUDY_STATUS_CLOSED));
|
| 17 |
$list = array();
|
| 18 |
foreach ($studies as $study) {
|
| 19 |
if (count(uts_session_load_all($study->nid, TRUE)) > 0) {
|
| 20 |
$list[] = t('!title<br />@description',
|
| 21 |
array('!title' => l($study->title, 'admin/uts/analyze/' . $study->nid), '@description' => t($study->body)));
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
if (empty($list)) {
|
| 26 |
return t('No studies to analyze.');
|
| 27 |
}
|
| 28 |
return t('<p>Select a study to analyze.</p>!studies', array('!studies' => theme('item_list', $list)));
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Menu callback: Display table of sessions related to study.
|
| 33 |
*
|
| 34 |
* @param interger $study_nid Current study NID.
|
| 35 |
* @return string HTML Output.
|
| 36 |
*/
|
| 37 |
function uts_analysis_list_sessions($study_nid) {
|
| 38 |
uts_analysis_set_title($study_nid);
|
| 39 |
|
| 40 |
drupal_add_js(drupal_get_path('module', 'uts') . '/uts.js');
|
| 41 |
drupal_add_css(drupal_get_path('module', 'uts') . '/uts.css', 'module');
|
| 42 |
jquery_ui_add('ui.accordion');
|
| 43 |
|
| 44 |
$js = array(
|
| 45 |
'images' => array(
|
| 46 |
theme('image', 'misc/menu-collapsed.png', 'Expand', 'Expand'),
|
| 47 |
theme('image', 'misc/menu-expanded.png', 'Collapsed', 'Collapsed'),
|
| 48 |
),
|
| 49 |
);
|
| 50 |
drupal_add_js(array('uts' => $js), 'setting');
|
| 51 |
|
| 52 |
module_load_include('inc', 'uts', 'uts.pages');
|
| 53 |
|
| 54 |
$study = node_load($study_nid);
|
| 55 |
$progress = uts_dashboard_studies_progress($study);
|
| 56 |
|
| 57 |
$tasks = uts_tasks_load($study_nid);
|
| 58 |
$header = array(t('Name'), t('Participants'), t('Completion rate'));
|
| 59 |
$rows = array();
|
| 60 |
foreach ($tasks as $task) {
|
| 61 |
$row = array();
|
| 62 |
$row[] = '<div id="uts-task-open-' . $task->nid . '" class="uts-task-open"><span></span>' . $task->title . '</div>';
|
| 63 |
$row[] = $progress[$task->nid]['participated'];
|
| 64 |
$row[] = theme('uts_progress', $progress[$task->nid]['completed'], $progress[$task->nid]['participated']);
|
| 65 |
|
| 66 |
$rows[] = array(
|
| 67 |
'data' => $row,
|
| 68 |
'class' => 'uts-task-head'
|
| 69 |
);
|
| 70 |
|
| 71 |
$row = array();
|
| 72 |
$row[] = array(
|
| 73 |
'data' => '<div id="uts-task-' . $task->nid . '" style="display: none;">' . uts_analysis_list_sessions_details($task) . '</div>',
|
| 74 |
'colspan' => 3
|
| 75 |
);
|
| 76 |
|
| 77 |
$rows[] = array(
|
| 78 |
'data' => $row,
|
| 79 |
'class' => 'uts-task-details',
|
| 80 |
'style' => 'display: none;'
|
| 81 |
);
|
| 82 |
}
|
| 83 |
return '<div class="dashboard-main">' . theme('table', $header, $rows) . uts_analysis_list_sessions_links($study_nid) . '</div>' .
|
| 84 |
'<div class="dashboard-sidebar">' . uts_analysis_list_sessions_status($study, $tasks, $progress) . '</div>';
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Create a summary of participant data for a particular task.
|
| 89 |
*
|
| 90 |
* @param object $task Task object.
|
| 91 |
* @return string HTML output.
|
| 92 |
*/
|
| 93 |
function uts_analysis_list_sessions_details($task) {
|
| 94 |
$out = '';
|
| 95 |
|
| 96 |
// Lime spent chart.
|
| 97 |
$chart = array(
|
| 98 |
'#chart_id' => 'time_spent_' . $task->nid,
|
| 99 |
'#title' => t('Time spent (seconds)'),
|
| 100 |
'#type' => CHART_TYPE_BAR_V_GROUPED,
|
| 101 |
'#size' => chart_size(250, 200),
|
| 102 |
'#grid_lines' => chart_grid_lines(15, 20),
|
| 103 |
'#bar_size' => chart_bar_size(20, 5),
|
| 104 |
'#data' => array(),
|
| 105 |
'#data_colors' => array()
|
| 106 |
);
|
| 107 |
|
| 108 |
$sessions = uts_session_load_all($task->study_nid);
|
| 109 |
$max = 0;
|
| 110 |
foreach ($sessions as $session_id) {
|
| 111 |
$session = uts_session_load($session_id);
|
| 112 |
list($start, $stop) = uts_data_get_timestamps($session->session_id, $task->nid, NULL, FALSE);
|
| 113 |
$time = $stop - $start;
|
| 114 |
$max = max($max, $time);
|
| 115 |
|
| 116 |
$chart['#data'][] = array($time);
|
| 117 |
$chart['#data_colors'][] = chart_unique_color($session->session_id, 'uts');
|
| 118 |
}
|
| 119 |
|
| 120 |
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][][] = chart_mixed_axis_range_label(0, $max);
|
| 121 |
|
| 122 |
$out .= chart_render($chart);
|
| 123 |
$out .= '<br />';
|
| 124 |
|
| 125 |
// Live comments.
|
| 126 |
$out .= '<b>' . t('Live feedback') . '</b>';
|
| 127 |
|
| 128 |
$out .= '<div id="uts-live-feedback">';
|
| 129 |
$out .= '<div id="uts-live-feedback-participants">';
|
| 130 |
$out .= '<div id="uts-live-feedback-label">' . t('Participants') . ':</div> ';
|
| 131 |
|
| 132 |
$data = array();
|
| 133 |
$i = 1;
|
| 134 |
foreach ($sessions as $session_id) {
|
| 135 |
$out .= '<div class="uts-live-feedback-participant" style="background-color: #' . chart_unique_color($session_id, 'uts') .
|
| 136 |
'">' . $i . '</div>';
|
| 137 |
|
| 138 |
list($start, $stop) = uts_data_get_timestamps($session_id, $task->nid);
|
| 139 |
if ($start) {
|
| 140 |
$data[$session_id] = uts_data_get($task->study_nid, $session_id, $start, $stop);
|
| 141 |
$data[$session_id] = $data[$session_id]['uts_live_feedback'];
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
$data[$session_id] = array(); // Add blank entry so that placeholder text is added.
|
| 145 |
}
|
| 146 |
$i++;
|
| 147 |
}
|
| 148 |
$out .= '</div>';
|
| 149 |
|
| 150 |
$i = 1;
|
| 151 |
foreach ($data as $session_id => $comments) {
|
| 152 |
$out .= '<div id="uts-comments-' . $i . '" class="uts-live-feedback-comments">';
|
| 153 |
$list = array();
|
| 154 |
foreach ($comments as $comment) {
|
| 155 |
$teaser = node_teaser($comment['message'], NULL, 50);
|
| 156 |
$teaser = (drupal_strlen($comment['message']) > drupal_strlen($teaser) ? $teaser . '...' : $teaser);
|
| 157 |
$list[] = l($teaser, '#') . '<div>' . $comment['message'] . '</div>';
|
| 158 |
}
|
| 159 |
|
| 160 |
$out .= ($list ? theme('item_list', $list, NULL, 'ul', array('class' => 'ui-accordion-container')) :
|
| 161 |
'<em>' . t('No comments to display.') . '</em>');
|
| 162 |
$out .= '</div>';
|
| 163 |
$i++;
|
| 164 |
}
|
| 165 |
|
| 166 |
$out .= '</div>';
|
| 167 |
|
| 168 |
return $out;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Create analyze links for each participant.
|
| 173 |
*
|
| 174 |
* @param interger $study_nid Current study NID.
|
| 175 |
* @return string HTML output.
|
| 176 |
*/
|
| 177 |
function uts_analysis_list_sessions_links($study_nid) {
|
| 178 |
$sessions = uts_session_load_all($study_nid);
|
| 179 |
|
| 180 |
$out = '<div id="uts-live-feedback-label">' . t('Analyze participant') . ':</div> ';
|
| 181 |
|
| 182 |
$data = array();
|
| 183 |
$i = 1;
|
| 184 |
foreach ($sessions as $session_id) {
|
| 185 |
$out .= '<a href="/admin/uts/analyze/' . $study_nid . '/' . $session_id . '">' .
|
| 186 |
'<div class="uts-live-feedback-participant" style="background-color: #' . chart_unique_color($session_id, 'uts') . '">' . $i .
|
| 187 |
'</div></a>';
|
| 188 |
$i++;
|
| 189 |
}
|
| 190 |
return $out;
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Generate a table of study status information.
|
| 195 |
*
|
| 196 |
* @param object $study Study object.
|
| 197 |
* @param array $tasks List of tasks related to study.
|
| 198 |
* @param array $progress Progress information.
|
| 199 |
* @return string HTML output.
|
| 200 |
*/
|
| 201 |
function uts_analysis_list_sessions_status($study, $tasks, $progress) {
|
| 202 |
$out = '';
|
| 203 |
$header = array(t('Key'), t('Value'));
|
| 204 |
$rows = array();
|
| 205 |
|
| 206 |
$rows[] = array(t('Status'), uts_get_study_status($study->study_status));
|
| 207 |
$rows[] = array(t('Participants'), t('@count / @required',
|
| 208 |
array('@count' => $progress['overal']['participated'], '@required' => $study->participant_count)));
|
| 209 |
$rate = round(($progress['overal']['completed'] / (count($tasks) * $study->participant_count)) * 100);
|
| 210 |
$rows[] = array(t('Completion rate'), $rate . '%');
|
| 211 |
|
| 212 |
$out .= theme('table', $header, $rows);
|
| 213 |
|
| 214 |
return $out;
|
| 215 |
}
|
| 216 |
|
| 217 |
/**
|
| 218 |
* Menu callback: Display details about session.
|
| 219 |
*
|
| 220 |
* @param interger $study_nid Current study NID.
|
| 221 |
* @param string $session_id Current session ID.
|
| 222 |
* @return string HTML Output.
|
| 223 |
*/
|
| 224 |
function uts_analysis_session_details($study_nid, $sessiond_id) {
|
| 225 |
uts_analysis_set_title($study_nid, $sessiond_id);
|
| 226 |
|
| 227 |
return 'TODO: session details and destroy link.';
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Menu callback: Returns to sessions overview page.
|
| 232 |
*
|
| 233 |
* @param interger $study_nid Current study NID.
|
| 234 |
* @return string HTML Output.
|
| 235 |
*/
|
| 236 |
function uts_analysis_sessions_goto($study_nid) {
|
| 237 |
drupal_goto('admin/uts/analyze/' . $study_nid);
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Menu callback: Display particular plugin generated analysis.
|
| 242 |
*
|
| 243 |
* @param interger $study_nid Current study NID.
|
| 244 |
* @param string $plugin Current analysis plug-in.
|
| 245 |
* @param string $mode Current analysis mode.
|
| 246 |
* @param string $session_id Current session ID.
|
| 247 |
* @return string HTML Output.
|
| 248 |
*/
|
| 249 |
function uts_analysis($study_nid, $plugin, $mode = NULL, $session_id = NULL) {
|
| 250 |
uts_analysis_set_title($study_nid, $session_id);
|
| 251 |
|
| 252 |
if (!uts_analysis_check_parameters($study_nid, $plugin, $mode, $session_id)) {
|
| 253 |
return '';
|
| 254 |
}
|
| 255 |
|
| 256 |
// Parameters have been checked, display data or perform action.
|
| 257 |
// Get timestamp range.
|
| 258 |
if ($session_id) {
|
| 259 |
list($start, $stop) = uts_data_get_timestamps($session_id); // TODO Provide timestamp filter.
|
| 260 |
}
|
| 261 |
else {
|
| 262 |
list($start, $stop) = array(NULL, NULL);
|
| 263 |
}
|
| 264 |
|
| 265 |
// Display data analysis from plug-in.
|
| 266 |
$output = uts_analysis_display($plugin, $mode, $study_nid, $session_id, $start, $stop);
|
| 267 |
|
| 268 |
// Add action links.
|
| 269 |
$base = "admin/uts/analyze/$study_nid/$plugin/$mode";
|
| 270 |
if ($session_id) {
|
| 271 |
$base = "admin/uts/analyze/$study_nid/$session_id/$plugin/$mode";
|
| 272 |
}
|
| 273 |
foreach (array('export') as $action) {
|
| 274 |
$output .= l(t('Export'), $base . "/$action");
|
| 275 |
}
|
| 276 |
|
| 277 |
return $output;
|
| 278 |
}
|
| 279 |
|
| 280 |
/**
|
| 281 |
* Menu callback: Perform an action on analysis item.
|
| 282 |
*
|
| 283 |
* @param unknown_type $session_id
|
| 284 |
* @param interger $study_nid Current study NID.
|
| 285 |
* @param string $plugin Current analysis plug-in.
|
| 286 |
* @param string $mode Current analysis mode.
|
| 287 |
* @param string $action Action to perform.
|
| 288 |
* @param string $session_id Current session ID.
|
| 289 |
*/
|
| 290 |
function uts_analysis_action($study_nid, $plugin, $mode, $action, $session_id = NULL) {
|
| 291 |
if (!uts_analysis_check_parameters($study_nid, $plugin, $mode, $session_id)) {
|
| 292 |
return '';
|
| 293 |
}
|
| 294 |
if (!in_array($action, array('export'))) {
|
| 295 |
drupal_set_message(t('Invalid action.'), 'error');
|
| 296 |
return '';
|
| 297 |
}
|
| 298 |
|
| 299 |
// Perform action.
|
| 300 |
if ($action == 'export') {
|
| 301 |
uts_analysis_export($plugin, $mode, $study_nid, $session_id, $start, $stop);
|
| 302 |
exit();
|
| 303 |
}
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
* Check the parameters to make sure they are valid.
|
| 308 |
*
|
| 309 |
* @param interger $study_nid Current study NID.
|
| 310 |
* @param string $plugin Current analysis plug-in.
|
| 311 |
* @param string $mode Current analysis mode.
|
| 312 |
* @param string $session_id Current session ID.
|
| 313 |
* @return boolean Valid parameters.
|
| 314 |
*/
|
| 315 |
function uts_analysis_check_parameters(&$study_nid, &$plugin, &$mode, &$session_id) {
|
| 316 |
$studies = array_merge(uts_studies_load(UTS_STUDY_STATUS_ACTIVE), uts_studies_load(UTS_STUDY_STATUS_CLOSED));;
|
| 317 |
$sessions = uts_session_load_all($study_nid);
|
| 318 |
$analysis_plugins = uts_data_analysis_list();
|
| 319 |
|
| 320 |
$found = FALSE;
|
| 321 |
foreach ($studies as $study) {
|
| 322 |
if ($study->nid == $study_nid) {
|
| 323 |
$found = TRUE;
|
| 324 |
break;
|
| 325 |
}
|
| 326 |
}
|
| 327 |
if (!$found) {
|
| 328 |
drupal_set_message(t('Invalid study NID.'), 'error');
|
| 329 |
return FALSE;
|
| 330 |
}
|
| 331 |
|
| 332 |
if ($session_id && !in_array($session_id, $sessions)) {
|
| 333 |
drupal_set_message(t('Invalid session ID.'), 'error');
|
| 334 |
return FALSE;
|
| 335 |
}
|
| 336 |
|
| 337 |
if (!array_key_exists($plugin, $analysis_plugins)) {
|
| 338 |
drupal_set_message(t('Invalid plugin.'), 'error');
|
| 339 |
return FALSE;
|
| 340 |
}
|
| 341 |
|
| 342 |
if ($plugin && !$mode) {
|
| 343 |
// Get default mode.
|
| 344 |
$plugin_modes = $analysis_plugins[$plugin];
|
| 345 |
$mode = $plugin_modes[0];
|
| 346 |
}
|
| 347 |
else if ($plugin && $mode) {
|
| 348 |
$plugin_modes = $analysis_plugins[$plugin];
|
| 349 |
if (!in_array($mode, $plugin_modes)) {
|
| 350 |
drupal_set_message(t('Invalid plugin mode.'), 'error');
|
| 351 |
return FALSE;
|
| 352 |
}
|
| 353 |
}
|
| 354 |
return TRUE;
|
| 355 |
}
|
| 356 |
|
| 357 |
/**
|
| 358 |
* Set title to include study and session ID if set.
|
| 359 |
*
|
| 360 |
* @param interger $study_nid Current study NID.
|
| 361 |
* @param string $session_id Current session ID.
|
| 362 |
*/
|
| 363 |
function uts_analysis_set_title($study_nid, $session_id = NULL) {
|
| 364 |
$study = node_load($study_nid);
|
| 365 |
if ($session_id) {
|
| 366 |
drupal_set_title(t('Analyze %study: %session', array('%study' => $study->title, '%session' => $session_id)));
|
| 367 |
}
|
| 368 |
else {
|
| 369 |
drupal_set_title(t('Analyze %study', array('%study' => $study->title)));
|
| 370 |
}
|
| 371 |
}
|
| 372 |
|
| 373 |
/**
|
| 374 |
* Get the display elements from the analysis plug-ins, sort them by weight,
|
| 375 |
* and collect the output.
|
| 376 |
*
|
| 377 |
* @param string $module Data colleciton module from which data will be retreived.
|
| 378 |
* @param string $mode The analysis mode.
|
| 379 |
* @param interger $study_nid Study NID.
|
| 380 |
* @param string $session_id Session ID.
|
| 381 |
* @param interger $start_timestamp Start timestamp.
|
| 382 |
* @param integer $stop_timestamp Stop timestamp.
|
| 383 |
* @return string HTML output.
|
| 384 |
*/
|
| 385 |
function uts_analysis_display($plugin, $mode, $study_nid, $session_id, $start_timestamp, $stop_timestamp) {
|
| 386 |
$elements = uts_data_analysis_display($plugin, $mode, $study_nid, $session_id, $start_timestamp, $stop_timestamp);
|
| 387 |
uasort($elements, 'element_sort');
|
| 388 |
|
| 389 |
// Output all the elements which have been sorted by weight.
|
| 390 |
$output = '';
|
| 391 |
foreach ($elements as $element) {
|
| 392 |
$output .= $element['#data'];
|
| 393 |
}
|
| 394 |
return $output;
|
| 395 |
}
|
| 396 |
|
| 397 |
/**
|
| 398 |
* Collection export files from analysis plug-ins, create ZIP archive, and
|
| 399 |
* output to browser.
|
| 400 |
*
|
| 401 |
* @param string $module Data colleciton module from which data will be retreived.
|
| 402 |
* @param string $mode The analysis mode.
|
| 403 |
* @param interger $study_nid Study NID.
|
| 404 |
* @param string $session_id Session ID.
|
| 405 |
* @param interger $start_timestamp Start timestamp.
|
| 406 |
* @param integer $stop_timestamp Stop timestamp.
|
| 407 |
*/
|
| 408 |
function uts_analysis_export($plugin, $mode, $study_nid, $session_id, $start_timestamp, $stop_timestamp) {
|
| 409 |
require_once drupal_get_path('module', 'uts') . '/uts.zip.inc';
|
| 410 |
|
| 411 |
// Get data and generate ZIP file.
|
| 412 |
$exports = uts_data_analysis_export($plugin, $mode, $study_nid, $session_id, $start_timestamp, $stop_timestamp);
|
| 413 |
$zip = new ZipFile();
|
| 414 |
foreach ($exports as $file => $data) {
|
| 415 |
$zip->addFile($data, $file);
|
| 416 |
}
|
| 417 |
|
| 418 |
// Output file.
|
| 419 |
header("Content-Disposition: attachment; filename=\"$plugin-$mode.zip\"");
|
| 420 |
header("Content-Type: application/zip");
|
| 421 |
header("Content-Transfer-Encoding: binary");
|
| 422 |
|
| 423 |
echo $zip->file();
|
| 424 |
}
|
| 425 |
|
| 426 |
/**
|
| 427 |
* Get an array containing the session operation links.
|
| 428 |
*
|
| 429 |
* @param $session Session object.
|
| 430 |
* @return Array of links.
|
| 431 |
*/
|
| 432 |
function uts_session_operations($session) {
|
| 433 |
$links = array();
|
| 434 |
if ($session->complete) {
|
| 435 |
$links['analyze'] = array(
|
| 436 |
'title' => t('analyze'),
|
| 437 |
'href' => "admin/uts/analyze/$session->study_nid/$session->session_id"
|
| 438 |
);
|
| 439 |
}
|
| 440 |
$links['session_destroy'] = array(
|
| 441 |
'title' => t('destroy'),
|
| 442 |
'href' => "admin/uts/analyze/$session->study_nid/$session->session_id/destroy"
|
| 443 |
);
|
| 444 |
return $links;
|
| 445 |
}
|
| 446 |
|
| 447 |
/**
|
| 448 |
* Menu callback -- ask for confirmation of session destruction then redirect
|
| 449 |
* to appropriate overview page.
|
| 450 |
*/
|
| 451 |
function uts_analysis_destroy_confirm(&$form_state, $session_id) {
|
| 452 |
$redirect = 'admin/uts/analyze/' . arg(3);
|
| 453 |
|
| 454 |
// Inject form values.
|
| 455 |
$form['session_id'] = array(
|
| 456 |
'#type' => 'value',
|
| 457 |
'#value' => $session_id,
|
| 458 |
);
|
| 459 |
$form['redirect'] = array(
|
| 460 |
'#type' => 'value',
|
| 461 |
'#value' => $redirect,
|
| 462 |
);
|
| 463 |
|
| 464 |
return confirm_form($form,
|
| 465 |
t('Are you sure you want to delete %session?', array('%session' => $session_id)),
|
| 466 |
isset($_GET['destination']) ? $_GET['destination'] : $redirect,
|
| 467 |
t('This action cannot be undone.'),
|
| 468 |
t('Delete'),
|
| 469 |
t('Cancel')
|
| 470 |
);
|
| 471 |
}
|
| 472 |
|
| 473 |
/**
|
| 474 |
* Execute session destruction.
|
| 475 |
*/
|
| 476 |
function uts_analysis_destroy_confirm_submit($form, &$form_state) {
|
| 477 |
if ($form_state['values']['confirm']) {
|
| 478 |
uts_session_destory($form_state['values']['session_id']);
|
| 479 |
}
|
| 480 |
|
| 481 |
// Set the redirect to the pre-determined location.
|
| 482 |
$form_state['redirect'] = $form_state['values']['redirect'];
|
| 483 |
}
|