-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteacher.rb
151 lines (126 loc) · 3.72 KB
/
teacher.rb
1
2
3
4
5
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true
require_relative 'user'
require_relative 'quiz'
require 'date'
# teacher class actions like creating quizzes,editing quizzes,view attempts,publish,unpublis,lock and unlock quizzes
class Teacher < User
attr_reader :quizzes
def initialize(name, email, password)
super
@quizzes = []
end
def options(_)
loop do
puts 'Choose an option: '
puts '1. Create Quiz'
puts '2. Edit Quiz'
puts '3. Publish Quiz'
puts '4. Un-publish Quiz'
puts '5. Lock Quiz'
puts '6. Unlock Quiz'
puts '7. View Attempts'
puts '8. Logout'
puts '9. Exit'
return if handle_teacher_options
end
end
def select_quizzes(quizzes, action)
quizzes.select { |quiz| quiz.public_send(action) }
end
def reject_quizzes(quizzes, action)
quizzes.reject {|quiz| quiz.public_send(action)}
end
def handle_teacher_options
choice = gets.strip.to_i
case choice
when 1 then create_quiz
when 2 then edit_quiz
when 3 then publish_quiz
when 4 then unpublish_quiz
when 5 then lock_quiz
when 6 then unlock_quiz
when 7 then view_attempts
when 8 then 'Logging out...'
when 9 then exit
else
puts 'Invalid option, please try again!'
end
end
def quiz_title_input
loop do
puts 'Enter Quiz Title: '
title = gets.chomp.strip
break title if title.match(/\A[a-zA-Z]+(?:\s[a-zA-Z]+)*\z/)
puts 'Title must contain alphabets and spaces'
end
end
def quiz_availability_input
loop do
puts "Enter days for quiz availability: "
days = gets.chomp.to_i
break days if days.between?(1, 99)
puts 'Invalid input! Please enter a valid number of days.'
end
end
def create_quiz
quiz = Quiz.new(quiz_title_input, Date.today(), quiz_availability_input)
quiz.add_question
@quizzes << quiz
puts 'Quiz created Successfully! '
end
def quiz_id_input(quizzes)
loop do
puts 'Enter Quiz ID to perform action: '
quiz_id = gets.chomp.to_i - 1
break quizzes[quiz_id] if @quizzes.at(quiz_id)
puts 'Invalid Quiz ID!'
end
end
def edit_quiz
return puts 'No quizzes available to edit!' if @quizzes.empty?
list_all_quizzes
quiz = quiz_id_input(@quizzes)
quiz.edit
puts 'Quiz updated successfully!'
end
def helper_listing_quizzes(quizzes)
quizzes.each_with_index do |quiz, index|
puts "Quiz ID: #{index + 1}, Title: #{quiz.title}, Published: #{quiz.published}, " \
"Locked: #{quiz.locked}, Deadline: #{quiz.deadline}, Quiz Attempts: #{quiz.attempts}"
end
end
def list_all_quizzes
helper_listing_quizzes(@quizzes)
end
def helper_for_publish_lock(action, selection_method)
quizzes = public_send(selection_method, @quizzes, action)
return puts 'No quizzes to show for action' if quizzes.empty?
helper_listing_quizzes(quizzes)
quiz_id_input(quizzes)
end
def publish_quiz
quiz = helper_for_publish_lock(:published, :reject_quizzes)
quiz.publish!(false) if quiz
end
def unpublish_quiz
quiz = helper_for_publish_lock(:published, :select_quizzes)
quiz.unpublish!(false) if quiz
end
def lock_quiz
quiz = helper_for_publish_lock(:locked, :reject_quizzes)
quiz.lock!(false) if quiz
end
def unlock_quiz
quiz = helper_for_publish_lock(:locked, :select_quizzes)
quiz.unlock!(false) if quiz
end
def view_attempts
return puts 'No quizzes available to view attempts.' if @quizzes.empty?
@quizzes.each_with_index do |quiz, index|
puts "Viewing attempts for Quiz ID: #{index + 1}, Title: #{quiz.title}"
next puts 'No attempts found for this quiz.' if quiz.attempts.zero?
quiz.view_all_attempts
end
false
end
end