Fixed incorrect file reference & form declaration typo
[project/project_issue_file_test.git] / pift.pages.inc
1 <?php
2
3 /**
4 * @file
5 * Provide general pages and UI functions.
6 *
7 * @author Jimmy Berry ("boombatower", http://drupal.org/user/214218)
8 */
9
10 /**
11 * Add additional description to file attachment form on issues.
12 */
13 function pift_pages_description_add(&$form, $form_state, $form_id) {
14 global $user;
15 // Add description on field_issue_files if it is set.
16 if (PIFT_DESCRIPTION) {
17 // Detect project issue node ID.
18 $nid = NULL;
19 // TODO: This is drupal.org specific, as any node type can be marked as an
20 // issue after the Project* D7 changes. This code assumes a single node
21 // type of 'project_issue'.
22 if ($form_id == 'project_issue_node_form') {
23 $nid = $form['#node']->field_project['und'][0]['target_id'];
24 if (isset($form['field_issue_files'][LANGUAGE_NONE]['#file_upload_description']) && $nid && pift_project_enabled($nid)) {
25 $description = filter_xss_admin(PIFT_DESCRIPTION);
26 $form['field_issue_files'][LANGUAGE_NONE]['#file_upload_description'] .= " " . $description;
27 }
28 }
29 }
30 }
31
32 /**
33 * Add testing setting.
34 */
35 function pift_pages_project_issue_settings($form, &$form_state, $node) {
36 $form = array();
37 $form['testing'] = array(
38 '#type' => 'fieldset',
39 '#title' => t('Testing'),
40 '#collapsible' => TRUE,
41 '#weight' => -9,
42 '#access' => user_access('pift enable project testing'),
43 );
44
45 // Build an array of core capatible version strings (ie 6.x, 7.x, etc).
46 $api_versions = pift_core_api_versions();
47 $versions = array();
48 foreach ($api_versions as $api_version) {
49 $term = taxonomy_term_load($api_version);
50 $versions[] = $term->name;
51 }
52 $form['testing']['pift_enable'] = array(
53 '#type' => 'checkbox',
54 '#title' => t('Enable automated testing'),
55 '#description' => t('Enable automated testing of @versions compatible branches and patches.',
56 array('@versions' => implode(', ', $versions))),
57 '#default_value' => pift_project_enabled($node->nid),
58 );
59 $form['testing']['submit'] = array(
60 '#type' => 'submit',
61 '#value' => t('Save Settings'),
62 );
63 return $form;
64 }
65
66 /**
67 * Store project testing preferences.
68 */
69 function pift_pages_project_issue_settings_submit($form, &$form_state) {
70 $enabled = pift_project_enabled($form_state['values']['nid']);
71 if ($form_state['values']['pift_enable'] && !$enabled) {
72 db_query('INSERT INTO {pift_project} VALUES (:nid)', array(':nid' => $form_state['values']['nid']));
73 }
74 else if (!$form_state['values']['pift_enable'] && $enabled) {
75 db_delete('pift_project')
76 ->condition('pid', $form_state['values']['nid'])
77 ->execute();
78 }
79 }
80
81 /**
82 * Confirm re-test.
83 *
84 * @param integer Test ID to re-test.
85 */
86 function pift_pages_retest_confirm_form($form, &$form_state, $test_id) {
87 $test = pift_test_get($test_id);
88
89 if ($test) {
90 $form = array();
91 // Key off type to determine if this is a file retest or branch retest
92 if ($test['type'] == PIFT_TYPE_FILE) {
93 // File test
94 $form['test'] = array(
95 '#type' => 'value',
96 '#value' => array(
97 'test_id' => $test_id,
98 'fid' => $test['fid'],
99 'nid' => ($test['fid'] ? $test['nid'] : $test['rid']),
100 'cid' => ($test['cid'] ? $test['cid'] : 0),
101 'status' => $test['status'],
102 'title' => ($test['fid'] ? $test['filename'] : project_release_get_version((object) $test)),
103 'type' => PIFT_TYPE_FILE,
104 ),
105 );
106 $form_state['#redirect'] = 'node/' . $form['test']['#value']['nid'];
107 }
108 elseif ($test['type'] == PIFT_TYPE_RELEASE) {
109 // Branch test
110 $form['test'] = array(
111 '#type' => 'value',
112 '#value' => array(
113 'test_id' => $test_id,
114 'status' => $test['status'],
115 'title' => "Test " . $test['test_id'],
116 'type' => PIFT_TYPE_RELEASE,
117 ),
118 );
119 // Get the project id
120 $pid = db_query_range("SELECT project_nid from {versioncontrol_release_labels} where release_nid = %d", $test['id'])->fetchField();
121 $form_state['#redirect'] = 'node/' . $pid . '/testing-status';
122 }
123 return confirm_form(
124 $form,
125 t('Are you sure you want to request that %title be re-tested?', array('%title' => $form['test']['#value']['title'])),
126 $form_state['#redirect'],
127 t('Once requested the test will be re-queued and results will be updated once the re-test has finished.'),
128 t('Re-test'),
129 t('Cancel')
130 );
131 }
132 else {
133 drupal_set_message(t('Invalid test.'), 'error');
134 }
135 }
136
137 /**
138 * Re-queue test if it still meets are criteria.
139 */
140 function pift_pages_retest_confirm_form_submit($form, &$form_state) {
141 global $user;
142 $test = $form_state['values']['test'];
143 if ($test['status'] > PIFT_STATUS_SENT) {
144 if ($test['type'] == PIFT_TYPE_FILE) {
145 // Base changes to be made in followup comment.
146 $node = node_load($test['nid']);
147 $node->nodechanges_uid = $user->uid;
148 $node->nodechanges_comment_body['value'] = theme('pift_auto_followup', array('type' => 'retest', 'nid' => $test['nid'], 'cid' => $test['cid'], 'filename' => $test['title']));
149 // If node issue status is not already a valid status then set to default
150 // retest status, otherwise leave status alone.
151 if (!in_array($node->field_issue_status[LANGUAGE_NONE][0]['value'], variable_get('pift_status', array()))) {
152 $node->field_issue_status[LANGUAGE_NONE][0]['value'] = PIFT_FOLLOWUP_RETEST;
153 }
154 // Add followup to issue.
155 node_save($node);
156 }
157
158 // Update test record to reflect change.
159 db_update('pift_test')
160 ->fields(array(
161 'status' => PIFT_STATUS_QUEUE,
162 ))
163 ->condition('test_id', $test['test_id'])
164 ->execute();
165
166 if (db_affected_rows()) {
167 drupal_set_message(t('%title has been submitted for re-testing. Please be patient while you wait for results.',
168 array('%title' => $test['title'])));
169 }
170 else {
171 drupal_set_message(t('Invalid test.'), 'error');
172 }
173 }
174 else {
175 drupal_set_message(t('%title is not currently eligible for re-testing.',
176 array('%title' => $test['title'])), 'error');
177 }
178 }
179
180 /**
181 * Confirm "delete test".
182 *
183 * @param integer Test ID to delete.
184 */
185 function pift_pages_delete_test_confirm_form($form, &$form_state, $test_id) {
186 $test = pift_test_get($test_id);
187
188 if ($test) {
189 $form = array();
190 $form['test'] = array(
191 '#type' => 'value',
192 '#value' => array(
193 'test_id' => $test_id,
194 'title' => "Test " . $test['test_id'],
195 ),
196 );
197 // Get the project id
198 $pid = db_query_range("SELECT project_nid from {versioncontrol_release_labels} where release_nid = :rid", 0, 1, array(':rid' => $test['id']))->fetchField();
199 $form_state['#redirect'] = 'node/' . $pid . '/testing-status';
200 return confirm_form(
201 $form,
202 t('Are you sure you want to delete %title?', array('%title' => $form['test']['#value']['title'])),
203 $form_state['#redirect'],
204 t('This will remove any existing results from the site, and reset the test status.') . '<br />' . t('This can help free up a branch test which is stuck in a "sent" state for long periods of time.'),
205 t('Delete Test'),
206 t('Cancel')
207 );
208 }
209 else {
210 drupal_set_message(t('Invalid test.'), 'error');
211 }
212 }
213
214 /**
215 * Delete test
216 */
217 function pift_pages_delete_test_confirm_form_submit($form, &$form_state) {
218 $test_id = $form_state['values']['test']['test_id'];
219 if (!empty($test_id)) {
220 db_delete('pift_test')
221 ->condition('test_id', $test_id)
222 ->execute();
223 }
224 }
225
226 /**
227 * Theme attachments into a table with test results.
228 *
229 * @param array $files List of file attachments.
230 * @param boolean $status Issue status.
231 * @return string HTML output.
232 */
233 function theme_pift_attachments($variables) {
234 // TODO: Validate whether these variables are still valid after the project*
235 // changes introduced with the D7 port.
236 $files = $variables['files'];
237 $status = $variables['status'];
238 $header = array(t('Attachment'), t('Size'), t('Status'), t('Test result'), t('Operations'));
239 $rows = array();
240 foreach ($files as $file) {
241 $row = array();
242
243 $row[] = l($file['filename'], file_create_url($file['filepath']));
244 $row[] = format_size($file['filesize']);
245
246 if ($file['test_id'] !== NULL) {
247 list($row[], $class) = pift_attachment_process($file['status'], (bool) $file['message']);
248 $message = $file['message'] ? check_plain($file['message']) : '<em>' . t('None') . '</em>';
249
250 // If file is being re-tested then prefix the previous result message.
251 if ($file['message'] && $file['status'] == PIFT_STATUS_QUEUE) {
252 $message = t('Previous result: ') . $message;
253 }
254
255 $row[] = $message;
256 }
257 else {
258 // Check if ignored due to issue status
259 if (!in_array($status, variable_get('pift_status', array()))) {
260 $row[] = '<em>' . t('Ignored: Check issue status.');
261 }
262 else {
263 $row[] = '<em>' . t('Ignored') . '</em>';
264 }
265 $row[] = '<em>' . t('None') . '</em>';
266 $class = '';
267 }
268
269 // Add valid operations.
270 $operations = array();
271 if (($file['status'] > PIFT_STATUS_QUEUE || $file['message']) && !empty($file['test_id'])) {
272 $operations[] = l(t('View details'), PIFT_SERVER . 'pifr/test/' . $file['test_id']);
273 }
274 if ($file['status'] > PIFT_STATUS_SENT && !($status == PROJECT_ISSUE_STATE_CLOSED) && !empty($file['test_id'])) {
275 $operations[] = l(t('Re-test'), 'pift/retest/' . $file['test_id']);
276 }
277 $row[] = ($operations ? implode(' | ', $operations) : '<em>' . t('None') . '</em>');
278
279 $rows[] = array(
280 'class' => $class,
281 'data' => $row,
282 );
283 }
284 return theme('table', array('header' => $header, 'rows' => $rows));
285 }
286
287 /**
288 * Process the specified attachment information and return the valid status
289 * message and and row CSS class.
290 *
291 * @param integer $status Test status, PIFT_STATUS_*.
292 * @param boolean $has_message Attachment has a message.
293 * @return array Cell value and CSS class.
294 */
295 function pift_attachment_process($status, $has_message) {
296 $class = '';
297 if ($status == PIFT_STATUS_QUEUE && $has_message) {
298 $cell = t('Queued for re-testing');
299 $class = 'pift-retest';
300 }
301 else if ($status == PIFT_STATUS_QUEUE) {
302 $cell = t('Queued for testing');
303 }
304 elseif ($status == PIFT_STATUS_SENT) {
305 $cell = t('Test request sent');
306 $class = 'pift-retest';
307 }
308 elseif ($status > PIFT_STATUS_SENT) {
309 $cell = t('Idle');
310 $class = $status == PIFT_STATUS_PASS ? 'pift-pass' : 'pift-fail';
311 }
312 return array($cell, $class);
313 }