whitmer / canvabadges

MIT License
29 stars 14 forks source link

No longer get students' progressions #9

Closed Kulgar closed 9 years ago

Kulgar commented 10 years ago

Hi Whitmer!

It's been a while! I have found a new big bug: my canvabadges application was no longer able to retrieve the progressions of students by modules and thus it was no longer validating my badges.

Let me point that out:

# models.rb file, in the check_badge_status function: 
modules_json = CanvasAPI.api_call("/api/v1/courses/#{badge_placement_config.course_id}/modules", self, true) if badge_placement_config.modules_required?
completed_module_ids = modules_json.select{|m| m['completed_at'] }.map{|m| m['id'] }.compact

The first line "modules_json" get all the modules back. But! when you want to retrieve the completed module, the array is empty. Why is that?

It seems that the attribute "completed_at" was no longer sent back from the canvas application. I have digged in a little and found this (from the canvas application):

# In the context_module_api_controller.rb:
if @student
        modules_and_progressions = modules.map { |m| [m, m.evaluate_for(@student, true)] }
else
        modules_and_progressions = modules.map { |m| [m, nil] }
end

Then the application uses this "modules_and_progressions" to give back the "completed_at" attribute if the user is a student. As the json that canvabadges gets back miss this data, I thought that @student was just nil at this point of the canvas application.

@student is set in a before filter in this same controller:

def find_student
    if params[:student_id]
      student_enrollments = @context.student_enrollments.for_user(params[:student_id])
      return render_unauthorized_action unless student_enrollments.any?{|e| e.grants_right?(@current_user, session, :read)}
      @student = student_enrollments.first.user
    elsif @context.grants_right?(@current_user, session, :participate_as_student)
      @student = @current_user
    else
      return true
    end
  end

Ok... So according to this function, the @student is set if params[:student_id] is set when performing the request. Then... I just added this param to the requested url in the check_badge_status function:

modules_json = CanvasAPI.api_call("/api/v1/courses/#{badge_placement_config.course_id}/modules?student_id=#{self.user_id}", self, true) if badge_placement_config.modules_required?

And it did the fix! Progressions were retrieved again and badges properly emitted.

I will make a pull request to add this fix.

If you have any feedbacks, don't hesitate!

Thanks!

whitmer commented 9 years ago

Hi Kulgar, sorry I missed this pull request from forever ago, you're absolutely right and normally I prefer merging in PRs to help share the credit, but I ended up fixing this one when someone else came across it as well. Thanks for your feedback!

Kulgar commented 9 years ago

Hi @whitmer ,

Well, as you can see we (our company) are really intersted in badges and we use them a lot with our own platform. We are facing many issues and thus I modify our badge application code to solve them.

So, that's why I do some PRs :)

Also... just to let you know, I added the i18n gem recently and made all the translations. Would you be interested in a pull request to add translations to canvabadges?

whitmer commented 9 years ago

Totally, that'd be great :-).