-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_test.module
More file actions
125 lines (107 loc) · 2.97 KB
/
ts_test.module
File metadata and controls
125 lines (107 loc) · 2.97 KB
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
<?php
/**
* Implements hook_menu().
*/
function ts_test_menu(){
$items = array();
// Doing a normal callback to render the drupal form.
// Instead of using drupal_get_form as callback.
$items['ts_test'] = array(
'title' => 'ThinkShout Test',
'page callback' => 'ts_test_callback',
'access callback' => TRUE,
'description' => 'ThinkShout test form.',
);
return $items;
}
/**
* Page callback for ts_test.
*/
function ts_test_callback(){
$output = '';
// Get the drupal form.
$form = drupal_get_form('ts_test_form');
// Some render fun.
$content = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#attributes' => array(
'class' => 'bmi-content',
),
'#value' => t('This is a simple BMI (Body Mass Index) calculator. Please enter your weight (in lbs) and your height (in inches). We will give your BMI'),
);
$output .= drupal_render($content);
$output .= drupal_render($form);
// Render and print drupal form.
return $output;
}
/**
* Form for ts_test.
*/
function ts_test_form($form, &$form_state) {
// Collect my weight value.
$form['my_weight'] = array(
'#type' => 'textfield',
'#title' => t('Weight'),
'#size' => 4,
'#description' => t('Please enter your weight in lbs.'),
'#required' => TRUE,
);
// Collect my height value.
$form['my_height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#size' => 4,
'#description' => t('Please enter your height in inches.'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Get My BMI'),
);
return $form;
}
/**
* Validation handler for ts_test_form().
*/
function ts_test_form_validate($form, &$form_state) {
if (isset($form_state['values']['my_weight'])) {
$weight = $form_state['values']['my_weight'];
if (!is_numeric($weight)) {
form_error($form['my_weight'], t('You entered a non-numeric value for your weight.'));
}
}
if (isset($form_state['values']['my_height'])) {
$height = $form_state['values']['my_height'];
if (!is_numeric($height)) {
form_error($form['my_height'], t('You entered a non-numeric value for your height.'));
}
}
}
/**
* Submit handler for ts_test_form().
*/
function ts_test_form_submit($form, &$form_state) {
$weight = $form_state['values']['my_weight'];
$height = $form_state['values']['my_height'];
$height_sq = $height * $height;
$bmi = ($weight / $height_sq) * 703;
if ($bmi <= 18.5) {
$weight_class = 'Underweight';
$status = 'warning';
}
elseif ($bmi > 18.5 && $bmi < 25) {
$weight_class = 'Normal weight';
$status = 'status';
}
elseif ($bmi >= 25 && $bmi < 30) {
$weight_class = 'Overweight';
$status = 'warning';
}
elseif ($bmi >= 30) {
$weight_class = 'Obese';
$status = 'error';
}
$message = t('According to your BMI: %bmi, You are %weight_class', array('%bmi' => $bmi, '%weight_class' => $weight_class));
drupal_set_message($message, $status);
}