|
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?://)?(' . 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
|
|
|
|
|
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
|
|
|
|
|
62
|
|
|
// courses |
|
63
|
|
|
} elseif (isset($match[7])) { |
|
64
|
|
|
$this->context = CanvasContext::COURSE(); |
|
65
|
|
|
$this->id = $match[7]; |
|
|
|
|
|
|
66
|
|
|
$this->verificationUrl = "courses/{$this->id}"; |
|
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"; |
|
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
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getCanonicalUrl() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->canonicalUrl; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getContext() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->context; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getId() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->id; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getVerificationUrl() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->verificationUrl; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function __toString() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->getCanonicalUrl(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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: