| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Provide particpation batch definitions.
|
| 6 |
*
|
| 7 |
* Copyright 2008 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Batch to create an environment.
|
| 12 |
*
|
| 13 |
* @param integer $study_nid Study NID.
|
| 14 |
*/
|
| 15 |
function uts_participate_environment_batch($study_nid) {
|
| 16 |
// Validate, create, e-mail, enable modules load session.
|
| 17 |
$batch = array(
|
| 18 |
'operations' => array(
|
| 19 |
array('uts_participate_environment_batch_session', array($study_nid)),
|
| 20 |
array('uts_participate_environment_batch_notify', array($study_nid)),
|
| 21 |
array('uts_participate_environment_batch_close', array($study_nid)),
|
| 22 |
),
|
| 23 |
'finished' => 'uts_participate_environment_batch_finished',
|
| 24 |
'title' => t('Create a session and an environment'),
|
| 25 |
'init_message' => t('Creating environment...'),
|
| 26 |
'error_message' => t('An error occured during environment setup.'),
|
| 27 |
);
|
| 28 |
|
| 29 |
batch_set($batch);
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Create a session and an environment.
|
| 34 |
*
|
| 35 |
* @param integer $study_nid Study NID.
|
| 36 |
*/
|
| 37 |
function uts_participate_environment_batch_session($study_nid, &$context) {
|
| 38 |
$session_id = uts_session_create($study_nid);
|
| 39 |
|
| 40 |
$context['message'] = t('Environment created.');
|
| 41 |
$context['results']['session_id'] = $session_id;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Send e-mail containing session information.
|
| 46 |
*
|
| 47 |
* @param integer $study_nid Study NID.
|
| 48 |
*/
|
| 49 |
function uts_participate_environment_batch_notify($study_nid, &$context) {
|
| 50 |
$session_id = $context['results']['session_id'];
|
| 51 |
|
| 52 |
if ($session_id) {
|
| 53 |
global $user;
|
| 54 |
$params = array();
|
| 55 |
$params['account'] = $user;
|
| 56 |
$params['study_nid'] = $study_nid;
|
| 57 |
$params['session_id'] = $session_id;
|
| 58 |
drupal_mail('uts', 'session_info', $user->mail, user_preferred_language($user), $params);
|
| 59 |
|
| 60 |
$context['message'] = t('E-mail sent reguarding environment information.');
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Check the number of existing sessions against the maximum participant count.
|
| 66 |
* If maximum count reached then close study.
|
| 67 |
*
|
| 68 |
* @param integer $study_nid Study NID.
|
| 69 |
*/
|
| 70 |
function uts_participate_environment_batch_close($study_nid, &$context) {
|
| 71 |
$study = node_load($study_nid);
|
| 72 |
$sessions = uts_session_load_all($study->nid);
|
| 73 |
if (count($sessions) >= $study->participant_count) {
|
| 74 |
$study->study_status = UTS_STUDY_STATUS_CLOSED;
|
| 75 |
$study->revision = FALSE;
|
| 76 |
$study = node_submit($study);
|
| 77 |
node_save($study);
|
| 78 |
$context['message'] = t('Maximum number of sessions reached, study closed.');
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Batch 'finished' callback.
|
| 84 |
*/
|
| 85 |
function uts_participate_environment_batch_finished($success, $results, $operations) {
|
| 86 |
if ($success) {
|
| 87 |
if ($results['session_id']) {
|
| 88 |
drupal_set_message(t('Environment created successfully.'));
|
| 89 |
drupal_goto('uts/session/' . $results['session_id']);
|
| 90 |
}
|
| 91 |
else {
|
| 92 |
drupal_set_message(t('Failed to setup environment. Please contact the administrator.'), 'error');
|
| 93 |
}
|
| 94 |
}
|
| 95 |
else {
|
| 96 |
$error_operation = reset($operations);
|
| 97 |
drupal_set_message('An error occurred while processing ' . $error_operation[0] . ' with arguments :' .
|
| 98 |
print_r($error_operation[0], TRUE));
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Setup session.
|
| 104 |
*
|
| 105 |
* @param string $session_id UTS session ID.
|
| 106 |
* @param array $modules Associative array of data collection module and status.
|
| 107 |
*/
|
| 108 |
function uts_participate_session_batch($session_id, $modules) {
|
| 109 |
$batch = array(
|
| 110 |
'operations' => array(
|
| 111 |
array('uts_participate_session_batch_modules', array($session_id, $modules)),
|
| 112 |
),
|
| 113 |
'finished' => 'uts_participate_session_batch_finished',
|
| 114 |
'title' => t('Start session'),
|
| 115 |
'init_message' => t('Configuring data plug-ins...'),
|
| 116 |
'error_message' => t('An error occured during session setup.'),
|
| 117 |
);
|
| 118 |
|
| 119 |
batch_set($batch);
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Update data collection plug-ins in testing environment.
|
| 124 |
*
|
| 125 |
* @param string $session_id UTS session ID.
|
| 126 |
* @param array $modules Associative array of data collection module and status.
|
| 127 |
*/
|
| 128 |
function uts_participate_session_batch_modules($session_id, $modules, &$context) {
|
| 129 |
$enable = array();
|
| 130 |
$disable = array();
|
| 131 |
foreach ($modules as $module => $status) {
|
| 132 |
if ($status != 'error') {
|
| 133 |
$enable[] = $module;
|
| 134 |
}
|
| 135 |
else {
|
| 136 |
$disable[] = $module;
|
| 137 |
}
|
| 138 |
}
|
| 139 |
// Enable to proctor to ensure that menu hooks are added appropriately.
|
| 140 |
$disable[] = 'uts_proctor'; // TODO If not disabled then menu hooks are not picked up for some reason.
|
| 141 |
$enable[] = 'uts_proctor';
|
| 142 |
|
| 143 |
if (!empty($disable)) {
|
| 144 |
uts_environment_invoke_callback('disable_modules', $session_id, implode(',', $disable));
|
| 145 |
}
|
| 146 |
if (!empty($enable)) {
|
| 147 |
uts_environment_invoke_callback('enable_modules', $session_id, implode(',', $enable));
|
| 148 |
}
|
| 149 |
$context['results']['session_id'] = $session_id;
|
| 150 |
$context['message'] = t('Data collection plug-ins configured.');
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Batch 'finished' callback.
|
| 155 |
*/
|
| 156 |
function uts_participate_session_batch_finished($success, $results, $operations) {
|
| 157 |
if ($success) {
|
| 158 |
// Should be inside session and will not see messages anyway.
|
| 159 |
uts_session_restore($results['session_id']);
|
| 160 |
drupal_goto('');
|
| 161 |
}
|
| 162 |
else {
|
| 163 |
$error_operation = reset($operations);
|
| 164 |
drupal_set_message('An error occurred while processing ' . $error_operation[0] . ' with arguments :' .
|
| 165 |
print_r($error_operation[0], TRUE));
|
| 166 |
}
|
| 167 |
}
|