Passed
Push — master ( 4ffe03...5d5113 )
by Roberto
02:32
created

find_users()   C

Complexity

Conditions 9
Paths 20

Size

Total Lines 69
Code Lines 37

Duplication

Lines 14
Ratio 20.29 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 37
nc 20
nop 1
dl 14
loc 69
rs 6.2192
c 3
b 0
f 0

How to fix   Long Method   

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
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * Relationship assignment candidate selector definition
19
 *
20
 * @package local_relationship
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
defined('MOODLE_INTERNAL') || die();
24
25
/**
26
 * relationship assignment candidates
27
 */
28
class local_relationship_candidate_selector extends user_selector_base {
29
    protected $relationshipgroup;
30
31
    public function __construct($name, $options) {
32
        $this->relationshipgroup = $options['relationshipgroup'];
33
        parent::__construct($name, $options);
34
    }
35
36
    public function find_users($search) {
37
        global $DB;
38
39
        list($usercondition, $params) = users_search_sql($search, 'u', $this->searchanywhere);
40
41 View Code Duplication
        if(!empty($this->validatinguserids)) {
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...
42
            list($usertest, $userparams) = $DB->get_in_or_equal($this->validatinguserids, SQL_PARAMS_NAMED, 'val');
43
            $usercondition .= " AND u.id*1000000+rc.id " . $usertest;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
            $params = array_merge($params, $userparams);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
45
        }
46
47
        $params['relationshipid'] = $this->relationshipgroup->relationshipid;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
        $params['relationshipgroupid'] = $this->relationshipgroup->id;
49
50
        $countfields  = 'SELECT COUNT(DISTINCT u.id)';
51
        $selectfields = "SELECT DISTINCT u.id*1000000+rc.id as id, rc.id AS relationshipcohortid, rc.roleid, rc.allowdupsingroups,
52
                                u.id AS userid, CONCAT(u.firstname, ' ', u.lastname) AS fullname,
53
                                CASE WHEN rm.id IS NULL THEN 0 ELSE 1 END AS in_group";
54
55
        $from = "FROM {relationship_cohorts} rc
56
                 JOIN {cohort_members} cm ON (cm.cohortid = rc.cohortid)
57
                 JOIN {user} u ON (u.id = cm.userid)
58
            LEFT JOIN {relationship_members} rm ON (rm.relationshipcohortid = rc.id AND rm.userid = cm.userid)
59
                WHERE rc.relationshipid = :relationshipid
60
                  AND NOT EXISTS (SELECT 1
61
                                    FROM {relationship_members} rm
62
                                   WHERE rm.relationshipcohortid = rc.id
63
                                     AND rm.userid = cm.userid
64
                                     AND rm.relationshipgroupid = :relationshipgroupid)
65
                  AND (rc.allowdupsingroups = 1
66
                       OR (rc.allowdupsingroups = 0 AND rm.id IS NULL))
67
                  AND {$usercondition}";
68
69
        $orderby= "ORDER BY roleid, in_group, fullname";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
70
71 View Code Duplication
        if (!$this->is_validating()) {
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...
72
            $sql = $countfields . "\n" . $from;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
            $count = $DB->count_records_sql($sql, $params);
74
            if ($count > $this->maxusersperpage) {
75
                return $this->too_many_results($search, $count);
76
            } else if ($count == 0) {
77
                return array();
78
            }
79
        }
80
81
        $sql = $selectfields . "\n" . $from .  "\n" . $orderby;
82
83
        $users = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
84
        $roleid = -1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
        $in_group = -1;
86
        $index = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
87
        foreach($DB->get_recordset_sql($sql, $params) AS $cand) {
88
            if($cand->roleid != $roleid || $cand->in_group != $in_group) {
89
                $role = $DB->get_record('role', array('id'=>$cand->roleid), '*', MUST_EXIST);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
                $role_name = role_get_name($role);
91
92
                $str_alloc = $cand->in_group == 1  ? get_string('allocated', 'local_relationship') : get_string('notallocated', 'local_relationship');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
93
                $index = $role_name . $str_alloc;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
94
                $users[$index] = array();
95
96
                $roleid = $cand->roleid;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
97
                $in_group = $cand->in_group;
98
            }
99
100
            $users[$index][$cand->id] = $cand;
101
        }
102
103
        return $users;
104
    }
105
106
    /**
107
     * Convert a user object to a string suitable for displaying as an option in the list box.
108
     *
109
     * @param object $user the user to display.
110
     * @return string a string representation of the user.
111
     */
112
    public function output_user($user) {
113
        return $user->fullname;
114
    }
115
116
    protected function get_options() {
117
        $options = parent::get_options();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
118
        $options['relationshipgroup'] = $this->relationshipgroup;
119
        return $options;
120
    }
121
122
}
123