class QuizDownloader
Attributes
options[R]
Public Class Methods
new(options)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 18 def initialize(options) @options = options end
Public Instance Methods
attemptlist(agent)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 75 def attemptlist(agent) login(agent,moodle_login_page(options.moodle_server),options.moodle_username,options.moodle_password) moodle_overview_url = moodle_quiz_report(options.moodle_server)+options.exam_id.to_s chatter "downloading overview" chatter "url:#{moodle_overview_url}" begin page = agent.get(moodle_overview_url) attempt_list = extract_attempt_list(page) rescue Mechanize::ResponseCodeError => e raise e unless (e.response_code == "404") raise PageException, "Exam not found - maybe the wrong id: #{options.exam_id} ?\n(while trying to access: #{moodle_overview_url})" end end
chatter(message)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 43 def chatter(message) puts "==== #{message}" if options.verbose end
download(attempt_list,agent)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 92 def download(attempt_list,agent) FileUtils.mkdir_p(options.outputdir) count = 0 attempt_list.each do | attempt| student_name, attempt_url = attempt attempt_url = attempt_url+"&showall=1" page = agent.get(attempt_url) chatter "downloading attempt" chatter "url #{attempt_url}" chatter attempt_url #student = extractUserName(page) student = student_name puts "Loading: #{student}" chatter "downloading overview" chatter "outputfile:#{options.outputdir}" kit = PDFKit.new(page.body) if options.html chatter "save html" outputfile_html = FileNameCreator.file_name_for(options.outputdir,student,'html') File.open(outputfile_html, 'w') { |file| file.write(page.body) } else chatter "kit to file" outputfile = FileNameCreator.file_name_for(options.outputdir,student) kit.to_file(outputfile) chatter "x" end count += 1 end puts "downloaded #{count} attempts" end
login_successful?(page)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 68 def login_successful?(page) page.search('#username').empty? end
run()
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 22 def run agent = Mechanize.new case options.command when :connect smoketest(options.moodle_server, options.moodle_username, options.moodle_password) when :list attempt_list = attemptlist(agent) puts "found #{attempt_list.length} attempts:" puts attempt_list.map{|a,b| a} when :download chatter "options #{options.inspect}" attempt_list = attemptlist(agent) download(attempt_list,agent) when :options puts "#{options.inspect}" else puts "command #{options.command} not recognized" end end
server_reachable?(page)
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 71 def server_reachable?(page) !page.nil? end
smoketest(server, username = "xxx", password ="xxx")
click to toggle source
# File lib/moodleQuizDownloader/quiz_downloader.rb, line 47 def smoketest(server, username = "xxx", password ="xxx") begin agent = Mechanize.new page = login(agent,moodle_login_page(server),username,password) unless server_reachable?(page) puts "Could not connect to server #{server}" else puts "Connection to server seems to be working..." if login_successful?(page) puts "and login has been successful." else puts "but could not login with the given credentials." end end rescue Exception => e puts "could not connect to server #{server}" #puts e.message end page end