diff --git a/app/controllers/peer_progress_controller.rb b/app/controllers/peer_progress_controller.rb new file mode 100644 index 0000000000..145c25e4f9 --- /dev/null +++ b/app/controllers/peer_progress_controller.rb @@ -0,0 +1,33 @@ +class PeerProgressController < ApplicationController + def show + student_id = params[:student_id].to_i + unit_id = params[:unit_id].to_i + + project = Project.find_by(user_id: student_id, unit_id: unit_id) + + return render json: { error: "Project not found" }, status: :not_found unless project + + target_grade = project.target_grade + + required_tasks = project.unit.tasks.where(project_id: project.id).select do |task| + task.task_definition.present? && + task.task_definition.target_grade.present? && + task.task_definition.target_grade <= target_grade + end + + tasks_required = required_tasks.count + tasks_completed = required_tasks.select(&:complete?).count + + progress_percentage = + tasks_required.positive? ? ((tasks_completed.to_f / tasks_required) * 100).round : 0 + + render json: { + student_id: student_id, + unit_id: unit_id, + target_grade: project.target_grade_desc, + tasks_completed: tasks_completed, + tasks_required: tasks_required, + progress_percentage: progress_percentage + } + end +end diff --git a/config/routes.rb b/config/routes.rb index ea52a79000..518a19da57 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ get 'api/submission/unit/:id/task_definitions/:task_def_id/download_submissions', to: 'task_downloads#index' get 'api/submission/unit/:id/task_definitions/:task_def_id/student_pdfs', to: 'task_submission_pdfs#index' get 'api/units/:id/all_resources', to: 'lecture_resource_downloads#index' + get 'api/peer_progress/:unit_id/:student_id', to: 'peer_progress#show' mount ApiRoot => '/' mount GrapeSwaggerRails::Engine => '/api/docs'