1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\CanvasICSSync\SyncIntoCanvas; |
4
|
|
|
|
5
|
|
|
class CalendarContext |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The canonical URL for this context in Canvas |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $canonicalUrl; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The context for this calendar in Canvas (user, group, course) |
15
|
|
|
* @var CanvasContext |
16
|
|
|
*/ |
17
|
|
|
protected $context; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Unique ID for this Canvas context |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* URL to verify this context against API |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $verificationUrl; |
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
|
|
|
/* |
44
|
|
|
* TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all) |
45
|
|
|
*/ |
46
|
|
|
/* |
47
|
|
|
* FIXME: users aren't working |
48
|
|
|
*/ |
49
|
|
|
/* |
50
|
|
|
* TODO: it would probably be better to look up users by email address than URL |
51
|
|
|
*/ |
52
|
|
|
/* get the context (user, course or group) for the canvas URL */ |
53
|
|
|
if (preg_match('%(https?://)?(' . |
54
|
|
|
/* |
55
|
|
|
* FIXME not clear that we should be using $_SESSION |
56
|
|
|
*/ |
57
|
|
|
parse_url($_SESSION[CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) { |
58
|
|
|
$this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953 |
59
|
|
|
|
60
|
|
|
// course or account groups |
61
|
|
|
if (isset($match[9]) || isset($match[11])) { |
62
|
|
|
$this->context = CanvasContext::GROUP(); // used for context_code in events |
63
|
|
|
$this->id = ($match[9] > $match[11] ? $match[9] : $match[11]); |
|
|
|
|
64
|
|
|
$this->verificationUrl = "groups/{$this->id}"; // used once to look up the object to be sure it really exists |
65
|
|
|
|
66
|
|
|
// courses |
67
|
|
|
} elseif (isset($match[7])) { |
68
|
|
|
$this->context = CanvasContext::COURSE(); |
69
|
|
|
$this->id = $match[7]; |
|
|
|
|
70
|
|
|
$this->verificationUrl = "courses/{$this->id}"; |
71
|
|
|
|
72
|
|
|
// users |
73
|
|
|
} elseif (isset($match[5])) { |
74
|
|
|
$this->context = CanvasContext::USER(); |
75
|
|
|
$this->id = $match[5]; |
|
|
|
|
76
|
|
|
$this->verificationUrl = "users/{$this->id}/profile"; |
77
|
|
|
|
78
|
|
|
// we're somewhere where we don't know where we are |
79
|
|
|
} else { |
80
|
|
|
throw new Exception( |
81
|
|
|
"'$canvasUrl' is not a recognizable calendar context" |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
throw new Exception( |
86
|
|
|
"'$canvasUrl' is not recognized as a URL to a calendar context on this Canvas instance" |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getCanonicalUrl() |
91
|
|
|
{ |
92
|
|
|
return $this->canonicalUrl; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getContext() |
96
|
|
|
{ |
97
|
|
|
return $this->context; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getId() |
101
|
|
|
{ |
102
|
|
|
return $this->id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getVerificationUrl() |
106
|
|
|
{ |
107
|
|
|
return $this->verificationUrl; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: