File size: 3,875 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
var profileViewModel;

function UserModel(data)
{
  this.account_id=ko.observable(data.id);
  this.uname=ko.observable(data.uname);  
}
function ProfileModel(data)
{
	this.profile_id = ko.observable(data.id);
	this.Name = ko.observable(data.Name);
	this.gender = ko.observable(data.gender);
	this.email = ko.observable(data.email);
	this.phone = ko.observable(data.phone);
	this.college = ko.observable(data.college);
	this.semester = ko.observable(data.semester);
	this.description = ko.observable(data.description);
}

function GameProfileModel(data)
{
	var self = this;
	self.GameProfile_id = ko.observable(data.id);
	self.points = ko.observable(data.points);
	self.badges = ko.observableArray();
	this.getBadges = function()
	{
  	$.get("/gameprofile/badges",
  	  {id:self.GameProfile_id()},
  		function(response)
  		{
       	self.badges(response["badges"]);
  		}
   	);
	}
}
var emptyUser = new function()
{
  this.account_id="";
  this.uname="";  
};

var emptyProfile = new function()
{
	this.id = "";
	this.Name = "";
	this.gender = "";
	this.email =  "";
	this.phone = "";
	this.college =  "";
	this.semester = "";
	this.description = "";
};

var emptyGameProfile = new function()
{
	this.id = "";
	this.points = "";
}
function ViewModal()
{
	var self = this;
	self.user = ko.observable(new UserModel(emptyUser));
	self.profile = ko.observable(new ProfileModel(emptyProfile));
	self.gameProfile = ko.observable(new GameProfileModel(emptyGameProfile));
	
	self.flocks = ko.observableArray();
	self.topgameProfiles = ko.observableArray();
	self.topprofiles = ko.observableArray();
	self.showParticipatedFlocks = function(profile) {
		$.get('/profile/events',
			{id : profile.profile_id},
			function(response)
			{
				self.flocks(response["flocks"]);
				$('#FlocksParticipatedModal').modal('show')	;
			}
		);	
	}
	self.toppoints = function(){
    $.get('/topgameprofiles/points',
      function(response)
      {
         var gameprofiles = $.map(response["gameprofiles"],function(item)
          {
          	var gameprofile =  new GameProfileModel(item);
          	gameprofile.getBadges();
          	return gameprofile;
          });
          self.topgameProfiles(gameprofiles);
      	  var profiles = $.map(response["profiles"],function(item) 
          {
            return new ProfileModel(item);
          });
          self.topprofiles(profiles);
 
 			}
		);
	}
		self.init = function() {
		$.get('/profile',
			function(data)
			{
				self.profile(new ProfileModel(data));
				   	if(data.Name == "" || data.Name == null ||data.gender == "" || data.gender == null || data.college == "" || data.college == null ||data.semester == "" || data.semester == null )
   					{
   						$('#EditProfileModal').modal('show')	;
   					}

			}	
		);
		$.get('/gameprofile',
		  function(data)
		  {
		  	self.gameProfile(new GameProfileModel(data));
		  	self.gameProfile().getBadges();
		  }
		);	
		
	}
	
	self.editdetails = function()
	{
		$.ajax({
			url: '/profile',
      data: {profile : self.profile()},
      type: "PUT",
      success: function(data) {
				if(!data.error)
					alert("Profile updated successfully ");
				else
					alert("it have some error");
      }
   	});  
	}
}
$(document).ready( function() {
    var profileViewModel = new ViewModal();
    profileViewModel.init();
    profileViewModel.toppoints();
  
   if($("#FlocksParticipatedModal").get(0))
    {
      ko.applyBindings(profileViewModel,$("#FlocksParticipatedModal").get(0));
    }  
   	if($("#profile1").get(0))
    {
      ko.applyBindings(profileViewModel,$("#profile1").get(0));
    }  
    if($("#BadgeslistModal").get(0))
    {
      ko.applyBindings(profileViewModel,$("#BadgeslistModal").get(0));
    }

    if($("#EditProfileModal").get(0))
    {
      ko.applyBindings(profileViewModel,$("#EditProfileModal").get(0));
    }
           
  }
);