privatemsg.devel_generate.inc

  1. 7-2 privatemsg.devel_generate.inc
  2. 6-2 privatemsg.devel_generate.inc

File

privatemsg.devel_generate.inc
View source
  1. <?php
  2. /**
  3. * Form Builder; Integrates with Devel Generate.
  4. *
  5. * @see privatemsg_devel_generate_form_validate()
  6. * @see privatemsg_devel_generate_form_submit()
  7. */
  8. function privatemsg_devel_generate_form() {
  9. // Check if authenticated users can write new messages.
  10. $perm = 'write privatemsg';
  11. $role_perms = user_role_permissions(array(DRUPAL_AUTHENTICATED_RID => 'authenticated user'));
  12. $perms = $role_perms[DRUPAL_AUTHENTICATED_RID];
  13. if (!in_array($perm, $perms)) {
  14. drupal_set_message(
  15. t('Authenticated users do not have %perm permission. This function will not run correctly. <a href="@url">Permissions page</a>.',
  16. array(
  17. '%perm' => $perm,
  18. '@url' => url(
  19. 'admin/people/permissions',
  20. array(
  21. 'fragment' => 'module-privatemsg',
  22. 'query' => array(
  23. 'destination' => 'admin/generate/privatemsg'
  24. )
  25. )
  26. )
  27. )
  28. ),
  29. 'warning');
  30. }
  31. $options = array();
  32. for ($i = 1; $i <= 10; $i++) {
  33. $options[$i] = $i;
  34. }
  35. $form['kill_content'] = array(
  36. '#type' => 'checkbox',
  37. '#title' => t('<strong>Delete all messages</strong> before generating new messages.'),
  38. '#default_value' => FALSE,
  39. );
  40. $form['num_threads'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('How many threads would you like to generate?'),
  43. '#default_value' => 10,
  44. '#size' => 10,
  45. );
  46. $form['max_thread_length'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('Max thread length?'),
  49. '#default_value' => 5,
  50. '#size' => 10,
  51. );
  52. $form['recipients'] = array(
  53. '#type' => 'fieldset',
  54. '#title' => t('Recipients'),
  55. '#description' => t('A random number of recipients will be generated for each message.'),
  56. );
  57. $form['recipients']['min_recipients'] = array(
  58. '#type' => 'select',
  59. '#title' => t('Minimum number of recipients for each message'),
  60. '#default_value' => 1,
  61. '#options' => $options,
  62. );
  63. $form['recipients']['max_recipients'] = array(
  64. '#type' => 'select',
  65. '#title' => t('Maximum number of recipients for each message'),
  66. '#default_value' => 3,
  67. '#options' => $options,
  68. );
  69. $options = array(1 => t('Now'));
  70. foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
  71. $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
  72. }
  73. $form['time_range'] = array(
  74. '#type' => 'select',
  75. '#title' => t('How far back in time should the messages be dated?'),
  76. '#description' => t('Message creation dates will be distributed randomly from the current time, back to the selected time.'),
  77. '#options' => $options,
  78. '#default_value' => 604800,
  79. );
  80. $form['subject_length'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Max word length of subjects'),
  83. '#default_value' => 8,
  84. '#size' => 10,
  85. );
  86. $form['submit'] = array(
  87. '#type' => 'submit',
  88. '#value' => t('Generate'),
  89. );
  90. $form['#redirect'] = FALSE;
  91. return $form;
  92. }
  93. /**
  94. * Validate; Integrates with Devel Generate.
  95. *
  96. * @see privatemsg_devel_generate_form()
  97. * @see privatemsg_devel_generate_form_submit()
  98. */
  99. function privatemsg_devel_generate_form_validate($form_id, &$form_state) {
  100. module_load_include('inc', 'devel_generate');
  101. $uids = devel_get_users();
  102. $max_recipients = $form_state['values']['max_recipients'];
  103. // Ensure that there are enough users to fit the max recipients.
  104. // Note, messages cannot be sent from anonymous users, so exclude them.
  105. if (count($uids) - 1 < $max_recipients) {
  106. form_set_error('recipients][max_recipients', t('You only have @count_uids users to handle @count_recipients recipients. You must increase the number of users.', array('@count_uids' => count($uids) - 1, '@count_recipients' => $max_recipients)));
  107. }
  108. }
  109. /**
  110. * Submit; Integrates with Devel Generate.
  111. *
  112. * @see privatemsg_devel_generate_form()
  113. * @see privatemsg_devel_generate_form_validate()
  114. */
  115. function privatemsg_devel_generate_form_submit($form_id, &$form_state) {
  116. if (!$form_state['values']['kill_content'] && $form_state['values']['num_threads'] <= 50 && $form_state['values']['max_thread_length'] <= 10) {
  117. privatemsg_devel_generate_threads($form_state);
  118. }
  119. else {
  120. privatemsg_devel_batch_generate_threads($form_state);
  121. }
  122. }
  123. /**
  124. * Mass Message Generation
  125. */
  126. function privatemsg_devel_generate_threads($form_state) {
  127. if ($form_state['values']['kill_content']) {
  128. privatemsg_devel_generate_kill_threads();
  129. }
  130. // Generate threads.
  131. for ($i = 1; $i <= $form_state['values']['num_threads']; $i++) {
  132. privatemsg_devel_generate_new_thread($form_state['values']);
  133. }
  134. drupal_set_message(format_plural($form_state['values']['num_threads'], '1 thread created.', '@count threads created'));
  135. }
  136. /**
  137. * Single Message Generation
  138. */
  139. function privatemsg_devel_generate_new_thread($values) {
  140. module_load_include('inc', 'devel_generate');
  141. // Make sure that uids are keyed by the actual user id.
  142. $uids = drupal_map_assoc(devel_get_users());
  143. // Do not allow anonymous (key) to send/receive private messages
  144. unset($uids[key($uids)]);
  145. $author = user_load($uids[array_rand($uids)]);
  146. $subject = devel_create_greeking(rand(1, $values['subject_length']), TRUE);
  147. $body = devel_create_content();
  148. $timestamp = rand(0, $values['time_range']);
  149. $options = array(
  150. 'author' => $author,
  151. 'timestamp' => REQUEST_TIME - $timestamp,
  152. );
  153. // Remove author when adding new recipients.
  154. unset($uids[array_search($author->uid, $uids)]);
  155. // Get a random amount of user ids.
  156. $num_recipients = rand($values['min_recipients'], $values['max_recipients']);
  157. $recipient_uids = array_rand($uids, $num_recipients);
  158. // Convert to array if just a single user has been loaded.
  159. if ($num_recipients > 1) {
  160. $recipients = user_load_multiple($recipient_uids);
  161. } else {
  162. $recipients = user_load($recipient_uids);
  163. $recipients = array($recipients);
  164. }
  165. // Generate message.
  166. $validated = privatemsg_new_thread($recipients, $subject, $body, $options);
  167. // Get thread information for generating replies.
  168. $thread_id = $validated['message']->thread_id;
  169. $num_replies = rand(0, $values['max_thread_length']);
  170. $reply_timestamp = $timestamp;
  171. // Add author back in to possible senders.
  172. $recipients[$author->uid] = $author;
  173. // Generate thread replies.
  174. for ($j = 0; $j <= $num_replies; $j++) {
  175. $reply_body = devel_create_content();
  176. $reply_author = $recipients[array_rand($recipients)];
  177. $reply_timestamp = rand(0, $reply_timestamp);
  178. $reply_options = array(
  179. 'author' => $reply_author,
  180. 'timestamp' => REQUEST_TIME - $reply_timestamp,
  181. );
  182. privatemsg_reply($thread_id, $reply_body, $reply_options);
  183. }
  184. }
  185. /**
  186. * Handle the privatemsg_devel_generate_form request to kill all of the threads.
  187. * This is used by both the batch and non-batch branches of the code.
  188. */
  189. function privatemsg_devel_generate_kill_threads() {
  190. db_delete('pm_index')->execute();
  191. $i = db_delete('pm_message')->execute();
  192. drupal_set_message(format_plural($i, 'Deleted one message', 'Deleted @count messages'));
  193. }
  194. function privatemsg_devel_batch_generate_threads($form_state) {
  195. $operations = array();
  196. // add the kill operation
  197. if ($form_state['values']['kill_content']) {
  198. $operations[] = array('privatemsg_devel_generate_batch_kill_threads', array());
  199. }
  200. // add the operations to create the nodes
  201. for ($num = 0; $num < $form_state['values']['num_threads']; $num ++) {
  202. $operations[] = array('privatemsg_devel_generate_batch_new_thread', array($form_state['values']));
  203. }
  204. // start the batch
  205. $batch = array(
  206. 'title' => t('Generating private messages'),
  207. 'operations' => $operations,
  208. 'finished' => 'privatemsg_devel_generate_batch_finished',
  209. 'file' => drupal_get_path('module', 'privatemsg') . '/privatemsg.devel_generate.inc',
  210. );
  211. batch_set($batch);
  212. }
  213. function privatemsg_devel_generate_batch_kill_threads(&$context) {
  214. privatemsg_devel_generate_kill_threads();
  215. }
  216. function privatemsg_devel_generate_batch_new_thread($values, &$context) {
  217. privatemsg_devel_generate_new_thread($values);
  218. $context['results']['num_mids'] ++;
  219. }
  220. function privatemsg_devel_generate_batch_finished($success, $results, $operations) {
  221. if ($success) {
  222. $message = t('Created @num_mids messages successfully.', array('@num_mids' => $results['num_mids']));
  223. }
  224. else {
  225. $message = t('Finished with an error.');
  226. }
  227. drupal_set_message($message);
  228. }

Functions

Namesort descending Description
privatemsg_devel_batch_generate_threads
privatemsg_devel_generate_batch_finished
privatemsg_devel_generate_batch_kill_threads
privatemsg_devel_generate_batch_new_thread
privatemsg_devel_generate_form Form Builder; Integrates with Devel Generate.
privatemsg_devel_generate_form_submit Submit; Integrates with Devel Generate.
privatemsg_devel_generate_form_validate Validate; Integrates with Devel Generate.
privatemsg_devel_generate_kill_threads Handle the privatemsg_devel_generate_form request to kill all of the threads. This is used by both the batch and non-batch branches of the code.
privatemsg_devel_generate_new_thread Single Message Generation
privatemsg_devel_generate_threads Mass Message Generation