@@ -53,7 +53,6 @@ |
||
53 | 53 | /** |
54 | 54 | * Update the MySQL connection |
55 | 55 | * |
56 | - * @param PDO $db |
|
57 | 56 | * @throws Exception If `$db` is null |
58 | 57 | */ |
59 | 58 | public static function setDatabase(PDO $database) |
@@ -12,110 +12,110 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class Syncable |
14 | 14 | { |
15 | - /** |
|
16 | - * Database connection |
|
17 | - * |
|
18 | - * @var PDO|null |
|
19 | - */ |
|
20 | - protected static $database; |
|
15 | + /** |
|
16 | + * Database connection |
|
17 | + * |
|
18 | + * @var PDO|null |
|
19 | + */ |
|
20 | + protected static $database; |
|
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 | - * Unique timestamp for the current sync |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected static $syncTimestamp; |
|
29 | + /** |
|
30 | + * Unique timestamp for the current sync |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected static $syncTimestamp; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Generate a unique identifier for this synchronization pass |
|
37 | - **/ |
|
38 | - public static function getSyncTimestamp() |
|
39 | - { |
|
40 | - if (empty(static::$syncTimestamp)) { |
|
41 | - $timestamp = new DateTime(); |
|
42 | - static::$syncTimestamp = |
|
43 | - $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) . |
|
44 | - Constants::SEPARATOR . md5( |
|
45 | - (php_sapi_name() == 'cli' ? |
|
46 | - 'cli' : $_SERVER['REMOTE_ADDR'] |
|
47 | - ) . time() |
|
48 | - ); |
|
49 | - } |
|
50 | - return static::$syncTimestamp; |
|
51 | - } |
|
35 | + /** |
|
36 | + * Generate a unique identifier for this synchronization pass |
|
37 | + **/ |
|
38 | + public static function getSyncTimestamp() |
|
39 | + { |
|
40 | + if (empty(static::$syncTimestamp)) { |
|
41 | + $timestamp = new DateTime(); |
|
42 | + static::$syncTimestamp = |
|
43 | + $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) . |
|
44 | + Constants::SEPARATOR . md5( |
|
45 | + (php_sapi_name() == 'cli' ? |
|
46 | + 'cli' : $_SERVER['REMOTE_ADDR'] |
|
47 | + ) . time() |
|
48 | + ); |
|
49 | + } |
|
50 | + return static::$syncTimestamp; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Update the MySQL connection |
|
55 | - * |
|
56 | - * @param PDO $db |
|
57 | - * @throws Exception If `$db` is null |
|
58 | - */ |
|
59 | - public static function setDatabase(PDO $database) |
|
60 | - { |
|
61 | - if (empty($database)) { |
|
62 | - throw new Exception( |
|
63 | - 'A non-null database connection is required' |
|
64 | - ); |
|
65 | - } else { |
|
66 | - static::$database = $database; |
|
67 | - } |
|
68 | - } |
|
53 | + /** |
|
54 | + * Update the MySQL connection |
|
55 | + * |
|
56 | + * @param PDO $db |
|
57 | + * @throws Exception If `$db` is null |
|
58 | + */ |
|
59 | + public static function setDatabase(PDO $database) |
|
60 | + { |
|
61 | + if (empty($database)) { |
|
62 | + throw new Exception( |
|
63 | + 'A non-null database connection is required' |
|
64 | + ); |
|
65 | + } else { |
|
66 | + static::$database = $database; |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get the MySQL connection |
|
72 | - * |
|
73 | - * @return mysqli |
|
74 | - */ |
|
75 | - public static function getDatabase() |
|
76 | - { |
|
77 | - return static::$db; |
|
78 | - } |
|
70 | + /** |
|
71 | + * Get the MySQL connection |
|
72 | + * |
|
73 | + * @return mysqli |
|
74 | + */ |
|
75 | + public static function getDatabase() |
|
76 | + { |
|
77 | + return static::$db; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Update the API connection |
|
82 | - * |
|
83 | - * @param CanvasPest $api |
|
84 | - * @throws Exception if `$api` is null |
|
85 | - */ |
|
86 | - public static function setApi(CanvasPest $api) |
|
87 | - { |
|
88 | - if (empty($api)) { |
|
89 | - throw new Exception( |
|
90 | - 'A non-null API connection is required' |
|
91 | - ); |
|
92 | - } else { |
|
93 | - static::$api = $api; |
|
94 | - } |
|
95 | - } |
|
80 | + /** |
|
81 | + * Update the API connection |
|
82 | + * |
|
83 | + * @param CanvasPest $api |
|
84 | + * @throws Exception if `$api` is null |
|
85 | + */ |
|
86 | + public static function setApi(CanvasPest $api) |
|
87 | + { |
|
88 | + if (empty($api)) { |
|
89 | + throw new Exception( |
|
90 | + 'A non-null API connection is required' |
|
91 | + ); |
|
92 | + } else { |
|
93 | + static::$api = $api; |
|
94 | + } |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Get the API connection |
|
99 | - * |
|
100 | - * @return CanvasPest|null |
|
101 | - */ |
|
102 | - public static function getApi() |
|
103 | - { |
|
104 | - return static::$api; |
|
105 | - } |
|
97 | + /** |
|
98 | + * Get the API connection |
|
99 | + * |
|
100 | + * @return CanvasPest|null |
|
101 | + */ |
|
102 | + public static function getApi() |
|
103 | + { |
|
104 | + return static::$api; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Save the syncable object to the MySQL database |
|
109 | - * |
|
110 | - * @return [type] [description] |
|
111 | - */ |
|
112 | - abstract public function save(); |
|
107 | + /** |
|
108 | + * Save the syncable object to the MySQL database |
|
109 | + * |
|
110 | + * @return [type] [description] |
|
111 | + */ |
|
112 | + abstract public function save(); |
|
113 | 113 | |
114 | - /** |
|
115 | - * Load a syncable object from the MySQL database |
|
116 | - * |
|
117 | - * @param int $id |
|
118 | - * @return Syncable|null |
|
119 | - */ |
|
120 | - abstract public static function load($id); |
|
114 | + /** |
|
115 | + * Load a syncable object from the MySQL database |
|
116 | + * |
|
117 | + * @param int $id |
|
118 | + * @return Syncable|null |
|
119 | + */ |
|
120 | + abstract public static function load($id); |
|
121 | 121 | } |
@@ -4,107 +4,107 @@ |
||
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->getContext(); |
|
109 | - } |
|
106 | + public function __toString() |
|
107 | + { |
|
108 | + return $this->getContext(); |
|
109 | + } |
|
110 | 110 | } |
@@ -10,166 +10,166 @@ discard block |
||
10 | 10 | |
11 | 11 | class Calendar |
12 | 12 | { |
13 | - /** |
|
14 | - * Canvas calendar context |
|
15 | - * @var CalendarContext |
|
16 | - */ |
|
17 | - protected $context; |
|
13 | + /** |
|
14 | + * Canvas calendar context |
|
15 | + * @var CalendarContext |
|
16 | + */ |
|
17 | + protected $context; |
|
18 | 18 | |
19 | - /** |
|
20 | - * ICS or webcal feed URL |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $feedUrl; |
|
19 | + /** |
|
20 | + * ICS or webcal feed URL |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $feedUrl; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Name of this calendar (extracted from feed) |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $name; |
|
25 | + /** |
|
26 | + * Name of this calendar (extracted from feed) |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $name; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Filter for events in this calendar |
|
33 | - * @var Filter |
|
34 | - */ |
|
35 | - protected $filter; |
|
31 | + /** |
|
32 | + * Filter for events in this calendar |
|
33 | + * @var Filter |
|
34 | + */ |
|
35 | + protected $filter; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Construct a Calendar object |
|
39 | - * |
|
40 | - * @param string $canvasUrl URL of a Canvas calendar context |
|
41 | - * @param string $feedUrl URL of a webcal or ICS calendar feed |
|
42 | - * @param boolean $enableFilter (Optional, default `false`) |
|
43 | - * @param string $include (Optional) Regular expression to select events |
|
44 | - * for inclusion in the calendar sync |
|
45 | - * @param string $exclude (Optional) Regular expression to select events |
|
46 | - * for exclusion from the calendar sync |
|
47 | - */ |
|
48 | - public function __construct($canvasUrl, $feedUrl, $enableFilter = false, $include = null, $exclude = null) |
|
49 | - { |
|
50 | - $this->setContext(new CalendarContext($canvasUrl)); |
|
51 | - $this->setFeed($feedUrl); |
|
52 | - $this->setFilter(new Filter( |
|
53 | - $enableFilter, |
|
54 | - $include, |
|
55 | - $exclude |
|
56 | - )); |
|
57 | - } |
|
37 | + /** |
|
38 | + * Construct a Calendar object |
|
39 | + * |
|
40 | + * @param string $canvasUrl URL of a Canvas calendar context |
|
41 | + * @param string $feedUrl URL of a webcal or ICS calendar feed |
|
42 | + * @param boolean $enableFilter (Optional, default `false`) |
|
43 | + * @param string $include (Optional) Regular expression to select events |
|
44 | + * for inclusion in the calendar sync |
|
45 | + * @param string $exclude (Optional) Regular expression to select events |
|
46 | + * for exclusion from the calendar sync |
|
47 | + */ |
|
48 | + public function __construct($canvasUrl, $feedUrl, $enableFilter = false, $include = null, $exclude = null) |
|
49 | + { |
|
50 | + $this->setContext(new CalendarContext($canvasUrl)); |
|
51 | + $this->setFeed($feedUrl); |
|
52 | + $this->setFilter(new Filter( |
|
53 | + $enableFilter, |
|
54 | + $include, |
|
55 | + $exclude |
|
56 | + )); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Set the Canvas calendar context |
|
61 | - * |
|
62 | - * @param CalendarContext $context |
|
63 | - * @throws Exception If `$context` is null |
|
64 | - */ |
|
65 | - public function setContext(CalendarContext $context) |
|
66 | - { |
|
67 | - if (!empty($context)) { |
|
68 | - $this->context = $context; |
|
69 | - } else { |
|
70 | - throw new Exception( |
|
71 | - 'Context cannot be null' |
|
72 | - ); |
|
73 | - } |
|
74 | - } |
|
59 | + /** |
|
60 | + * Set the Canvas calendar context |
|
61 | + * |
|
62 | + * @param CalendarContext $context |
|
63 | + * @throws Exception If `$context` is null |
|
64 | + */ |
|
65 | + public function setContext(CalendarContext $context) |
|
66 | + { |
|
67 | + if (!empty($context)) { |
|
68 | + $this->context = $context; |
|
69 | + } else { |
|
70 | + throw new Exception( |
|
71 | + 'Context cannot be null' |
|
72 | + ); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Get the Canvas calendar context |
|
78 | - * |
|
79 | - * @return CalendarContext |
|
80 | - */ |
|
81 | - public function getContext() |
|
82 | - { |
|
83 | - return $this->context; |
|
84 | - } |
|
76 | + /** |
|
77 | + * Get the Canvas calendar context |
|
78 | + * |
|
79 | + * @return CalendarContext |
|
80 | + */ |
|
81 | + public function getContext() |
|
82 | + { |
|
83 | + return $this->context; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Set the webcal or ICS feed URl for this calendar |
|
88 | - * |
|
89 | - * @param string $feedUrl |
|
90 | - * @throws Exception If `$feedUrl` is not a valid URL |
|
91 | - */ |
|
92 | - public function setFeedUrl($feedUrl) |
|
93 | - { |
|
94 | - if (!empty($feedUrl)) { |
|
95 | - /* crude test to see if the feed is a valid URL */ |
|
96 | - $handle = fopen($feedUrl, 'r'); |
|
97 | - if ($handle !== false) { |
|
98 | - $this->feedUrl = $feedUrl; |
|
99 | - } |
|
100 | - } |
|
101 | - throw new Exception( |
|
102 | - 'Feed must be a valid URL' |
|
103 | - ); |
|
104 | - } |
|
86 | + /** |
|
87 | + * Set the webcal or ICS feed URl for this calendar |
|
88 | + * |
|
89 | + * @param string $feedUrl |
|
90 | + * @throws Exception If `$feedUrl` is not a valid URL |
|
91 | + */ |
|
92 | + public function setFeedUrl($feedUrl) |
|
93 | + { |
|
94 | + if (!empty($feedUrl)) { |
|
95 | + /* crude test to see if the feed is a valid URL */ |
|
96 | + $handle = fopen($feedUrl, 'r'); |
|
97 | + if ($handle !== false) { |
|
98 | + $this->feedUrl = $feedUrl; |
|
99 | + } |
|
100 | + } |
|
101 | + throw new Exception( |
|
102 | + 'Feed must be a valid URL' |
|
103 | + ); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Get the feed URL for this calendar |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function getFeedUrl() |
|
112 | - { |
|
113 | - return $this->feedUrl; |
|
114 | - } |
|
106 | + /** |
|
107 | + * Get the feed URL for this calendar |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function getFeedUrl() |
|
112 | + { |
|
113 | + return $this->feedUrl; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Set the name of the calendar |
|
118 | - * |
|
119 | - * @param string $name |
|
120 | - */ |
|
121 | - public function setName($name) |
|
122 | - { |
|
123 | - $this->name = (string) $name; |
|
124 | - } |
|
116 | + /** |
|
117 | + * Set the name of the calendar |
|
118 | + * |
|
119 | + * @param string $name |
|
120 | + */ |
|
121 | + public function setName($name) |
|
122 | + { |
|
123 | + $this->name = (string) $name; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Get the name of the calendar |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function getName() |
|
132 | - { |
|
133 | - return $this->name; |
|
134 | - } |
|
126 | + /** |
|
127 | + * Get the name of the calendar |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function getName() |
|
132 | + { |
|
133 | + return $this->name; |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Set the regular expression filter for this calendar |
|
138 | - * |
|
139 | - * @param Filter $filter |
|
140 | - */ |
|
141 | - public function setFilter(Filter $filter) |
|
142 | - { |
|
143 | - $this->filter = $filter; |
|
144 | - } |
|
136 | + /** |
|
137 | + * Set the regular expression filter for this calendar |
|
138 | + * |
|
139 | + * @param Filter $filter |
|
140 | + */ |
|
141 | + public function setFilter(Filter $filter) |
|
142 | + { |
|
143 | + $this->filter = $filter; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Get the regular expression filter for this calendar |
|
148 | - * |
|
149 | - * @return Filter |
|
150 | - */ |
|
151 | - public function getFilter() |
|
152 | - { |
|
153 | - return $this->filter; |
|
154 | - } |
|
146 | + /** |
|
147 | + * Get the regular expression filter for this calendar |
|
148 | + * |
|
149 | + * @return Filter |
|
150 | + */ |
|
151 | + public function getFilter() |
|
152 | + { |
|
153 | + return $this->filter; |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * Generate a unique ID to identify this particular pairing of ICS feed and |
|
158 | - * Canvas calendar |
|
159 | - **/ |
|
160 | - protected function getPairingHash() |
|
161 | - { |
|
162 | - return md5($this->getContext()->getCanonicalUrl() . $this->getFeedUrl()); |
|
163 | - } |
|
156 | + /** |
|
157 | + * Generate a unique ID to identify this particular pairing of ICS feed and |
|
158 | + * Canvas calendar |
|
159 | + **/ |
|
160 | + protected function getPairingHash() |
|
161 | + { |
|
162 | + return md5($this->getContext()->getCanonicalUrl() . $this->getFeedUrl()); |
|
163 | + } |
|
164 | 164 | |
165 | - public function save() |
|
166 | - { |
|
167 | - $db = static::getDatabase(); |
|
168 | - $find = $db->prepare( |
|
169 | - "SELECT * FROM `calendars` WHERE `id` = :id" |
|
170 | - ); |
|
171 | - $update = $db->prepare( |
|
172 | - "UPDATE `calendars` |
|
165 | + public function save() |
|
166 | + { |
|
167 | + $db = static::getDatabase(); |
|
168 | + $find = $db->prepare( |
|
169 | + "SELECT * FROM `calendars` WHERE `id` = :id" |
|
170 | + ); |
|
171 | + $update = $db->prepare( |
|
172 | + "UPDATE `calendars` |
|
173 | 173 | SET |
174 | 174 | `name` = :name, |
175 | 175 | `canvas_url` = :canvas_url, |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | `exclude_regexp` = :exclude_regexp |
181 | 181 | WHERE |
182 | 182 | `id` = :id" |
183 | - ); |
|
184 | - $insert = $db->prepare( |
|
185 | - "INSERT INTO `calendars` |
|
183 | + ); |
|
184 | + $insert = $db->prepare( |
|
185 | + "INSERT INTO `calendars` |
|
186 | 186 | ( |
187 | 187 | `id`, |
188 | 188 | `name`, |
@@ -202,217 +202,217 @@ discard block |
||
202 | 202 | :include_regexp, |
203 | 203 | :exclude_regexp |
204 | 204 | )" |
205 | - ); |
|
206 | - $params = [ |
|
207 | - 'id' => $this->getPairingHash(), |
|
208 | - 'name' => $this->getName(), |
|
209 | - 'canvas_url' => $this->getContext()->getCanonicalUrl(), |
|
210 | - 'ics_url' => $this->getFeedUrl(), |
|
211 | - 'synced' => static::getSyncTimestamp(), |
|
212 | - 'enable_regex_filter' => $this->getFilter()->isEnabled(), |
|
213 | - 'include_regexp' => $this->getFilter()->getIncludeExpression(), |
|
214 | - 'exclude_regexp' => $this->getFilter()->getExcludeExpression() |
|
215 | - ]; |
|
205 | + ); |
|
206 | + $params = [ |
|
207 | + 'id' => $this->getPairingHash(), |
|
208 | + 'name' => $this->getName(), |
|
209 | + 'canvas_url' => $this->getContext()->getCanonicalUrl(), |
|
210 | + 'ics_url' => $this->getFeedUrl(), |
|
211 | + 'synced' => static::getSyncTimestamp(), |
|
212 | + 'enable_regex_filter' => $this->getFilter()->isEnabled(), |
|
213 | + 'include_regexp' => $this->getFilter()->getIncludeExpression(), |
|
214 | + 'exclude_regexp' => $this->getFilter()->getExcludeExpression() |
|
215 | + ]; |
|
216 | 216 | |
217 | - $find->execute($params); |
|
218 | - if ($find->fetch() !== false) { |
|
219 | - $update->execute($params); |
|
220 | - } else { |
|
221 | - $insert->execute($params); |
|
222 | - } |
|
223 | - } |
|
217 | + $find->execute($params); |
|
218 | + if ($find->fetch() !== false) { |
|
219 | + $update->execute($params); |
|
220 | + } else { |
|
221 | + $insert->execute($params); |
|
222 | + } |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * Load a Calendar from the database |
|
227 | - * |
|
228 | - * @param int $id |
|
229 | - * @return Calendar |
|
230 | - */ |
|
231 | - public static function load($id) |
|
232 | - { |
|
233 | - $find = static::getDatabase()->prepare( |
|
234 | - "SELECT * FROM `calendars` WHERE `id` = :id" |
|
235 | - ); |
|
236 | - $find->execute($id); |
|
237 | - if (($calendar = $find->fetch()) !== false) { |
|
238 | - return new Calendar( |
|
239 | - $calendar['canvas_url'], |
|
240 | - $calendar['ics_url'], |
|
241 | - $calendar['enable_regex_filter'], |
|
242 | - $calendar['include_regexp'], |
|
243 | - $calendar['exclude_regexp'] |
|
244 | - ); |
|
245 | - } |
|
246 | - return null; |
|
247 | - } |
|
225 | + /** |
|
226 | + * Load a Calendar from the database |
|
227 | + * |
|
228 | + * @param int $id |
|
229 | + * @return Calendar |
|
230 | + */ |
|
231 | + public static function load($id) |
|
232 | + { |
|
233 | + $find = static::getDatabase()->prepare( |
|
234 | + "SELECT * FROM `calendars` WHERE `id` = :id" |
|
235 | + ); |
|
236 | + $find->execute($id); |
|
237 | + if (($calendar = $find->fetch()) !== false) { |
|
238 | + return new Calendar( |
|
239 | + $calendar['canvas_url'], |
|
240 | + $calendar['ics_url'], |
|
241 | + $calendar['enable_regex_filter'], |
|
242 | + $calendar['include_regexp'], |
|
243 | + $calendar['exclude_regexp'] |
|
244 | + ); |
|
245 | + } |
|
246 | + return null; |
|
247 | + } |
|
248 | 248 | |
249 | - public function import(Log $log) |
|
250 | - { |
|
251 | - $db = static::getDatabase(); |
|
252 | - $api = static::getApi(); |
|
249 | + public function import(Log $log) |
|
250 | + { |
|
251 | + $db = static::getDatabase(); |
|
252 | + $api = static::getApi(); |
|
253 | 253 | |
254 | - /* |
|
254 | + /* |
|
255 | 255 | * This will throw an exception if it's not found |
256 | 256 | */ |
257 | - $canvasObject = $api->get($this->getContext()->getVerificationUrl()); |
|
257 | + $canvasObject = $api->get($this->getContext()->getVerificationUrl()); |
|
258 | 258 | |
259 | - if (!DataUtilities::URLexists($this>getFeedUrl())) { |
|
260 | - throw new Exception( |
|
261 | - "Cannot sync calendars with a valid calendar feed" |
|
262 | - ); |
|
263 | - } |
|
259 | + if (!DataUtilities::URLexists($this>getFeedUrl())) { |
|
260 | + throw new Exception( |
|
261 | + "Cannot sync calendars with a valid calendar feed" |
|
262 | + ); |
|
263 | + } |
|
264 | 264 | |
265 | - if ($log) { |
|
266 | - $log->log(static::getSyncTimestamp() . ' sync started', PEAR_LOG_INFO); |
|
267 | - } |
|
268 | - $this->save(); |
|
265 | + if ($log) { |
|
266 | + $log->log(static::getSyncTimestamp() . ' sync started', PEAR_LOG_INFO); |
|
267 | + } |
|
268 | + $this->save(); |
|
269 | 269 | |
270 | - $ics = new vcalendar([ |
|
271 | - 'unique_id' => __FILE__, |
|
272 | - 'url' => $this->getFeedUrl() |
|
273 | - ]); |
|
274 | - $ics->parse(); |
|
270 | + $ics = new vcalendar([ |
|
271 | + 'unique_id' => __FILE__, |
|
272 | + 'url' => $this->getFeedUrl() |
|
273 | + ]); |
|
274 | + $ics->parse(); |
|
275 | 275 | |
276 | - /* |
|
276 | + /* |
|
277 | 277 | * TODO: would it be worth the performance improvement to just process |
278 | 278 | * things from today's date forward? (i.e. ignore old items, even |
279 | 279 | * if they've changed...) |
280 | 280 | */ |
281 | - /* |
|
281 | + /* |
|
282 | 282 | * TODO:0 the best window for syncing would be the term of the course |
283 | 283 | * in question, right? issue:12 |
284 | 284 | */ |
285 | - /* |
|
285 | + /* |
|
286 | 286 | * TODO:0 Arbitrarily selecting events in for a year on either side of |
287 | 287 | * today's date, probably a better system? issue:12 |
288 | 288 | */ |
289 | - foreach ($ics->selectComponents( |
|
290 | - date('Y')-1, // startYear |
|
291 | - date('m'), // startMonth |
|
292 | - date('d'), // startDay |
|
293 | - date('Y')+1, // endYEar |
|
294 | - date('m'), // endMonth |
|
295 | - date('d'), // endDay |
|
296 | - 'vevent', // cType |
|
297 | - false, // flat |
|
298 | - true, // any |
|
299 | - true // split |
|
300 | - ) as $year) { |
|
301 | - foreach ($year as $month => $days) { |
|
302 | - foreach ($days as $day => $events) { |
|
303 | - foreach ($events as $i => $_event) { |
|
304 | - $event = new Event($_event, $this->getPairingHash()); |
|
305 | - if ($this->getFilter()->filterEvent($event)) { |
|
306 | - $eventCache = Event::load($this->getPairingHash(), $eventHash); |
|
307 | - if (empty($eventCache)) { |
|
308 | - /* multi-day event instance start times need to be changed to _this_ date */ |
|
309 | - $start = new DateTime( |
|
310 | - iCalUtilityFunctions::_date2strdate( |
|
311 | - $event->getProperty('DTSTART') |
|
312 | - ) |
|
313 | - ); |
|
314 | - $end = new DateTime( |
|
315 | - iCalUtilityFunctions::_date2strdate( |
|
316 | - $event->getProperty('DTEND') |
|
317 | - ) |
|
318 | - ); |
|
319 | - if ($event->getProperty('X-RECURRENCE')) { |
|
320 | - $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
321 | - $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
322 | - } |
|
323 | - $start->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
|
324 | - $end->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
|
289 | + foreach ($ics->selectComponents( |
|
290 | + date('Y')-1, // startYear |
|
291 | + date('m'), // startMonth |
|
292 | + date('d'), // startDay |
|
293 | + date('Y')+1, // endYEar |
|
294 | + date('m'), // endMonth |
|
295 | + date('d'), // endDay |
|
296 | + 'vevent', // cType |
|
297 | + false, // flat |
|
298 | + true, // any |
|
299 | + true // split |
|
300 | + ) as $year) { |
|
301 | + foreach ($year as $month => $days) { |
|
302 | + foreach ($days as $day => $events) { |
|
303 | + foreach ($events as $i => $_event) { |
|
304 | + $event = new Event($_event, $this->getPairingHash()); |
|
305 | + if ($this->getFilter()->filterEvent($event)) { |
|
306 | + $eventCache = Event::load($this->getPairingHash(), $eventHash); |
|
307 | + if (empty($eventCache)) { |
|
308 | + /* multi-day event instance start times need to be changed to _this_ date */ |
|
309 | + $start = new DateTime( |
|
310 | + iCalUtilityFunctions::_date2strdate( |
|
311 | + $event->getProperty('DTSTART') |
|
312 | + ) |
|
313 | + ); |
|
314 | + $end = new DateTime( |
|
315 | + iCalUtilityFunctions::_date2strdate( |
|
316 | + $event->getProperty('DTEND') |
|
317 | + ) |
|
318 | + ); |
|
319 | + if ($event->getProperty('X-RECURRENCE')) { |
|
320 | + $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
321 | + $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
322 | + } |
|
323 | + $start->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
|
324 | + $end->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
|
325 | 325 | |
326 | - try { |
|
327 | - $calendarEvent = $api->post( |
|
328 | - "/calendar_events", |
|
329 | - [ |
|
330 | - 'calendar_event' => [ |
|
331 | - 'context_code' => $this->getContext() . "_{$canvasObject['id']}", |
|
332 | - 'title' => preg_replace( |
|
333 | - '%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', |
|
334 | - '\\1', |
|
335 | - strip_tags($event->getProperty('SUMMARY')) |
|
336 | - ), |
|
337 | - 'description' => Markdown::defaultTransform(str_replace( |
|
338 | - '\n', |
|
339 | - "\n\n", |
|
340 | - $event->getProperty('DESCRIPTION', 1) |
|
341 | - )), |
|
342 | - 'start_at' => $start->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
|
343 | - 'end_at' => $end->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
|
344 | - 'location_name' => $event->getProperty('LOCATION') |
|
345 | - ] |
|
346 | - ] |
|
347 | - ); |
|
348 | - } catch (Exception $e) { |
|
349 | - if ($log) { |
|
350 | - $log->log($e->getMessage(), PEAR_LOG_ERR); |
|
351 | - } else { |
|
352 | - throw $e; |
|
353 | - } |
|
354 | - } |
|
326 | + try { |
|
327 | + $calendarEvent = $api->post( |
|
328 | + "/calendar_events", |
|
329 | + [ |
|
330 | + 'calendar_event' => [ |
|
331 | + 'context_code' => $this->getContext() . "_{$canvasObject['id']}", |
|
332 | + 'title' => preg_replace( |
|
333 | + '%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', |
|
334 | + '\\1', |
|
335 | + strip_tags($event->getProperty('SUMMARY')) |
|
336 | + ), |
|
337 | + 'description' => Markdown::defaultTransform(str_replace( |
|
338 | + '\n', |
|
339 | + "\n\n", |
|
340 | + $event->getProperty('DESCRIPTION', 1) |
|
341 | + )), |
|
342 | + 'start_at' => $start->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
|
343 | + 'end_at' => $end->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
|
344 | + 'location_name' => $event->getProperty('LOCATION') |
|
345 | + ] |
|
346 | + ] |
|
347 | + ); |
|
348 | + } catch (Exception $e) { |
|
349 | + if ($log) { |
|
350 | + $log->log($e->getMessage(), PEAR_LOG_ERR); |
|
351 | + } else { |
|
352 | + throw $e; |
|
353 | + } |
|
354 | + } |
|
355 | 355 | |
356 | - $eventCache = new Event($this->getPairingHash(), $canvasObject['id'], $eventHash); |
|
357 | - } |
|
358 | - $eventCache->save(); |
|
359 | - } |
|
360 | - } |
|
361 | - } |
|
362 | - } |
|
363 | - } |
|
356 | + $eventCache = new Event($this->getPairingHash(), $canvasObject['id'], $eventHash); |
|
357 | + } |
|
358 | + $eventCache->save(); |
|
359 | + } |
|
360 | + } |
|
361 | + } |
|
362 | + } |
|
363 | + } |
|
364 | 364 | |
365 | - $findDeletedEvents = $db->prepare( |
|
366 | - "SELECT * |
|
365 | + $findDeletedEvents = $db->prepare( |
|
366 | + "SELECT * |
|
367 | 367 | FROM `events` |
368 | 368 | WHERE |
369 | 369 | `calendar` = :calendar AND |
370 | 370 | `synced` != :synced |
371 | 371 | " |
372 | - ); |
|
373 | - $deleteCachedEvent = $db->prepare( |
|
374 | - "DELETE FROM `events` WHERE `id` = :id" |
|
375 | - ); |
|
376 | - $findDeletedEvents->execute([ |
|
377 | - 'calendar' => $this->getPairingHash(), |
|
378 | - 'synced' => static::getSyncTimestamp() |
|
379 | - ]); |
|
380 | - if (($deletedEvents = $findDeletedEvents->fetchAll()) !== false) { |
|
381 | - foreach ($deletedEvents as $eventCache) { |
|
382 | - try { |
|
383 | - $api->delete( |
|
384 | - "/calendar_events/{$eventCache['calendar_event[id]']}", |
|
385 | - [ |
|
386 | - 'cancel_reason' => getSyncTimestamp(), |
|
387 | - /* |
|
372 | + ); |
|
373 | + $deleteCachedEvent = $db->prepare( |
|
374 | + "DELETE FROM `events` WHERE `id` = :id" |
|
375 | + ); |
|
376 | + $findDeletedEvents->execute([ |
|
377 | + 'calendar' => $this->getPairingHash(), |
|
378 | + 'synced' => static::getSyncTimestamp() |
|
379 | + ]); |
|
380 | + if (($deletedEvents = $findDeletedEvents->fetchAll()) !== false) { |
|
381 | + foreach ($deletedEvents as $eventCache) { |
|
382 | + try { |
|
383 | + $api->delete( |
|
384 | + "/calendar_events/{$eventCache['calendar_event[id]']}", |
|
385 | + [ |
|
386 | + 'cancel_reason' => getSyncTimestamp(), |
|
387 | + /* |
|
388 | 388 | * TODO: this feels skeevy -- like the empty string |
389 | 389 | * will break |
390 | 390 | */ |
391 | - 'as_user_id' => ($this->getContext()->getContext() == 'user' ? $canvasObject['id'] : '') |
|
392 | - ] |
|
393 | - ); |
|
394 | - $deleteCachedEvent->execute($eventCache['id']); |
|
395 | - } catch (Pest_Unauthorized $e) { |
|
396 | - if ($log) { |
|
397 | - $log->log( |
|
398 | - "calendar_event[{$eventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", |
|
399 | - PEAR_LOG_WARN |
|
400 | - ); |
|
401 | - } else { |
|
402 | - throw $e; |
|
403 | - } |
|
404 | - } catch (Exception $e) { |
|
405 | - if ($log) { |
|
406 | - $log->log($e->getMessage(), PEAR_LOG_ERR); |
|
407 | - } else { |
|
408 | - throw $e; |
|
409 | - } |
|
410 | - } |
|
411 | - } |
|
412 | - } |
|
391 | + 'as_user_id' => ($this->getContext()->getContext() == 'user' ? $canvasObject['id'] : '') |
|
392 | + ] |
|
393 | + ); |
|
394 | + $deleteCachedEvent->execute($eventCache['id']); |
|
395 | + } catch (Pest_Unauthorized $e) { |
|
396 | + if ($log) { |
|
397 | + $log->log( |
|
398 | + "calendar_event[{$eventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", |
|
399 | + PEAR_LOG_WARN |
|
400 | + ); |
|
401 | + } else { |
|
402 | + throw $e; |
|
403 | + } |
|
404 | + } catch (Exception $e) { |
|
405 | + if ($log) { |
|
406 | + $log->log($e->getMessage(), PEAR_LOG_ERR); |
|
407 | + } else { |
|
408 | + throw $e; |
|
409 | + } |
|
410 | + } |
|
411 | + } |
|
412 | + } |
|
413 | 413 | |
414 | - if ($log) { |
|
415 | - $log->log(static::getSyncTimestamp() . ' sync finished', PEAR_LOG_INFO); |
|
416 | - } |
|
417 | - } |
|
414 | + if ($log) { |
|
415 | + $log->log(static::getSyncTimestamp() . ' sync finished', PEAR_LOG_INFO); |
|
416 | + } |
|
417 | + } |
|
418 | 418 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function setName($name) |
122 | 122 | { |
123 | - $this->name = (string) $name; |
|
123 | + $this->name = (string)$name; |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | **/ |
160 | 160 | protected function getPairingHash() |
161 | 161 | { |
162 | - return md5($this->getContext()->getCanonicalUrl() . $this->getFeedUrl()); |
|
162 | + return md5($this->getContext()->getCanonicalUrl().$this->getFeedUrl()); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | public function save() |
@@ -256,14 +256,14 @@ discard block |
||
256 | 256 | */ |
257 | 257 | $canvasObject = $api->get($this->getContext()->getVerificationUrl()); |
258 | 258 | |
259 | - if (!DataUtilities::URLexists($this>getFeedUrl())) { |
|
259 | + if (!DataUtilities::URLexists($this > getFeedUrl())) { |
|
260 | 260 | throw new Exception( |
261 | 261 | "Cannot sync calendars with a valid calendar feed" |
262 | 262 | ); |
263 | 263 | } |
264 | 264 | |
265 | 265 | if ($log) { |
266 | - $log->log(static::getSyncTimestamp() . ' sync started', PEAR_LOG_INFO); |
|
266 | + $log->log(static::getSyncTimestamp().' sync started', PEAR_LOG_INFO); |
|
267 | 267 | } |
268 | 268 | $this->save(); |
269 | 269 | |
@@ -287,10 +287,10 @@ discard block |
||
287 | 287 | * today's date, probably a better system? issue:12 |
288 | 288 | */ |
289 | 289 | foreach ($ics->selectComponents( |
290 | - date('Y')-1, // startYear |
|
290 | + date('Y') - 1, // startYear |
|
291 | 291 | date('m'), // startMonth |
292 | 292 | date('d'), // startDay |
293 | - date('Y')+1, // endYEar |
|
293 | + date('Y') + 1, // endYEar |
|
294 | 294 | date('m'), // endMonth |
295 | 295 | date('d'), // endDay |
296 | 296 | 'vevent', // cType |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | "/calendar_events", |
329 | 329 | [ |
330 | 330 | 'calendar_event' => [ |
331 | - 'context_code' => $this->getContext() . "_{$canvasObject['id']}", |
|
331 | + 'context_code' => $this->getContext()."_{$canvasObject['id']}", |
|
332 | 332 | 'title' => preg_replace( |
333 | 333 | '%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', |
334 | 334 | '\\1', |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | } |
413 | 413 | |
414 | 414 | if ($log) { |
415 | - $log->log(static::getSyncTimestamp() . ' sync finished', PEAR_LOG_INFO); |
|
415 | + $log->log(static::getSyncTimestamp().' sync finished', PEAR_LOG_INFO); |
|
416 | 416 | } |
417 | 417 | } |
418 | 418 | } |
@@ -6,130 +6,130 @@ |
||
6 | 6 | |
7 | 7 | class Event extends Syncable |
8 | 8 | { |
9 | - /** |
|
10 | - * Wrapped VEVENT |
|
11 | - * @var vevent |
|
12 | - */ |
|
13 | - protected $vevent; |
|
14 | - |
|
15 | - /** |
|
16 | - * Calendar ID |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $calendar; |
|
20 | - |
|
21 | - /** |
|
22 | - * Unique hash identifying this event |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $hash; |
|
26 | - |
|
27 | - protected $canvasId; |
|
28 | - |
|
29 | - public function __construct($veventOrHash, $calendar, $calendarEventId = null) |
|
30 | - { |
|
31 | - if (empty($veventOrHash)) { |
|
32 | - throw new Exception("Valid VEVENT or hash required"); |
|
33 | - } |
|
34 | - if (is_a($veventOrHash, vevent::class)) { |
|
35 | - $this->vevent = $veventOrHash; |
|
36 | - } else { |
|
37 | - $this->hash = (string) $veventOrHash; |
|
38 | - } |
|
39 | - |
|
40 | - if (empty($calendar)) { |
|
41 | - throw new Exception("Calendar ID required"); |
|
42 | - } |
|
43 | - $this->calendar = (string) $calendar; |
|
44 | - |
|
45 | - $this->getHash(); |
|
46 | - } |
|
47 | - |
|
48 | - public function getProperty($property) |
|
49 | - { |
|
50 | - return (empty($this->vevent) ? false : $this->vevent->getProperty($property)); |
|
51 | - } |
|
52 | - |
|
53 | - const FIELD_MAP = [ |
|
54 | - 'calendar_event[title]' => 'SUMMARY', |
|
55 | - 'calendar_event[description]' => 'DESCRIPTION', |
|
56 | - 'calendar_event[start_at]' => [ |
|
57 | - 0 => 'X-CURRENT-DTSTART', |
|
58 | - 1 => 'DTSTART' |
|
59 | - ], |
|
60 | - 'calendar_event[end_at]' => [ |
|
61 | - 0 => 'X-CURRENT-DTEND', |
|
62 | - 1 => 'DTEND' |
|
63 | - ], |
|
64 | - 'calendar_event[location_name]' => 'LOCATION' |
|
65 | - ]; |
|
66 | - |
|
67 | - /** |
|
68 | - * Generate a hash of this version of an event to cache in the database |
|
69 | - **/ |
|
70 | - public function getHash($algorithm = 'md5') |
|
71 | - { |
|
72 | - if (empty($this->hash)) { |
|
73 | - $blob = ''; |
|
74 | - foreach (static::$FIELD_MAP as $field) { |
|
75 | - if (is_array($field)) { |
|
76 | - foreach ($field as $option) { |
|
77 | - if (!empty($property = $this->getProperty($option))) { |
|
78 | - $blob .= serialize($property); |
|
79 | - break; |
|
80 | - } |
|
81 | - } |
|
82 | - } else { |
|
83 | - if (!empty($property = $this->getProperty($field))) { |
|
84 | - $blob .= serialize($property); |
|
85 | - } |
|
86 | - } |
|
87 | - } |
|
88 | - $this->hash = hash($algorithm, $blob); |
|
89 | - } |
|
90 | - return $this->hash; |
|
91 | - } |
|
92 | - |
|
93 | - public function isCached() |
|
94 | - { |
|
95 | - return !empty(static::load($this->getHash(), $this->calendar)); |
|
96 | - } |
|
97 | - |
|
98 | - public function save() |
|
99 | - { |
|
100 | - $db = static::getDatabase(); |
|
101 | - |
|
102 | - $find = $db->prepare( |
|
103 | - "SELECT * |
|
9 | + /** |
|
10 | + * Wrapped VEVENT |
|
11 | + * @var vevent |
|
12 | + */ |
|
13 | + protected $vevent; |
|
14 | + |
|
15 | + /** |
|
16 | + * Calendar ID |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $calendar; |
|
20 | + |
|
21 | + /** |
|
22 | + * Unique hash identifying this event |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $hash; |
|
26 | + |
|
27 | + protected $canvasId; |
|
28 | + |
|
29 | + public function __construct($veventOrHash, $calendar, $calendarEventId = null) |
|
30 | + { |
|
31 | + if (empty($veventOrHash)) { |
|
32 | + throw new Exception("Valid VEVENT or hash required"); |
|
33 | + } |
|
34 | + if (is_a($veventOrHash, vevent::class)) { |
|
35 | + $this->vevent = $veventOrHash; |
|
36 | + } else { |
|
37 | + $this->hash = (string) $veventOrHash; |
|
38 | + } |
|
39 | + |
|
40 | + if (empty($calendar)) { |
|
41 | + throw new Exception("Calendar ID required"); |
|
42 | + } |
|
43 | + $this->calendar = (string) $calendar; |
|
44 | + |
|
45 | + $this->getHash(); |
|
46 | + } |
|
47 | + |
|
48 | + public function getProperty($property) |
|
49 | + { |
|
50 | + return (empty($this->vevent) ? false : $this->vevent->getProperty($property)); |
|
51 | + } |
|
52 | + |
|
53 | + const FIELD_MAP = [ |
|
54 | + 'calendar_event[title]' => 'SUMMARY', |
|
55 | + 'calendar_event[description]' => 'DESCRIPTION', |
|
56 | + 'calendar_event[start_at]' => [ |
|
57 | + 0 => 'X-CURRENT-DTSTART', |
|
58 | + 1 => 'DTSTART' |
|
59 | + ], |
|
60 | + 'calendar_event[end_at]' => [ |
|
61 | + 0 => 'X-CURRENT-DTEND', |
|
62 | + 1 => 'DTEND' |
|
63 | + ], |
|
64 | + 'calendar_event[location_name]' => 'LOCATION' |
|
65 | + ]; |
|
66 | + |
|
67 | + /** |
|
68 | + * Generate a hash of this version of an event to cache in the database |
|
69 | + **/ |
|
70 | + public function getHash($algorithm = 'md5') |
|
71 | + { |
|
72 | + if (empty($this->hash)) { |
|
73 | + $blob = ''; |
|
74 | + foreach (static::$FIELD_MAP as $field) { |
|
75 | + if (is_array($field)) { |
|
76 | + foreach ($field as $option) { |
|
77 | + if (!empty($property = $this->getProperty($option))) { |
|
78 | + $blob .= serialize($property); |
|
79 | + break; |
|
80 | + } |
|
81 | + } |
|
82 | + } else { |
|
83 | + if (!empty($property = $this->getProperty($field))) { |
|
84 | + $blob .= serialize($property); |
|
85 | + } |
|
86 | + } |
|
87 | + } |
|
88 | + $this->hash = hash($algorithm, $blob); |
|
89 | + } |
|
90 | + return $this->hash; |
|
91 | + } |
|
92 | + |
|
93 | + public function isCached() |
|
94 | + { |
|
95 | + return !empty(static::load($this->getHash(), $this->calendar)); |
|
96 | + } |
|
97 | + |
|
98 | + public function save() |
|
99 | + { |
|
100 | + $db = static::getDatabase(); |
|
101 | + |
|
102 | + $find = $db->prepare( |
|
103 | + "SELECT * |
|
104 | 104 | FROM `events` |
105 | 105 | WHERE |
106 | 106 | `event_hash` = :event_hash AND |
107 | 107 | `calendar` = :calendar" |
108 | - ); |
|
109 | - $update = $db->prepare( |
|
110 | - "UPDATE `events` |
|
108 | + ); |
|
109 | + $update = $db->prepare( |
|
110 | + "UPDATE `events` |
|
111 | 111 | SET |
112 | 112 | `calendar` = :calendar, |
113 | 113 | `calendar_event[id]` = :calendar_event_id" |
114 | - ) |
|
115 | - } |
|
114 | + ) |
|
115 | + } |
|
116 | 116 | |
117 | - public static function load($id, $calendar) |
|
118 | - { |
|
119 | - $find = static::getDatabase()->prepare( |
|
120 | - "SELECT * |
|
117 | + public static function load($id, $calendar) |
|
118 | + { |
|
119 | + $find = static::getDatabase()->prepare( |
|
120 | + "SELECT * |
|
121 | 121 | FROM `events` |
122 | 122 | WHERE |
123 | 123 | `event_hash` = :event_hash AND |
124 | 124 | `calendar` = :calendar" |
125 | - ); |
|
126 | - $find->execute([ |
|
127 | - 'event_hash' => $id, |
|
128 | - 'calendar' => $calendar |
|
129 | - ]); |
|
130 | - if (($cache = $find->fetch()) !== false) { |
|
131 | - return new Event($cache['event_hash'], $cache['calendar']); |
|
132 | - } |
|
133 | - return null; |
|
134 | - } |
|
125 | + ); |
|
126 | + $find->execute([ |
|
127 | + 'event_hash' => $id, |
|
128 | + 'calendar' => $calendar |
|
129 | + ]); |
|
130 | + if (($cache = $find->fetch()) !== false) { |
|
131 | + return new Event($cache['event_hash'], $cache['calendar']); |
|
132 | + } |
|
133 | + return null; |
|
134 | + } |
|
135 | 135 | } |
@@ -34,13 +34,13 @@ |
||
34 | 34 | if (is_a($veventOrHash, vevent::class)) { |
35 | 35 | $this->vevent = $veventOrHash; |
36 | 36 | } else { |
37 | - $this->hash = (string) $veventOrHash; |
|
37 | + $this->hash = (string)$veventOrHash; |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | if (empty($calendar)) { |
41 | 41 | throw new Exception("Calendar ID required"); |
42 | 42 | } |
43 | - $this->calendar = (string) $calendar; |
|
43 | + $this->calendar = (string)$calendar; |
|
44 | 44 | |
45 | 45 | $this->getHash(); |
46 | 46 | } |