-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfRoll.java
270 lines (222 loc) · 12.7 KB
/
ProfRoll.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Author: Kristian Brasel
* Project: Roll Call
* Date: Summer 2017
* */
package rollCall;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.CellData;
import com.google.api.services.sheets.v4.model.ExtendedValue;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.SpreadsheetProperties;
import com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest;
import com.google.api.services.sheets.v4.model.ValueRange;
public class ProfRoll extends RollBook {
/*Constructor*/
protected ProfRoll(String studId, String course, String textDate) throws IOException {
super(studId, course, textDate);
}
/*Constructor*/
protected ProfRoll(String studId, String course) throws IOException {
super(studId, course);
}
/*Constructor*/
protected ProfRoll(String studId) throws IOException {
super(studId);
}
/*Constructor*/
protected ProfRoll(int timeLimit, double latitude, double longitude) throws IOException {
super(timeLimit, latitude, longitude);
}
protected ProfRoll() throws IOException{
super();
}
protected String saveSpreadsheetAddress(String ssid) throws IOException{
writeTo(ssid, "C:\\CSC131\\spreadsheetId.txt");
return "Spreadsheet ID has been saved for future use";
}
/* ***********************************************************************************************************************
* Used to set the title of the Spreadsheet. Professor enters his name, and the spreadsheet is titled "(name)'s RollCall"
* Ideally this would be used only when setting up the spreadsheet for the first time, by the professor
* Parameter:
* title: this is the desired title for the spreadsheet
* */
public void setTitle(String title) throws IOException{
List<Request> requests = new ArrayList<>();
// Change the spreadsheet's title.
requests.add(new Request()
.setUpdateSpreadsheetProperties(new UpdateSpreadsheetPropertiesRequest()
.setProperties(new SpreadsheetProperties()
.setTitle(title))
.setFields("title"+"'s RollCall")));
// Update
BatchUpdateSpreadsheetRequest body =
new BatchUpdateSpreadsheetRequest().setRequests(requests);
BatchUpdateSpreadsheetResponse response =
getService().spreadsheets().batchUpdate(getSpreadsheetId(), body).execute();
}
// Adds geolocation to sheet
public void placeLocation() throws IOException{
double[] latLong = getLocation();
int row = findRow( getSheet(), "schedule!A:A");
int latCol = findCol( "latitude", "schedule!3:3");
int lonCol = findCol( "longitude", "schedule!3:3");
// Create requests object
List<Request> requests = new ArrayList<>();
// add rollKey to expiration
List<CellData> valuesLat = new ArrayList<>();
addCellData(valuesLat, String.valueOf(latLong[0])); // add rollKey to CellData arraylist
addToRequest(requests, 0, row-1, latCol, valuesLat); // Prepare request with proper row and column and its value
// add expiration to request
List<CellData> valuesLon = new ArrayList<>();
addCellData(valuesLon, String.valueOf(latLong[1])); // add rollKey to CellData arraylist
addToRequest(requests, 0, row-1, lonCol, valuesLon); // Prepare request with proper row and column and its value
update(requests);
}
/***********************************************************************************************
* clearGeoLoc()
* Sets the values for lat and lon to 0, this will allow students to log in from any location.
* This can be used for online classes, or in cases where geolocation is malfunctioning.
* Parameter:
* course: This function only clears geoloc data for the course specified
* return:
* Notifies the user that students can now log in from anywhere. *
* */
public String clearGeoLoc(String course) throws IOException{
double[] latLong = getLocation();
int row = findRow( course, "schedule!A:A");
int latCol = findCol( "latitude", "schedule!3:3");
int lonCol = findCol( "longitude", "schedule!3:3");
// Create requests object
List<Request> requests = new ArrayList<>();
// add rollKey to expiration
List<CellData> valuesLat = new ArrayList<>();
addCellData(valuesLat, "0"); // add rollKey to CellData arraylist
addToRequest(requests, 0, row-1, latCol, valuesLat); // Prepare request with proper row and column and its value
// add expiration to request
List<CellData> valuesLon = new ArrayList<>();
addCellData(valuesLon, "0"); // add rollKey to CellData arraylist
addToRequest(requests, 0, row-1, lonCol, valuesLon); // Prepare request with proper row and column and its value
update(requests);
return("Geolocation values were cleared for " + course + ", students can now log in from any location.");
} // end ClearGeoLoc()
/******************************************************************************************************************
* Set Professor defined key for date and class
* Parameters:
* sheet: String of the sheet used for the class
* return:
* a string either indicating a successful log of roll key, or if a roll key has already been logged a message
* is returned reminding the professor of the current roll key.
*/
public String setKey(String rollKey) throws IOException{
//String rollKey = JOptionPane.showInputDialog("Enter roll key");
int emptyCol = 0; // this will be used to store the first empty col
String dateRange = getSheet() + "!3:3"; // a string made up of the sheetId and range. Date row is row 3
ValueRange response = getService().spreadsheets().values()
.get(getSpreadsheetId(), dateRange).execute();
List<List<Object>> values = response.getValues(); // date row
if (values == null || values.size() == 0) { // checks to make sure there is data in list
System.out.println("No data found in row 3.");
}else {
for (List<?> col : values) { // finds number of entries in date row.
emptyCol = col.size(); // gives length of date row
if(col.get(emptyCol-1).equals(getTxtDate())){ // checks to see if date was already entered
String rollRow = getSheet() + "!1:1"; // Roll row
ValueRange responseR = getService().spreadsheets().values()
.get(getSpreadsheetId(), rollRow).execute();
List<List<Object>> valuesR = responseR.getValues(); // list with rollkey row
for (List<?> colR : valuesR) { // tells professor the date was already written, reminds him or rollKey
return(errorRed("A roll key has already been recorded for " + getClassAndDate() + ". Your roll key is " + colR.get(emptyCol-1))); // exits method. Entering new date is no longer necessary.
} // end for
} // end if
} // end for
} // end else
// Create requests object
List<Request> requests = new ArrayList<>();
// add rollKey to expiration
List<CellData> valuesRollKey = new ArrayList<>();
addCellData(valuesRollKey, rollKey); // add rollKey to CellData arraylist
addToRequest(requests, getSheetId(), 0, emptyCol, valuesRollKey); // Prepare request with proper row and column and its value
// add expiration to request
List<CellData> valuesExpKey = new ArrayList<>();
addCellData(valuesExpKey, getExpirationTime()); // add rollKey to CellData arraylist
addToRequest(requests, getSheetId(), 1, emptyCol, valuesExpKey); // Prepare request with proper row and column and its value
// add date to request
List<CellData> valuesDate = new ArrayList<>();
addCellData(valuesDate, getTxtDate()); // Add string of current date to value
addToRequest(requests, getSheetId(), 2, emptyCol, valuesDate); // Prepare request with proper row and column and its value
// Fill col with "NO"
String range = getSheet() ; // The range needs to be more dynamic
ValueRange responseNO = getService().spreadsheets().values()
.get(getSpreadsheetId(), range)
.execute();
List<List<Object>> valuesNO = responseNO.getValues();
int count = 1;
if (valuesNO == null || valuesNO.size() == 0) {
System.out.println("No data found.");
}else {
for (List<?> row : valuesNO) {
count++; // this tells me how many rows there are, and used to print "NO"
} // end for
} // end else
List<CellData> valuesNOKey = new ArrayList<>();
valuesNOKey.add(new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("NO")));
int noRow = 3;
// initialize attendance for all students "No"
for(noRow = 3; noRow <= count-2; noRow++){
addToRequest(requests, getSheetId(), noRow, emptyCol, valuesNOKey); // add request with proper row and column and its value
} // end for
update(requests);
return "Congratulations, your roll key for " + getClassAndDate() + " is " + rollKey;
} // end setKey()
/* **********************************************************************************************************************************
* Professor marks student present and returns appropriate string to inform user if roll was successfully recorded, or what errors were found.
* This is used by the professor under special circumstances, such as a student's battery dying, excused absence, or error with the application.
* Parameters:
* rollKey: This is the roll key the student gets from the professor at login
* studId: Students Id
* return:
* a message informing the user that their attendance was logged, or an error message explaining why it wasn't
* */
protected String profLogStudIn() throws IOException{
if(findRow(getSheet(), "schedule") == -1){
return("I'm sorry, this course is not in this roll book."); // validate course selection
} // end if
// find row and col
int col = findCol(getTxtDate(), getSheet()+"!3:3"); // find todays date
int row = findRow(getStudentId(), getSheet()+"!D:D");
// check to make sure row and col were found
if (col == -1){ // date was not found
return "I'm sorry, I could not find a record for " + getClassAndDate() + ".";
}
if(row == -1){ // student Id was not found
return errorRed("You entered an invalid Student ID for " + getSheet() + ".");
}
if( isThis("NO", getSheet(), row, col)){ // log attendance
List<Request> requests = new ArrayList<>(); // Create requests object
List<CellData> valuesAdd = new ArrayList<>(); // Create values object
addCellData(valuesAdd, "YES"); // Add string of current date to value
addToRequest(requests, getSheetId(), row-1, col, valuesAdd); // Prepare request with proper row and column and its value, place Date
update(requests);
return ("Thank you " + getFirstName(row) + "'s attendence has been logged for " + getClassAndDate() + ".");
} else{ // already logged in
return (getFirstName(row)+"'s attendence has ALREADY been logged for " + getClassAndDate() + ".");
} // end else
} // end profLogStudIn()
public List<List<Object>> getProfReport(String course) throws IOException{
List<List<Object>> attendence = new ArrayList<List<Object>>();
String range = course;//+"!A3:Z99"; // The range needs to be more dynamic 131-1 refers to the sheetID
//String studentID = JOptionPane.showInputDialog("What is your Student ID?");
range = getSheet();
ValueRange response = getService().spreadsheets().values()
.get(getSpreadsheetId(), range)
.execute();
List<List<Object>> values = response.getValues();
return values;
} // end checkStudentAtt()
} // end profRoll()