/[drupal]/contributions/modules/randomizer/randomizer.module
ViewVC logotype

Contents of /contributions/modules/randomizer/randomizer.module

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


Revision 1.7 - (show annotations) (download) (as text)
Wed Feb 13 15:45:32 2008 UTC (21 months, 1 week ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +4 -4 lines
File MIME type: text/x-php
coding style fixes recommended by coder.module; put the proper parameters in hook_help()
1 <?php
2 // $Id: randomizer.module,v 1.6 2008/01/04 20:27:39 deekayen Exp $
3
4 /**
5 * @file
6 * Generate a set of pseudo-random numbers
7 *
8 * @author David Kent Norman
9 * @link http://deekayen.net/
10 * @link http://drupal.org/project/randomizer
11 * @todo markers, alternate rand generators (rand vs mt_rand), seeding, excel export, randomize form input
12 */
13
14 define('RANDOMIZER_MAX_SETS', 10);
15 define('RANDOMIZER_MAX_PER_SET', 500);
16
17 /**
18 * hook_help()
19 *
20 * @param string
21 * @return string
22 */
23 function randomizer_help($path, $arg) {
24 switch ($path) {
25 case 'admin/help#randomizer':
26 return t('<p>This module is designed to assist researchers and students who want an easy way to perform random sampling or assign participants to experimental conditions.</p>');
27 break;
28 case 'admin/settings/randomizer':
29 return t('Set default values for the public form.');
30 break;
31 case 'randomizer':
32 return t('Randomizer provides a pseudo-random number generator for students and researchers interested in conducting random assignment and random sampling.');
33 break;
34 }
35 }
36
37 /**
38 * Implementation of hook_block().
39 */
40 function randomizer_block($op = 'list', $delta = 0, $edit = array()) {
41 switch ($op) {
42 case 'list':
43 $blocks[0]['info'] = 'Random number';
44 return $blocks;
45 break;
46
47 case 'configure':
48 $form['randomizer_block_title'] = array(
49 '#type' => 'textfield',
50 '#title' => t('Block title'),
51 '#default_value' => variable_get('randomizer_block_title', 'Random number'),
52 '#size' => 30,
53 '#maxlength' => 250,
54 '#required' => true
55 );
56 return $form;
57 break;
58
59 case 'save':
60 variable_set('randomizer_block_title', $edit['randomizer_block_title']);
61
62 case 'view':
63 if (user_access('access randomizer')) {
64 $block['subject'] = variable_get('randomizer_block_title', t('Random number'));
65 $block['content'] = mt_rand();
66 return $block;
67 }
68 break;
69 }
70 }
71
72 /**
73 * Implementation of hook_perm().
74 *
75 * @return array
76 */
77 function randomizer_perm() {
78 return array('access randomizer', 'administer randomizer');
79 }
80
81 /**
82 * Implementation of hook_menu().
83 *
84 * @return array
85 */
86 function randomizer_menu() {
87 $items = array();
88 $items['randomizer'] = array(
89 'title' => 'Randomizer',
90 'page callback' => 'drupal_get_form',
91 'page arguments' => array('randomizer_page'),
92 'access callback' => 'user_access',
93 'access arguments' => array('access randomizer'),
94 'type' => MENU_SUGGESTED_ITEM
95 );
96 $items['admin/settings/randomizer'] = array(
97 'title' => 'Randomizer',
98 'description' => 'Set bounds for list generation.',
99 'page callback' => 'drupal_get_form',
100 'page arguments' => array('randomizer_admin_settings'),
101 'access callback' => 'user_access',
102 'access arguments' => array('administer site configuration'),
103 'type' => MENU_NORMAL_ITEM
104 );
105 return $items;
106 }
107
108 /**
109 * Get form and return it
110 *
111 * The suffle() docs on php.net have a simpler method to generate random numbers,
112 * but this more lengthy method should allow for differing random number generators
113 *
114 * @return string
115 */
116 function randomizer_page() {
117 $form = array();
118 $error = false;
119
120 if (isset($_POST['op'])) {
121 if (!is_numeric($_POST['randomizer_sets'])) {
122 $error = true;
123 form_set_error('randomizer_sets', t('The entered sets of numbers must be numeric.'));
124 }
125 if ($_POST['randomizer_sets'] > variable_get('randomizer_max_sets', RANDOMIZER_MAX_SETS)) {
126 $error = true;
127 form_set_error('randomizer_sets', t('The entered number of sets exceeds the maximum number of allowed sets allowed by the administrator.'));
128 }
129 if (!is_numeric($_POST['randomizer_per_set'])) {
130 $error = true;
131 form_set_error('randomizer_per_set', t('The entered numbers per set must be numeric.'));
132 }
133 if ($_POST['randomizer_per_set'] > variable_get('randomizer_max_per_set', RANDOMIZER_MAX_PER_SET)) {
134 $error = true;
135 form_set_error('randomizer_per_set', t('The entered amount of numbers per set exceeds the maximum number allowed allowed by the administrator.'));
136 }
137 if (!is_numeric($_POST['randomizer_range_from'])) {
138 $error = true;
139 form_set_error('randomizer_range_from', t('The From value must be numeric.'));
140 }
141 if (!is_numeric($_POST['randomizer_range_to'])) {
142 $error = true;
143 form_set_error('randomizer_range_to', t('The To value must be numeric.'));
144 }
145 if ($_POST['randomizer_range_from'] == $_POST['randomizer_range_to']) {
146 $error = true;
147 form_set_error('randomizer_range_to', t('The range may not have equal From and To values.'));
148 }
149 $max = mt_getrandmax();
150 if ($_POST['randomizer_range_from'] > $max) {
151 $error = true;
152 form_set_error('randomizer_range_from', t("The From value may not exceed $max."));
153 }
154 if ($_POST['randomizer_range_to'] > $max) {
155 $error = true;
156 form_set_error('randomizer_range_to', t("The To value may not exceed $max."));
157 }
158 unset($max);
159
160 if (!$error) {
161 if (strcmp($_POST['randomizer_range_from'], $_POST['randomizer_range_to']) > 0) {
162 // reverse values so the smaller one is first when sent to the rand generator
163 $tmp = $_POST['randomizer_range_from'];
164 $_POST['randomizer_range_from'] = $_POST['randomizer_range_to'];
165 $_POST['randomizer_range_to'] = tmp;
166 unset($tmp);
167 }
168
169 $_POST['randomizer_range_from'] = (int)$_POST['randomizer_range_from'];
170 $_POST['randomizer_range_to'] = (int)$_POST['randomizer_range_to'];
171
172 for ($i=0; $i<$_POST['randomizer_sets']; $i++) {
173 $list = array();
174 for ($j=0; $j < $_POST['randomizer_per_set']; ) {
175 $number = mt_rand($_POST['randomizer_range_from'], $_POST['randomizer_range_to']);
176 if (!$_POST['randomizer_unique'] || ($_POST['randomizer_unique'] && !in_array($number, $list))) {
177 $list[] = $number;
178 $j++;
179 }
180 }
181 if ($_POST['randomizer_sort'] == '1') {
182 sort($list);
183 }
184 elseif ($_POST['randomizer_sort'] == '2') {
185 rsort($list);
186 }
187 $form['randomizer_result-'. $i] = array(
188 '#type' => 'textarea',
189 '#default_value' => check_plain(implode($_POST['randomizer_delimiter'], $list)),
190 '#rows' => 10, '#cols' => 45,
191 '#prefix' => t('Set #%i', array('%i' => $i+1)),
192 '#attributes' => array('onclick' => 'this.focus();this.select()'),
193 '#suffix' => '<hr />'
194 );
195 }
196 }
197 }
198
199 return array_merge($form, randomizer_form());
200 }
201
202 /**
203 * Settings hook
204 *
205 * @return array
206 */
207 function randomizer_admin_settings() {
208 $form = array();
209 $form['randomizer_max_sets'] = array(
210 '#type' => 'textfield',
211 '#default_value' => variable_get('randomizer_max_sets', RANDOMIZER_MAX_SETS),
212 '#size' => 5,
213 '#maxlength' => 5,
214 '#title' => t('Maximum set of numbers users may generate')
215 );
216 $form['randomizer_max_per_set'] = array(
217 '#type' => 'textfield',
218 '#default_value' => variable_get('randomizer_max_per_set', RANDOMIZER_MAX_PER_SET),
219 '#size' => 5,
220 '#maxlength' => 5,
221 '#title' => t('Maximum numbers per set users may generate')
222 );
223 $form = array_merge($form, randomizer_form());
224 return system_settings_form($form);
225 }
226
227 /**
228 * Default form for the public and settings output
229 *
230 * @param array $append form fields to prepend to the default form
231 * @return array
232 */
233 function randomizer_form($form = array()) {
234 $form['#redirect'] = FALSE;
235 $form['randomizer_sets'] = array(
236 '#type' => 'textfield',
237 '#size' => 5,
238 '#maxlength' => 10,
239 '#default_value' => variable_get('randomizer_sets', 1),
240 '#title' => t('How many sets of numbers do you want to generate?'),
241 '#description' => t('In some cases, you may wish to generate more than one set of numbers at a time (e.g., when randomly assigning participants to experimental conditions in a "blocked" research design).'),
242 '#required' => true
243 );
244 $form['randomizer_per_set'] = array(
245 '#type' => 'textfield',
246 '#size' => 5,
247 '#maxlength' => 10,
248 '#default_value' => variable_get('randomizer_per_set', 5),
249 '#title' => t('How many numbers per set?'),
250 '#description' => t('A request for 6 numbers might yield the following set of random numbers: 4, 8, 10, 13, 25, 26.'),
251 '#required' => true
252 );
253 $form['randomizer_range'] = array(
254 '#type' => 'fieldset',
255 '#title' => t('Number range (e.g., 1-50)'),
256 '#description' => t('Specify the upper and lower range of the numbers you want to generate. For example, a range of 1 up to 50 would only generate random numbers between 1 and 50 (inclusive) (e.g., 4, 13, 8, 26, 50). Type the lowest number you want in the "From" field and the highest number you want in the "To" field.'),
257 '#collapsed' => false,
258 '#collapsible' => false
259 );
260 $form['randomizer_range']['randomizer_range_from'] = array(
261 '#type' => 'textfield',
262 '#size' => 10,
263 '#maxlength' => 10,
264 '#default_value' => variable_get('randomizer_range_from', 1),
265 '#title' => t('From'),
266 '#required' => true
267 );
268 $form['randomizer_range']['randomizer_range_to'] = array(
269 '#type' => 'textfield',
270 '#size' => 10,
271 '#maxlength' => 10,
272 '#default_value' => variable_get('randomizer_range_to', 50),
273 '#title' => t('To'),
274 '#required' => true
275 );
276 $form['randomizer_options'] = array(
277 '#type' => 'fieldset',
278 '#title' => t('Optional output modifications'),
279 '#collapsed' => true,
280 '#collapsible' => true,
281 );
282 $form['randomizer_options']['randomizer_unique'] = array(
283 '#type' => 'select',
284 '#options' => array(1 => t('Yes'), 0 => t('No')),
285 '#default_value' => variable_get('randomizer_unique', 1),
286 '#title' => t('Each number in a set to remain unique?'),
287 '#description' => t('Selecting "Yes" means a number will appear only once in a given set (e.g., 10, 4, 2003, 8, 26). Selecting "No" means numbers may repeat themselves within a given set (e.g., 10, 4, 2003, 10, 25). Please note: The numbers remain unique only within a given set, not across multiple sets. So, if you run multiple sets, any given number in Set #1 could easily show up again in Set #2.')
288 );
289 $form['randomizer_options']['randomizer_sort'] = array(
290 '#type' => 'select',
291 '#options' => array(t('No'), t('Yes: Least to Greatest'), t('Yes: Greatest to Least')),
292 '#default_value' => variable_get('randomizer_sort', 0),
293 '#title' => t('Sort outputted numbers?')
294 );
295 /* $form['randomizer_options']['randomizer_markers'] = array(
296 '#type' => 'select',
297 '#options' => array(t('Place Markers Off'), t('Place Markers Within'), t('Place Markers Across')),
298 '#default_value' => variable_get('randomizer_markers', 0),
299 '#title' => t('Do you wish each number in a set to remain unique?'),
300 '#description' => t('Place Markers lets you know in what place in the sequence a particular random number falls by marking it with a number immediately prior to it. This option is especially helpful for doing random assignment by blocks.')
301 ); */
302 $form['randomizer_options']['randomizer_delimiter'] = array(
303 '#type' => 'textfield',
304 '#size' => 10,
305 '#default_value' => variable_get('randomizer_delimiter', ', '),
306 '#title' => t('How do you want to delimit the output?'),
307 '#description' => t('Characters to insert between output numbers')
308 );
309 $form['randomizer_submit'] = array(
310 '#type' => 'submit',
311 '#value' => t('Randomize')
312 );
313
314 return $form;
315 }

  ViewVC Help
Powered by ViewVC 1.1.2