Completed
Push — master ( ded118...cf7347 )
by Michael
02:48
created

statistics.php ➔ lx_Statistics()   F

Complexity

Conditions 15
Paths 2754

Size

Total Lines 109
Code Lines 79

Duplication

Lines 14
Ratio 12.84 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 79
c 1
b 0
f 0
nc 2754
nop 0
dl 14
loc 109
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 42 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * $Id: statistics.php v 1.0 8 JUN 2011 Yerres Exp $
4
 * Module: Lexikon - glossary module
5
 * Version: v 1.00
6
 * Release Date: 18 Dec 2011
7
 * Author: Yerres
8
 * adapted from news
9
 * Licence: GNU
10
 */
11
12
include( "admin_header.php" );
13
$myts =& MyTextSanitizer::getInstance();
14
if (!isset($op)) {
15
    $op = '';
16
}
17
18
/**
19
 * Statistics about the Glossary: Definitions, Categories, views and authors
20
 *
21
 * You can reach the statistics from the admin part of the news module by clicking on the "Statistics" tabs
22
 * The number of visible elements in each table is equal to the module's option called "perpage"
23
 * There are three kind of different statistics :
24
 * - Categories statistics
25
 *   For each Category you can see its number of definitions, the number of time each Definition was viewed,
26
 *   the number of unused i.e. offline or submitted Definitions and the number of unique authors.
27
 * - Definitions statistics
28
 *   This part consists of 2 tables :
29
 *   a) Most read definitions
30
 *      This table resumes, for all the terms in your database, the most read Definitions.
31
 *      The table contains, for each term, its Category, name, author and the number of views.
32
 *   b) Less read Definitions
33
 *      That's the opposite action of the previous table and its content is the same
34
 * - Authors statistics
35
 *   This part also consists of 2 tables
36
 *   a) Most read authors
37
 *		To create this table, the program computes the total number of reads per author and displays the most read author and the number of views
38
 *   b) Biggest contributors
39
 *      The goal of this table is to know who is creating the biggest number of terms.
40
 **/
41
42
function lx_Statistics(){
43
    global $xoopsModule, $xoopsConfig, $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
44
    xoops_cp_header();
45
    $myts =& MyTextSanitizer::getInstance();
46
   
47
//    lx_adminMenu(3, _INFO);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
    
49
    $stats = array();
0 ignored issues
show
Unused Code introduced by
$stats is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
    //$stats=lx_GetStatistics(10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
     $stats=lx_GetStatistics($xoopsModuleConfig['perpage']);
52
    $totals=array(0,0,0,0);
53
54
//   printf("<h1>%s</h1>\n",_AM_LEXIKON_STATS);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    $indexAdmin = new ModuleAdmin();
56
    echo $indexAdmin->addNavigation('statistics.php');
57
   // First part of the stats, everything about categories
58
    $termspercategory=$stats['termspercategory'];
59
    $readspercategory=$stats['readspercategory'];
60
    $offlinepercategory=$stats['offlinepercategory'];
61
    $authorspercategory=$stats['authorspercategory'];
62
    $class='';
63
64
    echo "<div style='text-align: center;'><b>" . _AM_LEXIKON_STATS0 . "</b><br />\n";
65
    echo "<table width='100%' style=\"margin-top: 6px; clear:both;\" cellspacing='2' cellpadding='3' border='0' >";
66
    echo "<tr class='bg3'><td align='center'>"._AM_LEXIKON_ENTRYCATNAME."</td><td align='center'>" . _AM_LEXIKON_TOTALENTRIES . "</td><td>" . _READS . "</td><td>" . _AM_LEXIKON_STATS6 ."</td><td>" ._AM_LEXIKON_STATS1 ."</td></tr>";
67
    
68
    foreach ( $termspercategory as $categoryID => $data ) {
69
        $url=XOOPS_URL . '/modules/' . $xoopsModule -> dirname() . '/category.php?categoryID=' . $categoryID;
70
        $views=0;
71
        if(array_key_exists($categoryID,$readspercategory)) {
72
            $views=$readspercategory[$categoryID];
73
        }
74
        $offline=0;
75
        if(array_key_exists($categoryID,$offlinepercategory)) {
76
            $offline=$offlinepercategory[$categoryID];
77
        }
78
        $authors=0;
79
        if(array_key_exists($categoryID,$authorspercategory)) {
80
            $authors=$authorspercategory[$categoryID];
81
        }
82
        $terms=$data['cpt'];
83
84
        $totals[0]+=$terms;
85
        $totals[1]+=$views;
86
        $totals[2]+=$offline;
87
        $class = ($class == 'even') ? 'odd' : 'even';
88
        printf("<tr class='".$class."'><td align='left'><a href='%s' target ='_blank'>%s</a></td><td align='right'>%u</td><td align='right'>%u</td><td align='right'>%u</td><td align='right'>%u</td></tr>\n",$url,$myts->displayTarea($data['name']),$terms,$views,$offline,$authors);
89
    }
90
    $class = ($class == 'even') ? 'odd' : 'even';
91
    printf("<tr class='".$class."'><td align='center'><b>%s</b></td><td align='right'><b>%u</b></td><td align='right'><b>%u</b></td><td align='right'><b>%u</b></td><td>&nbsp;</td>\n",_AM_LEXIKON_STATS2,$totals[0],$totals[1],$totals[2]);
92
    echo '</table></div><br /><br /><br />';
93
94
    // Second part of the stats, everything about reads
95
    // a) Most read definitions
96
    $mostreadterms=$stats['mostreadterms'];
97
    
98
    echo "<div style='text-align: center;'><b>" . _AM_LEXIKON_STATS3 . '</b><br /><br />' . _AM_LEXIKON_STATS4 . "<br />\n";
99
    echo "<table width='100%' style=\"margin-top: 6px; clear:both;\" cellspacing='2' cellpadding='3' border='0' >
100
	<tr class='bg3'><td align='center'>"._AM_LEXIKON_ENTRYCATNAME."</td><td align='center'>" . _AM_LEXIKON_ENTRYTERM . "</td><td>" . _AM_LEXIKON_AUTHOR . "</td><td>" . _READS . "</td></tr>\n";
101 View Code Duplication
    foreach ( $mostreadterms as $entryID => $data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
        $url1=XOOPS_URL . '/modules/' . $xoopsModule -> dirname() . '/category.php?categoryID=' . $data['categoryID'];
103
        $url2=XOOPS_URL . '/modules/' . $xoopsModule -> dirname() . '/entry.php?entryID=' . $entryID;
104
        $sentby = XoopsUserUtility::getUnameFromId($data['uid']);
105
        $class = ($class == 'even') ? 'odd' : 'even';
106
        printf("<tr class='".$class."'><td align='left'><a href='%s' target ='_blank'>%s</a></td><td align='left'><a href='%s' target='_blank'>%s</a></td><td>%s</td><td align='right'>%u</td></tr>\n",$url1,$myts->displayTarea($data['name']),$url2,$myts->displayTarea($data['term']),$sentby,$data['counter']);
107
    }
108
    echo '</table>';
109
110
    // b) Less read definitions
111
    $lessreadnews=$stats['lessreadterms'];
112
    echo '<br /><br />'._AM_LEXIKON_STATS5;
113
    echo "<table width='100%' style=\"margin-top: 6px; clear:both;\" cellspacing='2' cellpadding='3' border='0' >
114
	<tr class='bg3'><td align='center'>"._AM_LEXIKON_ENTRYCATNAME."</td><td align='center'>" . _AM_LEXIKON_ENTRYTERM . "</td><td>" . _AM_LEXIKON_AUTHOR . "</td><td>" . _READS . "</td></tr>\n";
115 View Code Duplication
    foreach ( $lessreadnews as $entryID => $data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
        $url1=XOOPS_URL . '/modules/' . $xoopsModule -> dirname() . '/category.php?categoryID=' . $data['categoryID'];
117
        $url2=XOOPS_URL . '/modules/' . $xoopsModule -> dirname() . '/entry.php?entryID=' . $entryID;
118
        $sentby = XoopsUserUtility::getUnameFromId($data['uid']);
119
        $class = ($class == 'even') ? 'odd' : 'even';
120
        printf("<tr class='".$class."'><td align='left'><a href='%s' target ='_blank'>%s</a></td><td align='left'><a href='%s' target='_blank'>%s</a></td><td>%s</td><td align='right'>%u</td></tr>\n",$url1,$myts->displayTarea($data['name']),$url2,$myts->displayTarea($data['term']),$sentby,$data['counter']);
121
    }
122
    echo '</table>';
123
    echo '<br /><br /><br />';
124
125
    // Last part of the stats, everything about authors
126
    // a) Most read authors
127
    $mostreadauthors=$stats['mostreadauthors'];
128
    echo "<div style='text-align: center;'><b>" . _AM_LEXIKON_STATS10 . '</b><br /><br />' . _AM_LEXIKON_STATS7 . "<br />\n";
129
    echo "<table width='100%' style=\"margin-top: 6px; clear:both;\" cellspacing='2' cellpadding='3' border='0' >
130
	<tr class='bg3'><td>"._AM_LEXIKON_AUTHOR.'</td><td>' . _READS . "</td></tr>\n";
131
    foreach ( $mostreadauthors as $uid => $reads) {
132
        $sentby = XoopsUserUtility::getUnameFromId($uid);
133
        $class = ($class == 'even') ? 'odd' : 'even';
134
        printf("<tr class='".$class."'><td align='left'>%s</td><td align='right'>%u</td></tr>\n",$sentby,$reads);
135
    }
136
    echo '</table>';
137
138
    // c) Biggest contributors
139
    $biggestcontributors=$stats['biggestcontributors'];
140
    echo '<br /><br />'._AM_LEXIKON_STATS9;
141
    echo "<table width='100%' style=\"margin-top: 6px; clear:both;\" cellspacing='2' cellpadding='3' border='0' >
142
	<tr class='bg3'><td>"._AM_LEXIKON_AUTHOR."</td><td>" . _AM_LEXIKON_STATS11 . "</td></tr>\n";
143
    foreach ( $biggestcontributors as $uid => $count) {
144
        $url=XOOPS_URL . '/userinfo.php?uid=' . $uid;
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
        $sentby = XoopsUserUtility::getUnameFromId($uid);
146
        $class = ($class == 'even') ? 'odd' : 'even';
147
        printf("<tr class='".$class."'><td align='left'>%s</td><td align='right'>%u</td></tr>\n",$sentby,$count);
148
    }
149
    echo '</table></div><br />';
150
}
151
152
/* -- Available operations -- */
153
$op = isset($_GET['op']) ? $_GET['op'] : (isset($_POST['op']) ? $_POST['op'] : '');
154
switch ( $op ) {
155
156
default:
157
    lx_Statistics();
158
    break;
159
}
160
161
xoops_cp_footer();
162