~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/groups/media/groups.js

  • Committer: Matt Giuca
  • Date: 2010-07-27 06:53:03 UTC
  • Revision ID: matt.giuca@gmail.com-20100727065303-gs7fn3gc3ccaqfux
Changed database schema 'semester' table. 'year' and 'semester' fields now allow any length, not just 4 or 1 respectively. (LP: #610330).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
serviceapp = 'userservice';
24
24
 
25
 
function create(offeringid)
26
 
{
27
 
    /* TODO: Write this function */
28
 
}
29
 
 
30
 
/* Displays the selected group in the group adminsitration area
31
 
 */
32
 
function manage_subject()
33
 
{
34
 
    /* Get the subject id from the select box */
35
 
    var subject_select = document.getElementById("subject_select");
36
 
    var subject_div = document.getElementById("subject_div");
37
 
    var subjectid = parseInt(subject_select.value);
38
 
 
39
 
    /* List active offerings */
40
 
    dom_removechildren(subject_div);
41
 
    var p = dom_make_text_elem("h2", "Active Offerings");
42
 
    subject_div.appendChild(p);
43
 
    var callback = function(xhr) 
44
 
    {
45
 
        var offerings = JSON.parse(xhr.responseText);
46
 
        for (var i=0; i<offerings.length; i++)
47
 
        {
48
 
            var offering = offerings[i];
49
 
            var text = offering.subj_name + ", Semester " + offering.semester + " " + offering.year;
50
 
            var h3 = dom_make_text_elem("h3", text);
51
 
            subject_div.appendChild(h3);
52
 
            var div = document.createElement("div");
53
 
            subject_div.appendChild(div);
54
 
            manage_subject_offering(offering['offeringid'], div);
55
 
        }
56
 
    }
57
 
    ajax_call(callback, serviceapp, 'get_active_offerings', {'subjectid': subjectid}, 'GET');
58
 
}
59
 
 
60
 
/* Manages the display of a single offerings groups
61
 
 * Takes a offeringid and element to attach the results to
62
 
 */
63
 
function manage_subject_offering(offeringid, elem)
64
 
{
65
 
    var callback = function(xhr)
66
 
    {
67
 
        var projectsets = JSON.parse(xhr.responseText);
68
 
        var dl = document.createElement("dl");
69
 
        elem.appendChild(dl);
70
 
        /* Add details for each project set */
71
 
        for (var i=0; i<projectsets.length; i++)
72
 
        {
73
 
            var projectset = projectsets[i];
74
 
            var groups = projectset.groups;
75
 
            
76
 
            var dt = dom_make_text_elem("dt", "Project Set "+(i+1)+" ");
77
 
            var input = document.createElement("input");
78
 
            input.value = "New Group";
79
 
            input.type = 'button';
80
 
            input.setAttribute("onclick", "create_new_group("+projectset['projectsetid']+")");
81
 
            dt.appendChild(input);
82
 
            dl.appendChild(dt);
83
 
            var dd = document.createElement("dd");
84
 
            var ul = document.createElement("ul");
85
 
            dd.appendChild(ul);
86
 
            /* Add each group in the project set */
87
 
            for (var j=0; j<groups.length; j++)
88
 
            {
89
 
                var group = groups[j];
90
 
                var namespace = "project_group_" + group.groupid;
91
 
                var li = dom_make_text_elem("li", group['groupnm']+" ");
92
 
                li.id = namespace;
93
 
                var button = document.createElement("a");
94
 
                button.id = namespace+"_button";
95
 
                button.setAttribute("class", "choice");
96
 
                button.textContent = '(manage)';
97
 
                button.setAttribute("onclick",
98
 
                    "manage_group("+offeringid+","+group.groupid+",'"+namespace+"')");
99
 
                li.appendChild(button);
100
 
                ul.appendChild(li);
101
 
            }
102
 
            dl.appendChild(dd);
103
 
        }
104
 
    }
105
 
    ajax_call(callback, serviceapp, 'get_project_groups', {'offeringid': offeringid}, 'GET');
106
 
}
107
 
 
108
25
/* Creates a group */
109
26
function create_new_group(projectsetid)
110
27
{
111
 
    groupnm = window.prompt('Please enter a name for the group');
112
 
    args = {'projectsetid': projectsetid, 'groupnm':groupnm, 'nick': groupnm};
113
 
    response = ajax_call(null, serviceapp, 'create_group', args, 'POST');
114
 
    if (response.status == 200)
115
 
    {
116
 
        /* pass */
117
 
    }
118
 
    else
119
 
    {
120
 
        alert("Error: Could not add group. Does it already exits?");
121
 
    }
122
 
    /* Reload the display */
123
 
    manage_subject();
 
28
    groupnm = window.prompt('Please enter a name for the group','');
 
29
 
 
30
    /* If the provided name is empty, or the prompt is cancelled, stop. */
 
31
    if (groupnm)
 
32
    {
 
33
        args = {
 
34
            'projectsetid': projectsetid,
 
35
            'groupnm':groupnm,
 
36
            'nick': groupnm
 
37
        };
 
38
        response = ajax_call(null, serviceapp, 'create_group', args, 'POST');
 
39
        if (response.status == 200)
 
40
        {
 
41
            /* pass */
 
42
        }
 
43
        else if (response.status == 400)
 
44
        {
 
45
            alert("Could not create group: " + response.getResponseHeader('X-IVLE-Error'));
 
46
        }
 
47
        else
 
48
        {
 
49
            alert("Error: Could not add group. Does it already exist?");
 
50
        }
 
51
        /* Reload the display */
 
52
        window.location.href = window.location.href;
 
53
    }
124
54
}
125
55
 
126
56
function manage_group(offeringid, groupid, namespace)
127
57
{
128
 
    var elem = document.getElementById(namespace);
129
58
    var button = document.getElementById(namespace+"_button");
130
59
    var manage_div = document.createElement("div")
131
 
    elem.insertBefore(manage_div, button);
132
 
 
 
60
    manage_div.id = namespace + "_contents";
 
61
 
 
62
    /* Get the td which is button's parent (the 'actions' column) */
 
63
    button_td = button.parentNode;
 
64
    button_td.appendChild(manage_div);
 
65
 
 
66
    /* Refresh contents */
 
67
    list_projectgroup_contents(offeringid, groupid, manage_div.id);
 
68
 
 
69
    /* Remove the button element */
 
70
    button_td.removeChild(button);
 
71
}
 
72
 
 
73
/* Lists the information about a particular project group identified by groupid 
 
74
 * in an offering identified by offeringid in the element with id elemnm. May 
 
75
 * be called multiple times safely to refresh the displayed information.
 
76
 */
 
77
function list_projectgroup_contents(offeringid, groupid, elemnm)
 
78
{
 
79
    var contents = document.getElementById(elemnm);
 
80
    dom_removechildren(contents);
133
81
    var callback = function(xhr)
134
82
    {
135
83
        var members = JSON.parse(xhr.responseText);
136
84
        var available = members.available;
137
85
        var groupmembers = members.groupmembers;
138
86
        
 
87
        /* Existing members */
 
88
        var ul = document.createElement("ul");
 
89
        for (var i=0; i<groupmembers.length; i++)
 
90
        {
 
91
            var member = groupmembers[i];
 
92
 
 
93
            var li = dom_make_text_elem("li", member.fullname + " (" +
 
94
                                              member.login + ") ");
 
95
            var rmbutton = document.createElement("input");
 
96
            rmbutton.value = "Remove";
 
97
            rmbutton.type = "image";
 
98
            /* XXX: There must be a better way to do this! */
 
99
            rmbutton.src = "/+media/ivle.webapp.core/images/interface/delete.png";
 
100
 
 
101
            $(rmbutton).click(function(offeringid, login, groupid, elemnm)
 
102
            {
 
103
                return function() {
 
104
                    if (!confirm("Are you sure want to revoke this user's membership?"))
 
105
                        return;
 
106
                    this.disabled = true;
 
107
                    var args = {'login': login, 'groupid': groupid};
 
108
                    ajax_call(null, serviceapp, 'unassign_group', args, 'POST');
 
109
                    list_projectgroup_contents(offeringid, groupid, elemnm);
 
110
                };
 
111
            }(offeringid, member.login, groupid, elemnm));
 
112
 
 
113
            li.appendChild(rmbutton);
 
114
            ul.appendChild(li);
 
115
        }
 
116
 
139
117
        /* Add member box */
 
118
        var add_li = document.createElement("li");
140
119
        var select = document.createElement("select");
141
120
        for (var i=0; i<available.length; i++)
142
121
        {
145
124
            select.appendChild(option);
146
125
        }
147
126
        var button = document.createElement("input");
148
 
        button.value = "Add new group member";
 
127
        button.value = "Add";
149
128
        button.type = 'button';
150
 
        button.addEventListener("click", function()
 
129
        $(button).click(function()
151
130
        {
 
131
            this.disabled = true;
152
132
            args = {'login': select.value, 'groupid': groupid};
153
133
            ajax_call(null, serviceapp, 'assign_group', args, 'POST');
154
 
        }, false);
155
 
        manage_div.appendChild(select);
156
 
        manage_div.appendChild(button);
157
 
        
158
 
        /* Existing members */
159
 
        manage_div.appendChild(dom_make_text_elem("p", "Group Members:"));
160
 
        var ul = document.createElement("ul");
161
 
        for (var i=0; i<groupmembers.length; i++)
162
 
        {
163
 
            var li = dom_make_text_elem("li", groupmembers[i].login);
164
 
            ul.appendChild(li);
165
 
        }
166
 
        manage_div.appendChild(ul);
 
134
            list_projectgroup_contents(offeringid, groupid, elemnm);
 
135
        });
 
136
        add_li.appendChild(select);
 
137
        add_li.appendChild(button);
 
138
        ul.appendChild(add_li);
 
139
        contents.appendChild(ul);
167
140
    }
168
141
    var args = {'offeringid': offeringid, 'groupid': groupid};
169
142
    ajax_call(callback, serviceapp, 'get_group_membership', args, 'GET');
170
 
    /* Remove the button element */
171
 
    elem.removeChild(button);
 
143
 
172
144
}