~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/boards/python/admin/group_edit.py

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2006-12-15 23:08:17 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215230817-exr5ks1hd73s3tlk
Tags: 8.2.2-1
* New upstream bugfix release, fixes among other things the support for
  the version of gnucap shipped in etch.
* Add missing dependency on python-gtk2 (Closes: #396523).
* Removed reference to non-existent sound file from memory.c (upstream
  fix - impacts 8.2 as well).  
* Now suggests gnuchess, gnucap, and tuxpaint.
* Updated extended description for the main package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  gcompris - group_edit.py
 
2
 
3
# Copyright (C) 2005 Bruno Coudoin and Yves Combe
 
4
 
5
#   This program is free software; you can redistribute it and/or modify
 
6
#   it under the terms of the GNU General Public License as published by
 
7
#   the Free Software Foundation; either version 2 of the License, or
 
8
#   (at your option) any later version.
 
9
 
10
#   This program is distributed in the hope that it will be useful,
 
11
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#   GNU General Public License for more details.
 
14
 
15
#   You should have received a copy of the GNU General Public License
 
16
#   along with this program; if not, write to the Free Software
 
17
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
19
 
 
20
 
 
21
import gtk
 
22
import gobject
 
23
from gettext import gettext as _
 
24
 
 
25
import group_user_list
 
26
 
 
27
import constants
 
28
 
 
29
# Database
 
30
from pysqlite2 import dbapi2 as sqlite
 
31
 
 
32
# User List Management
 
33
(
 
34
  COLUMN_USERID,
 
35
  COLUMN_FIRSTNAME,
 
36
  COLUMN_LASTNAME,
 
37
  COLUMN_USER_EDITABLE
 
38
) = range(4)
 
39
 
 
40
 
 
41
class GroupEdit(gtk.Window):
 
42
 
 
43
    def __init__(self, db_connect, db_cursor,
 
44
                 class_id, class_name,
 
45
                 group_id, group_name, group_description,
 
46
                 group_user):
 
47
        # Create the toplevel window
 
48
        gtk.Window.__init__(self)
 
49
 
 
50
        self.cur = db_cursor
 
51
        self.con = db_connect
 
52
 
 
53
        self.group_id = group_id
 
54
        self.class_id = class_id
 
55
        
 
56
        # A pointer to the group_user_list class
 
57
        # Will be called to refresh the list when edit is done
 
58
        self.group_user = group_user
 
59
        
 
60
        self.set_title(_("Editing a Group"))
 
61
        self.set_border_width(8)
 
62
        self.set_default_size(320, 350)
 
63
 
 
64
        self.group_name = group_name
 
65
        if(self.group_name):
 
66
            frame = gtk.Frame(_("Editing group: ") + self.group_name +
 
67
                              _(" for class: ") + class_name)
 
68
            self.new_group = False
 
69
        else:
 
70
            frame = gtk.Frame(_("Editing a new group"))
 
71
            self.new_group = True
 
72
            self.group_name =""
 
73
            group_description = ""
 
74
 
 
75
 
 
76
        self.add(frame)
 
77
        
 
78
        # Main VBOX
 
79
        vbox = gtk.VBox(False, 8)
 
80
        vbox.set_border_width(8)
 
81
        frame.add(vbox)
 
82
 
 
83
        # Label and Entry for the group and description
 
84
        table = gtk.Table(2, 2, homogeneous=False)
 
85
        table.set_border_width(0)
 
86
        table.set_row_spacings(0)
 
87
        table.set_col_spacings(20)
 
88
        vbox.pack_start(table, True, True, 0)
 
89
        
 
90
        label = gtk.Label(_('Group:'))
 
91
        label.set_alignment(0, 0)
 
92
        table.attach(label, 0, 1, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
 
93
        self.entry_group = gtk.Entry()
 
94
        self.entry_group.set_max_length(20)
 
95
        self.entry_group.insert_text(self.group_name, position=0)
 
96
        table.attach(self.entry_group, 1, 2, 0, 1,
 
97
                     xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
 
98
 
 
99
        # FIXME: How to remove the selection
 
100
        
 
101
        # Label and Entry for the first name
 
102
        label = gtk.Label(_('Description:'))
 
103
        label.set_alignment(0, 0)
 
104
        table.attach(label, 0, 1, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
 
105
        self.entry_description = gtk.Entry()
 
106
        self.entry_description.set_max_length(30)
 
107
        self.entry_description.insert_text(group_description, position=0)
 
108
        table.attach(self.entry_description, 1, 2, 1, 2,
 
109
                     xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
 
110
 
 
111
 
 
112
        # Top message gives instructions
 
113
        label = gtk.Label(_('Assign all the users belonging to this group'))
 
114
        vbox.pack_start(label, False, False, 0)
 
115
        vbox.pack_start(gtk.HSeparator(), False, False, 0)
 
116
 
 
117
        # Lower area
 
118
        hbox = gtk.HBox(False, 8)
 
119
        vbox.pack_start(hbox, True, True, 0)
 
120
 
 
121
        # Left list
 
122
        # ---------
 
123
 
 
124
        # Create the table
 
125
        sw = gtk.ScrolledWindow()
 
126
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
 
127
        sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
128
 
 
129
        # create tree model
 
130
        self.model_left = self.__create_model(False, class_id, group_id)
 
131
 
 
132
        # create tree view
 
133
        treeview = gtk.TreeView(self.model_left)
 
134
        treeview.set_rules_hint(True)
 
135
        treeview.set_search_column(COLUMN_FIRSTNAME)
 
136
        treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
 
137
        
 
138
        sw.add(treeview)
 
139
        
 
140
        # add columns to the tree view
 
141
        self.__add_columns(treeview)
 
142
 
 
143
        hbox.pack_start(sw, True, True, 0)
 
144
 
 
145
 
 
146
        # Middle Button
 
147
        # -------------
 
148
        vbox2 = gtk.VBox(False, 8)
 
149
        vbox2.set_border_width(8)
 
150
        hbox.pack_start(vbox2, True, True, 0)
 
151
 
 
152
        button = gtk.Button(stock='gtk-add')
 
153
        button.connect("clicked", self.add_user, treeview)
 
154
        vbox2.pack_start(button, False, False, 0)
 
155
        button.show()
 
156
 
 
157
        button_delete = gtk.Button(stock='gtk-remove')
 
158
        vbox2.pack_start(button_delete, False, False, 0)
 
159
        button_delete.show()
 
160
 
 
161
 
 
162
        # Right List
 
163
        # ----------
 
164
 
 
165
        # Create the table
 
166
        sw2 = gtk.ScrolledWindow()
 
167
        sw2.set_shadow_type(gtk.SHADOW_ETCHED_IN)
 
168
        sw2.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
169
 
 
170
        # create tree model
 
171
        self.model_right = self.__create_model(True, class_id, group_id)
 
172
 
 
173
        # create tree view
 
174
        treeview2 = gtk.TreeView(self.model_right)
 
175
        treeview2.set_rules_hint(True)
 
176
        treeview2.set_search_column(COLUMN_FIRSTNAME)
 
177
        treeview2.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
 
178
 
 
179
        sw2.add(treeview2)
 
180
        
 
181
        # add columns to the tree view
 
182
        self.__add_columns(treeview2)
 
183
 
 
184
        hbox.pack_start(sw2, True, True, 0)
 
185
 
 
186
        # Confirmation Buttons
 
187
        # --------------------
 
188
        vbox.pack_start(gtk.HSeparator(), False, False, 0)
 
189
 
 
190
        bbox = gtk.HBox(homogeneous=False, spacing=8)
 
191
        
 
192
        button = gtk.Button(stock='gtk-help')
 
193
        bbox.pack_start(button, expand=False, fill=False, padding=0)
 
194
 
 
195
        button = gtk.Button(stock='gtk-ok')
 
196
        bbox.pack_end(button, expand=False, fill=False, padding=0)
 
197
        button.connect("clicked", self.ok)
 
198
 
 
199
        button = gtk.Button(stock='gtk-close')
 
200
        bbox.pack_end(button, expand=False, fill=False, padding=0)
 
201
        button.connect("clicked", self.close)
 
202
 
 
203
        vbox.pack_start(bbox, False, False, 0)
 
204
 
 
205
 
 
206
        # Missing callbacks
 
207
        button_delete.connect("clicked", self.remove_user, treeview2)
 
208
 
 
209
        # Ready GO
 
210
        self.show_all()
 
211
 
 
212
 
 
213
    # -------------------
 
214
    # GROUP Management
 
215
    # -------------------
 
216
 
 
217
    # Add user in the model
 
218
    def add_user_in_model(self, model, user):
 
219
        iter = model.append()
 
220
        model.set (iter,
 
221
                   COLUMN_USERID,    user[COLUMN_USERID],
 
222
                   COLUMN_FIRSTNAME, user[COLUMN_FIRSTNAME],
 
223
                   COLUMN_LASTNAME,  user[COLUMN_LASTNAME],
 
224
                   COLUMN_USER_EDITABLE,  False
 
225
                   )
 
226
 
 
227
    # class_id: only users in this class are inserted
 
228
    # group_id: only users in this group are inserted
 
229
    # If with = True,  create a list only with user in the given class_id and group_id.
 
230
    #           False, create a list only with user in the given class_id but NOT this group_id
 
231
    def __create_model(self, with, class_id, group_id):
 
232
 
 
233
        model = gtk.ListStore(
 
234
            gobject.TYPE_INT,
 
235
            gobject.TYPE_STRING,
 
236
            gobject.TYPE_STRING,
 
237
            gobject.TYPE_BOOLEAN)
 
238
 
 
239
        # Grab the all the users from this class
 
240
        self.cur.execute('SELECT user_id,firstname,lastname FROM users WHERE class_id=? ORDER BY login', (class_id,))
 
241
        user_data = self.cur.fetchall()
 
242
 
 
243
        for user in user_data:
 
244
 
 
245
            # Check our user is already in the group
 
246
            self.cur.execute('SELECT * FROM list_users_in_groups WHERE group_id=? AND user_id=?',
 
247
                             (group_id, user[0]))
 
248
            user_is_already = self.cur.fetchall()
 
249
            
 
250
            if(with and user_is_already):
 
251
                self.add_user_in_model(model, user)
 
252
            elif(not with and not user_is_already):
 
253
                self.add_user_in_model(model, user)
 
254
 
 
255
 
 
256
        return model
 
257
 
 
258
    def __add_columns(self, treeview):
 
259
 
 
260
        model = treeview.get_model()
 
261
        
 
262
        # columns for first name
 
263
        renderer = gtk.CellRendererText()
 
264
        renderer.set_data("column", COLUMN_FIRSTNAME)
 
265
        column = gtk.TreeViewColumn(_('First Name'), renderer,
 
266
                                    text=COLUMN_FIRSTNAME,
 
267
                                    editable=COLUMN_USER_EDITABLE)
 
268
        column.set_sort_column_id(COLUMN_FIRSTNAME)
 
269
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
270
        column.set_fixed_width(constants.COLUMN_WIDTH_FIRSTNAME)
 
271
        treeview.append_column(column)
 
272
 
 
273
        # column for last name
 
274
        renderer = gtk.CellRendererText()
 
275
        renderer.set_data("column", COLUMN_LASTNAME)
 
276
        column = gtk.TreeViewColumn(_('Last Name'), renderer,
 
277
                                    text=COLUMN_LASTNAME,
 
278
                                    editable=COLUMN_USER_EDITABLE)
 
279
        column.set_sort_column_id(COLUMN_LASTNAME)
 
280
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
281
        column.set_fixed_width(constants.COLUMN_WIDTH_LASTNAME)
 
282
        treeview.append_column(column)
 
283
 
 
284
 
 
285
    # Add a user from the left list to the right list
 
286
    #
 
287
    def add_user(self, button, treeview):
 
288
 
 
289
        model = treeview.get_model()
 
290
        
 
291
        treestore, paths = treeview.get_selection().get_selected_rows()
 
292
        
 
293
        paths.reverse()
 
294
        
 
295
        for path in paths:
 
296
            
 
297
            iter = treestore.get_iter(path)
 
298
            
 
299
            path = model.get_path(iter)[0]
 
300
            user_id        = model.get_value(iter, COLUMN_USERID)
 
301
            user_firstname = model.get_value(iter, COLUMN_FIRSTNAME)
 
302
            user_lastname  = model.get_value(iter, COLUMN_LASTNAME)
 
303
            model.remove(iter)
 
304
 
 
305
            # Add in the the right view
 
306
            self.add_user_in_model(self.model_right, (user_id, user_firstname, user_lastname))
 
307
            
 
308
            # Save the change in the base
 
309
            self.cur.execute('INSERT OR REPLACE INTO list_users_in_groups (group_id, user_id) VALUES (?, ?)',
 
310
                             (self.group_id, user_id))
 
311
            self.con.commit()
 
312
 
 
313
 
 
314
    # Add a user from the left list to the right list
 
315
    #
 
316
    def remove_user(self, button, treeview):
 
317
 
 
318
        model = treeview.get_model()
 
319
        
 
320
        treestore, paths = treeview.get_selection().get_selected_rows()
 
321
        
 
322
        paths.reverse()
 
323
        
 
324
        for path in paths:
 
325
            
 
326
            iter = treestore.get_iter(path)
 
327
            
 
328
            path = model.get_path(iter)[0]
 
329
            user_id        = model.get_value(iter, COLUMN_USERID)
 
330
            user_firstname = model.get_value(iter, COLUMN_FIRSTNAME)
 
331
            user_lastname  = model.get_value(iter, COLUMN_LASTNAME)
 
332
            model.remove(iter)
 
333
 
 
334
            # Add in the the left view
 
335
            self.add_user_in_model(self.model_left, (user_id, user_firstname, user_lastname))
 
336
            
 
337
            # Save the change in the base
 
338
            self.cur.execute('delete from list_users_in_groups where group_id=? and user_id=?',
 
339
                             (self.group_id, user_id))
 
340
            self.con.commit()
 
341
 
 
342
 
 
343
 
 
344
    # Done, can quit this dialog
 
345
    #
 
346
    def close(self, button):
 
347
        self.group_user.reload_group()
 
348
        self.destroy()
 
349
        
 
350
    # Done, can quit this dialog with saving
 
351
    #
 
352
    def ok(self, button):
 
353
 
 
354
        # Tell the user he must provide enough information
 
355
        if(self.entry_group.get_text() == ""):
 
356
            dialog = gtk.MessageDialog(None,
 
357
                                       gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
 
358
                                       gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
 
359
                                       _("You need to provide at least a name for your group"))
 
360
            dialog.run()
 
361
            dialog.destroy()
 
362
            return
 
363
 
 
364
        # If the group name as changed, check it does not exists already
 
365
        if(self.entry_group.get_text() != self.group_name):
 
366
            # Check the group does not exist already
 
367
            self.cur.execute('SELECT name FROM groups WHERE name=?',
 
368
                             (self.entry_group.get_text(),))
 
369
            if(self.cur.fetchone()):
 
370
                dialog = gtk.MessageDialog(None,
 
371
                                           gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
 
372
                                           gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
 
373
                                           _("There is already a group with this name"))
 
374
                dialog.run()
 
375
                dialog.destroy()
 
376
                return
 
377
 
 
378
        #
 
379
        # Now everything is correct, create the group
 
380
        #
 
381
 
 
382
        group_data = (self.group_id,
 
383
                      self.entry_group.get_text(),
 
384
                      self.class_id,
 
385
                      self.entry_description.get_text()
 
386
                      )
 
387
 
 
388
        if(self.new_group):
 
389
            # Create the new group
 
390
            group_id = constants.get_next_group_id(self.con, self.cur)
 
391
            self.cur.execute('INSERT INTO groups (group_id, name, class_id, description) ' +
 
392
                             'VALUES ( ?, ?, ?, ?)',
 
393
                             (group_data));
 
394
        else:
 
395
            # Save the group changes
 
396
            self.cur.execute('UPDATE groups SET name=?, description=? where group_id=?',
 
397
                             (self.entry_group.get_text(),
 
398
                              self.entry_description.get_text(),
 
399
                              self.group_id));
 
400
        self.con.commit()
 
401
 
 
402
        # Close the dialog window now
 
403
        self.group_user.reload_group()
 
404
 
 
405
        self.destroy()
 
406