~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: 2009-02-25 02:17:36 UTC
  • mfrom: (1109)
  • mto: This revision was merged to the branch mainline in revision 1119.
  • Revision ID: matt.giuca@gmail.com-20090225021736-fnnpc7stnemede00
MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* IVLE - Informatics Virtual Learning Environment
2
 
 * Copyright (C) 2007-2008 The University of Melbourne
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 *
18
 
 * Module: Group management system (client)
19
 
 * Author: Matt Giuca
20
 
 * Date: 21/7/2008
21
 
 */
22
 
 
23
 
serviceapp = 'userservice';
24
 
 
25
 
/* Creates a group */
26
 
function create_new_group(projectsetid)
27
 
{
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
 
    }
54
 
}
55
 
 
56
 
function manage_group(offeringid, groupid, namespace)
57
 
{
58
 
    var elem = document.getElementById(namespace);
59
 
    var button = document.getElementById(namespace+"_button");
60
 
    var manage_div = document.createElement("div")
61
 
    manage_div.id = namespace + "_contents";
62
 
    elem.insertBefore(manage_div, button);
63
 
    
64
 
    /* Refresh contents */
65
 
    list_projectgroup_contents(offeringid, groupid, manage_div.id);
66
 
 
67
 
    /* Remove the button element */
68
 
    elem.removeChild(button);
69
 
}
70
 
 
71
 
/* Lists the information about a particular project group identified by groupid 
72
 
 * in an offering identified by offeringid in the element with id elemnm. May 
73
 
 * be called multiple times safely to refresh the displayed information.
74
 
 */
75
 
function list_projectgroup_contents(offeringid, groupid, elemnm)
76
 
{
77
 
    var contents = document.getElementById(elemnm);
78
 
    dom_removechildren(contents);
79
 
    var callback = function(xhr)
80
 
    {
81
 
        var members = JSON.parse(xhr.responseText);
82
 
        var available = members.available;
83
 
        var groupmembers = members.groupmembers;
84
 
        
85
 
        /* Existing members */
86
 
        var ul = document.createElement("ul");
87
 
        for (var i=0; i<groupmembers.length; i++)
88
 
        {
89
 
            var member = groupmembers[i];
90
 
 
91
 
            var li = dom_make_text_elem("li", member.fullname + " (" +
92
 
                                              member.login + ")");
93
 
            var rmbutton = document.createElement("input");
94
 
            rmbutton.value = "Remove";
95
 
            rmbutton.type = "image";
96
 
            /* XXX: There must be a better way to do this! */
97
 
            rmbutton.src = "/+media/ivle.webapp.groups/cross.png";
98
 
 
99
 
            $(rmbutton).click(function(offeringid, login, groupid, elemnm)
100
 
            {
101
 
                return function() {
102
 
                    if (!confirm("Are you sure want to revoke this user's membership?"))
103
 
                        return;
104
 
                    this.disabled = true;
105
 
                    var args = {'login': login, 'groupid': groupid};
106
 
                    ajax_call(null, serviceapp, 'unassign_group', args, 'POST');
107
 
                    list_projectgroup_contents(offeringid, groupid, elemnm);
108
 
                };
109
 
            }(offeringid, member.login, groupid, elemnm));
110
 
 
111
 
            li.appendChild(rmbutton);
112
 
            ul.appendChild(li);
113
 
        }
114
 
 
115
 
        /* Add member box */
116
 
        var add_li = document.createElement("li");
117
 
        var select = document.createElement("select");
118
 
        for (var i=0; i<available.length; i++)
119
 
        {
120
 
            var option = dom_make_text_elem("option", available[i].login);
121
 
            option.value = available[i].login;
122
 
            select.appendChild(option);
123
 
        }
124
 
        var button = document.createElement("input");
125
 
        button.value = "Add";
126
 
        button.type = 'button';
127
 
        $(button).click(function()
128
 
        {
129
 
            this.disabled = true;
130
 
            args = {'login': select.value, 'groupid': groupid};
131
 
            ajax_call(null, serviceapp, 'assign_group', args, 'POST');
132
 
            list_projectgroup_contents(offeringid, groupid, elemnm);
133
 
        });
134
 
        add_li.appendChild(select);
135
 
        add_li.appendChild(button);
136
 
        ul.appendChild(add_li);
137
 
        contents.appendChild(ul);
138
 
    }
139
 
    var args = {'offeringid': offeringid, 'groupid': groupid};
140
 
    ajax_call(callback, serviceapp, 'get_group_membership', args, 'GET');
141
 
 
142
 
}