Class: SRS::CLI::Reschedule

Inherits:
Object
  • Object
show all
Defined in:
lib/srs/cli/reschedule.rb

Instance Method Summary collapse

Instance Method Details

#getScheduler(schedulername) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/srs/cli/reschedule.rb', line 67

def getScheduler(schedulername)
  begin
    require "./schedulers/#{schedulername}"
  rescue LoadError
    begin
      require "srs/schedulers/#{schedulername}"
    rescue LoadError
      puts "Couldn't find scheduler #{schedulername}."
      return nil
    end
  end

  SRS::Schedulers.const_get(schedulername.to_sym).new
end

#help ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/srs/cli/reschedule.rb', line 82

def help()
  puts "srs reschedule <id> <score>\n\nRescedules the exercise being set by schedule id according to the score\nsupplied.  Makes use of the scheduler defined for that schedule.  The score\npassed in will typically be that returned from do-exercise.\n    EOF\nend\n"

#run!(arguments) ⇒ Object



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
# File 'lib/srs/cli/reschedule.rb', line 6

def run!(arguments)
  if not SRS::Workspace.initialised? then
    puts "Current directory is not an SRS Workspace"
    return 3
  end

  schedule_id = arguments.shift
  score = arguments.shift.to_f

  is_new = false;
  schedulefile = "schedule/#{schedule_id}"

  if not File.exists?(schedulefile) then
    schedulefile = "schedule/pending/#{schedule_id}"
    is_new = true
    if not File.exists?(schedulefile) then
      puts "No content with that ID exists"
      return 4
    end
  end

  headers = {}
  File.open(schedulefile, "r") do |file|
    while( line = file.gets() ) do
      if line.strip.empty? then
        break
      end

      key, *val = line.split(':').map{|e| e.strip}
      headers[key] = val.join(':')
    end
  end

  if not headers.has_key?("Scheduler") then
    puts "Schedule #{schedule_id} has no scheduler!\n"
    return 6
  end

  exercise = headers.delete("Exercise")
  schedulername = headers.delete("Scheduler")

  scheduler = getScheduler(schedulername)
  headersOut = is_new ? scheduler.first_rep(score) : scheduler.rep(score, headers)

  FileUtils.rm_rf( schedulefile )

  fileOut = "schedule/#{schedule_id}"
  File.open(fileOut, "w") do |file|
    file.puts "Exercise: #{exercise}"
    file.puts "Scheduler: #{schedulername}"
    headersOut.each do |key, value|
      file.puts "#{key}: #{value.to_s}"
    end
  end

  puts "Exercise rescheduled for #{headersOut["Due"]}"
  puts "Exercise failed; marked for repetition" if headersOut["Repeat"]

  return 0
end