[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/project_issue/generate/ -> project_issue_generate.inc (source)

   1  <?php
   2  // $Id: project_issue_generate.inc,v 1.13 2009/03/21 18:50:17 thehunmonkgroup Exp $
   3  
   4  // If not in 'safe mode', increase the maximum execution time.
   5  if (!ini_get('safe_mode')) {
   6    set_time_limit(240);
   7  }
   8  
   9  /**
  10   * Generate some random project issues.
  11   *
  12   * @param $number
  13   *  Number of issues to generate.
  14   */
  15  function project_issue_generate_issues($number) {
  16    module_load_include('inc', 'devel', 'devel_generate');
  17    module_load_include('inc', 'node', 'node.pages');
  18  
  19    $empty_variables = array();
  20    // After retrieving the possible values for each of these variables, we
  21    // need to check and make sure that at least one value was returned. If
  22    // not, we're likely to get fatal errors later on in this function.
  23    $projects = _project_issue_generate_get_field('projects');
  24    if (empty($projects)) { $empty_variables[] = 'projects'; }
  25    $categories = _project_issue_generate_get_field('categories');
  26    if (empty($categories)) { $empty_variables[] = 'categories'; }
  27    $priorities = _project_issue_generate_get_field('priorities');
  28    if (empty($priorities)) { $empty_variables[] = 'priorities'; }
  29    $users = _project_issue_generate_get_field('users');
  30    if (empty($users)) { $empty_variables[] = 'users'; }
  31    $loaded_users = array();
  32  
  33    // Return now if any of the above variables were empty.
  34    if (!empty($empty_variables)) {
  35      drupal_set_message(t('No values for %variables could be found to use with automatically generated issues, so no issues were created.', array('%variables' => implode(', ', $empty_variables))), 'error');
  36      return;
  37    }
  38  
  39    srand((double) microtime() * 1000000);
  40    for ($i = 0; $i < $number; $i++) {
  41      $project = $projects[array_rand($projects)];
  42      $project_node = node_load($project->nid);
  43      $components = unserialize($project->components);
  44      $releases = project_release_get_releases($project_node, FALSE);
  45      $issue = new stdClass();
  46      $issue->pid = $project->nid;
  47      $issue->category = $categories[array_rand($categories)];
  48      $issue->component = $components[array_rand($components)];
  49      $issue->priority = array_rand($priorities);
  50      $issue->title = devel_create_greeking(rand(2, 15), true);
  51      $issue->body = devel_create_content();
  52      $issue->rid = array_rand($releases);
  53  
  54      // The user must be chosen before status (sid) so that we can make sure
  55      // that the status is set such that the user would have permission to
  56      // set the status as such.
  57      $account = user_load(array('uid' => $users[array_rand($users)]->uid));
  58      $issue->name = $account->uid;
  59      $issue->sid = array_rand(_project_issue_generate_get_permitted_sids($account));
  60      if (!isset($issue->sid)) {
  61        unset($issue);
  62        continue;
  63      }
  64  
  65      // Currently, project_issue nodes can either be unassigned or assigned
  66      // to the user creating the project_issue node (or in the case of comments
  67      // the user creating the comment).
  68      $possible_assignments = array(0, $account->uid);
  69      $issue->assigned = $possible_assignments[array_rand($possible_assignments)];
  70      $issue->type = 'project_issue';
  71      $issue->comment = COMMENT_NODE_READ_WRITE;
  72  
  73      node_save($issue);
  74    }
  75  
  76    drupal_set_message(format_plural($number, 'Your issue has been created.', '@count issues have been created.'));
  77  }
  78  
  79  /**
  80   * Generate some random project issues.
  81   *
  82   * @param $number
  83   *  Number of comments to generate.
  84   */
  85  function project_issue_generate_issue_comments($number) {
  86  
  87    $empty_variables = array();
  88    // After retrieving the possible values for each of these variables, we
  89    // need to check and make sure that at least one value was returned. If
  90    // not, we're likely to get fatal errors later on in this function.
  91    $issues = _project_issue_generate_get_field('issues');
  92    if (empty($issues)) { $empty_variables[] = 'issues'; }
  93    $projects = _project_issue_generate_get_field('projects');
  94    if (empty($projects)) { $empty_variables[] = 'projects'; }
  95    $categories = _project_issue_generate_get_field('categories');
  96    if (empty($categories)) { $empty_variables[] = 'categories'; }
  97    $priorities = _project_issue_generate_get_field('priorities');
  98    if (empty($priorities)) { $empty_variables[] = 'priorities'; }
  99    $users = _project_issue_generate_get_field('users');
 100    if (empty($users)) { $empty_variables[] = 'users'; }
 101  
 102    // Return now if any of the above variables were empty.
 103    if (!empty($empty_variables)) {
 104      drupal_set_message(t('No values for %variables could be found to use with automatically generated comments, so no issue comments were created.', array('%variables' => implode(', ', $empty_variables))), 'error');
 105      return;
 106    }
 107  
 108    srand((double) microtime() * 1000000);
 109  
 110    for ($i = 0; $i < $number; $i++) {
 111      // Select a random issue to add a comment to.
 112      $k = array_rand($issues);
 113  
 114      // Update our copy of the issue to randomly decide what to change with
 115      // the new comment. This way, if we decide not to alter something, we
 116      // can leave the current value alone.
 117      $account = user_load(array('uid' => $users[array_rand($users)]->uid));
 118      $project = $projects[array_rand($projects)];
 119      if (rand(0, 1)) {
 120        $issues[$k]->pid = $project->nid;
 121        // If the project changes, the version must also change.
 122        $project_node = node_load($issues[$k]->pid, NULL, TRUE);
 123        $releases = project_release_get_releases($project_node, FALSE);
 124        $issues[$k]->rid = array_rand($releases);
 125  
 126        // And so must the component.
 127        $components = $project_node->project_issue['components'];
 128        $issues[$k]->component = $components[array_rand($components)];
 129      }
 130      elseif (rand(0, 1)) {
 131        $components = unserialize($project->components);
 132        $issues[$k]->component = $components[array_rand($components)];
 133      }
 134      if (rand(0, 1)) {
 135        $issues[$k]->category = $categories[array_rand($categories)];
 136      }
 137      if (rand(0, 1)) {
 138        $issues[$k]->priority = array_rand($priorities);
 139      }
 140      if (rand(0, 1)) {
 141        $issues[$k]->title = devel_create_greeking(rand(2, 10), true);
 142      }
 143      if (rand(0, 1)) {
 144        $issues[$k]->sid = array_rand(_project_issue_generate_get_permitted_sids($account));
 145        if (!isset($issues[$k]->sid)) {
 146          unset($comment);
 147          continue;
 148        }
 149      }
 150      if (rand(0, 1)) {
 151        $project_node = node_load($issues[$k]->pid, NULL, TRUE);
 152        $releases = project_release_get_releases($project_node, FALSE);
 153        $issues[$k]->rid = array_rand($releases);
 154      }
 155      // Construct a comment to put the issue into the state we just decided.
 156      $comment = array();
 157      $comment['uid'] = $account->uid;
 158      $comment['project_info'] = array('pid' => $issues[$k]->pid, 'component' => $issues[$k]->component, 'rid' => $issues[$k]->rid);
 159      $comment['category'] = $issues[$k]->category;
 160      $comment['priority'] = $issues[$k]->priority;
 161      $comment['title'] = $issues[$k]->title;
 162      $comment['sid'] = $issues[$k]->sid;
 163      if (rand(0, 1)) {
 164        $comment['comment'] = devel_create_content();
 165      }
 166      else {
 167        $comment['comment'] = '';
 168      }
 169      $comment['format'] = 1;
 170      $comment['cid'] = '';
 171      $comment['nid'] = $issues[$k]->nid;
 172  
 173      project_issue_add_followup($comment);
 174    }
 175    drupal_set_message(format_plural($number, 'Your comment has been created.', '@count comments have been created.'));
 176  }
 177  
 178  function _project_issue_generate_get_field($field, $pool_size = 100) {
 179    module_load_include('inc', 'project_issue', 'issue');
 180  
 181    switch ($field) {
 182      case 'issues':
 183        $issues = array();
 184        $results = db_query('SELECT p.nid, p.pid, p.category, p.component, p.priority, p.sid, p.rid, n.title FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid');
 185        while ($result = db_fetch_object($results)) {
 186          $issues[] = $result;
 187        }
 188        return $issues;
 189  
 190      case 'projects':
 191        $projects = array();
 192        $result = db_query('SELECT nid, components FROM {project_issue_projects}');
 193        while ($project = db_fetch_object($result)) {
 194          $projects[] = $project;
 195        }
 196        return $projects;
 197  
 198      case 'categories':
 199        return array_keys(project_issue_category());
 200  
 201      case 'priorities':
 202        return project_issue_priority();
 203  
 204      case 'users':
 205        // Determine what role ids have permission to create project_issue nodes.
 206        $users = array();
 207        $allowed_roles = user_roles(FALSE, 'create project issues');
 208  
 209        // If any authenticated user can create project_issue nodes,
 210        // then there is no need for an INNER JOIN in our query.
 211        // Otherwise, the query needs to INNER JOIN on the users
 212        // table so that only users with roles that are allowed to
 213        // create project_issue nodes are selected.
 214        if (isset($allowed_roles[DRUPAL_AUTHENTICATED_RID])) {
 215          $join = '';
 216          $where = '';
 217        }
 218        else {
 219          $join = 'INNER JOIN {users_roles} ur ON u.uid = ur.uid';
 220          $where = "WHERE ur.rid IN (". implode(', ', array_keys($allowed_roles)) .")";
 221        }
 222        $sql = "SELECT u.uid FROM {users} u $join $where ORDER BY RAND() LIMIT %d";
 223        $result = db_query($sql, $pool_size);
 224        while ($user = db_fetch_object($result)) {
 225          $users[] = $user;
 226        }
 227        return $users;
 228    }
 229  }
 230  
 231  function _project_issue_generate_get_permitted_sids($user) {
 232    static $permitted_states;
 233  
 234    if (!isset($permitted_states)) {
 235      $permitted_states = array();
 236    }
 237  
 238    if (isset($user->uid)) {
 239      if (isset($permitted_states[$user->uid])) {
 240        return $permitted_states[$user->uid];
 241      }
 242      else {
 243        $states = project_issue_state($sid = 0, TRUE, TRUE, 0, FALSE, $user);
 244        $permitted_states[$user->uid] = $states;
 245        return $states;
 246      }
 247    }
 248    return array();
 249  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7