Completed
Push — develop ( 52c290...19c595 )
by Seth
02:32
created
src/SyncIntoCanvas/Event.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -4,40 +4,40 @@
 block discarded – undo
4 4
 
5 5
 class Event
6 6
 {
7
-    const FIELD_MAP = [
8
-        'calendar_event[title]' => 'SUMMARY',
9
-        'calendar_event[description]' => 'DESCRIPTION',
10
-        'calendar_event[start_at]' => [
11
-            0 => 'X-CURRENT-DTSTART',
12
-            1 => 'DTSTART'
13
-        ],
14
-        'calendar_event[end_at]' => [
15
-            0 => 'X-CURRENT-DTEND',
16
-            1 => 'DTEND'
17
-        ],
18
-        'calendar_event[location_name]' => 'LOCATION'
19
-    ];
7
+	const FIELD_MAP = [
8
+		'calendar_event[title]' => 'SUMMARY',
9
+		'calendar_event[description]' => 'DESCRIPTION',
10
+		'calendar_event[start_at]' => [
11
+			0 => 'X-CURRENT-DTSTART',
12
+			1 => 'DTSTART'
13
+		],
14
+		'calendar_event[end_at]' => [
15
+			0 => 'X-CURRENT-DTEND',
16
+			1 => 'DTEND'
17
+		],
18
+		'calendar_event[location_name]' => 'LOCATION'
19
+	];
20 20
 
21
-    /**
22
-     * Generate a hash of this version of an event to cache in the database
23
-     **/
24
-    public function getEventHash($event)
25
-    {
26
-        $blob = '';
27
-        foreach (static::$FIELD_MAP as $field) {
28
-            if (is_array($field)) {
29
-                foreach ($field as $option) {
30
-                    if (!empty($property = $event->getProperty($option))) {
31
-                        $blob .= serialize($property);
32
-                        break;
33
-                    }
34
-                }
35
-            } else {
36
-                if (!empty($property = $event->getProperty($field))) {
37
-                    $blob .= serialize($property);
38
-                }
39
-            }
40
-        }
41
-        return md5($blob);
42
-    }
21
+	/**
22
+	 * Generate a hash of this version of an event to cache in the database
23
+	 **/
24
+	public function getEventHash($event)
25
+	{
26
+		$blob = '';
27
+		foreach (static::$FIELD_MAP as $field) {
28
+			if (is_array($field)) {
29
+				foreach ($field as $option) {
30
+					if (!empty($property = $event->getProperty($option))) {
31
+						$blob .= serialize($property);
32
+						break;
33
+					}
34
+				}
35
+			} else {
36
+				if (!empty($property = $event->getProperty($field))) {
37
+					$blob .= serialize($property);
38
+				}
39
+			}
40
+		}
41
+		return md5($blob);
42
+	}
43 43
 }
Please login to merge, or discard this patch.
src/SyncIntoCanvas/Schedule.php 2 patches
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -4,23 +4,23 @@
 block discarded – undo
4 4
 
5 5
 class Schedule
6 6
 {
7
-    protected $syncTimestamp;
7
+	protected $syncTimestamp;
8 8
 
9
-    /**
10
-     * Generate a unique identifier for this synchronization pass
11
-     **/
12
-    public function getSyncTimestamp()
13
-    {
14
-        if (empty($this->syncTimestamp)) {
15
-            $timestamp = new DateTime();
16
-            $this->syncTimestamp =
17
-                $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) .
18
-                Constants::SEPARATOR . md5(
19
-                    (php_sapi_name() == 'cli' ?
20
-                        'cli' : $_SERVER['REMOTE_ADDR']
21
-                    ) . time()
22
-                );
23
-        }
24
-        return $this->syncTimestamp;
25
-    }
9
+	/**
10
+	 * Generate a unique identifier for this synchronization pass
11
+	 **/
12
+	public function getSyncTimestamp()
13
+	{
14
+		if (empty($this->syncTimestamp)) {
15
+			$timestamp = new DateTime();
16
+			$this->syncTimestamp =
17
+				$timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) .
18
+				Constants::SEPARATOR . md5(
19
+					(php_sapi_name() == 'cli' ?
20
+						'cli' : $_SERVER['REMOTE_ADDR']
21
+					) . time()
22
+				);
23
+		}
24
+		return $this->syncTimestamp;
25
+	}
26 26
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -14,11 +14,11 @@
 block discarded – undo
14 14
         if (empty($this->syncTimestamp)) {
15 15
             $timestamp = new DateTime();
16 16
             $this->syncTimestamp =
17
-                $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) .
18
-                Constants::SEPARATOR . md5(
17
+                $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT).
18
+                Constants::SEPARATOR.md5(
19 19
                     (php_sapi_name() == 'cli' ?
20 20
                         'cli' : $_SERVER['REMOTE_ADDR']
21
-                    ) . time()
21
+                    ).time()
22 22
                 );
23 23
         }
24 24
         return $this->syncTimestamp;
Please login to merge, or discard this patch.
src/SyncIntoCanvas/Syncable.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -12,86 +12,86 @@
 block discarded – undo
12 12
  */
13 13
 abstract class Syncable
14 14
 {
15
-    /**
16
-     * MySQL connection
17
-     *
18
-     * @var mysqli|null
19
-     */
20
-    protected static $mysql;
15
+	/**
16
+	 * MySQL connection
17
+	 *
18
+	 * @var mysqli|null
19
+	 */
20
+	protected static $mysql;
21 21
 
22
-    /**
23
-     * Canvas API connection
24
-     *
25
-     * @var CanvasPest|null
26
-     */
27
-    protected static $api;
22
+	/**
23
+	 * Canvas API connection
24
+	 *
25
+	 * @var CanvasPest|null
26
+	 */
27
+	protected static $api;
28 28
 
29
-    /**
30
-     * Update the MySQL connection
31
-     *
32
-     * @param mysqli $mysql
33
-     * @throws Exception If `$mysql` is null
34
-     */
35
-    public static function setMySql(mysqli $mysql)
36
-    {
37
-        if (empty($mysql)) {
38
-            throw new Exception(
39
-                'A non-null MySQL connection is required'
40
-            );
41
-        } else {
42
-            static::$mysql = $mysql;
43
-        }
44
-    }
29
+	/**
30
+	 * Update the MySQL connection
31
+	 *
32
+	 * @param mysqli $mysql
33
+	 * @throws Exception If `$mysql` is null
34
+	 */
35
+	public static function setMySql(mysqli $mysql)
36
+	{
37
+		if (empty($mysql)) {
38
+			throw new Exception(
39
+				'A non-null MySQL connection is required'
40
+			);
41
+		} else {
42
+			static::$mysql = $mysql;
43
+		}
44
+	}
45 45
 
46
-    /**
47
-     * Get the MySQL connection
48
-     *
49
-     * @return mysqli
50
-     */
51
-    public static function getMySql()
52
-    {
53
-        return static::$mysql;
54
-    }
46
+	/**
47
+	 * Get the MySQL connection
48
+	 *
49
+	 * @return mysqli
50
+	 */
51
+	public static function getMySql()
52
+	{
53
+		return static::$mysql;
54
+	}
55 55
 
56
-    /**
57
-     * Update the API connection
58
-     *
59
-     * @param CanvasPest $api
60
-     * @throws Exception if `$api` is null
61
-     */
62
-    public static function setApi(CanvasPest $api)
63
-    {
64
-        if (empty($api)) {
65
-            throw new Exception(
66
-                'A non-null API connection is required'
67
-            );
68
-        } else {
69
-            static::$api = $api;
70
-        }
71
-    }
56
+	/**
57
+	 * Update the API connection
58
+	 *
59
+	 * @param CanvasPest $api
60
+	 * @throws Exception if `$api` is null
61
+	 */
62
+	public static function setApi(CanvasPest $api)
63
+	{
64
+		if (empty($api)) {
65
+			throw new Exception(
66
+				'A non-null API connection is required'
67
+			);
68
+		} else {
69
+			static::$api = $api;
70
+		}
71
+	}
72 72
 
73
-    /**
74
-     * Get the API connection
75
-     *
76
-     * @return CanvasPest|null
77
-     */
78
-    public static function getApi()
79
-    {
80
-        return static::$api;
81
-    }
73
+	/**
74
+	 * Get the API connection
75
+	 *
76
+	 * @return CanvasPest|null
77
+	 */
78
+	public static function getApi()
79
+	{
80
+		return static::$api;
81
+	}
82 82
 
83
-    /**
84
-     * Save the syncable object to the MySQL database
85
-     *
86
-     * @return [type] [description]
87
-     */
88
-    abstract public function save();
83
+	/**
84
+	 * Save the syncable object to the MySQL database
85
+	 *
86
+	 * @return [type] [description]
87
+	 */
88
+	abstract public function save();
89 89
 
90
-    /**
91
-     * Load a syncable object from the MySQL database
92
-     *
93
-     * @param int $id
94
-     * @return Syncable
95
-     */
96
-    abstract public static function load($id);
90
+	/**
91
+	 * Load a syncable object from the MySQL database
92
+	 *
93
+	 * @param int $id
94
+	 * @return Syncable
95
+	 */
96
+	abstract public static function load($id);
97 97
 }
Please login to merge, or discard this patch.
src/SyncIntoCanvas/CalendarContext.php 2 patches
Indentation   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -4,107 +4,107 @@
 block discarded – undo
4 4
 
5 5
 class CalendarContext
6 6
 {
7
-    /**
8
-     * The canonical URL for this context in Canvas
9
-     * @var string
10
-     */
11
-    protected $canonicalUrl;
7
+	/**
8
+	 * The canonical URL for this context in Canvas
9
+	 * @var string
10
+	 */
11
+	protected $canonicalUrl;
12 12
 
13
-    /**
14
-     * The context for this calendar in Canvas (user, group, course)
15
-     * @var CanvasContext
16
-     */
17
-    protected $context;
13
+	/**
14
+	 * The context for this calendar in Canvas (user, group, course)
15
+	 * @var CanvasContext
16
+	 */
17
+	protected $context;
18 18
 
19
-    /**
20
-     * Unique ID for this Canvas context
21
-     * @var int
22
-     */
23
-    protected $id;
19
+	/**
20
+	 * Unique ID for this Canvas context
21
+	 * @var int
22
+	 */
23
+	protected $id;
24 24
 
25
-    /**
26
-     * URL to verify this context against API
27
-     * @var string
28
-     */
29
-    protected $verificationUrl;
25
+	/**
26
+	 * URL to verify this context against API
27
+	 * @var string
28
+	 */
29
+	protected $verificationUrl;
30 30
 
31
-    /**
32
-     * Compute the calendar context for the canvas object based on its URL
33
-     *
34
-     * @param string $canvasUrl URL to the context for a calendar on this
35
-     *     Canvas instance
36
-     * @throws Exception If `$canvasInstance` is not a URL on this Canvas
37
-     *     instance
38
-     * @throws Exception if `$canvasInstance` is not a URL to a recognizable
39
-     *     calendar context
40
-     */
41
-    public function __construct($canvasUrl)
42
-    {
43
-        /*
31
+	/**
32
+	 * Compute the calendar context for the canvas object based on its URL
33
+	 *
34
+	 * @param string $canvasUrl URL to the context for a calendar on this
35
+	 *     Canvas instance
36
+	 * @throws Exception If `$canvasInstance` is not a URL on this Canvas
37
+	 *     instance
38
+	 * @throws Exception if `$canvasInstance` is not a URL to a recognizable
39
+	 *     calendar context
40
+	 */
41
+	public function __construct($canvasUrl)
42
+	{
43
+		/*
44 44
          * TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all)
45 45
          */
46
-        /*
46
+		/*
47 47
          * FIXME: users aren't working
48 48
          */
49
-        /*
49
+		/*
50 50
          * TODO: it would probably be better to look up users by email address than URL
51 51
          */
52
-        /* get the context (user, course or group) for the canvas URL */
53
-        if (preg_match('%(https?://)?(' . parse_url($_SESSION[Constants::CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
54
-            $this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953
52
+		/* get the context (user, course or group) for the canvas URL */
53
+		if (preg_match('%(https?://)?(' . parse_url($_SESSION[Constants::CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
54
+			$this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953
55 55
 
56
-            // course or account groups
57
-            if (isset($match[9]) || isset($match[11])) {
58
-                $this->context = CanvasContext::GROUP(); // used for context_code in events
59
-                $this->id = ($match[9] > $match[11] ? $match[9] : $match[11]);
60
-                $this->verificationUrl = "groups/{$this->id}"; // used once to look up the object to be sure it really exists
56
+			// course or account groups
57
+			if (isset($match[9]) || isset($match[11])) {
58
+				$this->context = CanvasContext::GROUP(); // used for context_code in events
59
+				$this->id = ($match[9] > $match[11] ? $match[9] : $match[11]);
60
+				$this->verificationUrl = "groups/{$this->id}"; // used once to look up the object to be sure it really exists
61 61
 
62
-            // courses
63
-            } elseif (isset($match[7])) {
64
-                $this->context = CanvasContext::COURSE();
65
-                $this->id = $match[7];
66
-                $this->verificationUrl = "courses/{$this->id}";
62
+			// courses
63
+			} elseif (isset($match[7])) {
64
+				$this->context = CanvasContext::COURSE();
65
+				$this->id = $match[7];
66
+				$this->verificationUrl = "courses/{$this->id}";
67 67
 
68
-            // users
69
-            } elseif (isset($match[5])) {
70
-                $this->context = CanvasContext::USER();
71
-                $this->id = $match[5];
72
-                $this->verificationUrl = "users/{$this->id}/profile";
68
+			// users
69
+			} elseif (isset($match[5])) {
70
+				$this->context = CanvasContext::USER();
71
+				$this->id = $match[5];
72
+				$this->verificationUrl = "users/{$this->id}/profile";
73 73
 
74
-            // we're somewhere where we don't know where we are
75
-            } else {
76
-                throw new Exception(
77
-                    "'$canvasUrl' is not a recognizable calendar context"
78
-                );
79
-            }
80
-        }
81
-        throw new Exception(
82
-            "'$canvasUrl' is not recognized as a URL to a calendar context on this Canvas instance"
83
-        );
84
-    }
74
+			// we're somewhere where we don't know where we are
75
+			} else {
76
+				throw new Exception(
77
+					"'$canvasUrl' is not a recognizable calendar context"
78
+				);
79
+			}
80
+		}
81
+		throw new Exception(
82
+			"'$canvasUrl' is not recognized as a URL to a calendar context on this Canvas instance"
83
+		);
84
+	}
85 85
 
86
-    public function getCanonicalUrl()
87
-    {
88
-        return $this->canonicalUrl;
89
-    }
86
+	public function getCanonicalUrl()
87
+	{
88
+		return $this->canonicalUrl;
89
+	}
90 90
 
91
-    public function getContext()
92
-    {
93
-        return $this->context;
94
-    }
91
+	public function getContext()
92
+	{
93
+		return $this->context;
94
+	}
95 95
 
96
-    public function getId()
97
-    {
98
-        return $this->id;
99
-    }
96
+	public function getId()
97
+	{
98
+		return $this->id;
99
+	}
100 100
 
101
-    public function getVerificationUrl()
102
-    {
103
-        return $this->verificationUrl;
104
-    }
101
+	public function getVerificationUrl()
102
+	{
103
+		return $this->verificationUrl;
104
+	}
105 105
 
106
-    public function __toString()
107
-    {
108
-        return $this->getCanonicalUrl();
109
-    }
106
+	public function __toString()
107
+	{
108
+		return $this->getCanonicalUrl();
109
+	}
110 110
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -50,7 +50,7 @@
 block discarded – undo
50 50
          * TODO: it would probably be better to look up users by email address than URL
51 51
          */
52 52
         /* get the context (user, course or group) for the canvas URL */
53
-        if (preg_match('%(https?://)?(' . parse_url($_SESSION[Constants::CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
53
+        if (preg_match('%(https?://)?('.parse_url($_SESSION[Constants::CANVAS_INSTANCE_URL], PHP_URL_HOST).'/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
54 54
             $this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953
55 55
 
56 56
             // course or account groups
Please login to merge, or discard this patch.
src/SyncIntoCanvas/CanvasContext.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@
 block discarded – undo
6 6
 
7 7
 class CanvasContext extends Enum
8 8
 {
9
-    const COURSE = 'course';
10
-    const USER = 'user';
11
-    const GROUP = 'group';
9
+	const COURSE = 'course';
10
+	const USER = 'user';
11
+	const GROUP = 'group';
12 12
 }
Please login to merge, or discard this patch.
src/SyncIntoCanvas/Calendar.php 2 patches
Indentation   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -4,220 +4,220 @@
 block discarded – undo
4 4
 
5 5
 class Calendar
6 6
 {
7
-    /**
8
-     * Canvas calendar context
9
-     * @var CalendarContext
10
-     */
11
-    protected $context;
12
-
13
-    /**
14
-     * ICS or webcal feed URL
15
-     * @var string
16
-     */
17
-    protected $feedUrl;
18
-
19
-    /**
20
-     * Name of this calendar (extracted from feed)
21
-     * @var string
22
-     */
23
-    protected $name;
24
-
25
-    /**
26
-     * Filter for events in this calendar
27
-     * @var Filter
28
-     */
29
-    protected $filter;
30
-
31
-    /**
32
-     * Construct a Calendar object
33
-     *
34
-     * @param string $canvasUrl URL of a Canvas calendar context
35
-     * @param string $feedUrl URL of a webcal or ICS calendar feed
36
-     * @param boolean $enableFilter (Optional, default `false`)
37
-     * @param string $include (Optional) Regular expression to select events
38
-     *     for inclusion in the calendar sync
39
-     * @param string $exclude (Optional) Regular expression to select events
40
-     *     for exclusion from the calendar sync
41
-     */
42
-    public function __construct($canvasUrl, $feedUrl, $enableFilter = false, $include = null, $exclude = null)
43
-    {
44
-        $this->setContext(new CalendarContext($canvasUrl));
45
-        $this->setFeed($feedUrl);
46
-        $this->setFilter(new Filter(
47
-            $enableFilter,
48
-            $include,
49
-            $exclude
50
-        ));
51
-    }
52
-
53
-    /**
54
-     * Set the Canvas calendar context
55
-     *
56
-     * @param CalendarContext $context
57
-     * @throws Exception If `$context` is null
58
-     */
59
-    public function setContext(CalendarContext $context)
60
-    {
61
-        if (!empty($context)) {
62
-            $this->context = $context;
63
-        } else {
64
-            throw new Exception(
65
-                'Context cannot be null'
66
-            );
67
-        }
68
-    }
69
-
70
-    /**
71
-     * Get the Canvas calendar context
72
-     *
73
-     * @return CalendarContext
74
-     */
75
-    public function getContext()
76
-    {
77
-        return $this->context;
78
-    }
79
-
80
-    /**
81
-     * Set the webcal or ICS feed URl for this calendar
82
-     *
83
-     * @param string $feedUrl
84
-     * @throws Exception If `$feedUrl` is not a valid URL
85
-     */
86
-    public function setFeedUrl($feedUrl)
87
-    {
88
-        if (!empty($feedUrl)) {
89
-            /* crude test to see if the feed is a valid URL */
90
-            $handle = fopen($feedUrl, 'r');
91
-            if ($handle !== false) {
92
-                $this->feedUrl = $feedUrl;
93
-            }
94
-        }
95
-        throw new Exception(
96
-            'Feed must be a valid URL'
97
-        );
98
-    }
99
-
100
-    /**
101
-     * Get the feed URL for this calendar
102
-     *
103
-     * @return string
104
-     */
105
-    public function getFeedUrl()
106
-    {
107
-        return $this->feedUrl;
108
-    }
109
-
110
-    /**
111
-     * Set the name of the calendar
112
-     *
113
-     * @param string $name
114
-     */
115
-    public function setName($name)
116
-    {
117
-        $this->name = (string) $name;
118
-    }
119
-
120
-    /**
121
-     * Get the name of the calendar
122
-     *
123
-     * @return string
124
-     */
125
-    public function getName()
126
-    {
127
-        return $this->name;
128
-    }
129
-
130
-    /**
131
-     * Set the regular expression filter for this calendar
132
-     *
133
-     * @param Filter $filter
134
-     */
135
-    public function setFilter(Filter $filter)
136
-    {
137
-        $this->filter = $filter;
138
-    }
139
-
140
-    /**
141
-     * Get the regular expression filter for this calendar
142
-     *
143
-     * @return Filter
144
-     */
145
-    public function getFilter()
146
-    {
147
-        return $this->filter;
148
-    }
149
-
150
-    /**
151
-     * Generate a unique ID to identify this particular pairing of ICS feed and
152
-     * Canvas calendar
153
-     **/
154
-    protected function getPairingHash()
155
-    {
156
-        global $metadata;
157
-        return md5($this->getContext() . $this->getFeedUrl());
158
-    }
159
-
160
-    public function save()
161
-    {
162
-        $sql = static::getMySql();
163
-
164
-        $params = [
165
-            'id' => $sql->escape_string($this->getPairingHash()),
166
-            'name' => $sql->escape_string($this->getName()),
167
-            'canvas_url' => $sql->escape_string($this->getContext()->getCanonicalUrl()),
168
-            'ics_url' => $sql->escape_string($this->getFeedUrl()),
169
-            'enable_regex_filter' => $this->getFilter()->isEnabled(),
170
-            'include_regexp' => $sql->escape_string($this->getFilter()->getIncludeExpression()),
171
-            'exclude_regexp' => $sql->escape_string($this->getFilter()->getExcludeExpression())
172
-        ];
173
-        foreach ($params as $field => $value) {
174
-            if (empty($value)) {
175
-                $params[$field] = 'NULL';
176
-            } else {
177
-                $params[$field] = "'$value'";
178
-            }
179
-        }
180
-        $response = static::getMySql()->query("
7
+	/**
8
+	 * Canvas calendar context
9
+	 * @var CalendarContext
10
+	 */
11
+	protected $context;
12
+
13
+	/**
14
+	 * ICS or webcal feed URL
15
+	 * @var string
16
+	 */
17
+	protected $feedUrl;
18
+
19
+	/**
20
+	 * Name of this calendar (extracted from feed)
21
+	 * @var string
22
+	 */
23
+	protected $name;
24
+
25
+	/**
26
+	 * Filter for events in this calendar
27
+	 * @var Filter
28
+	 */
29
+	protected $filter;
30
+
31
+	/**
32
+	 * Construct a Calendar object
33
+	 *
34
+	 * @param string $canvasUrl URL of a Canvas calendar context
35
+	 * @param string $feedUrl URL of a webcal or ICS calendar feed
36
+	 * @param boolean $enableFilter (Optional, default `false`)
37
+	 * @param string $include (Optional) Regular expression to select events
38
+	 *     for inclusion in the calendar sync
39
+	 * @param string $exclude (Optional) Regular expression to select events
40
+	 *     for exclusion from the calendar sync
41
+	 */
42
+	public function __construct($canvasUrl, $feedUrl, $enableFilter = false, $include = null, $exclude = null)
43
+	{
44
+		$this->setContext(new CalendarContext($canvasUrl));
45
+		$this->setFeed($feedUrl);
46
+		$this->setFilter(new Filter(
47
+			$enableFilter,
48
+			$include,
49
+			$exclude
50
+		));
51
+	}
52
+
53
+	/**
54
+	 * Set the Canvas calendar context
55
+	 *
56
+	 * @param CalendarContext $context
57
+	 * @throws Exception If `$context` is null
58
+	 */
59
+	public function setContext(CalendarContext $context)
60
+	{
61
+		if (!empty($context)) {
62
+			$this->context = $context;
63
+		} else {
64
+			throw new Exception(
65
+				'Context cannot be null'
66
+			);
67
+		}
68
+	}
69
+
70
+	/**
71
+	 * Get the Canvas calendar context
72
+	 *
73
+	 * @return CalendarContext
74
+	 */
75
+	public function getContext()
76
+	{
77
+		return $this->context;
78
+	}
79
+
80
+	/**
81
+	 * Set the webcal or ICS feed URl for this calendar
82
+	 *
83
+	 * @param string $feedUrl
84
+	 * @throws Exception If `$feedUrl` is not a valid URL
85
+	 */
86
+	public function setFeedUrl($feedUrl)
87
+	{
88
+		if (!empty($feedUrl)) {
89
+			/* crude test to see if the feed is a valid URL */
90
+			$handle = fopen($feedUrl, 'r');
91
+			if ($handle !== false) {
92
+				$this->feedUrl = $feedUrl;
93
+			}
94
+		}
95
+		throw new Exception(
96
+			'Feed must be a valid URL'
97
+		);
98
+	}
99
+
100
+	/**
101
+	 * Get the feed URL for this calendar
102
+	 *
103
+	 * @return string
104
+	 */
105
+	public function getFeedUrl()
106
+	{
107
+		return $this->feedUrl;
108
+	}
109
+
110
+	/**
111
+	 * Set the name of the calendar
112
+	 *
113
+	 * @param string $name
114
+	 */
115
+	public function setName($name)
116
+	{
117
+		$this->name = (string) $name;
118
+	}
119
+
120
+	/**
121
+	 * Get the name of the calendar
122
+	 *
123
+	 * @return string
124
+	 */
125
+	public function getName()
126
+	{
127
+		return $this->name;
128
+	}
129
+
130
+	/**
131
+	 * Set the regular expression filter for this calendar
132
+	 *
133
+	 * @param Filter $filter
134
+	 */
135
+	public function setFilter(Filter $filter)
136
+	{
137
+		$this->filter = $filter;
138
+	}
139
+
140
+	/**
141
+	 * Get the regular expression filter for this calendar
142
+	 *
143
+	 * @return Filter
144
+	 */
145
+	public function getFilter()
146
+	{
147
+		return $this->filter;
148
+	}
149
+
150
+	/**
151
+	 * Generate a unique ID to identify this particular pairing of ICS feed and
152
+	 * Canvas calendar
153
+	 **/
154
+	protected function getPairingHash()
155
+	{
156
+		global $metadata;
157
+		return md5($this->getContext() . $this->getFeedUrl());
158
+	}
159
+
160
+	public function save()
161
+	{
162
+		$sql = static::getMySql();
163
+
164
+		$params = [
165
+			'id' => $sql->escape_string($this->getPairingHash()),
166
+			'name' => $sql->escape_string($this->getName()),
167
+			'canvas_url' => $sql->escape_string($this->getContext()->getCanonicalUrl()),
168
+			'ics_url' => $sql->escape_string($this->getFeedUrl()),
169
+			'enable_regex_filter' => $this->getFilter()->isEnabled(),
170
+			'include_regexp' => $sql->escape_string($this->getFilter()->getIncludeExpression()),
171
+			'exclude_regexp' => $sql->escape_string($this->getFilter()->getExcludeExpression())
172
+		];
173
+		foreach ($params as $field => $value) {
174
+			if (empty($value)) {
175
+				$params[$field] = 'NULL';
176
+			} else {
177
+				$params[$field] = "'$value'";
178
+			}
179
+		}
180
+		$response = static::getMySql()->query("
181 181
             SELECT * FROM `calendars` WHERE `id` = '$_id'
182 182
         ");
183
-        $previous = $response->fetch_assoc();
184
-        if ($previous) {
185
-            $query = "UPDATE `calendars` SET\n";
186
-            foreach ($params as $field => $value) {
187
-                if ($key != 'id') {
188
-                    $query .= "`$field` = $value\n";
189
-                }
190
-            }
191
-            $query .= "WHERE `id` = '{$params['id']}'";
192
-        } else {
193
-            $query = "INSERT INTO `calendars` (\n`";
194
-            $query .= implode('`, `', array_keys($params));
195
-            $query .= "`) VALUES (";
196
-            $query .= implode(', ', $params);
197
-            $query .= ')';
198
-        }
199
-        if (static::getMySql($query)->query() === false) {
200
-            throw new Exception(
201
-                "Failed to store calendar ID {$this->id} to database"
202
-            );
203
-        }
204
-    }
205
-
206
-    /**
207
-     * Load a Calendar from the MySQL database
208
-     *
209
-     * @param int $id
210
-     * @return Calendar
211
-     */
212
-    public static function load($id)
213
-    {
214
-        $sql = static::getMySql();
215
-        $query = "SELECT * FROM `calendars` WHERE `id` = '" . $sql->escape_string($id) . "'";
216
-        $response = $sql->query($query);
217
-        if ($response) {
218
-            $calendar = $response->fetch_assoc();
219
-            return new Calendar($calendar['canvas_url'], $calendar['ics_url'], $calendar['enable_regex_filter'], $calendar['include_regexp'], $calendar['exclude_regexp']);
220
-        }
221
-        return null;
222
-    }
183
+		$previous = $response->fetch_assoc();
184
+		if ($previous) {
185
+			$query = "UPDATE `calendars` SET\n";
186
+			foreach ($params as $field => $value) {
187
+				if ($key != 'id') {
188
+					$query .= "`$field` = $value\n";
189
+				}
190
+			}
191
+			$query .= "WHERE `id` = '{$params['id']}'";
192
+		} else {
193
+			$query = "INSERT INTO `calendars` (\n`";
194
+			$query .= implode('`, `', array_keys($params));
195
+			$query .= "`) VALUES (";
196
+			$query .= implode(', ', $params);
197
+			$query .= ')';
198
+		}
199
+		if (static::getMySql($query)->query() === false) {
200
+			throw new Exception(
201
+				"Failed to store calendar ID {$this->id} to database"
202
+			);
203
+		}
204
+	}
205
+
206
+	/**
207
+	 * Load a Calendar from the MySQL database
208
+	 *
209
+	 * @param int $id
210
+	 * @return Calendar
211
+	 */
212
+	public static function load($id)
213
+	{
214
+		$sql = static::getMySql();
215
+		$query = "SELECT * FROM `calendars` WHERE `id` = '" . $sql->escape_string($id) . "'";
216
+		$response = $sql->query($query);
217
+		if ($response) {
218
+			$calendar = $response->fetch_assoc();
219
+			return new Calendar($calendar['canvas_url'], $calendar['ics_url'], $calendar['enable_regex_filter'], $calendar['include_regexp'], $calendar['exclude_regexp']);
220
+		}
221
+		return null;
222
+	}
223 223
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
      */
115 115
     public function setName($name)
116 116
     {
117
-        $this->name = (string) $name;
117
+        $this->name = (string)$name;
118 118
     }
119 119
 
120 120
     /**
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
     protected function getPairingHash()
155 155
     {
156 156
         global $metadata;
157
-        return md5($this->getContext() . $this->getFeedUrl());
157
+        return md5($this->getContext().$this->getFeedUrl());
158 158
     }
159 159
 
160 160
     public function save()
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
     public static function load($id)
213 213
     {
214 214
         $sql = static::getMySql();
215
-        $query = "SELECT * FROM `calendars` WHERE `id` = '" . $sql->escape_string($id) . "'";
215
+        $query = "SELECT * FROM `calendars` WHERE `id` = '".$sql->escape_string($id)."'";
216 216
         $response = $sql->query($query);
217 217
         if ($response) {
218 218
             $calendar = $response->fetch_assoc();
Please login to merge, or discard this patch.