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

local_relationship_observer::cohort_removed()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 2
b 0
f 0
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
 * Event handler definition
19
 *
20
 * @package local_relationship
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
24
defined('MOODLE_INTERNAL') || die();
25
26
require_once($CFG->dirroot.'/local/relationship/lib.php');
27
28
/**
29
 * Event handler for relationship local plugin.
30
 *
31
 * We try to keep everything in sync via listening to events,
32
 * it may fail sometimes, so we always do a full sync in cron too.
33
 */
34
class local_relationship_observer
35
{
36
37
    /**
38
     * Event processor - cohort removed.
39
     * @param \core\event\cohort_deleted $event
40
     * @return bool
41
     */
42
    public static function cohort_removed(\core\event\cohort_deleted $event) {
43
        global $DB;
44
45
        $relationshipcohorts = $DB->get_records('relationship_cohorts', array('cohortid'=>$event->objectid));
46
        if (!empty($relationshipcohorts)) {
47
            foreach ($relationshipcohorts as $relationshipcohort) {
48
                relationship_delete_cohort($relationshipcohort);
49
            }
50
        }
51
52
        return true;
53
    }
54
55
    /**
56
     * Event processor - cohort member added.
57
     * @param \core\event\cohort_member_added $event
58
     * @return bool
59
     */
60
    public static function member_added(\core\event\cohort_member_added $event)
61
    {
62
        global $DB;
63
64
        if ($rcs = $DB->get_records('relationship_cohorts', array('uniformdistribution' => 1, 'cohortid' => $event->objectid))) {
65
            $user = array($event->relateduserid);
66
            foreach ($rcs AS $rc) {
67
                relationship_uniformly_distribute_users($rc, $user);
68
            }
69
        }
70
71
        return true;
72
    }
73
74
    /**
75
     * Event processor - cohort member removed.
76
     * @param \core\event\cohort_member_removed $event
77
     * @return bool
78
     */
79
    public static function member_removed(\core\event\cohort_member_removed $event)
80
    {
81
        global $DB;
82
83
        $sql = "SELECT rm.relationshipgroupid, rm.relationshipcohortid, rm.userid
84
                  FROM {relationship_cohorts} rc
85
                  JOIN {relationship_groups} rg
86
                    ON (rg.relationshipid = rc.relationshipid)
87
                  JOIN {relationship_members} rm
88
                    ON (rm.relationshipgroupid = rg.id AND rm.relationshipcohortid = rc.id)
89
                 WHERE rc.cohortid = :cohortid
90
                   AND rm.userid = :userid";
91
92
        $params = array('cohortid' => $event->objectid, 'userid' => $event->relateduserid);
93
        $rs = $DB->get_records_sql($sql, $params);
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...
94
        foreach ($rs AS $rec) {
95
            relationship_remove_member($rec->relationshipgroupid, $rec->relationshipcohortid, $rec->userid);
96
        }
97
98
        return true;
99
    }
100
}
101