From 21c9acc6368c7d32dae6c6b23a875bbecece3dd1 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 12:47:47 +0200 Subject: [PATCH 1/7] Added PPGetStatHTML class --- .../all/modules/custom/ppgetstat/includes/ppgetstat.html.inc | 0 docroot/sites/all/modules/custom/ppgetstat/ppgetstat.info | 2 ++ 2 files changed, 2 insertions(+) create mode 100644 docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc diff --git a/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc b/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc new file mode 100644 index 0000000..e69de29 diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.info b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.info index f854bd5..d0ef68a 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.info +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.info @@ -2,3 +2,5 @@ name = ppgetstat description = Get drupal.org statistic core = 7.x version = 1.0 + +files[] = includes/ppgetstat.html.inc From 07528e3e7e5cab7b5235c006b7160f612d259fb3 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 12:48:00 +0200 Subject: [PATCH 2/7] Added PPGetStatHTML class --- .../ppgetstat/includes/ppgetstat.html.inc | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc b/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc index e69de29..bacf7ba 100644 --- a/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc +++ b/docroot/sites/all/modules/custom/ppgetstat/includes/ppgetstat.html.inc @@ -0,0 +1,61 @@ +document = new DOMDocument(); + // Handle errors/warnings and don't mess up output of your script. + // @see http://stackoverflow.com/a/17559716 + $this->libxmlState = libxml_use_internal_errors(true); + $this->document->loadHTML($content); + } + + /** + * Clear XML library errors. + */ + public function __destruct() { + libxml_clear_errors(); + libxml_use_internal_errors($this->libxmlState); + } + + /** + * Make XPath query to the document object. + * + * @return DOMXPath + * XPath object to perform queries. + */ + public function xpath() { + if (NULL === $this->xpath) { + $this->xpath = new DOMXPath($this->document); + } + + return $this->xpath; + } + +} From 0ebfc8e46340f159d33d0ceede39202b09f1da7c Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 12:51:22 +0200 Subject: [PATCH 3/7] Managed constants, added functions: ppgetstat_stats_period(), ppgetstat_get_github_username_by_doid(), implemented GH username parsing --- .../modules/custom/ppgetstat/ppgetstat.module | 76 +++++++++++++++++-- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.module b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.module index 82ce7f5..80fec4c 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.module @@ -10,11 +10,15 @@ define('PPGETSTAT_USER_NODE_TYPE', 'user'); define('PPGETSTAT_STATSJOBS_PROCESS_NUMBER', 100); // Number of calls to drupal.org per cron run. define('PPGETSTAT_DORGSCRAPPING_PROCESS_NUMBER', 800); +// Hours, minutes, seconds. +define('PPGETSTAT_TIME_DAY_GRANULARITY', 24 * 60 * 60); +// Week timestamp. +define('PPGETSTAT_TIME_WEEK_GRANULARITY', 7 * PPGETSTAT_TIME_DAY_GRANULARITY); +// Month timestamp. +define('PPGETSTAT_TIME_MONTH_GRANULARITY', 30 * PPGETSTAT_TIME_DAY_GRANULARITY); // If no stats available we scan commits for last half a year. -define('PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING', variable_get('ppgetstat_stats_period', 6 * 30 * 24 * 60 * 60)); -// We group statistics per week. -define('PPGETSTAT_TIME_PERIOD_GRANULARITY', 7 * 24 * 60 * 60); -// Commmits type of data. +define('PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING', ppgetstat_stats_period()); +// Commits type of data. define('PPGETSTAT_TYPE_COMMITS', 1); // Page cache expiry time. define('PPGETSTAT_PAGE_CACHE_EXPIRY', 6 * 60 * 60); @@ -69,6 +73,32 @@ function ppgetstat_menu() { return $items; } +/** + * Get/set scanning period. + * + * @param int $months + * Number of months. + * + * @return int + * Timestamp. + */ +function ppgetstat_stats_period($months = NULL) { + if (isset($months)) { + $do = 'set'; + } + else { + // If value doesn't specified then work as getter. + $do = 'get'; + // Default number of months to scan. + $months = 6; + } + + $value = $months * PPGETSTAT_TIME_MONTH_GRANULARITY; + $result = call_user_func("variable_$do", __FUNCTION__, $value); + + return NULL === $result ? $value : $result; +} + /** * Check that user is already imported from D.org to database. * @@ -125,6 +155,34 @@ function ppgetstat_get_dorg_user($argument) { return FALSE; } +/** + * Parse GitHub username from Drupal.org. + * + * @param int $doid + * Drupal.org used ID. + * + * @return string + * GitHub user name. + */ +function ppgetstat_get_github_username_by_doid($doid) { + $page = drupal_http_request(PPGETSTAT_DEFAULT_API_URL . "/user/$doid"); + + if (!empty($page->data)) { + $data = new PPGetStatHTML($page->data); + $links = $data + ->xpath() + ->query("//div[contains(@class, 'field-social-links')]//a[contains(@href, 'github')]/@href"); + + if ($links->length > 0) { + $url = parse_url($links[$links->length - 1]->value); + + return ltrim($url['path'], '/'); + } + } + + return ''; +} + /** * Implements hook_cron(). */ @@ -488,7 +546,7 @@ function _ppgetstat_parse_commits($page_content, $data) { break; } - $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_WEEK_GRANULARITY); $last_commits = variable_get('ppgetstat_ppgetstat_last_commit_timestamp', array($data['doid'] => array($period_timestamp => 0))); // Save latest commit's date per week into variable. @@ -710,7 +768,13 @@ function _ppgetstat_process_dorg_users(array $users_data) { } $node->title = $data->name; - $node->field_user_id[LANGUAGE_NONE] = array(array('value' => $uid)); + + foreach (array( + 'field_user_id' => $uid, + 'field_github_username' => ppgetstat_get_github_username_by_doid($uid), + ) as $field_name => $value) { + $node->{$field_name}[LANGUAGE_NONE] = array(array('value' => $value)); + } if (!empty($data->field_organizations)) { foreach ($data->field_organizations as $organization) { From 10c51149ede894eb106f30e27680175a7e57887c Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 12:52:14 +0200 Subject: [PATCH 4/7] Fixed comment and GH username scanning --- .../custom/pp_comparing/pp_comparing.module | 4 +-- .../modules/custom/ppgetstat/ppcc/ppcc.module | 2 +- .../custom/ppgetstat/ppcc/ppcc.pages.inc | 4 +-- .../custom/ppgetstat/ppcmnt/ppcmnt.module | 21 +++++++-------- .../custom/ppgetstat/ppcmnt/ppcmnt.pages.inc | 4 +-- .../custom/ppgetstat/ppgetstat.pages.inc | 26 +++++++++---------- .../modules/custom/ppgetstat/ppgh/ppgh.module | 6 ++--- 7 files changed, 31 insertions(+), 36 deletions(-) diff --git a/docroot/sites/all/modules/custom/pp_comparing/pp_comparing.module b/docroot/sites/all/modules/custom/pp_comparing/pp_comparing.module index 856e9dd..dacaddc 100755 --- a/docroot/sites/all/modules/custom/pp_comparing/pp_comparing.module +++ b/docroot/sites/all/modules/custom/pp_comparing/pp_comparing.module @@ -123,7 +123,7 @@ function _pp_comparing_users_data($users, $types, $dates) { $db_data = $query->execute()->fetchAllAssoc('timestamp'); $beginning = (!empty($dates['from'])) ? $dates['from'] : REQUEST_TIME - PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING; - $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_WEEK_GRANULARITY); $till_time = !empty($dates['till']) ? $dates['till'] : REQUEST_TIME; while ($timestamp < $till_time) { @@ -134,7 +134,7 @@ function _pp_comparing_users_data($users, $types, $dates) { $table_data[$timestamp]['timestamp'] = format_date($timestamp, 'custom', 'j M y'); $table_data[$timestamp][$doid] = $commits; - $timestamp += PPGETSTAT_TIME_PERIOD_GRANULARITY; + $timestamp += PPGETSTAT_TIME_WEEK_GRANULARITY; } } diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.module b/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.module index 47fbec9..96e3cc0 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.module @@ -94,7 +94,7 @@ function _ppcc_parse_core_commits($page_content, $data) { continue; } - $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_WEEK_GRANULARITY); $last_commits = variable_get('ppgetstat_ppcc_last_commit_timestamp', array($data['doid'] => array($period_timestamp => 0))); // Save latest commit's date per week into variable. diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.pages.inc b/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.pages.inc index c680cca..693773e 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.pages.inc +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcc/ppcc.pages.inc @@ -66,7 +66,7 @@ function _ppcc_commits_data($node) { )->fetchAllKeyed(); $beginning = REQUEST_TIME - PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING; - $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_WEEK_GRANULARITY); $table_data = array(); while ($timestamp < REQUEST_TIME) { @@ -79,7 +79,7 @@ function _ppcc_commits_data($node) { $commits, ); - $timestamp += PPGETSTAT_TIME_PERIOD_GRANULARITY; + $timestamp += PPGETSTAT_TIME_WEEK_GRANULARITY; } return $table_data; diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module index edfb88e..8d9c43e 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module @@ -144,27 +144,24 @@ function _ppcmnt_parse_posts_list($page_content, $data) { * Parse post page for comments. */ function _ppcmnt_parse_post_page($page_content, $data) { - $user_data = json_decode($page_content); - $count = count($user_data->list); + $end = REQUEST_TIME - variable_get('ppgetstat_stats_period'); - $comments_counter_array = []; - $list = &$user_data->list; + $result = []; - $end = REQUEST_TIME - variable_get('ppgetstat_stats_period'); - for ($i = ($count - 1); $i >= 0; $i--) { - if ($list[$i]->created < $end) { + foreach ($user_data->list as $i => $item) { + if ($item->created < $end) { continue; } - $period_timestamp = $list[$i]->created - ($list[$i]->created % PPGETSTAT_TIME_PERIOD_GRANULARITY); - $comments_counter_array[$period_timestamp][] = $list[$i]->cid; + + $result[$item->created - ($item->created % PPGETSTAT_TIME_WEEK_GRANULARITY)][] = $item->cid; } - if (count($comments_counter_array) != 0) { - $comments_counter_array['#type'] = PPGETSTAT_TYPE_COMMENTS; + if (count($result) > 0) { + $result['#type'] = PPGETSTAT_TYPE_COMMENTS; } - return $comments_counter_array; + return $result; } /** diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.pages.inc b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.pages.inc index d74a102..83231bc 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.pages.inc +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.pages.inc @@ -65,7 +65,7 @@ function _pppcmnt_load_the_data($node) { )->fetchAllKeyed(); $beginning = REQUEST_TIME - PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING; - $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_WEEK_GRANULARITY); $table_data = array(); while ($timestamp < REQUEST_TIME) { @@ -78,7 +78,7 @@ function _pppcmnt_load_the_data($node) { $commits, ); - $timestamp += PPGETSTAT_TIME_PERIOD_GRANULARITY; + $timestamp += PPGETSTAT_TIME_WEEK_GRANULARITY; } return $table_data; diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc index c3c3a36..eb764c6 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc @@ -65,7 +65,7 @@ function _ppgetstat_get_commits_data($node) { )->fetchAllKeyed(); $beginning = REQUEST_TIME - PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING; - $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $timestamp = $beginning - ($beginning % PPGETSTAT_TIME_WEEK_GRANULARITY); $table_data = array(); while ($timestamp < REQUEST_TIME) { @@ -78,7 +78,7 @@ function _ppgetstat_get_commits_data($node) { $commits, ); - $timestamp += PPGETSTAT_TIME_PERIOD_GRANULARITY; + $timestamp += PPGETSTAT_TIME_WEEK_GRANULARITY; } return $table_data; @@ -87,7 +87,12 @@ function _ppgetstat_get_commits_data($node) { /** * Form builder for reset stats form. */ -function ppgetstat_reset_form($form, $form_state) { +function ppgetstat_reset_form(array $form, array &$form_state) { + $titles = array(); + + foreach (node_load_multiple(array(), array('type' => PPGETSTAT_USER_NODE_TYPE)) as $user_node) { + $titles[] = $user_node->title; + } $form['markup'] = array( '#type' => 'item', @@ -96,17 +101,12 @@ function ppgetstat_reset_form($form, $form_state) { $form['stats_period'] = array( '#type' => 'textfield', - '#title' => t('Scanning period'), + '#title' => t('Scanning period (number of months)'), '#element_validate' => array('element_validate_integer_positive'), - '#default_value' => variable_get('ppgetstat_stats_period', 6 * 30 * 24 * 60 * 60), - '#description' => t('For debugging purposes we can shorten period of grabbing stats. By default it is half a year 15552000, but we can set it to one month 2592000.') + '#default_value' => PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING / PPGETSTAT_TIME_MONTH_GRANULARITY, + '#description' => t('For debugging purposes we can shorten period of grabbing stats.'), ); - $titles = array(); - foreach (node_load_multiple(array(), array('type' => PPGETSTAT_USER_NODE_TYPE)) as $user_node) { - $titles[] = $user_node->title; - } - $form['users'] = array( '#type' => 'select', '#multiple' => TRUE, @@ -127,9 +127,9 @@ function ppgetstat_reset_form($form, $form_state) { /** * Submit handler for reset form. */ -function ppgetstat_reset_form_submit($form, $form_state) { +function ppgetstat_reset_form_submit(array $form, array &$form_state) { if (!empty($form_state['values']['stats_period'])) { - variable_set('ppgetstat_stats_period', $form_state['values']['stats_period']); + ppgetstat_stats_period($form_state['values']['stats_period']); variable_del('ppgetstat_last_statsjobs_timestamp'); db_query('TRUNCATE TABLE {ppgetstat}'); diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module b/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module index fac9065..9412b27 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module @@ -41,7 +41,7 @@ function ppgh_ppgetstat_stats_job($user_node) { * Build url for scanning github activity. */ function _ppgh_get_github_activity_tracking_url($data) { - $url[] = 'https://github.com/users/' . $data['github_username'] . '/contributions_calendar_data'; + $url[] = 'https://github.com/users/' . $data['github_username'] . '/contributions'; return $url; } @@ -49,8 +49,6 @@ function _ppgh_get_github_activity_tracking_url($data) { * Parse github activity data page. */ function _ppgh_parse_github_activity($page_content, $data) { - $last_scan = $data['last_scan']; - if (empty($page_content)) { throw new Exception('Empty page content.'); } @@ -63,7 +61,7 @@ function _ppgh_parse_github_activity($page_content, $data) { foreach ($json_decoded as $day) { $commit_timestamp = strtotime($day[0]); $day_commits_count = (int) $day[1]; - $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_PERIOD_GRANULARITY); + $period_timestamp = $commit_timestamp - ($commit_timestamp % PPGETSTAT_TIME_WEEK_GRANULARITY); if (!isset($commits_counter_array[$period_timestamp])) { $commits_counter_array[$period_timestamp] = 0; From 23e5f7a8ba027e5d8cc0a2fafde99011b61d9fe6 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 13:10:20 +0200 Subject: [PATCH 5/7] Fixed comment scanning --- docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module | 2 +- docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module index 8d9c43e..c1cba1b 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module @@ -145,7 +145,7 @@ function _ppcmnt_parse_posts_list($page_content, $data) { */ function _ppcmnt_parse_post_page($page_content, $data) { $user_data = json_decode($page_content); - $end = REQUEST_TIME - variable_get('ppgetstat_stats_period'); + $end = REQUEST_TIME - PPGETSTAT_DEFAULT_TIME_PERIOD_FOR_SCANNING; $result = []; diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module b/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module index 9412b27..cf66172 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgh/ppgh.module @@ -41,7 +41,7 @@ function ppgh_ppgetstat_stats_job($user_node) { * Build url for scanning github activity. */ function _ppgh_get_github_activity_tracking_url($data) { - $url[] = 'https://github.com/users/' . $data['github_username'] . '/contributions'; + $url[] = 'https://github.com/users/' . $data['github_username'] . '/contributions_calendar_data'; return $url; } From 1682b8dd39c964342d91ea03329255b6c77aa878 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 13:37:56 +0200 Subject: [PATCH 6/7] Merge origin/master into BR0kEN-/issue-93 (#2) --- .../custom/ppgetstat/ppcmnt/ppcmnt.module | 9 +- .../custom/ppgetstat/ppgetstat.pages.inc | 123 +++++++++--------- 2 files changed, 70 insertions(+), 62 deletions(-) diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module index a65dd93..c1cba1b 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module +++ b/docroot/sites/all/modules/custom/ppgetstat/ppcmnt/ppcmnt.module @@ -45,21 +45,22 @@ function ppcmnt_ppgetstat_stats_job($user_node) { * Build urls for scanning core commits. */ function _ppcmnt_get_comments_tracking_urls($data) { - $last_scan = $data['last_scan']; $cmt_zero_page = _ppgetstat_fetch_page('https://www.drupal.org/api-d7/comment.json?author=' . urlencode($data['doid'])); $user_data = json_decode($cmt_zero_page); $count = parse_url($user_data->last); + $urls = array(); + parse_str($count['query'], $query); + if (empty($user_data->list)) { - return NULL; + return $urls; } if ($query['page'] == 0) { return array($user_data->self); } // In worst scenario we expect one page of posts per four hours. // See webchick as example https://www.drupal.org/user/24967/track - $days_number = round((REQUEST_TIME - $last_scan) / (4 * 60 * 60)); - $urls = array(); + // $days_number = round((REQUEST_TIME - $last_scan) / (4 * 60 * 60)); for ($i = 0; $i <= (int)$query['page']; $i++) { $urls[] = "https://www.drupal.org/api-d7/comment.json?name=" . urlencode($data['doid']) . "&page=" . $i; } diff --git a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc index 861f0ca..eb764c6 100755 --- a/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc +++ b/docroot/sites/all/modules/custom/ppgetstat/ppgetstat.pages.inc @@ -179,99 +179,106 @@ function ppgetstat_reset_form_submit(array $form, array &$form_state) { /** * Form builder for reset stats form. */ -function ppgetstat_sync_user_list($form, $form_state) { - // Getting list of all available users list for analyzing. - $user_lists_vocab = taxonomy_vocabulary_machine_name_load('users_list'); - $terms = taxonomy_get_tree($user_lists_vocab->vid); - $options = array( - 0 => t('- None -'), - ); - foreach ($terms as $term) { +function ppgetstat_sync_user_list(array $form, array &$form_state) { + $options = array(); + + // Getting list of all available user lists for analyzing. + foreach (taxonomy_get_tree(taxonomy_vocabulary_machine_name_load('users_list')->vid) as $term) { $options[$term->tid] = $term->name; } + $form['users_list'] = array( '#type' => 'select', - '#title' => t('Select users group scan to'), + '#title' => t('Select users group to scan'), '#options' => $options, + '#empty_value' => 0, + '#disabled' => array('direct_user', array('!value' => '')), ); $form['direct_user'] = array( '#type' => 'textfield', - '#title' => t('Direct user rescan'), - '#description' => t('If this field is not empty, only this user would be updated'), + '#title' => t('User name or ID'), + '#description' => t('If this field is not empty, only this user would be updated.'), + '#disabled' => array('users_list', array('!value' => 0)), ); $form['submit'] = array( '#type' => 'submit', - '#value' => t('test request') + '#value' => t('Submit request') ); + foreach ($form as $item => $definition) { + // Transform custom #disabled property into #states. + if (isset($definition['#disabled']) && is_array($definition['#disabled'])) { + list($field, $conditions) = $definition['#disabled']; + unset($form[$item]['#disabled']); + + $form[$item]['#states']['disabled'][":input[name*=$field]"] = $conditions; + } + } + return $form; } /** * Submit callback for sync user list form */ -function ppgetstat_sync_user_list_submit($form, $form_state) { - $direct_user = $form_state['values']['direct_user']; - if (!empty($direct_user)) { - $r = _ppgetstat_api_call( - 'https://www.drupal.org/', - 'api-d7/user.json', - array( - 'uid' => $direct_user - ) - ); - $users_data = array( - $r->list[0]->uid => $r->list[0] - ); - - _ppgetstat_process_dorg_users($users_data, array($r->list[0]->uid)); - drupal_set_message('User has been updated'); +function ppgetstat_sync_user_list_submit(array $form, array &$form_state) { + if (!empty($form_state['values']['direct_user'])) { + $user_data = ppgetstat_get_dorg_user($form_state['values']['direct_user']); + + if (FALSE === $user_data) { + drupal_set_message(t('User cannot be found by "@argument" as an ID or name.', array( + '@argument' => $form_state['values']['direct_user'], + )), 'warning'); + } + else { + _ppgetstat_process_dorg_users(array($user_data)); + drupal_set_message(t('User "@name" has been updated.', array( + '@name' => $user_data->name, + ))); + } + } + elseif (!empty($form_state['values']['users_list'])) { + $term = taxonomy_term_load($form_state['values']['users_list']); + $field_query = field_get_items('taxonomy_term', $term, 'field_query'); + $variables = array('limit' => 7); - } else { - $users_list_tid = $form_state['values']['users_list']; - $term = taxonomy_term_load($users_list_tid); - if (!empty($term)) { - $field_query = field_get_items('taxonomy_term', $term, 'field_query'); - $variables = array('limit' => 7); + if (FALSE !== $field_query) { foreach ($field_query as $item) { $variables[$item['first']] = $item['second']; } + } - $r = _ppgetstat_api_call( - 'https://www.drupal.org/', - 'api-d7/user.json', - $variables - ); - - if (!empty($r) && (!empty($r->last))) { - $last_url = parse_url($r->last); - parse_str($last_url['query'], $params); - $page = $params['page']; - for ($i = ($page - 1); $i >= 0; $i--) { - $operations[] = array( - '_ppgetstat_sync_user_list_step', - array($variables, $i), - ); - } - $batch = array( - 'operations' => $operations, - 'finished' => '_ppgetstat_batch_finished', + $r = _ppgetstat_api_call('api-d7/user.json', $variables); + + if (!empty($r->last)) { + $last_url = parse_url($r->last); + parse_str($last_url['query'], $params); + + for ($i = $params['page'] - 1; $i >= 0; $i--) { + $operations[] = array('_ppgetstat_sync_user_list_step', array($variables, $i)); + } + + if (isset($operations)) { + batch_set(array( 'title' => t('Processing import...'), + 'finished' => '_ppgetstat_batch_finished', + 'operations' => $operations, 'init_message' => t('Batch is starting.'), - ); - batch_set($batch); + )); } else { - drupal_set_message('Error with getting initial data', 'error'); + drupal_set_message(t('Nothing to process.'), 'warning'); } - } else { - drupal_set_message('Import process could not be started'); + drupal_set_message(t('Error with getting initial data.'), 'error'); } } + else { + drupal_set_message(t('You are not selected any import criteria.'), 'warning'); + } } From e1abfc530be540ab21f5c062cc7bb5e4b1869d69 Mon Sep 17 00:00:00 2001 From: Sergii Bondarenko Date: Thu, 4 Feb 2016 14:20:00 +0200 Subject: [PATCH 7/7] Fixed an issue with division by zero --- .../content_types/community_quality.inc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docroot/sites/all/modules/custom/pp_frontpage/plugins/content_types/community_quality.inc b/docroot/sites/all/modules/custom/pp_frontpage/plugins/content_types/community_quality.inc index 70c1f48..64b0dbc 100644 --- a/docroot/sites/all/modules/custom/pp_frontpage/plugins/content_types/community_quality.inc +++ b/docroot/sites/all/modules/custom/pp_frontpage/plugins/content_types/community_quality.inc @@ -16,7 +16,7 @@ function pp_frontpage_community_quality_content_type_render($subtype, $conf, $pa //@TODO add user list filtering for future. $all_user_count = db_select('node', 'n') ->fields('n', array('nid')) - ->condition('n.type', 'user') + ->condition('type', PPGETSTAT_USER_NODE_TYPE) ->countQuery() ->execute() ->fetchField(); @@ -37,13 +37,15 @@ function pp_frontpage_community_quality_content_type_render($subtype, $conf, $pa ->execute() ->fetchField(); - $block->content = '
-
    -
  • '.t('Total count').' : ' . $all_user_count . '
  • -
  • '.t('Active committers').' : ' . $active_by_commits_count . ' (' . number_format($active_by_commits_count/$all_user_count, 2)*100 . ')%
  • -
  • '.t('Active commenters').' : ' . $active_by_comments_count . ' (' . number_format($active_by_comments_count/$all_user_count, 2)*100 . ')%
  • -
-
'; + if ($all_user_count > 0) { + $block->content = '
+
    +
  • '.t('Total count').' : ' . $all_user_count . '
  • +
  • '.t('Active committers').' : ' . $active_by_commits_count . ' (' . number_format($active_by_commits_count/$all_user_count, 2)*100 . ')%
  • +
  • '.t('Active commenters').' : ' . $active_by_comments_count . ' (' . number_format($active_by_comments_count/$all_user_count, 2)*100 . ')%
  • +
+
'; + } return $block; }