Class: Stellar::Gradebook::Student

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/gradebook.rb

Overview

Stellar client scoped to a student’s Gradebook page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, email, name, gradebook) ⇒ Student

Creates a Stellar client scoped to a student’s Gradebook page.

Parameters:

  • url (URI)

    URL to the student’s grade page

  • gradebook (Stellar::Gradebook)

    Stellar client scoped to the course gradebook containing this assignment



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/stellar/gradebook.rb', line 270

def initialize(url, email, name, gradebook)
  @url = url
  @email = email
  @name = name
  @gradebook = gradebook
  @client = gradebook.client
  
  @grades = nil
  @input_names = nil
  @comment = nil
end

Instance Attribute Details

#clientObject (readonly)

Generic Stellar client used to make requests.



263
264
265
# File 'lib/stellar/gradebook.rb', line 263

def client
  @client
end

#emailObject (readonly)

The student’s e-mail.



254
255
256
# File 'lib/stellar/gradebook.rb', line 254

def email
  @email
end

#gradebookObject (readonly)

The course Gradebook that this student entry belongs to.



260
261
262
# File 'lib/stellar/gradebook.rb', line 260

def gradebook
  @gradebook
end

#nameObject (readonly)

The student’s full name.



251
252
253
# File 'lib/stellar/gradebook.rb', line 251

def name
  @name
end

#urlObject (readonly)

URL of the student’s page of grades in the Gradebook.



257
258
259
# File 'lib/stellar/gradebook.rb', line 257

def url
  @url
end

Instance Method Details

#commentString

The instructor’s comment for the student.

Returns:

  • (String)

    the content of the comment



293
294
295
296
# File 'lib/stellar/gradebook.rb', line 293

def comment
  reload! unless @comment
  @comment
end

#gradesHash

The student’s grades for all assignments.

Returns:

  • (Hash)

    map between assignment names and the student’s scores



285
286
287
288
# File 'lib/stellar/gradebook.rb', line 285

def grades
  reload! unless @grades
  @grades
end

#reload!Stellar::Gradebook::Student

Reloads the information in the student’s grades page.

Returns:



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/stellar/gradebook.rb', line 301

def reload!
  page = @client.get_nokogiri @url

  @grades = {}
  @input_names = {} 
  page.css('.gradeTable tbody tr').each do |tr|
    name = tr.css('a[href*="assignment"]').inner_text
    input_field = tr.css('input[type="text"][name*="oints"]').first
    @input_names[name] = input_field['name']
    @grades[name] = input_field['value'].empty? ? nil :
        input_field['value'].to_f
  end
  @comment = page.css('textarea[name*="comment"]').inner_text
  
  self
end

#update_comment(text) ⇒ Stellar::Gradebook::Student

Changes the comment on the student’s grades page.

Parameters:

  • text (String)

    the new comment text

Returns:



342
343
344
345
346
347
348
349
# File 'lib/stellar/gradebook.rb', line 342

def update_comment(text)
  page = @client.get @url
  grade_form = page.form_with :action => /detail/i
  grade_form.field_with(:name => /comment/i).value = text
  grade_form.submit grade_form.button_with(:class => /save/)
  
  reload!
end

#update_grades(new_grades) ⇒ Stellar::Gradebook::Student

Changes some of the student’s grades.

Parameters:

  • new_grades (Hash)

    maps assignment names to the desired scores

Returns:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/stellar/gradebook.rb', line 322

def update_grades(new_grades)
  reload! unless @input_names
  
  page = @client.get @url
  grade_form = page.form_with :action => /detail/i
  new_grades.each do |assignment_name, new_grade|
    unless input_name = @input_names[assignment_name]
      raise ArgumentError, "Invalid assignment #{assignment_name}"
    end
    grade_form.field_with(:name => input_name).value = new_grade.to_s
  end
  grade_form.submit grade_form.button_with(:class => /save/)
  
  reload!
end