Class: SakaiInfo::CLI::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/cli/query.rb

Class Method Summary collapse

Class Method Details

.process(args, flags) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sakai-info/cli/query.rb', line 15

def self.process(args, flags)
  # TODO: expand query functionality from proof of concept
  object_type = args.shift
  fields = []
  site_id = nil

  if object_type == "quiz"
    flags.each do |flag|
      case flag
      when /^--fields=/
        fields = flag.split("=")[1].split(",")
      when /^--site=/
        site_id = flag.split("=")[1]
      else
        STDERR.puts "ERROR: Unrecognized query flag"
        exit 1
      end
    end

    if site_id.nil?
      STDERR.puts "ERROR: No site ID was provided"
      exit 1
    end

    puts PublishedQuiz.find_ids_by_site_id(site_id)

  elsif object_type == "userid" or object_type == "user_id" or object_type == "uid"
    id = args.shift
    begin
      user = User.find(id)
      puts user.id
    rescue ObjectNotFoundException => e
      STDERR.puts "ERROR: #{e}"
      exit 1
    end

  elsif object_type == "deleted-content"
    userid = nil
    while flags.length > 0
      flag = flags.shift
      if flag =~ /^--by=/
        userid = flag.split('=')[1]
      end
    end

    if userid.nil?
      STDERR.puts "ERROR: you must specify --by=<userid>"
      exit 1
    end

    begin
      user = User.find(userid)
      deleted_resources = DeletedContentResource.find_by_delete_userid(user.id)
      deleted_resources.each do |dr|
        puts dr.id
      end
    rescue ObjectNotFoundException => e
      STDERR.puts "ERROR: #{e}"
      exit 1
    end

  elsif object_type == "site"
    title = args.shift

    Site.find_by_title(title).each do |site|
      puts site.to_csv(:id, :title)
    end

  elsif object_type == "user"
    name = args.shift

    User.find_by_name(name).each do |user|
      puts user.to_csv(:eid, :name)
    end

  elsif object_type == "quiz-attempt"
    eid = args.shift
    quiz_id = args.shift
    user = nil
    quiz = nil

    begin
      user = User.find(eid)
      quiz = PublishedQuiz.find(quiz_id)
    rescue ObjectNotFoundException => e
      STDERR.puts "ERROR: #{e}"
      exit 1
    end

    if user.nil? or quiz.nil?
      STDERR.puts "ERROR: could not find user or quiz object"
      exit 1
    end

    attempts = quiz.user_attempts(user.id)
    if attempts.nil? or attempts.empty?
      STDERR.puts "ERROR: no attempts found"
    end

    attempts.each do |att|
      puts att.to_yaml
    end

  else
    STDERR.puts "ERROR: Unrecognized object type"
    exit 1
  end
end