• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MarkUsProject / Markus / 20035011161

08 Dec 2025 04:22PM UTC coverage: 91.522% (+0.01%) from 91.51%
20035011161

Pull #7754

github

web-flow
Merge 5409b7c35 into ba6f02ef8
Pull Request #7754: ISSUE-7711: Add GET - api/.../groups/test_results api route

914 of 1805 branches covered (50.64%)

Branch coverage included in aggregate %.

67 of 67 new or added lines in 4 files covered. (100.0%)

33 existing lines in 2 files now uncovered.

43700 of 46942 relevant lines covered (93.09%)

121.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.41
/app/controllers/api/groups_controller.rb
1
module Api
1✔
2
  # Allows for listing Markus groups for a particular assignment.
3
  # Uses Rails' RESTful routes (check 'rake routes' for the configured routes)
4
  class GroupsController < MainApiController
1✔
5
    # Define default fields to display for index and show methods
6
    DEFAULT_FIELDS = [:id, :group_name].freeze
1✔
7

8
    # Create an assignment's group
9
    # Requires: assignment_id
10
    # Optional: filter, fields
11
    def create
1✔
12
      assignment = Assignment.find(params[:assignment_id])
10✔
13
      begin
14
        group = assignment.add_group_api(params[:new_group_name], params[:members])
10✔
15
        respond_to do |format|
7✔
16
          format.xml do
7✔
17
            render xml: group.to_xml(root: 'group', skip_types: 'true')
7✔
18
          end
19
          format.json { render json: group.to_json }
7✔
20
        end
21
      rescue StandardError => e
22
        render 'shared/http_status', locals: { code: '422', message:
3✔
23
          e.message }, status: :unprocessable_content
24
      end
25
    end
26

27
    # Returns an assignment's groups along with their attributes
28
    # Requires: assignment_id
29
    # Optional: filter, fields
30
    def index
1✔
31
      groups = get_collection(assignment.groups) || return
12✔
32

33
      group_data = include_memberships(groups)
11✔
34

35
      respond_to do |format|
11✔
36
        format.xml do
11✔
37
          render xml: group_data.to_xml(root: 'groups', skip_types: 'true')
4✔
38
        end
39
        format.json { render json: group_data }
18✔
40
      end
41
    end
42

43
    # Returns a single group along with its attributes
44
    # Requires: id
45
    # Optional: fields
46
    def show
1✔
47
      group_data = include_memberships(Group.where(id: record.id))
6✔
48

49
      # We found a grouping for that assignment
50
      respond_to do |format|
6✔
51
        format.xml do
6✔
52
          render xml: group_data.to_xml(root: 'groups', skip_types: 'true')
3✔
53
        end
54
        format.json { render json: group_data }
9✔
55
      end
56
    end
57

58
    # Include student_memberships and user info
59
    def include_memberships(groups)
1✔
60
      groups.joins(groupings: [:assignment, { student_memberships: [:role] }])
17✔
61
            .where('assessments.id': params[:assignment_id])
62
            .pluck_to_hash(*DEFAULT_FIELDS, :membership_status, :role_id)
63
            .group_by { |h| h.slice(*DEFAULT_FIELDS) }
23✔
64
            .map { |k, v| k.merge(members: v.map { |h| h.except(*DEFAULT_FIELDS) }) }
46✔
65
    end
66

67
    def add_members
1✔
68
      if self.grouping.nil?
8✔
69
        # The group doesn't have a grouping associated with that assignment
70
        render 'shared/http_status', locals: { code: '422', message:
2✔
71
          'The group is not involved with that assignment' }, status: :unprocessable_content
72
        return
2✔
73
      end
74

75
      students = current_course.students.joins(:user).where('users.user_name': params[:members])
6✔
76
      students.each do |student|
6✔
77
        set_membership_status = if grouping.student_memberships.empty?
10✔
78
                                  StudentMembership::STATUSES[:inviter]
2✔
79
                                else
80
                                  StudentMembership::STATUSES[:accepted]
8✔
81
                                end
82
        grouping.invite(student.user_name, set_membership_status, invoked_by_instructor: true)
10✔
83
        grouping.reload
10✔
84
      end
85

86
      render 'shared/http_status', locals: { code: '200', message:
6✔
87
        HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
88
    end
89

90
    # Update the group's marks for the given assignment.
91
    def update_marks
1✔
92
      result = self.grouping&.current_submission_used&.get_latest_result
6✔
93
      return page_not_found('No submission exists for that group') if result.nil?
6✔
94

95
      # We shouldn't be able to update marks if marking is already complete.
96
      if result.marking_state == Result::MARKING_STATES[:complete]
6✔
97
        render 'shared/http_status', locals: { code: '404', message:
2✔
98
          'Marking for that submission is already completed' }, status: :not_found
99
        return
2✔
100
      end
101
      matched_criteria = assignment.criteria.where(name: params.keys)
4✔
102
      if matched_criteria.empty?
4✔
103
        render 'shared/http_status', locals: { code: '404', message:
×
104
          'No criteria were found that match that request.' }, status: :not_found
105
        return
×
106
      end
107

108
      matched_criteria.each do |crit|
4✔
109
        mark_to_change = result.marks.find_or_initialize_by(criterion_id: crit.id)
4✔
110
        mark_to_change.mark = params[crit.name] == 'nil' ? nil : params[crit.name].to_f
4✔
111
        unless mark_to_change.save
4✔
112
          # Some error occurred (including invalid mark)
113
          render 'shared/http_status', locals: { code: '500', message:
×
114
            mark_to_change.errors.full_messages.first }, status: :internal_server_error
115
          return
×
116
        end
117
      end
118
      result.save
4✔
119
      render 'shared/http_status', locals: { code: '200', message:
4✔
120
        HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
121
    end
122

123
    def create_extra_marks
1✔
124
      result = self.grouping&.current_submission_used&.get_latest_result
5✔
125
      return page_not_found('No submission exists for that group') if result.nil?
5✔
126

127
      begin
128
        ExtraMark.create!(result_id: result.id, extra_mark: params[:extra_marks],
4✔
129
                          description: params[:description], unit: ExtraMark::POINTS)
130
      rescue ActiveRecord::RecordInvalid => e
131
        # Some error occurred
132
        render 'shared/http_status', locals: { code: '500', message:
2✔
133
            e.message }, status: :internal_server_error
134
        return
2✔
135
      end
136
      render 'shared/http_status', locals: { code: '200', message:
2✔
137
          'Extra mark created successfully' }, status: :ok
138
    end
139

140
    def remove_extra_marks
1✔
141
      result = self.grouping&.current_submission_used&.get_latest_result
5✔
142
      return page_not_found('No submission exists for that group') if result.nil?
5✔
143
      extra_mark = ExtraMark.find_by(result_id: result.id,
4✔
144
                                     description: params[:description],
145
                                     extra_mark: params[:extra_marks])
146
      if extra_mark.nil?
4✔
147
        render 'shared/http_status', locals: { code: '404', message:
2✔
148
            'No such Extra Mark exist for that result' }, status: :not_found
149
        return
2✔
150
      end
151
      begin
152
        extra_mark.destroy
2✔
153
      rescue ActiveRecord::RecordNotDestroyed => e
154
        # Some other error occurred
155
        render 'shared/http_status', locals: { code: '500', message:
×
156
            e.message }, status: :internal_server_error
157
        return
×
158
      end
159
      # Successfully deleted the Extra Mark; render success
160
      render 'shared/http_status', locals: { code: '200', message:
2✔
161
          'Extra mark removed successfully' }, status: :ok
162
    end
163

164
    def annotations
1✔
165
      if record # this is a member route
2✔
166
        grouping_relation = assignment.groupings.where(group_id: record.id)
2✔
167
      else # this is a collection route
168
        grouping_relation = assignment.groupings
×
169
      end
170

171
      pluck_keys = ['annotations.type as type',
2✔
172
                    'annotation_texts.content as content',
173
                    'submission_files.filename as filename',
174
                    'submission_files.path as path',
175
                    'annotations.page as page',
176
                    'group_id',
177
                    'annotation_categories.annotation_category_name as category',
178
                    'annotations.creator_id as creator_id',
179
                    'annotation_texts.creator_id as content_creator_id',
180
                    'annotations.line_end as line_end',
181
                    'annotations.line_start as line_start',
182
                    'annotations.column_start as column_start',
183
                    'annotations.column_end as column_end',
184
                    'annotations.x1 as x1',
185
                    'annotations.y1 as y1',
186
                    'annotations.x2 as x2',
187
                    'annotations.y2 as y2']
188

189
      annotations = grouping_relation.left_joins(current_submission_used:
2✔
190
                                              [submission_files:
191
                                                 [annotations:
192
                                                    [annotation_text: :annotation_category]]])
193
                                     .where(assessment_id: params[:assignment_id])
194
                                     .where.not('annotations.id': nil)
195
                                     .pluck_to_hash(*pluck_keys)
196
      respond_to do |format|
2✔
197
        format.xml do
2✔
198
          render xml: annotations.to_xml(root: 'annotations', skip_types: 'true')
2✔
199
        end
200
        format.json do
2✔
201
          render json: annotations.to_json
×
202
        end
203
      end
204
    end
205

206
    def test_results
1✔
207
      test_runs = grouping&.test_runs&.includes(test_group_results: [:test_group,
8✔
208
                                                                     :test_results])&.order(created_at: :desc)
209

210
      if test_runs.blank?
8✔
211
        return render 'shared/http_status',
1✔
212
                      locals: { code: '404', message: 'No test results found for this group' },
213
                      status: :not_found
214
      end
215

216
      results_data = test_runs.map do |test_run|
7✔
217
        {
218
          id: test_run.id,
8✔
219
          status: test_run.status,
220
          created_at: test_run.created_at,
221
          problems: test_run.problems,
222
          test_groups: test_run.test_group_results.map do |test_group_result|
223
            {
224
              name: test_group_result.test_group.name,
6✔
225
              marks_earned: test_group_result.marks_earned,
226
              marks_total: test_group_result.marks_total,
227
              time: test_group_result.time,
228
              tests: test_group_result.test_results.order(:position).map do |test|
229
                {
230
                  name: test.name,
6✔
231
                  status: test.status,
232
                  marks_earned: test.marks_earned,
233
                  marks_total: test.marks_total,
234
                  output: test.output,
235
                  time: test.time
236
                }
237
              end
238
            }
239
          end
240
        }
241
      end
242

243
      respond_to do |format|
7✔
244
        format.xml { render xml: results_data.to_xml(root: 'test_runs', skip_types: 'true') }
9✔
245
        format.json { render json: results_data }
12✔
246
      end
247
    end
248

249
    def add_annotations
1✔
250
      result = self.grouping&.current_result
1✔
251
      return page_not_found('No submission exists for that group') if result.nil?
1✔
252

253
      force_complete = params.fetch(:force_complete, false)
1✔
254
      # We shouldn't be able to update annotations if marking is already complete, unless forced.
255
      if result.marking_state == Result::MARKING_STATES[:complete] && !force_complete
1✔
UNCOV
256
        return page_not_found('Marking for that submission is already completed')
×
257
      end
258

259
      annotation_texts = []
1✔
260
      annotations = []
1✔
261
      count = result.submission.annotations.count + 1
1✔
262
      annotation_category = nil
1✔
263
      submission_file = nil
1✔
264
      params[:annotations].each_with_index do |annot_params, i|
1✔
265
        if annot_params[:annotation_category_name].nil?
2✔
UNCOV
266
          annotation_category_id = nil
×
267
        else
268
          name = annot_params[:annotation_category_name]
2✔
269
          if annotation_category.nil? || annotation_category.annotation_category_name != name
2✔
270
            annotation_category = assignment.annotation_categories.find_or_create_by(
1✔
271
              annotation_category_name: name
272
            )
273
          end
274
          annotation_category_id = annotation_category.id
2✔
275
        end
276
        if submission_file.nil? || submission_file.filename != annot_params[:filename]
2✔
277
          submission_file = result.submission.submission_files.find_by(filename: annot_params[:filename])
1✔
278
        end
279

280
        annotation_texts << {
2✔
281
          content: annot_params[:content],
282
          annotation_category_id: annotation_category_id,
283
          creator_id: current_role.id,
284
          last_editor_id: current_role.id
285
        }
286
        annotations << {
2✔
287
          line_start: annot_params[:line_start],
288
          line_end: annot_params[:line_end],
289
          column_start: annot_params[:column_start],
290
          column_end: annot_params[:column_end],
291
          annotation_text_id: nil,
292
          submission_file_id: submission_file.id,
293
          creator_id: current_role.id,
294
          creator_type: current_role.type,
295
          is_remark: !result.remark_request_submitted_at.nil?,
296
          annotation_number: count + i,
297
          result_id: result.id
298
        }
299
      end
300
      imported = AnnotationText.insert_all! annotation_texts
1✔
301
      imported.rows.zip(annotations) do |t, a|
1✔
302
        a[:annotation_text_id] = t[0]
2✔
303
      end
304
      TextAnnotation.insert_all! annotations
1✔
305
      render 'shared/http_status', locals: { code: '200', message:
1✔
306
        HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
307
    end
308

309
    # Return key:value pairs of group_name:group_id
310
    def group_ids_by_name
1✔
311
      reversed = assignment.groups.pluck(:group_name, :id).to_h
4✔
312
      respond_to do |format|
4✔
313
        format.xml do
4✔
314
          render xml: reversed.to_xml(root: 'groups', skip_types: 'true')
2✔
315
        end
316
        format.json do
4✔
317
          render json: reversed.to_json
2✔
318
        end
319
      end
320
    end
321

322
    # Allow user to set marking state to complete
323
    def update_marking_state
1✔
324
      if has_missing_params?([:marking_state])
4✔
325
        # incomplete/invalid HTTP params
UNCOV
326
        render 'shared/http_status', locals: { code: '422', message:
×
327
            HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
UNCOV
328
        return
×
329
      end
330
      result = self.grouping&.current_submission_used&.get_latest_result
4✔
331
      return page_not_found('No submission exists for that group') if result.nil?
4✔
332
      result.marking_state = params[:marking_state]
4✔
333
      if result.save
4✔
334
        render 'shared/http_status', locals: { code: '200', message:
4✔
335
            HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
336
      else
UNCOV
337
        render 'shared/http_status', locals: { code: '500', message:
×
338
            result.errors.full_messages.first }, status: :internal_server_error
339
      end
340
    end
341

342
    def add_tag
1✔
343
      grouping = self.grouping
2✔
344
      tag = self.assignment.tags.find_by(id: params[:tag_id])
2✔
345
      if tag.nil? || grouping.nil?
2✔
346
        raise 'tag or group not found'
1✔
347
      else
348
        grouping.tags << tag
1✔
349
        render 'shared/http_status', locals: { code: '200', message:
1✔
350
          HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
351
      end
352
    rescue StandardError
353
      render 'shared/http_status', locals: { code: '404', message: I18n.t('tags.not_found') }, status: :not_found
1✔
354
    end
355

356
    def remove_tag
1✔
357
      grouping = self.grouping
2✔
358
      tag = grouping.tags.find_by(id: params[:tag_id])
2✔
359
      if tag.nil? || grouping.nil?
2✔
360
        raise 'tag or grouping not found'
1✔
361
      else
362
        grouping.tags.destroy(tag)
1✔
363
        render 'shared/http_status', locals: { code: '200', message:
1✔
364
          HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
365
      end
366
    rescue StandardError
367
      render 'shared/http_status', locals: { code: '404', message: I18n.t('tags.not_found') }, status: :not_found
1✔
368
    end
369

370
    def extension
1✔
371
      grouping = Grouping.find_by(group_id: params[:id], assignment: params[:assignment_id])
12✔
372
      case request.method
12✔
373
      when 'DELETE'
374
        if grouping.extension.present?
2✔
375
          grouping.extension.destroy!
1✔
376
          # Successfully deleted the extension; render success
377
          render 'shared/http_status', locals: { code: '200', message:
1✔
378
            HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
379
        else
380
          # cannot delete a non existent extension; render failure
381
          render 'shared/http_status', locals: { code: '422', message:
1✔
382
            HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
383
        end
384
      when 'POST'
385
        if grouping.extension.nil?
5✔
386
          extension_values = extension_params
4✔
387
          extension_values[:time_delta] = time_delta_params if extension_values[:time_delta].present?
4✔
388
          grouping.create_extension!(extension_values)
4✔
389
          # Successfully created the extension record; render success
390
          render 'shared/http_status', locals: { code: '201', message:
2✔
391
            HttpStatusHelper::ERROR_CODE['message']['201'] }, status: :created
392
        else
393
          # cannot create extension as it already exists; render failure
394
          render 'shared/http_status', locals: { code: '422', message:
1✔
395
            HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
396
        end
397
      when 'PATCH'
398
        if grouping.extension.present?
4✔
399
          extension_values = extension_params
3✔
400
          extension_values[:time_delta] = time_delta_params if extension_values[:time_delta].present?
3✔
401
          grouping.extension.update!(extension_values)
3✔
402
          # Successfully updated the extension record; render success
403
          render 'shared/http_status', locals: { code: '200', message:
3✔
404
            HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
405
        else
406
          # cannot update extension as it does not exists; render failure
407
          render 'shared/http_status', locals: { code: '422', message:
1✔
408
            HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
409
        end
410
      end
411
    rescue ActiveRecord::RecordInvalid => e
412
      render 'shared/http_status', locals: { code: '422', message: e.to_s }, status: :unprocessable_content
2✔
413
    end
414

415
    def collect_submission
1✔
416
      @grouping = Grouping.find_by(group_id: params[:id], assignment: params[:assignment_id])
3✔
417
      unless @grouping.current_submission_used.nil?
3✔
418
        released = @grouping.current_submission_used.results.exists?(released_to_students: true)
1✔
419
        if released
1✔
420
          render 'shared/http_status', locals: { code: '422', message:
1✔
421
            HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_content
422
          return
1✔
423
        end
424
      end
425

426
      @revision_identifier = params[:revision_identifier]
2✔
427
      apply_late_penalty = if params[:apply_late_penalty].nil?
2✔
UNCOV
428
                             false
×
429
                           else
430
                             params[:apply_late_penalty]
2✔
431
                           end
432
      retain_existing_grading = if params[:retain_existing_grading].nil?
2✔
UNCOV
433
                                  false
×
434
                                else
435
                                  params[:retain_existing_grading]
2✔
436
                                end
437
      SubmissionsJob.perform_now([@grouping],
2✔
438
                                 revision_identifier: @revision_identifier,
439
                                 collect_current: params[:collect_current],
440
                                 apply_late_penalty: apply_late_penalty,
441
                                 retain_existing_grading: retain_existing_grading)
442

443
      render 'shared/http_status', locals: { code: '201', message:
2✔
444
        HttpStatusHelper::ERROR_CODE['message']['201'] }, status: :created
445
    end
446

447
    def add_test_run
1✔
448
      m_logger = MarkusLogger.instance
15✔
449
      # Validate test results against contract schema
450
      validation = TestResultsContract.new.call(params[:test_results].as_json)
15✔
451

452
      if validation.failure?
15✔
453
        return render json: { errors: validation.errors.to_hash }, status: :unprocessable_content
1✔
454
      end
455

456
      # Verify grouping exists and is authorized (grouping helper method handles this)
457
      if grouping.nil?
14✔
UNCOV
458
        return render json: { errors: 'Group not found for this assignment' }, status: :not_found
×
459
      end
460

461
      # Verify submission exists before attempting to create test run
462
      submission = grouping.current_submission_used
14✔
463
      if submission.nil?
14✔
464
        return render json: { errors: 'No submission exists for this grouping' }, status: :unprocessable_content
1✔
465
      end
466

467
      begin
468
        ActiveRecord::Base.transaction do
13✔
469
          test_run = TestRun.create!(
13✔
470
            status: :in_progress,
471
            role: current_role,
472
            grouping: grouping,
473
            submission: submission
474
          )
475

476
          test_run.update_results!(JSON.parse(params[:test_results].to_json))
10✔
477
          render json: { status: 'success', test_run_id: test_run.id }, status: :created
7✔
478
        end
479
      rescue ActiveRecord::RecordInvalid => e
480
        render json: { errors: e.record.errors.full_messages }, status: :unprocessable_content
3✔
481
      rescue StandardError => e
482
        m_logger.log("Test results processing failed: #{e.message}\n#{e.backtrace.join("\n")}")
3✔
483
        render json: { errors: 'Failed to process test results' }, status: :internal_server_error
3✔
484
      end
485
    end
486

487
    private
1✔
488

489
    def assignment
1✔
490
      return @assignment if defined?(@assignment)
80✔
491

492
      @assignment = Assignment.find_by(id: params[:assignment_id])
73✔
493
    end
494

495
    def grouping
1✔
496
      @grouping ||= record.grouping_for_assignment(assignment.id)
112✔
497
    end
498

499
    def annotations_params
1✔
UNCOV
500
      params.require(annotations: [
×
501
        :annotation_category_name,
502
        :column_end,
503
        :column_start,
504
        :content,
505
        :filename,
506
        :line_end,
507
        :line_start
508
      ])
509
    end
510

511
    def time_delta_params
1✔
512
      params = extension_params[:time_delta]
5✔
513
      Extension::PARTS.sum { |part| params[part].to_i.public_send(part) }
25✔
514
    end
515

516
    def extension_params
1✔
517
      params.require(:extension).permit({ time_delta: [:weeks, :days, :hours, :minutes] }, :apply_penalty, :note)
12✔
518
    end
519
  end
520
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc