-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
177 lines (163 loc) · 6.02 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<!-- This title is not used. -->
<title>Top Users</title>
<!-- Use Bootstrap to match the look of OU Campus. -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style type="text/css">
body {
margin: 10px;
background-color: transparent;
}
</style>
<!-- jQuery IS REQUIRED for gadgetlib.js to work. -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<!--
gadgetlib.js is a library of basic functions that gadgets can use. It creates a global
`gadget` object that has a few useful methods. (See comments in gadgetlib.js.)
-->
<script type="text/javascript" src="lib/gadgetlib.js"></script>
<script type="text/javascript" src="lib/moment.js"></script>
<script type="text/javascript">
$(document).ready(function () {
/*
First, call `gadget.ready()` to make sure the gadget has obtained an API token
to use for making OU Campus API calls. If your gadget will not make any API calls,
you can dispense with this method. This asynchronous method returns a jQuery
Promise object.
Then, call `gadget.fetch()` to get the gadget's config data from the system. This
method, which also returns a jQuery Promise object, uses the API, which is why it
needs to follow the call to `gadget.ready()`.
If you don't need the config data, you don't need to call gadget.fetch().
*/
gadget.ready().then(gadget.fetch).then(function () {
$('#main').css({ 'font-size': gadget.getConfig('font_size') });
});
});
$(gadget).on({
'expanded': function (evt) {
// This event is triggered when the user expands (makes visible) a sidebar gadget.
var activities_list = [];
var lastDate = moment();
var num_loaded = 0;
function getActivities(x){
var d = $.Deferred();
var apiHost = gadget.get('apihost');
var authToken = {"authorization_token":gadget.get('token')};
$.ajax({
type: 'GET',
url:apiHost + "/activity/list?site=outc16&count=50&start=" + x,
data:authToken,
success:function(result){
for (var i = 0; i < result.length; i++) {
lastDate = moment(result[i].date_format, "M/DD/YY h:mm A");
if(moment(result[i].date_format, "M/DD/YY h:mm A") > moment().subtract(1, 'months')){
activities_list.push(result[i]);
num_loaded++;
}
}
d.resolve(activities_list);
},
error:function(jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
return d.resolve([]);
}
});
return d;
}
function organizeByUser(activities){
var users = [];
for (var i = 0; i < activities.length; i++) {
var userIndex = searchListDictionaries(users,{user:activities[i].username},true);
if(userIndex === -1){
users.push({user:activities[i].username,activities:[activities[i]]});
}
else{
var user = users[userIndex];
user.activities.push(users[userIndex]);
}
}
return users;
}
function searchListDictionaries (list,keyvaluelist,bool_index){
var bool_index = typeof bool_index !== 'undefined' ? bool_index : false;
try{
var numcriterion = 0;
for (var key in keyvaluelist) {
numcriterion++;
}
for(var i = 0; i < list.length; i++){
var counter = 0;
for (var key in keyvaluelist) {
if(list[i][key] == keyvaluelist[key]){
counter++;
if(counter == numcriterion){
if(bool_index){return i;}
else{return list[i];}
}
}
}
}
if(bool_index){return -1;}else{return null;}
}
catch(err){
if(bool_index){return -1;}else{return null;}
}
}
function orderUsers(users, asc){
var asc = typeof asc !== 'undefined' ? asc : true;
users.sort(function(a, b) {
if(asc === true){
return a.activities.length - b.activities.length;
}
else{
return b.activities.length - a.activities.length;
}
});
return users;
}
(function recurse(i,l) {
getActivities(i).then(function() {
if(lastDate>moment().subtract(1, 'months') && i != num_loaded){
num_loaded = activities_list.length - 1;
recurse(num_loaded,activities_list);
}
else{
var users = organizeByUser(activities_list);
var orderedUsers = orderUsers(users,false);
var output = "";
output += "<h3><u>Top Users</u></h3>";
for(var c = 0; (c < orderedUsers.length) && (c < parseInt(gadget.getConfig('num_users'))); c++){
output += "User: " + orderedUsers[c].user + "<br>";
output += "Activities: " + orderedUsers[c].activities.length + "<br>";
output += "<br>";
}
$("#main").html(output);
}
});
})(num_loaded,activities_list);
},
'configuration': function (evt, config) {
// If the user changes the gadget's configuration through the configuration modal,
// the gadget will hear about it and get the new config in the data argument here.
$('#main').css({ 'font-size': config.font_size });
}
});
</script>
</head>
<body>
<div id="main">
Loading. <img src="loading.gif"/>
</div>
<!--
The following hidden div is only needed if you'll be editing your gadget in
OU Campus's source code editor. OU Campus automatically adds a DirectEdit link
to HTML files that you edit in OU Campus. This div hides the DirectEdit link.
-->
<div style="display:none">
<!-- ouc:ob --><!-- /ouc:ob -->
</div>
</body>
</html>