Issues (1839)

boinc/modules/boincteam/boincteam.admin.inc (1 issue)

1
<?php
2
// $Id$
3
4
/**
5
 * @file
6
 * Administration page callbacks for the boicnteam module.
7
 */
8
9
/**
10
* Form builder. Configure annotations.
11
*
12
* @ingroup forms
13
* @see system_settings_form().
14
*/
15
16
/**
17
 * Utility Form to delete teams suspected of being spammers.
18
 */
19
function boincteam_utility_delete(&$form_state) {
20
  $form = array();
21
22
  $form['help'] = array(
23
    '#type' => 'fieldset',
24
    '#title' => t('Help'),
25
    '#collapsible' => TRUE,
26
  );
27
28
  $form['help']['text'] = array(
29
    '#type' => 'item',
30
    '#description' => t('This utility page will help in identifying BOINC teams created by SPAMMERs which can be deleted. The search will find BOINC teams that meet the following criteria.
31
<ul>
32
<li>Team has zero or one members.</li>
33
<li>The team has zero (0) total credits.</li>
34
<li>The team description contains a link.</li>
35
<li>The team is NOT a BOINC-wide team.</li>
36
</ul>
37
<p> <b>Warning:</b> There is no way to UNDO or UNDELETE a team after it has been deleted. It is a good idea to make a backup of the BOINC project database before deleting team information.</p>
38
<p> <i>How teams are deleted</i>: Select the teams to be deleted from the table below. Click "Delete Selected Teams" button to begin a batch operation to delete the teams. If you select teams, and then page through to another page of the table, your selections will be lost. I.e., only the teams select on the current page will be deleted.
39
<p> The site must be offline/in maintenance mode before searching for and deleting teams. If not, the delete button below is disabled.
40
'),
41
  );
42
43
  // This variable name has been changed for Drupal 7/8.
44
  $maintenance_mode = variable_get('site_offline');
45
  if (!$maintenance_mode) {
46
    drupal_set_message(t('WARNING: Site is online (not in maintenance mode)! Deleting teams is only allowed when site is offline. Change this setting in ') . l(t('Site maintenance'), '/admin/settings/site-maintenance'), 'warning');
47
  }
48
49
  $form['teamdelete'] = array(
50
    '#type' => 'fieldset',
51
    '#title' => t('Teams to delete'),
52
  );
53
  $form['teamdelete']['deleteall'] = array(
54
    '#type' => 'submit',
55
    '#disabled' => !($maintenance_mode),
56
    '#value' => t('Delete Selected Teams'),
57
    '#submit' => array('boincteam_utility_delete_team'),
58
    '#attributes' => array(
59
        'onclick' => 'return confirm(\'' . t('You are about to delete the selected teams suspected of being SPAM. Do you want to continue?') . '\')',
60
    ),
61
  );
62
  $form['teamdelete']['results_table'] = array(
63
    '#value' => drupal_get_form('boincteam_utility_team_table'),
64
  );
65
66
  return $form;
67
}
68
69
function boincteam_utility_team_table() {
70
71
  $limit=50;
72
  $sql = "SELECT team.id AS id, team.name AS name, team.nusers AS nusers FROM {team} WHERE team.nusers<2 AND team.total_credit=0 AND team.seti_id=0 AND team.description REGEXP '<a href'";
73
74
  // @todo - add bts() or t()
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
75
  $resultheader = array(
76
    array(
77
      'data' => 'Team ID',
78
      'field' => 'id',
79
      'sort' => 'asc',
80
    ),
81
    array(
82
      'data' => 'Team Name',
83
      'field' => 'name',
84
    ),
85
    array(
86
      'data' => 'Num Users',
87
      'field' => 'nusers',
88
    ),
89
    array(
90
      'data' => 'Link to Team',
91
      'field' => 'link',
92
    ),
93
  );
94
95
  $tablesort = tablesort_sql($resultheader);
96
  db_set_active('boinc_rw');
97
  $db_res = pager_query($sql . $tablesort, $limit);
98
  db_set_active('default');
99
100
  $form = array();
101
  $form['options'] = array();
102
  $checkboxes = array();
103
  if ($db_res) {
104
    while ($result = db_fetch_object($db_res)) {
105
      $drupalnid = boincteam_lookup_nid($result->id);
106
      if ($drupalnid) {
107
        $tlink = l($result->name, '/community/teams/' . $drupalnid);
108
      }
109
      else {
110
        $tlink = '';
111
      }
112
      // only keys no values
113
      $checkboxes[$result->id] = '';
114
115
      $form['id'][$result->id] = array(
116
        '#value' => $result->id,
117
      );
118
      $form['name'][$result->id] = array(
119
        '#value' => $result->name,
120
      );
121
      $form['nusers'][$result->id] = array(
122
        '#value' => $result->nusers,
123
      );
124
      $form['link'][$result->id] = array(
125
        '#value' => $tlink,
126
      );
127
    }
128
129
    $form['checkboxes'] = array('#type' => 'checkboxes', '#options' => $checkboxes);
130
    $form['pager'] = array('#value' => theme('pager', NULL, $limit, 0));
131
132
    return $form;
133
  }
134
  return "<p>No teams matched the criteria for a SPAM team.</p>";
135
}
136
137
/**
138
 * Submit handler - also creates the batch job to delete the teams.
139
 */
140
function boincteam_utility_delete_team($form, &$form_state) {
141
  $checkedteamids = $form_state['clicked_button']['#post']['checkboxes'];
142
  // Use batch to delete teams
143
  $operations = array();
144
  foreach ($checkedteamids as $id) {
145
    $operations[] = array('boincteam_utility_batch_process', array($id));
146
  }
147
  $batch = array(
148
    'title' => t('Processing BOINC Teams'),
149
    'operations' => $operations,
150
    'init_message' => t('Starting team deletion'),
151
    'finished' => 'boincteam_utility_batch_finished',
152
    'file' => drupal_get_path('module', 'boincteam') . '/boincteam.admin.inc',
153
  );
154
  batch_set($batch);
155
  //batch_process('/admin/boinc/utility-team-delete');
156
}
157
158
/*
159
 * Theme the boincteam_utility_team_table form/table
160
 *
161
 * Parameters:
162
 *   @params $form
163
 *     Input form (table)
164
 */
165
function theme_boincteam_utility_team_table($form) {
166
  //define table header
167
  $header = array(
168
    theme('table_select_header_cell'), //using that previously empty field
169
    array('data' => t('Team ID'), 'field' => 'id', 'sort' => 'asc'),
170
    array('data' => t('Name'), 'field' => 'name'),
171
    array('data' => t('Num Users'), 'field' => 'nusers'),
172
    array('data' => t('Link to Team'), 'field' => 'link'),
173
  );
174
  $rows = array();
175
  $output = "";
176
  if(!empty($form['checkboxes']['#options'])) {
177
    foreach (element_children($form['id']) as $key) {
178
      $rows[] = array(
179
        drupal_render($form['checkboxes'][$key]),
180
        drupal_render($form['id'][$key]),
181
        drupal_render($form['name'][$key]),
182
        drupal_render($form['nusers'][$key]),
183
        drupal_render($form['link'][$key]),
184
      );
185
    }
186
  }
187
  else {
188
    $rows[] = array(array('data' => '<div class="error">No teams found</div>', 'colspan' => 5));
189
  }
190
  $output .= theme('table', $header, $rows);
191
  if ($form['pager']['#value']) {
192
    $output .= drupal_render($form['pager']);
193
  }
194
195
  $output .= drupal_render($form);
196
  return $output;
197
}
198
199
/**
200
 * Function which is run during the batch job, deletes a single team
201
 * from the procject team table, as well as drupal's boincteam
202
 * table. It also removes the team's drupal page (node) if found.
203
 */
204
function boincteam_utility_batch_process($id, &$context) {
205
  $boincteam = boincteam_load($id);
206
  $drupalid = boincteam_lookup_nid($id);
207
208
  // Delete entry in team table
209
  db_set_active('boinc_rw');
210
  $sql1='DELETE FROM {team} WHERE id=%d';
211
  $db_res = db_query($sql1, $id);
212
  db_set_active('default');
213
214
  // Delete entry in boincteam table
215
  $sql2='DELETE FROM {boincteam} WHERE id=%d';
216
  $db_res = db_query($sql2, $id);
217
218
  // Delete the drupal node (if present)
219
  if ($drupalid) {
220
    node_delete($drupalid);
221
  }
222
223
  $context['results'][] = $id . ' : ' . check_plain($boincteam->name);
224
  $context['message'] = t('Processing team @name', array('@name' => $boincteam->name));
225
}
226
227
/**
228
 * Function runs at the end of the batch operation, reporting success
229
 * or failure.
230
 */
231
function boincteam_utility_batch_finished($success, $results, $operations) {
232
  if ($success) {
233
    $message = 'Info: Delete SPAM Teams utility- ' . count($results) . ' team(s) deleted.';
234
    drupal_set_message($message, 'info');
235
  }
236
  else {
237
    // An error occurred.
238
    // $operations contains the operations that remained unprocessed.
239
    $error_operation = reset($operations);
240
    $message = 'Error: An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
241
    drupal_set_message($message, 'error');
242
  }
243
}