forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_expander.rb
More file actions
887 lines (787 loc) · 31.6 KB
/
Copy pathvariable_expander.rb
File metadata and controls
887 lines (787 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#
# Copyright (C) 2015 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
# NOTE: To update the VariableExpansion docs run `script/generate_lti_variable_substitution_markdown`
module Lti
class VariableExpander
SUBSTRING_REGEX = /(?<=\${).*?(?=})/.freeze #matches only the stuff inside `${}`
attr_reader :context, :root_account, :controller, :current_user
attr_accessor :current_pseudonym, :content_tag, :assignment,
:tool_setting_link_id, :tool_setting_binding_id, :tool_setting_proxy_id, :tool, :attachment,
:collaboration
def self.register_expansion(name, permission_groups, expansion_proc, *guards)
@expansions ||= {}
@expansions["$#{name}".to_sym] = VariableExpansion.new(name, permission_groups, expansion_proc, *guards)
end
def self.expansions
@expansions || {}
end
CONTROLLER_GUARD = -> { !!@controller }
COURSE_GUARD = -> { @context.is_a? Course }
TERM_START_DATE_GUARD = -> { @context.is_a?(Course) && @context.enrollment_term &&
@context.enrollment_term.start_at }
USER_GUARD = -> { @current_user }
SIS_USER_GUARD = -> { @current_user && @current_user.pseudonym && @current_user.pseudonym.sis_user_id }
PSEUDONYM_GUARD = -> { sis_pseudonym }
ENROLLMENT_GUARD = -> { @current_user && @context.is_a?(Course) }
ROLES_GUARD = -> { @current_user && (@context.is_a?(Course) || @context.is_a?(Account)) }
CONTENT_TAG_GUARD = -> { @content_tag }
ASSIGNMENT_GUARD = -> { @assignment }
COLLABORATION_GUARD = -> { @collaboration }
MEDIA_OBJECT_GUARD = -> { @attachment && @attachment.media_object}
USAGE_RIGHTS_GUARD = -> { @attachment && @attachment.usage_rights}
MEDIA_OBJECT_ID_GUARD = -> {@attachment && (@attachment.media_object || @attachment.media_entry_id )}
LTI1_GUARD = -> { @tool.is_a?(ContextExternalTool) }
MASQUERADING_GUARD = -> { !!@controller && @controller.logged_in_user != @current_user }
def initialize(root_account, context, controller, opts = {})
@root_account = root_account
@context = context
@controller = controller
@request = controller.request if controller
opts.each { |opt, val| instance_variable_set("@#{opt}", val) }
end
def lti_helper
@lti_helper ||= Lti::SubstitutionsHelper.new(@context, @root_account, @current_user, @tool)
end
def current_user=(current_user)
@lti_helper = nil
@current_user = current_user
end
def [](key)
k = (key[0] == '$' && key) || "$#{key}"
if (expansion = self.class.expansions[k.respond_to?(:to_sym) && k.to_sym])
expansion.expand(self)
end
end
def expand_variables!(var_hash)
var_hash.update(var_hash) do |_, v|
if (expansion = v.respond_to?(:to_sym) && self.class.expansions[v.to_sym])
expansion.expand(self)
elsif v.respond_to?(:to_s) && v.to_s =~ SUBSTRING_REGEX
expand_substring_variables(v)
else
v
end
end
end
def enabled_capability_params(enabled_capabilities)
enabled_capabilities.each_with_object({}) do |capability, hash|
if (expansion = capability.respond_to?(:to_sym) && self.class.expansions["$#{capability}".to_sym])
hash[expansion.default_name] = expansion.expand(self) if expansion.default_name.present?
end
end
end
# an opaque identifier that uniquely identifies the context of the tool launch
# @launch_parameter context_id
# @example
# ```
# cdca1fe2c392a208bd8a657f8865ddb9ca359534
# ```
register_expansion 'Context.id', [],
-> { Lti::Asset.opaque_identifier_for(@context) },
default_name: 'context_id'
# communicates the kind of browser window/frame where the Canvas has launched a tool
# @launch_parameter launch_presentation_document_target
# @example
# ```
# iframe
# ```
register_expansion 'Message.documentTarget', [],
-> { IMS::LTI::Models::Messages::Message::LAUNCH_TARGET_IFRAME },
default_name: 'launch_presentation_document_target'
# returns the current locale
# @launch_parameter launch_presentation_locale
# @example
# ```
# de
# ```
register_expansion 'Message.locale', [],
-> { I18n.locale || I18n.default_locale },
default_name: 'launch_presentation_locale'
# returns a unique identifier for the Tool Consumer (Canvas)
# @launch_parameter tool_consumer_instance_guid
# @example
# ```
# 0dWtgJjjFWRNT41WdQMvrleejGgv7AynCVm3lmZ2:canvas-lms
# ```
register_expansion 'ToolConsumerInstance.guid', [],
-> { @root_account.lti_guid },
default_name: 'tool_consumer_instance_guid'
# returns the canvas domain for the current context.
# @example
# ```
# canvas.instructure.com
# ```
register_expansion 'Canvas.api.domain', [],
-> { HostUrl.context_host(@root_account, @request.host) },
CONTROLLER_GUARD
# returns the api url for the members of the collaboration
# @example
# ```
# https://canvas.instructure.com/api/v1/collaborations/1/members
# ```
register_expansion 'Canvas.api.collaborationMembers.url', [],
-> { @controller.api_v1_collaboration_members_url(@collaboration) },
CONTROLLER_GUARD,
COLLABORATION_GUARD
# returns the base URL for the current context.
# @example
# ```
# https://canvas.instructure.com
# ```
register_expansion 'Canvas.api.baseUrl', [],
-> { "#{@request.scheme}://#{HostUrl.context_host(@root_account, @request.host)}" },
CONTROLLER_GUARD
# returns the URL for the membership service associated with the current context.
#
# This variable is for future use only. Complete support for the IMS Membership Service has not been added to Canvas. This will be updated when we fully support and certify the IMS Membership Service.
# @example
# ```
# https://canvas.instructure.com/api/lti/courses/1/membership_service
# ```
register_expansion 'ToolProxyBinding.memberships.url', [],
-> { @controller.polymorphic_url([@context, :membership_service]) },
CONTROLLER_GUARD,
-> { @context.is_a?(Course) || @context.is_a?(Group) }
# returns the account id for the current context.
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.account.id', [],
-> { lti_helper.account.id }
# returns the account name for the current context.
# @example
# ```
# School Name
# ```
register_expansion 'Canvas.account.name', [],
-> { lti_helper.account.name }
# returns the account's sis source id for the current context.
# @example
# ```
# sis_account_id_1234
# ```
register_expansion 'Canvas.account.sisSourceId', [],
-> { lti_helper.account.sis_source_id }
# returns the Root Account ID for the current context.
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.rootAccount.id', [],
-> { @root_account.id }
# returns the root account's sis source id for the current context.
# @example
# ```
# sis_account_id_1234
# ```
register_expansion 'Canvas.rootAccount.sisSourceId', [],
-> { @root_account.sis_source_id }
# returns the URL for the external tool that was launched. Only available for LTI 1.
# @example
# ```
# http://example.url/path
# ```
register_expansion 'Canvas.externalTool.url', [],
-> { @controller.named_context_url(@tool.context, :api_v1_context_external_tools_update_url,
@tool.id, include_host:true) },
CONTROLLER_GUARD,
LTI1_GUARD
# returns the URL for the common css file.
# @example
# ```
# http://example.url/path.css
# ```
register_expansion 'Canvas.css.common', [],
-> { URI.parse(@request.url)
.merge(@controller.view_context.stylesheet_path(@controller.css_url_for(:common))).to_s },
CONTROLLER_GUARD
# returns the shard id for the current context.
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.shard.id', [],
-> { Shard.current.id }
# returns the root account's global id for the current context.
# @duplicates Canvas.user.globalId
# @example
# ```
# 123400000000123
# ```
register_expansion 'Canvas.root_account.global_id', [],
-> { @root_account.global_id }
# returns the root account id for the current context.
# @deprecated
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.root_account.id', [],
-> { @root_account.id }
# returns the account uuid for the current context.
# @example
# ```
# Ioe3sJPt0KZp9Pw6xAvcHuLCl0z4TvPKP0iIOLbo
# ```
register_expansion 'vnd.Canvas.root_account.uuid', [],
-> { @root_account.uuid },
default_name: 'vnd_canvas_root_account_uuid'
# returns the root account sis source id for the current context.
# @deprecated
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.root_account.sisSourceId', [],
-> { @root_account.sis_source_id }
# returns the current course id.
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.course.id', [],
-> { @context.id },
COURSE_GUARD
# returns the current course name.
# @example
# ```
# Course Name
# ```
register_expansion 'Canvas.course.name', [],
-> { @context.name },
COURSE_GUARD
# returns the current course sis source id.
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.course.sisSourceId', [],
-> { @context.sis_source_id },
COURSE_GUARD
# returns the current course start date.
# @example
# ```
# YYY-MM-DD HH:MM:SS -0700
# ```
register_expansion 'Canvas.course.startAt', [],
-> { @context.start_at },
COURSE_GUARD
# returns the current course workflow state. Workflow states of "claimed" or "created"
# indicate an unpublished course.
# @example
# ```
# active
# ```
register_expansion 'Canvas.course.workflowState', [],
-> { @context.workflow_state },
COURSE_GUARD
# returns the current course's term start date.
# @example
# ```
# YYY-MM-DD HH:MM:SS -0700
# ```
register_expansion 'Canvas.term.startAt', [],
-> { @context.enrollment_term.start_at },
TERM_START_DATE_GUARD
# returns the current course sis source id
# to return the section source id use Canvas.course.sectionIds
# @launch_parameter lis_course_section_sourcedid
# @example
# ```
# 1234
# ```
register_expansion 'CourseSection.sourcedId', [],
-> { @context.sis_source_id },
COURSE_GUARD,
default_name: 'lis_course_section_sourcedid'
# returns the current course enrollment state
# @example
# ```
# active
# ```
register_expansion 'Canvas.enrollment.enrollmentState', [],
-> { lti_helper.enrollment_state },
COURSE_GUARD
# returns the current course membership roles
# @example
# ```
# StudentEnrollment
# ```
register_expansion 'Canvas.membership.roles', [],
-> { lti_helper.current_canvas_roles },
ROLES_GUARD
# This is a list of IMS LIS roles should have a different key
# @example
# ```
# urn:lti:sysrole:ims/lis/None
# ```
register_expansion 'Canvas.membership.concludedRoles', [],
-> { lti_helper.concluded_lis_roles },
COURSE_GUARD
# Returns the context ids from the course that the current course was copied from (excludes cartridge imports).
#
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.course.previousContextIds', [],
-> { lti_helper.previous_lti_context_ids },
COURSE_GUARD
# Returns the course ids of the course that the current course was copied from (excludes cartridge imports).
#
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.course.previousCourseIds', [],
-> { lti_helper.previous_course_ids },
COURSE_GUARD
# Returns the full name of the launching user.
# @launch_parameter lis_person_name_full
# @example
# ```
# John Doe
# ```
register_expansion 'Person.name.full', [],
-> { @current_user.name },
USER_GUARD,
default_name: 'lis_person_name_full'
# Returns the last name of the launching user.
# @launch_parameter lis_person_name_family
# @example
# ```
# Doe
# ```
register_expansion 'Person.name.family', [],
-> { @current_user.last_name },
USER_GUARD,
default_name: 'lis_person_name_family'
# Returns the first name of the launching user.
# @launch_parameter lis_person_name_given
# @example
# ```
# John
# ```
register_expansion 'Person.name.given', [],
-> { @current_user.first_name },
USER_GUARD,
default_name: 'lis_person_name_given'
# Returns the primary email of the launching user.
# @launch_parameter lis_person_contact_email_primary
# @example
# ```
# john.doe@example.com
# ```
register_expansion 'Person.email.primary', [],
-> { lti_helper.email },
USER_GUARD,
default_name: 'lis_person_contact_email_primary'
# Returns the institution assigned email of the launching user.
# @example
# ```
# john.doe@example.com
# ```
register_expansion 'vnd.Canvas.Person.email.sis', [],
-> {lti_helper.sis_email}, SIS_USER_GUARD
# Returns the name of the timezone of the launching user.
# @example
# ```
# America/Denver
# ```
register_expansion 'Person.address.timezone', [],
-> { Time.zone.tzinfo.name },
USER_GUARD
# Returns the profile picture URL of the launching user.
# @launch_parameter user_image
# @example
# ```
# https://example.com/picture.jpg
# ```
register_expansion 'User.image', [],
-> { @current_user.avatar_url },
USER_GUARD,
default_name: 'user_image'
# Returns the Canvas user_id of the launching user.
# @duplicates Canvas.user.id
# @launch_parameter user_id
# @example
# ```
# 420000000000042
# ```
register_expansion 'User.id', [],
-> { @current_user.id },
USER_GUARD,
default_name: 'user_id'
# Returns the Canvas user_id of the launching user.
# @duplicates User.id
# @example
# ```
# 420000000000042
# ```
register_expansion 'Canvas.user.id', [],
-> { @current_user.id },
USER_GUARD
# Returns the users preference for high contrast colors (an accessibility feature).
# @example
# ```
# false
# ```
register_expansion 'Canvas.user.prefersHighContrast', [],
-> { @current_user.prefers_high_contrast? ? 'true' : 'false' },
USER_GUARD
# returns the context ids for the groups the user belongs to in the course.
# @example
# ```
# 1c16f0de65a080803785ecb3097da99872616f0d,d4d8d6ae1611e2c7581ce1b2f5c58019d928b79d,...
# ```
register_expansion 'Canvas.group.contextIds', [],
-> { @current_user.groups.active.where(context_type: 'Course', context_id: @context.id).map do |g|
Lti::Asset.opaque_identifier_for(g)
end.join(',') },
-> { @current_user && @context.is_a?(Course) }
# Returns the [IMS LTI membership service](https://www.imsglobal.org/specs/ltimemv1p0/specification-3) roles for filtering via query parameters.
# @launch_parameter roles
# @example
# ```
# http://purl.imsglobal.org/vocab/lis/v2/institution/person#Administrator
# ```
register_expansion 'Membership.role', [],
-> { lti_helper.all_roles('lis2') },
USER_GUARD,
default_name: 'roles'
# Returns list of [LIS role full URNs](https://www.imsglobal.org/specs/ltiv1p0/implementation-guide#toc-16).
# @duplicates ext_roles which is sent by default
# @example
# ```
# urn:lti:instrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Instructor,urn:lti:sysrole:ims/lis/SysAdmin,urn:lti:sysrole:ims/lis/User
# ```
register_expansion 'Canvas.xuser.allRoles', [],
-> { lti_helper.all_roles }
# Returns the Canvas global user_id of the launching user.
# @duplicates Canvas.root_account.global_id
# @example
# ```
# 420000000000042
# ```
register_expansion 'Canvas.user.globalId', [],
-> { @current_user.global_id},
USER_GUARD
# Returns true for root account admins and false for all other roles.
# @example
# ```
# true
# ```
register_expansion 'Canvas.user.isRootAccountAdmin', [],
-> { @current_user.roles(@root_account).include? 'root_admin' },
USER_GUARD
# Username/Login ID for the primary pseudonym for the user for the account.
# This may not be the pseudonym the user is actually logged in with.
# @duplicates Canvas.user.loginId
# @example
# ```
# jdoe
# ```
register_expansion 'User.username', [],
-> { sis_pseudonym.unique_id },
PSEUDONYM_GUARD
# Username/Login ID for the primary pseudonym for the user for the account.
# This may not be the pseudonym the user is actually logged in with.
# @duplicates User.username
# @example
# ```
# jdoe
# ```
register_expansion 'Canvas.user.loginId', [],
-> { sis_pseudonym.unique_id },
PSEUDONYM_GUARD
# Returns the sis source id for the primary pseudonym for the user for the account
# This may not be the pseudonym the user is actually logged in with.
# @duplicates Person.sourcedId
# @example
# ```
# sis_user_42
# ```
register_expansion 'Canvas.user.sisSourceId', [],
-> { sis_pseudonym.sis_user_id },
PSEUDONYM_GUARD
# Returns the integration id for the primary pseudonym for the user for the account
# This may not be the pseudonym the user is actually logged in with.
# @example
# ```
# integration_user_42
# ```
register_expansion 'Canvas.user.sisIntegrationId', [],
-> { sis_pseudonym.integration_id },
PSEUDONYM_GUARD
# Returns the sis source id for the primary pseudonym for the user for the account
# This may not be the pseudonym the user is actually logged in with.
# @duplicates Canvas.user.sisSourceId
# @example
# ```
# sis_user_42
# ```
register_expansion 'Person.sourcedId', [],
-> { sis_pseudonym.sis_user_id },
PSEUDONYM_GUARD,
default_name: 'lis_person_sourcedid'
# Returns the logout service url for the user.
# This is the pseudonym the user is actually logged in as.
# It may not hold all the sis info needed in other launch substitutions.
# @example
# ```
# https://<domain>.instructure.com/api/lti/v1/logout_service/<external_tool_id>-<user_id>-<current_unix_timestamp>-<opaque_string>
# ```
register_expansion 'Canvas.logoutService.url', [],
-> { @controller.lti_logout_service_url(Lti::LogoutService.create_token(@tool, @current_pseudonym)) },
CONTROLLER_GUARD,
-> { @current_pseudonym && @tool }
# Returns the Canvas user_id for the masquerading user.
# This is the pseudonym the user is actually logged in as.
# It may not hold all the sis info needed in other launch substitutions.
#
# @example
# ```
# 420000000000042
# ```
register_expansion 'Canvas.masqueradingUser.id', [],
-> { @controller.logged_in_user.id },
MASQUERADING_GUARD
# Returns the 40 character opaque user_id for masquerading user.
# This is the pseudonym the user is actually logged in as.
# It may not hold all the sis info needed in other launch substitutions.
#
# @example
# ```
# da12345678cb37ba1e522fc7c5ef086b7704eff9
# ```
register_expansion 'Canvas.masqueradingUser.userId', [],
-> { @tool.opaque_identifier_for(@controller.logged_in_user) },
MASQUERADING_GUARD
# Returns the xapi url for the user.
# @example
# ```
# https://<domain>.instructure.com/api/lti/v1/xapi/<external_tool_id>-<user_id>-<course_id>-<current_unix_timestamp>-<opaque_id>
# ```
register_expansion 'Canvas.xapi.url', [],
-> { @controller.lti_xapi_url(Lti::AnalyticsService.create_token(@tool, @current_user, @context)) },
-> { @current_user && @context.is_a?(Course) && @tool }
# Returns the caliper url for the user.
# @example
# ```
# https://<domain>.instructure.com/api/lti/v1/caliper/<external_tool_id>-<user_id>-<course_id>-<current_unix_timestamp>-<opaque_id>
# ```
register_expansion 'Caliper.url', [],
-> { @controller.lti_caliper_url(Lti::AnalyticsService.create_token(@tool, @current_user, @context)) },
CONTROLLER_GUARD,
-> { @current_user && @context.is_a?(Course) && @tool }
# Returns a comma separated list of section_id's that the user is enrolled in.
#
# @example
# ```
# 42, 43
# ```
register_expansion 'Canvas.course.sectionIds', [],
-> { lti_helper.section_ids },
ENROLLMENT_GUARD
# Returns a comma separated list of section sis_id's that the user is enrolled in.
#
# @example
# ```
# section_sis_id_1, section_sis_id_2
# ```
register_expansion 'Canvas.course.sectionSisSourceIds', [],
-> { lti_helper.section_sis_ids },
ENROLLMENT_GUARD
# Returns the module_id that the module item was launched from.
#
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.module.id', [],
-> {
@content_tag.context_module_id
},
CONTENT_TAG_GUARD
# Returns the module_item_id of the module item that was launched.
#
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.moduleItem.id', [],
-> {
@content_tag.id
},
CONTENT_TAG_GUARD
# Returns the assignment_id of the assignment that was launched.
#
# @example
# ```
# 1234
# ```
register_expansion 'Canvas.assignment.id', [],
-> { @assignment.id },
ASSIGNMENT_GUARD
# Returns the title of the assignment that was launched.
#
# @example
# ```
# Deep thought experiment
# ```
register_expansion 'Canvas.assignment.title', [],
-> { @assignment.title },
ASSIGNMENT_GUARD
# Returns the points possible of the assignment that was launched.
#
# @example
# ```
# 100
# ```
register_expansion 'Canvas.assignment.pointsPossible', [],
-> { TextHelper.round_if_whole(@assignment.points_possible) },
ASSIGNMENT_GUARD
# @deprecated in favor of ISO8601
register_expansion 'Canvas.assignment.unlockAt', [],
-> { @assignment.unlock_at },
ASSIGNMENT_GUARD
# @deprecated in favor of ISO8601
register_expansion 'Canvas.assignment.lockAt', [],
-> { @assignment.lock_at },
ASSIGNMENT_GUARD
# @deprecated in favor of ISO8601
register_expansion 'Canvas.assignment.dueAt', [],
-> { @assignment.due_at },
ASSIGNMENT_GUARD
# Returns the `unlock_at` date of the assignment that was launched.
# Only available when launched as an assignment with an `unlock_at` set.
# @example
# ```
# YYYY-MM-DDT07:00:00Z
# ```
register_expansion 'Canvas.assignment.unlockAt.iso8601', [],
-> { @assignment.unlock_at.utc.iso8601 },
-> {@assignment && @assignment.unlock_at.present?}
# Returns the `lock_at` date of the assignment that was launched.
# Only available when launched as an assignment with a `lock_at` set.
#
# @example
# ```
# YYYY-MM-DDT07:00:00Z
# ```
register_expansion 'Canvas.assignment.lockAt.iso8601', [],
-> { @assignment.lock_at.utc.iso8601 },
-> {@assignment && @assignment.lock_at.present?}
# Returns the `due_at` date of the assignment that was launched.
# Only available when launched as an assignment with a `due_at` set.
#
# @example
# ```
# YYYY-MM-DDT07:00:00Z
# ```
register_expansion 'Canvas.assignment.dueAt.iso8601', [],
-> { @assignment.due_at.utc.iso8601 },
-> {@assignment && @assignment.due_at.present?}
# Returns true if the assignment that was launched is published.
# Only available when launched as an assignment.
# @example
# ```
# true
# ```
register_expansion 'Canvas.assignment.published', [],
-> { @assignment.workflow_state == 'published' },
ASSIGNMENT_GUARD
# Returns the endpoint url for accessing link-level tool settings
# Only available for LTI 2.0
# @example
# ```
# https://<domain>.instructure.com/api/lti/tool_settings/<link_id>
# ```
register_expansion 'LtiLink.custom.url', [],
-> { @controller.show_lti_tool_settings_url(@tool_setting_link_id) },
CONTROLLER_GUARD,
-> { @tool_setting_link_id }
# Returns the endpoint url for accessing context-level tool settings
# Only available for LTI 2.0
# @example
# ```
# https://<domain>.instructure.com/api/lti/tool_settings/<binding_id>
# ```
register_expansion 'ToolProxyBinding.custom.url', [],
-> { @controller.show_lti_tool_settings_url(@tool_setting_binding_id) },
CONTROLLER_GUARD,
-> { @tool_setting_binding_id }
# Returns the endpoint url for accessing system-wide tool settings
# Only available for LTI 2.0
# @example
# ```
# https://<domain>.instructure.com/api/lti/tool_settings/<proxy_id>
# ```
register_expansion 'ToolProxy.custom.url', [],
-> { @controller.show_lti_tool_settings_url(@tool_setting_proxy_id) },
-> { !!@controller && @tool_setting_proxy_id }
# Returns the [Tool Consumer Profile](https://www.imsglobal.org/specs/ltiv2p0/implementation-guide#toc-46) url for the tool.
# Only available for LTI 2.0
# @example
# ```
# https://<domain>.instructure.com/api/lti/courses/<course_id>/tool_consumer_profile/<opaque_id>
# https://<domain>.instructure.com/api/lti/accounts/<account_id>/tool_consumer_profile/<opaque_id>
# ```
register_expansion 'ToolConsumerProfile.url', [],
-> { @controller.polymorphic_url([@tool.context, :tool_consumer_profile], tool_consumer_profile_id: Lti::ToolConsumerProfile::DEFAULT_TCP_UUID)},
CONTROLLER_GUARD,
-> { @tool && @tool.is_a?(Lti::ToolProxy) }
register_expansion 'Canvas.file.media.id', [],
-> { (@attachment.media_object && @attachment.media_object.media_id) || @attachment.media_entry_id },
MEDIA_OBJECT_ID_GUARD
register_expansion 'Canvas.file.media.type', [],
-> {@attachment.media_object.media_type},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.duration', [],
-> {@attachment.media_object.duration},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.size', [],
-> {@attachment.media_object.total_size},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.title', [],
-> {@attachment.media_object.user_entered_title || @attachment.media_object.title},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.usageRights.name', [],
-> {@attachment.usage_rights.license_name},
USAGE_RIGHTS_GUARD
register_expansion 'Canvas.file.usageRights.url', [],
-> {@attachment.usage_rights.license_url},
USAGE_RIGHTS_GUARD
register_expansion 'Canvas.file.usageRights.copyrightText', [],
-> {@attachment.usage_rights.legal_copyright},
USAGE_RIGHTS_GUARD
private
def sis_pseudonym
@sis_pseudonym ||= @current_user.find_pseudonym_for_account(@root_account) if @current_user
end
def expand_substring_variables(value)
value.to_s.scan(SUBSTRING_REGEX).inject(value) do |v, match|
substring = "${#{match}}"
v.gsub(substring, (self[match] || substring).to_s)
end
end
end
end