forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfavorites_controller.rb
More file actions
280 lines (257 loc) · 9.31 KB
/
Copy pathfavorites_controller.rb
File metadata and controls
280 lines (257 loc) · 9.31 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
#
# Copyright (C) 2011 - present 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/>.
# @API Favorites
#
# @model Favorite
# {
# "id": "Favorite",
# "description": "",
# "required": [""],
# "properties": {
# "context_id": {
# "description": "The ID of the object the Favorite refers to",
# "example": 1170,
# "type": "integer"
# },
# "context_type": {
# "description": "The type of the object the Favorite refers to (currently, only 'Course' is supported)",
# "example": "Course",
# "type": "string",
# "allowableValues": {
# "values": [
# "Course"
# ]
# }
# }
# }
# }
#
class FavoritesController < ApplicationController
before_action :require_user
before_action :check_defaults, :only => [:remove_favorite_course]
after_action :touch_user, :only => [:add_favorite_course, :remove_favorite_course, :reset_course_favorites]
include Api::V1::Favorite
include Api::V1::Course
include Api::V1::Group
# @API List favorite courses
# Retrieve the list of favorite courses for the current user. If the user has not chosen
# any favorites, then a selection of currently enrolled courses will be returned.
#
# @argument exclude_blueprint_courses [Boolean]
# When set, only return courses that are not configured as blueprint courses.
#
# See the {api:CoursesController#index List courses API} for details on accepted include[] parameters.
#
# @returns [Course]
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/courses \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def list_favorite_courses
includes = Set.new(Array(params[:include]))
courses = @current_user.menu_courses
if courses.any? && value_to_boolean(params[:exclude_blueprint_courses])
mc_ids = MasterCourses::MasterTemplate.active.where(:course_id => courses).pluck(:course_id)
courses.reject!{|c| mc_ids.include?(c.id)}
end
render :json => courses.map { |course|
enrollments = nil
unless Array(params[:exclude]).include?('enrollments')
enrollments = course.current_enrollments.where(:user_id => @current_user).to_a
if includes.include?('observed_users') &&
enrollments.any?(&:assigned_observer?)
enrollments.concat(ObserverEnrollment.observed_enrollments_for_courses(course, @current_user))
end
end
course_json(course, @current_user, session, includes, enrollments)
}
end
# @API List favorite groups
# Retrieve the list of favorite groups for the current user. If the user has not chosen
# any favorites, then a selection of groups that the user is a member of will be returned.
#
#
#
# @returns [Group]
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/groups \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def list_favorite_groups
fave_group_memberships = nil
@current_user.shard.activate do
fave_group_memberships = @current_user.groups.active.shard(@current_user).where(id: @current_user.favorite_context_ids("Group"))
end
if fave_group_memberships.any?
render :json => fave_group_memberships.map{ |g| group_json(g, @current_user,session)}
else
render :json => @current_user.groups.active.shard(@current_user).map{ |g| group_json(g, @current_user,session)}
end
end
# @API Add course to favorites
# Add a course to the current user's favorites. If the course is already
# in the user's favorites, nothing happens.
#
# @argument id [Required, String]
# The ID or SIS ID of the course to add. The current user must be
# registered in the course.
#
# @returns Favorite
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/courses/1170 \
# -X POST \
# -H 'Authorization: Bearer <ACCESS_TOKEN>' \
# -H 'Content-Length: 0'
#
def add_favorite_course
course = api_find(Course, params[:id])
fave = nil
@current_user.shard.activate do
Favorite.unique_constraint_retry do
fave = @current_user.favorites.where(:context_type => 'Course', :context_id => course).first
fave ||= @current_user.favorites.create!(:context => course)
end
end
render :json => favorite_json(fave, @current_user, session)
end
# @API Add group to favorites
# Add a group to the current user's favorites. If the group is already
# in the user's favorites, nothing happens.
#
# @argument id [Required, String]
# The ID or SIS ID of the group to add. The current user must be
# a member of the group.
#
# @returns Favorite
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/group/1170 \
# -X POST \
# -H 'Authorization: Bearer <ACCESS_TOKEN>' \
# -H 'Content-Length: 0'
#
def add_favorite_groups
group = api_find(Group, params[:id])
fave = nil
@current_user.shard.activate do
Favorite.unique_constraint_retry do
fave = @current_user.favorites.where(:context_type => 'Group', :context_id => group).first
fave ||= @current_user.favorites.create!(:context => group)
end
end
render :json => favorite_json(fave, @current_user, session)
end
# @API Remove course from favorites
# Remove a course from the current user's favorites.
#
# @argument id [Required, String]
# the ID or SIS ID of the course to remove
#
# @returns Favorite
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/courses/1170 \
# -X DELETE \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def remove_favorite_course
# allow removing a Favorite whose context object no longer exists
# but also allow referencing by sis id, if possible
courses = api_find_all(Course, [params[:id]])
course_id = Shard.relative_id_for(courses.any? ? courses.first.id : params[:id], Shard.current, @current_user.shard)
fave = @current_user.favorites.where(:context_type => 'Course', :context_id => course_id).first
if fave
result = favorite_json(fave, @current_user, session)
fave.destroy
render :json => result
else
# can't really return a 404 here without making browsers freak out
# in the Courses UI (it's easy for the client's state to get out of
# sync with the server's, especially with multiple browsers open)
render :json => {}
end
end
# @API Remove group from favorites
# Remove a group from the current user's favorites.
#
# @argument id [Required, String]
# the ID or SIS ID of the group to remove
#
# @returns Favorite
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/groups/1170 \
# -X DELETE \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def remove_favorite_groups
group = api_find_all(Group, [params[:id]])
group_id= Shard.relative_id_for(group.any? ? group.first.id : params[:id], Shard.current, @current_user.shard)
fave = @current_user.favorites.where(:context_type => 'Group', :context_id => group_id).first
if fave
result = favorite_json(fave, @current_user, session)
fave.destroy
render :json => result
else
# can't really return a 404 here without making browsers freak out
# in the Group UI (it's easy for the client's state to get out of
# sync with the server's, especially with multiple browsers open)
render :json => {}
end
end
# @API Reset course favorites
# Reset the current user's course favorites to the default
# automatically generated list of enrolled courses
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/courses \
# -X DELETE \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def reset_course_favorites
@current_user.favorites.by('Course').destroy_all
render :json => { :status => 'ok' }
end
# @API Reset group favorites
# Reset the current user's group favorites to the default
# automatically generated list of enrolled group
#
# @example_request
# curl https://<canvas>/api/v1/users/self/favorites/group \
# -X DELETE \
# -H 'Authorization: Bearer <ACCESS_TOKEN>'
#
def reset_groups_favorites
@current_user.favorites.by('Group').destroy_all
render :json => { :status => 'ok' }
end
protected
# When we have other favorites, this needs to be modified to handle the other
# types, rather than just courses.
def check_defaults
return unless @current_user.favorites.count == 0
@current_user.menu_courses.each do |course|
@current_user.favorites.create :context => course
end
end
def touch_user
# Menu is cached, clear it
@current_user.touch
end
end