/[drupal]/contributions/modules/uts/uts.node.inc
ViewVC logotype

Contents of /contributions/modules/uts/uts.node.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.39 - (show annotations) (download) (as text)
Thu Jun 4 01:09:46 2009 UTC (5 months, 3 weeks ago) by boombatower
Branch: MAIN
CVS Tags: HEAD
Changes since 1.38: +30 -1 lines
File MIME type: text/x-php
#477088: provide related link on study/task node view.
1 <?php
2 // $Id: uts.node.inc,v 1.38 2009/01/15 06:57:34 boombatower Exp $
3 /**
4 * @file
5 * Provide nodeapi implementation.
6 *
7 * Copyright 2008 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
8 */
9
10 /**
11 * Implementation of hook_node_info().
12 */
13 function uts_node_info() {
14 return array(
15 UTS_STUDY => array(
16 'name' => t('UTS Study'),
17 'module' => 'uts',
18 'description' => t('A study is the basic unit of the Usability Testing Suite.' .
19 'Participants complete a study and the results are evaluated.'),
20 'body_label' => t('Description'),
21 'custom' => TRUE,
22 'locked' => TRUE
23 ),
24 UTS_TASK => array(
25 'name' => t('UTS Task'),
26 'module' => 'uts',
27 'description' => t('An item to be complete by a participant as part of a study.'),
28 'body_label' => t('Description'),
29 'custom' => TRUE,
30 'locked' => TRUE
31 ),
32 UTS_ENVIRONMENT => array(
33 'name' => t('UTS Environment'),
34 'module' => 'uts',
35 'description' => t('Environment to be used as the basis for participants when' .
36 'starting a study.'),
37 'body_label' => t('Description'),
38 'custom' => TRUE,
39 'locked' => TRUE
40 )
41 );
42 }
43
44 /**
45 * Implementation of hook_form().
46 */
47 function uts_form(&$node) {
48 $form = array();
49 $form['#submit'] = array();
50
51 $type = node_get_types('type', $node);
52
53 if ($type->has_title) {
54 $form['title'] = array(
55 '#type' => 'textfield',
56 '#title' => check_plain($type->title_label),
57 '#required' => TRUE,
58 '#default_value' => $node->title,
59 '#weight' => -10
60 );
61 }
62
63 if ($type->has_body) {
64 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
65 }
66
67 if ($type->orig_type == UTS_STUDY) {
68 if (isset($node->nid)) {
69 // Don't bother displaying status when creating study.
70 $form['study_status'] = array(
71 '#type' => 'select',
72 '#title' => t('Status'),
73 '#description' => t('Mark the study active when it is ready for participants, and closed when it is
74 no longer open for further participation. If a maximum participant count is
75 specified then it will automatically be closed once the number has been reached.'),
76 '#options' => array(
77 UTS_STUDY_STATUS_IN_DEVELOPMENT,
78 UTS_STUDY_STATUS_ACTIVE,
79 UTS_STUDY_STATUS_CLOSED
80 ),
81 '#weight' => -9,
82 '#default_value' => (isset($node->study_status) ? $node->study_status : ''),
83 );
84 $form['study_status']['#options'] = drupal_map_assoc($form['study_status']['#options'], 'uts_get_study_status');
85 }
86 else {
87 // Set to default value.
88 $form['study_status'] = array(
89 '#type' => 'value',
90 '#value' => UTS_STUDY_STATUS_IN_DEVELOPMENT
91 );
92 }
93
94 $form['study_type'] = array(
95 '#type' => 'select',
96 '#title' => t('Type'),
97 '#description' => t('The type of study that is to be offered.'),
98 '#options' => array(
99 UTS_STUDY_TYPE_TASK
100 ),
101 '#weight' => -8,
102 '#default_value' => (isset($node->study_type) ? $node->study_type : '')
103 );
104 $form['study_type']['#options'] = drupal_map_assoc($form['study_type']['#options'], 'uts_get_study_type');
105
106 $form['participant_count'] = array(
107 '#type' => 'textfield',
108 '#title' => t('Number of participants'),
109 '#description' => t('The maximum number of participants that can participate in the study.'),
110 '#size' => 4,
111 '#maxlength' => 4,
112 '#required' => TRUE,
113 '#weight' => -7,
114 '#default_value' => (isset($node->participant_count) ? $node->participant_count : '')
115 );
116 $form['body_field']['#weight'] = -6;
117 $form['audience'] = array(
118 '#type' => 'fieldset',
119 '#title' => t('Audience'),
120 '#description' => t('Define what type of participants are relevant for the study.'),
121 '#weight' => -5
122 );
123 $form['audience']['audience_role'] = array(
124 '#type' => 'select',
125 '#title' => t('Role'),
126 '#options' => array(
127 UTS_STUDY_AUDIENCE_ROLE_USER,
128 UTS_STUDY_AUDIENCE_ROLE_DESIGNER,
129 UTS_STUDY_AUDIENCE_ROLE_PROGRAMER
130 ),
131 '#weight' => -10,
132 '#default_value' => (isset($node->audience_role) ? $node->audience_role : '')
133 );
134 $form['audience']['audience_role']['#options'] = drupal_map_assoc($form['audience']['audience_role']['#options'], 'uts_get_study_audience_role');
135
136 $form['audience']['audience_level'] = array(
137 '#type' => 'select',
138 '#title' => t('Experience level'),
139 '#options' => array(
140 UTS_STUDY_AUDIENCE_LEVEL_BEGINNER,
141 UTS_STUDY_AUDIENCE_LEVEL_INTERMEDIATE,
142 UTS_STUDY_AUDIENCE_LEVEL_ADVANCED
143 ),
144 '#weight' => -9,
145 '#default_value' => (isset($node->audience_level) ? $node->audience_level : '')
146 );
147 $form['audience']['audience_level']['#options'] = drupal_map_assoc($form['audience']['audience_level']['#options'], 'uts_get_study_audience_level');
148
149 $form['data_collection'] = array(
150 '#type' => 'fieldset',
151 '#title' => t('Data collection'),
152 '#description' => t('Choose what data plug-ins to enable and which to require.'),
153 '#weight' => -4,
154 '#tree' => TRUE,
155 '#theme' => 'uts_form_data_collection'
156 );
157
158 $plugins = uts_data_collection_list();
159 foreach ($plugins as $module => $plugin) {
160 $item = array();
161 $item['enabled'] = array(
162 '#type' => 'checkbox',
163 '#default_value' => (isset($node->data_collection[$module]) ? TRUE : FALSE)
164 );
165 $item['required'] = array(
166 '#type' => 'checkbox',
167 '#default_value' => (isset($node->data_collection[$module]) && $node->data_collection[$module]['required'] ? TRUE : FALSE)
168 );
169 $item['name'] = array(
170 '#type' => 'item',
171 '#value' => $plugin['name']
172 );
173 $item['description'] = array(
174 '#type' => 'item',
175 '#value' => $plugin['description']
176 );
177 $item['software_required'] = array(
178 '#type' => 'item',
179 '#value' => $plugin['software_require']
180 );
181 $form['data_collection'][$module] = $item;
182 }
183
184 $form['base_environment'] = array(
185 '#type' => 'fieldset',
186 '#title' => t('Base environment'),
187 '#description' => t('Manage the environment in which participants will begin their session. If no base environment is
188 created then a base Drupal installation will be used as the starting point.'),
189 '#weight' => -3,
190 );
191 $form['base_environment']['base_environment'] = array(
192 '#type' => 'select',
193 '#title' => t('Environment'),
194 '#description' => t('Choose from existing base environments.'),
195 '#options' => array(0 => '--'),
196 '#default_value' => (isset($node->base_environment) ? $node->base_environment : ''),
197 '#weight' => -10
198 );
199 $environments = uts_environments_load();
200 foreach ($environments as $environment) {
201 $form['base_environment']['base_environment']['#options'][$environment->nid] = $environment->title;
202 }
203 $form['base_environment']['manage'] = array(
204 '#type' => 'item',
205 '#value' => l(t('Manage environments'), 'admin/uts/environments'),
206 '#weight' => -9
207 );
208 $form['#redirect'] = 'admin/uts/studies';
209 }
210 else if ($type->orig_type == UTS_TASK) {
211 if (is_numeric($study_nid = arg(3)) && arg(4) == 'add') {
212 // Task will be added to a study.
213 uts_set_title($study_nid);
214 }
215 else if (isset($node->study_nid) && $study_nid = $node->study_nid) {
216 uts_set_title($study_nid);
217 }
218 else {
219 unset($study_nid);
220 }
221
222 // Get list of studies for select.
223 $studies = uts_studies_load();
224 $list = array();
225 $list[] = t('--');
226 foreach ($studies as $study) {
227 $list[$study->nid] = $study->title;
228 }
229
230 $form['study_nid'] = array(
231 '#type' => 'select',
232 '#title' => t('Parent study'),
233 '#options' => $list,
234 '#default_value' => (isset($study_nid) ? $study_nid : ''),
235 '#weight' => -9.5
236 );
237 $form['body_field']['#weight'] = -9;
238 $form['max_time'] = array(
239 '#type' => 'textfield',
240 '#title' => t('Maximum time'),
241 '#description' => t('The maximum time a participant will be allowed for the task in seconds.'),
242 '#size' => 6,
243 '#maxlength' => 6,
244 '#required' => TRUE,
245 '#weight' => -8,
246 '#default_value' => (isset($node->max_time) ? $node->max_time : '')
247 );
248 $form['#redirect'] = 'admin/uts/tasks/' . $form['study_nid']['#default_value'];
249 }
250 else if ($type->orig_type == UTS_ENVIRONMENT) {
251 $form['prefix'] = array(
252 '#type' => 'value',
253 '#value' => (isset($node->environment_prefix) ? $node->environment_prefix : '')
254 );
255 $form['#redirect'] = 'admin/uts/environments';
256 }
257
258 return $form;
259 }
260
261 /**
262 * Theme the data collection list into a table.
263 *
264 * @return string HTML output.
265 */
266 function theme_uts_form_data_collection($data_collection) {
267 $header = array(t('Enable'), t('Require'), t('Name'), t('Description'), t('Client software'));
268 $rows = array();
269 foreach (element_children($data_collection) as $module) {
270 $plugin = &$data_collection[$module];
271 $row = array();
272 $row[] = drupal_render($plugin['enabled']);
273 $row[] = drupal_render($plugin['required']);
274 $row[] = drupal_render($plugin['name']);
275 $row[] = drupal_render($plugin['description']);
276 $row[] = ($plugin['software_required']['#value'] ? t('Required') : t('Not required'));
277
278 $rows[] = $row;
279 }
280 if (empty($rows)) {
281 return t('No data collection plug-ins enabled.');
282 }
283 return theme('table', $header, $rows);
284 }
285
286 /**
287 * Implementation of hook_validate().
288 */
289 function uts_validate($node, &$form) {
290 $type = node_get_types('type', $node);
291
292 if ($type->orig_type == UTS_STUDY) {
293 if ($node->study_status == UTS_STUDY_STATUS_ACTIVE) {
294 // Make sure the study has at least one task.
295 if (!(isset($node->nid) && count(uts_tasks_load($node->nid)) > 0)) {
296 form_set_error('study_status', t('The study must have at least one task to be considred active.'));
297 }
298 }
299 if (!(is_numeric($node->participant_count) && (int) $node->participant_count == (float) $node->participant_count)) {
300 form_set_error('participant_count', t('Participant count must be an integer.'));
301 }
302 }
303 else if ($type->orig_type == UTS_TASK) {
304 if (!(is_numeric($node->max_time) && (int) $node->max_time == (float) $node->max_time)) {
305 form_set_error('max_time', t('Maximum time must be an integer.'));
306 }
307 }
308 }
309
310 /**
311 * Implementation of hook_load().
312 */
313 function uts_load($node) {
314 $type = node_get_types('type', $node);
315
316 if ($type->orig_type == UTS_STUDY) {
317 // Load study data.
318 $result = db_query('SELECT study_status, study_type, participant_count, audience_role, audience_level, base_environment
319 FROM {uts_study}
320 WHERE vid = %d',
321 $node->vid);
322 $study = db_fetch_object($result);
323
324 // Load data collection information.
325 $result = db_query('SELECT name, required
326 FROM {uts_study_data}
327 WHERE study_nid = %d', $node->nid);
328 $plugins = array();
329 while ($plugin = db_fetch_object($result)) {
330 $plugins[$plugin->name] = array('enabled' => TRUE, 'required' => (bool) $plugin->required);
331 }
332 $study->data_collection = $plugins;
333 return $study;
334 }
335 else if ($type->orig_type == UTS_TASK) {
336 // Load task data.
337 $result = db_query('SELECT study_nid, max_time, weight
338 FROM {uts_task}
339 WHERE vid = %d',
340 $node->vid);
341 return db_fetch_object($result);
342 }
343 else if ($type->orig_type == UTS_ENVIRONMENT) {
344 // Load environment data.
345 $result = db_query('SELECT prefix
346 FROM {uts_environment}
347 WHERE nid = %d',
348 $node->nid);
349 return db_fetch_object($result);
350 }
351 }
352
353 /**
354 * Implementation of hook_insert().
355 */
356 function uts_insert($node) {
357 $type = node_get_types('type', $node);
358
359 if ($type->orig_type == UTS_STUDY) {
360 db_query('INSERT INTO {uts_study} (nid, vid, study_status, study_type, participant_count, audience_role, audience_level, base_environment)
361 VALUES (%d, %d, %d, %d, %d, %d, %d, %d)',
362 $node->nid, $node->vid, $node->study_status, $node->study_type, $node->participant_count,
363 $node->audience_role, $node->audience_level, $node->base_environment);
364
365 uts_update_data_collection($node);
366
367 if (!$node->revision) {
368 // First time saving study, display a message explaining what they may want to do next.
369 if ($node->study_type == UTS_STUDY_TYPE_TASK) {
370 drupal_set_message(t('You can now <a href="@tasks">add tasks</a> to your study.',
371 array('@tasks' => url('admin/uts/tasks/' . $node->nid . '/add'))));
372 }
373 }
374 }
375 else if ($type->orig_type == UTS_TASK) {
376 db_query('INSERT INTO {uts_task} (nid, vid, max_time)
377 VALUES (%d, %d, %d)',
378 $node->nid, $node->vid, $node->max_time);
379 if ($node->study_nid) {
380 // Insert task into specific study.
381 uts_tasks_import($node->study_nid, array($node->nid));
382 }
383 }
384 else if ($type->orig_type == UTS_ENVIRONMENT) {
385 if (!$node->revision) {
386 // Create base environment for the first time.
387 $node->prefix = 'utsbase' . mt_rand(1000, 1000000);
388 uts_environment_invoke_callback('create', $node->prefix, 'base');
389 }
390 db_query("INSERT INTO {uts_environment} (nid, prefix)
391 VALUES (%d, '%s')",
392 $node->nid, $node->prefix);
393 }
394 }
395
396 /**
397 * Implementation of hook_update().
398 */
399 function uts_update($node) {
400 if ($node->revision) {
401 uts_insert($node);
402 return;
403 }
404
405 $type = node_get_types('type', $node);
406
407 if ($type->orig_type == UTS_STUDY) {
408 db_query('UPDATE {uts_study}
409 SET study_status = %d,
410 study_type = %d,
411 participant_count = %d,
412 audience_role = %d,
413 audience_level = %d,
414 base_environment = %d
415 WHERE vid = %d',
416 $node->study_status, $node->study_type, $node->participant_count, $node->audience_role,
417 $node->audience_level, $node->base_environment, $node->vid);
418
419 uts_update_data_collection($node);
420 }
421 else if ($type->orig_type == UTS_TASK) {
422 db_query('UPDATE {uts_task}
423 SET max_time = %d
424 WHERE vid = %d',
425 $node->max_time, $node->vid);
426 }
427 else if ($type->orig_type == UTS_ENVIRONMENT) {
428 // Once prefix has been created it cannot be changed.
429 }
430 }
431
432 /**
433 * Update data colleciton information for the specified node.
434 *
435 * @param object $node Node object.
436 */
437 function uts_update_data_collection($node) {
438 db_query('DELETE FROM {uts_study_data}
439 WHERE study_nid = %d', $node->nid);
440
441 if (isset($node->data_collection)) {
442 // Check for no data collection modules enabled.
443 foreach ($node->data_collection as $module => $state) {
444 if ($state['enabled'] || $state['required']) {
445 db_query("INSERT INTO {uts_study_data} (study_nid, name, required)
446 VALUES (%d, '%s', %d)", $node->nid, $module, $state['required']);
447 }
448 }
449 }
450 }
451
452 /**
453 * Implementation of hook_delete().
454 */
455 function uts_delete($node) {
456 $type = node_get_types('type', $node);
457
458 if ($type->orig_type == UTS_STUDY) {
459 // Delete all child tasks.
460 $tasks = uts_tasks_load($node->nid);
461 foreach ($tasks as $task) {
462 node_delete($task->nid);
463 }
464
465 // Remove all data collected.
466 uts_data_delete($node->nid);
467
468 // Remove sessions related to study.
469 uts_session_destroy_all($node->nid);
470
471 // Remove study data.
472 db_query('DELETE FROM {uts_study}
473 WHERE nid = %d', $node->nid);
474 }
475 else if ($type->orig_type == UTS_TASK) {
476 db_query('DELETE FROM {uts_task}
477 WHERE nid = %d', $node->nid);
478 }
479 else if ($type->orig_type == UTS_ENVIRONMENT) {
480 // Remove prefixed environment.
481 uts_environment_invoke_callback('destroy', $node->prefix);
482
483 // Remove base environment from any studies that may reference it.
484 db_query('UPDATE {uts_study}
485 SET base_environment = %d
486 WHERE base_environment = %d', 0, $node->nid);
487
488 db_query('DELETE FROM {uts_environment}
489 WHERE nid = %d', $node->nid);
490 }
491 }
492
493 /**
494 * Implementation of hook_view().
495 */
496 function uts_view($node, $teaser = FALSE, $page = FALSE) {
497 $node = node_prepare($node, $teaser);
498 $type = node_get_types('type', $node);
499
500 if ($type->orig_type == UTS_STUDY) {
501 // Basic meta data.
502 $node->content['study_status'] = array(
503 '#value' => t('<b>Study status:</b> @status<br />', array('@status' => uts_get_study_status($node->study_status))),
504 '#weight' => -10
505 );
506 $node->content['study_type'] = array(
507 '#value' => t('<b>Study type:</b> @type<br />', array('@type' => uts_get_study_type($node->study_type))),
508 '#weight' => -9
509 );
510 $node->content['audience'] = array(
511 '#value' => t('<b>Audience:</b> @role (@level)<br />',
512 array('@role' => uts_get_study_audience_role($node->audience_role),
513 '@level' => uts_get_study_audience_level($node->audience_level))),
514 '#weight' => -8
515 );
516 $node->content['participant_count'] = array(
517 '#value' => t('<b>Number of participants:</b> @count<br />', array('@count' => $node->participant_count)),
518 '#weight' => -7
519 );
520 if ($node->base_environment) {
521 $base_environment = node_load($node->base_environment);
522 $environment = l(t($base_environment->title), 'node/' . $base_environment->nid);
523 }
524 else {
525 $environment = t('Default installation');
526 }
527 $node->content['base_environment'] = array(
528 '#value' => t('<b>Base environment:</b> !environment<br />', array('!environment' => $environment)),
529 '#weight' => -7
530 );
531
532 if (!$teaser) {
533 // Related tasks.
534 $tasks = uts_tasks_load($node->nid);
535 $list = array();
536 $max_time = 0;
537 foreach ($tasks as $task) {
538 $list[] = t('<b>!title</b><br />@body', array('!title' => l($task->title, 'node/' . $task->nid), '@body' => $task->body));
539 $max_time += $task->max_time;
540 }
541 $node->content['tasks'] = array(
542 '#value' => t('<b>Tasks:</b><br />!list', array('!list' => theme('item_list', $list))),
543 '#weight' => 1
544 );
545
546 // Calculate max time.
547 $node->content['max_time'] = array(
548 '#value' => t('<b>Maximum time:</b> @time<br />', array('@time' => format_interval($max_time))),
549 '#weight' => -6
550 );
551
552 // Data collectin plug-ins.
553 $plugins = uts_data_collection_list();
554 $list = array();
555 foreach ($node->data_collection as $plugin => $state) {
556 $list[] = '<b>' . $plugins[$plugin]['name'] . '</b>: ' . $plugins[$plugin]['description'];
557 }
558 $node->content['data_collection'] = array(
559 '#value' => t('<b>Data colleciton plug-ins:</b><br />!list', array('!list' => theme('item_list', $list))),
560 '#weight'=> 2
561 );
562 }
563 }
564 else if ($type->orig_type == UTS_TASK) {
565 $node->content['max_time'] = array(
566 '#value' => t('<b>Maximum time:</b> @time<br />', array('@time' => format_interval($node->max_time))),
567 '#weight' => -10
568 );
569 }
570 else if ($type->orig_type == UTS_ENVIRONMENT) {
571 $node->content['prefix'] = array(
572 '#value' => t('<b>Prefix:</b> @prefix<br />', array('@prefix' => $node->prefix)),
573 '#weight' => -10
574 );
575 }
576 return $node;
577 }
578
579 /**
580 * Implementation of hook_link().
581 */
582 function uts_link($type, $object, $teaser = FALSE) {
583 $links = array();
584
585 if (user_access('manage studies')) {
586 if ($object->type == UTS_STUDY) {
587 $links['add_task'] = array(
588 'title' => t('Add task'),
589 'href' => "admin/uts/tasks/$object->nid/add",
590 'attributes' => array('title' => t('Add a task to this study.'))
591 );
592 $links['manage_tasks'] = array(
593 'title' => t('Manage tasks'),
594 'href' => "admin/uts/tasks/$object->nid",
595 'attributes' => array('title' => t('Manage this study\'s tasks.'))
596 );
597 if (uts_session_load_all($object->nid, TRUE)) {
598 $links['analyze_study'] = array(
599 'title' => t('Analyze study'),
600 'href' => "admin/uts/analyze/$object->nid",
601 'attributes' => array('title' => t('Analyze the results of this study.'))
602 );
603 }
604 }
605 else if ($object->type == UTS_TASK) {
606 $links['add_task'] = array(
607 'title' => t('Add task'),
608 'href' => "admin/uts/tasks/$object->study_nid/add",
609 'attributes' => array('title' => t('Add a task to the study.'))
610 );
611 $links['manage_tasks'] = array(
612 'title' => t('Manage tasks'),
613 'href' => "admin/uts/tasks/$object->study_nid",
614 'attributes' => array('title' => t('Manage the study\'s tasks.'))
615 );
616 $links['view_study'] = array(
617 'title' => t('View study'),
618 'href' => "node/$object->study_nid",
619 'attributes' => array('title' => t('View the parent study.'))
620 );
621 }
622 else if ($object->type == UTS_ENVIRONMENT) {
623 $links['open_environment'] = array(
624 'title' => t('Open environment'),
625 'href' => "admin/uts/environments/$object->nid/open",
626 'attributes' => array('title' => t('Open the environment to view or edit it.'))
627 );
628 }
629 }
630
631 return $links;
632 }

  ViewVC Help
Powered by ViewVC 1.1.2