casetracker_actions.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/casetracker/casetracker_actions/casetracker_actions.module

casetracker_actions.module

Implements Actions integration for Case Tracker

Functions & methods

NameDescription
casetracker_actions_action_infoImplementation of hook_action_info() Used to define new actions: set priority, set status, set type, set project, set assign-to, take case.
casetracker_actions_set_assign_to_actionSet Assign to action behavior.
casetracker_actions_set_assign_to_action_formSelect a user to assign for the Set Assign to action.
casetracker_actions_set_assign_to_action_submit
casetracker_actions_set_priority_actionSet Priority action behavior.
casetracker_actions_set_priority_action_formSelect a priority level for the Set Priority action.
casetracker_actions_set_priority_action_submit
casetracker_actions_set_project_actionSet Project action behavior.
casetracker_actions_set_project_action_formSelect a project for the Set Project action
casetracker_actions_set_project_action_submit
casetracker_actions_set_status_actionSet Status action behavior.
casetracker_actions_set_status_action_formSelect a status condition for the Set Status action.
casetracker_actions_set_status_action_submit
casetracker_actions_set_type_actionSet Type action behavior.
casetracker_actions_set_type_action_formSelect a type for the Set Type action.
casetracker_actions_set_type_action_submit
casetracker_actions_take_case_actionTake Case action behavior.
_casetracker_actions_actgeneralized casetracker update function All casetracker actions have the same fundamental behavior.
_casetracker_actions_get_nidGet nid from action invocation. Failing that, grab it off the current node.
_casetracker_actions_logWatchdog logging & verbose user message for actions
_casetracker_actions_update_by_commentUpdate case by programmatically inserting a comment posted by current user.

File

View source
  1. <?php
  2. /**
  3. * casetracker_actions.module
  4. * @file
  5. * Implements Actions integration for Case Tracker
  6. */
  7. /**
  8. * Implementation of hook_action_info()
  9. * Used to define new actions:
  10. * set priority, set status, set type, set project, set assign-to, take case.
  11. */
  12. function casetracker_actions_action_info() {
  13. return array(
  14. 'casetracker_actions_set_priority_action' => array(
  15. 'description' => t('Set case priority'),
  16. 'type' => 'node',
  17. 'configurable' => TRUE,
  18. 'hooks' => array('any' => TRUE), // this is a sloppy cheat.
  19. ),
  20. 'casetracker_actions_set_status_action' => array(
  21. 'description' => t('Set case status'),
  22. 'type' => 'node',
  23. 'configurable' => TRUE,
  24. 'hooks' => array('any' => TRUE),
  25. ),
  26. 'casetracker_actions_set_type_action' => array(
  27. 'description' => t('Set case type'),
  28. 'type' => 'node',
  29. 'configurable' => TRUE,
  30. 'hooks' => array('any' => TRUE),
  31. ),
  32. 'casetracker_actions_set_assign_to_action' => array(
  33. 'description' => t('Set case assigned user'),
  34. 'type' => 'node',
  35. 'configurable' => TRUE,
  36. 'hooks' => array('any' => TRUE),
  37. ),
  38. 'casetracker_actions_set_project_action' => array(
  39. 'description' => t('Set case project'),
  40. 'type' => 'node',
  41. 'configurable' => TRUE,
  42. 'hooks' => array('any' => TRUE),
  43. ),
  44. 'casetracker_actions_take_case_action' => array(
  45. 'description' => t('Take over case'),
  46. 'type' => 'node',
  47. 'configurable' => FALSE,
  48. 'hooks' => array('any' => TRUE),
  49. ),
  50. );
  51. }
  52. /**
  53. * Definitions of Action Behaviors
  54. */
  55. /**
  56. * Set Priority action behavior.
  57. */
  58. function casetracker_actions_set_priority_action(&$object, $context = array()) {
  59. $nid = _casetracker_actions_get_nid( $object->nid, $context['nid'] );
  60. _casetracker_actions_act( $nid, 'case_priority_id', $context['priority']);
  61. }
  62. /**
  63. * Set Status action behavior.
  64. */
  65. function casetracker_actions_set_status_action(&$object, $context = array()) {
  66. $nid = _casetracker_actions_get_nid( $object->nid, $context['nid'] );
  67. _casetracker_actions_act($nid, 'case_status_id', $context['status']);
  68. }
  69. /**
  70. * Set Type action behavior.
  71. */
  72. function casetracker_actions_set_type_action(&$object, $context = array()) {
  73. $nid = _casetracker_actions_get_nid( $object->nid, $context['nid'] );
  74. _casetracker_actions_act($nid, 'case_type_id', $context['type']);
  75. }
  76. /**
  77. * Set Assign to action behavior.
  78. */
  79. function casetracker_actions_set_assign_to_action(&$object, $context = array()) {
  80. $nid = _casetracker_actions_get_nid( $object->nid, $context['nid'] );
  81. _casetracker_actions_act($nid, 'assign_to', $context['assign_to']);
  82. }
  83. /**
  84. * Set Project action behavior.
  85. */
  86. function casetracker_actions_set_project_action(&$object, $context = array()) {
  87. $nid = _casetracker_actions_get_nid($object->nid, $context['nid']);
  88. _casetracker_actions_act($nid, 'pid', $context['project']);
  89. }
  90. /**
  91. * Take Case action behavior.
  92. */
  93. function casetracker_actions_take_case_action(&$object, $context = array()) {
  94. global $user;
  95. $nid = _casetracker_actions_get_nid( $object->nid, $context['nid'] );
  96. _casetracker_actions_act($nid, 'assign_to', $user->name);
  97. }
  98. /**
  99. * Action Configuration Forms
  100. */
  101. /**
  102. * Select a priority level for the Set Priority action.
  103. */
  104. function casetracker_actions_set_priority_action_form($context) {
  105. $case_priority_options = casetracker_realm_load('priority');
  106. $default_priority = variable_get('casetracker_default_case_priority',
  107. key($case_priority_options));
  108. $form['casetracker_actions_priority'] = array(
  109. '#title' => t('Priority'),
  110. '#type' => 'select',
  111. '#description' => t('The case will be updated with the priority selected above.'),
  112. '#default_value' => $default_priority,
  113. '#options' => $case_priority_options,
  114. );
  115. return $form;
  116. }
  117. function casetracker_actions_set_priority_action_submit($form, $form_state) {
  118. return array('priority' => $form_state['values']['casetracker_actions_priority']);
  119. }
  120. /**
  121. * Select a status condition for the Set Status action.
  122. */
  123. function casetracker_actions_set_status_action_form($context) {
  124. $case_status_options = casetracker_realm_load('status');
  125. $default_status = variable_get('casetracker_default_case_status',
  126. key($case_status_options));
  127. $form['casetracker_actions_status'] = array(
  128. '#title' => t('Status'),
  129. '#type' => 'select',
  130. '#description' => t('The case will be updated with the status selected above.'),
  131. '#default_value' => $default_status,
  132. '#options' => $case_status_options,
  133. );
  134. return $form;
  135. }
  136. function casetracker_actions_set_status_action_submit($form, $form_state) {
  137. return array('status' => $form_state['values']['casetracker_actions_status']);
  138. }
  139. /**
  140. * Select a type for the Set Type action.
  141. */
  142. function casetracker_actions_set_type_action_form($context) {
  143. $case_type_options = casetracker_realm_load('type');
  144. $default_type = variable_get('casetracker_default_case_type', key($case_type_options));
  145. $form['casetracker_actions_type'] = array(
  146. '#title' => t('Type'),
  147. '#type' => 'select',
  148. '#description' => t('The case will be updated with the type selected above.'),
  149. '#default_value' => $default_type,
  150. '#options' => $case_type_options,
  151. );
  152. return $form;
  153. }
  154. function casetracker_actions_set_type_action_submit($form, $form_state) {
  155. return array('type' => $form_state['values']['casetracker_actions_type']);
  156. }
  157. /**
  158. * Select a user to assign for the Set Assign to action.
  159. */
  160. function casetracker_actions_set_assign_to_action_form($context) {
  161. $options = casetracker_user_options();
  162. $form['casetracker_actions_assign_to'] = array(
  163. '#type' => 'textfield',
  164. '#title' => t('Assign to'),
  165. '#autocomplete_path' => 'casetracker_autocomplete',
  166. '#required' => TRUE,
  167. '#size' => 12,
  168. );
  169. if (count($options) < 20) {
  170. $normalized = array();
  171. foreach ($options as $uid=>$name) {
  172. $normalized[$uid] = $name;
  173. }
  174. $form['casetracker_actions_assign_to'] = array(
  175. '#type' => 'radios',
  176. '#title' => t('Assign to'),
  177. '#required' => TRUE,
  178. '#options' => $normalized,
  179. );
  180. }
  181. $form['casetracker_actions_assign_to']['#default_value'] = variable_get(
  182. 'casetracker_default_assign_to', t('Unassigned'));
  183. return $form;
  184. }
  185. function casetracker_actions_set_assign_to_action_submit($form, $form_state) {
  186. return array('assign_to' => $form_state['values']['casetracker_actions_assign_to']);
  187. }
  188. /**
  189. * Select a project for the Set Project action
  190. */
  191. function casetracker_actions_set_project_action_form($context) {
  192. $project_options = casetracker_project_options();
  193. // if there's no project ID from the URL, or more than one project,
  194. // we'll create a select menu for the user; otherwise, we'll save
  195. // the passed (or only) project ID into a hidden field.
  196. if (count($project_options) > 1) {
  197. $form['casetracker_actions_pid'] = array(
  198. '#title' => t('Project'),
  199. '#type' => 'select',
  200. '#options' => $project_options,
  201. );
  202. }
  203. else {
  204. $form['casetracker']['pid'] = array(
  205. '#type' => 'value',
  206. '#value' => !empty($default_project) ? $default_project : key($project_options),
  207. );
  208. }
  209. return $form;
  210. }
  211. function casetracker_actions_set_project_action_submit($form, $form_state) {
  212. return array('project' => $form_state['values']['casetracker_actions_pid']);
  213. }
  214. /**
  215. * Helper Functions
  216. */
  217. /**
  218. * generalized casetracker update function
  219. * All casetracker actions have the same fundamental behavior.
  220. */
  221. function _casetracker_actions_act($nid, $field, $value) {
  222. if(!$nid) return;
  223. // make sure we have a valid case node
  224. $node = node_load(array('nid' => $nid, 'type' => 'casetracker_basic_case'));
  225. $success = _casetracker_actions_update_by_comment(
  226. $field, $value, $node);
  227. if($success) {
  228. _casetracker_actions_log($field, $value, $nid);
  229. }
  230. }
  231. /**
  232. * Get nid from action invocation.
  233. * Failing that, grab it off the current node.
  234. */
  235. function _casetracker_actions_get_nid($object_nid, $context_nid) {
  236. // get the nid from the object.
  237. if (isset($object_nid)) {
  238. $nid = $object_nid;
  239. }
  240. elseif (isset($context_nid)) {
  241. $nid = $context_nid;
  242. }
  243. elseif (arg(0) == 'node') {
  244. $nid = arg(1);
  245. }
  246. return $nid;
  247. }
  248. /**
  249. * Update case by programmatically inserting a comment posted by current user.
  250. */
  251. function _casetracker_actions_update_by_comment($field, $value, $node) {
  252. global $user;
  253. if (user_access('post comments')
  254. && (user_access('administer comments') || node_comment_mode($nid)
  255. == COMMENT_NODE_READ_WRITE)) {
  256. $case_info = get_object_vars($node->casetracker);
  257. $case_info[$field] = ($field == 'assign_to') ? casetracker_get_name($value) : $value;
  258. if ($field != 'assign_to') {
  259. $case_info['assign_to'] = casetracker_get_name($case_info['assign_to']);
  260. }
  261. // casetracker uses revision_id as a primary key for casetracker_case table.
  262. $edit = array(
  263. 'nid' => $node->nid,
  264. 'pid' => 0, // comment parent id
  265. 'comment' => t('Case changed in bulk update.'), // comment body
  266. 'subject' => t('Update'),
  267. 'uid' => $user->uid,
  268. 'revision_id' => $node->vid,
  269. 'casetracker' => $case_info,
  270. );
  271. return comment_save($edit);
  272. }
  273. drupal_set_message(t('You may not post an update to case #%nid. Either you do not have the necessary permissions or the case has been locked.', array('%nid' => $nid)));
  274. return 0;
  275. }
  276. /**
  277. * Watchdog logging & verbose user message for actions
  278. */
  279. function _casetracker_actions_log($field, $value, $nid) {
  280. watchdog('action', 'Case nid %nid: %field set to %value.',
  281. array('%field'=>$field, '%value'=>$value, '%nid'=>$nid));
  282. drupal_set_message(t("Case #%nid: %field set to %value", array('%nid' => $nid, '%field' => $field, '%value' => $value)));
  283. }