Completed
Push — develop ( 879d74...52dce9 )
by Seth
04:03
created

Toolbox::filterEvent()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 13
nop 2
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
1
<?php
2
namespace smtech\CanvasICSSync;
3
4
use smtech\LTI\Configuration\Option;
5
use Battis\HierarchicalSimpleCache;
6
7
/**
8
 * St. Marks Reflexive Canvas LTI Example toolbox
9
 *
10
 * Adds some common, useful methods to the St. Mark's-styled
11
 * ReflexiveCanvasLTI Toolbox
12
 *
13
 * @author  Seth Battis <[email protected]>
14
 * @version v1.2
15
 */
16
class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox
17
{
18
    /**
19
     * Configure course and account navigation placements
20
     *
21
     * @return Generator
22
     */
23
    public function getGenerator()
24
    {
25
        parent::getGenerator();
26
        $this->generator->setOptionProperty(
27
            Option::COURSE_NAVIGATION(),
28
            'visibility',
29
            'admins'
30
        );
31
        return $this->generator;
32
    }
33
34
    /**
35
     * Load the app schema into the database
36
     *
37
     * @return void
38
     */
39
    public function loadSchema()
40
    {
41
        /* ...so that we can find the LTI_Tool_Provider database schema (oy!) */
42
        foreach (explode(';', file_get_contents(dirname(__DIR__) . '/schema.sql')) as $query) {
43
            if (!empty(trim($query))) {
44
                /*
45
                 * TODO should there be some sort of testing or logging here?
46
                 *      If _some_ tables are present, that will trigger
47
                 *      reloading all tables, which will generate ignorable
48
                 *      errors.
49
                 */
50
                $this->mysql_query($query);
51
            }
52
        }
53
        $this->log('Application database schema loaded.');
54
    }
55
56
    /**
57
     * Check to see if a URL exists
58
     **/
59
    public function urlExists($url)
60
    {
61
        $handle = fopen($url, 'r');
62
        return $handle !== false;
63
    }
64
65
    /**
66
     * compute the calendar context for the canvas object based on its URL
67
     **/
68
    public function getCanvasContext($canvasUrl)
69
    {
70
        /*
71
         * TODO: accept calendar2?contexts links too (they would be an intuitively
72
         * obvious link to use, after all)
73
         */
74
        /*
75
         * FIXME: users aren't working
76
         */
77
        /*
78
         * TODO: it would probably be better to look up users by email address than
79
         * URL
80
         */
81
        /* get the context (user, course or group) for the canvas URL */
82
        $canvasContext = array();
83
        if (preg_match(
84
            '%(https?://)?(' .
85
            parse_url($this->config('TOOL_CANVAS_API')['url'], PHP_URL_HOST) .
86
            '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%',
87
            $canvasUrl,
88
            $matches
89
        )) {
90
            $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953
91
92
            // course or account groups
93
            if (isset($matches[9]) || isset($matches[11])) {
94
                $canvasContext['context'] = 'group'; // used to for context_code in events
95
                $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]);
96
97
                /* used once to look up the object to be sure it really exists */
98
                $canvasContext['verification_url'] = "groups/{$canvasContext['id']}";
99
100
            // courses
101
            } elseif (isset($matches[7])) {
102
                $canvasContext['context'] = 'course';
103
                $canvasContext['id'] = $matches[7];
104
                $canvasContext['verification_url'] = "courses/{$canvasContext['id']}";
105
106
            // users
107
            } elseif (isset($matches[5])) {
108
                $canvasContext['context'] = 'user';
109
                $canvasContext['id'] = $matches[5];
110
                $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile";
111
112
            // we're somewhere where we don't know where we are
113
            } else {
114
                return false;
115
            }
116
            return $canvasContext;
117
        }
118
        return false;
119
    }
120
121
    /**
122
     * Filter and clean event data before posting to Canvas
123
     *
124
     * This must happen AFTER the event hash has been calculated!
125
     **/
126
    public function filterEvent($event, $calendarCache)
127
    {
128
         return (
129
            (
130
                // TODO actual multi-day events would be nice
131
                // only include first day of multi-day events
132
                $event->getProperty('X-OCCURENCE') == false ||
133
                preg_match('/^day 1 of \d+$/i', $event->getProperty('X-OCCURENCE')[1])
134
            ) &&
135
            (
136
                // include this event if filtering is off...
137
                 $calendarCache['enable_regexp_filter'] == false ||
138
                 (
139
                    (
140
                        ( // if filtering is on, and there's an include pattern test that pattern...
141
                            !empty($calendarCache['include_regexp']) &&
142
                            preg_match("%{$calendarCache['include_regexp']}%", $event->getProperty('SUMMARY'))
143
                        )
144
                    ) &&
145
                    !( // if there is an exclude pattern, make sure that this event is NOT excluded
146
                        !empty($calendarCache['exclude_regexp']) &&
147
                        preg_match("%{$calendarCache['exclude_regexp']}%", $event->getProperty('SUMMARY'))
148
                    )
149
                )
150
            )
151
        );
152
    }
153
}
154