Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/python/admin/class_list.py
blob: 66d25c881dd097d5f708e015f0dbf9ab45f592c6 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#  gcompris - class_list.py
#
# Copyright (C) 2005 Bruno Coudoin and Yves Combe
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import gnomecanvas
import gcompris
import gcompris.utils
import gcompris.skin
import gtk
import gtk.gdk
import gobject
from gettext import gettext as _

# Database
from pysqlite2 import dbapi2 as sqlite

import constants

import class_edit
import user_list

# Class Management
(
  COLUMN_CLASSID,
  COLUMN_NAME,
  COLUMN_TEACHER,
) = range(3)


class Class_list:
  """GCompris Class List Table"""


  # area is the drawing area for the list
  def __init__(self, frame, db_connect, db_cursor):

      self.cur = db_cursor
      self.con = db_connect

      self.class_data = []

      # ---------------
      # Class Management
      # ---------------

      # create tree model
      model = self.__create_model_class()

      # Main box is vertical
      top_box = gtk.VBox(False, 8)
      top_box.show()
      frame.add(top_box)

      # First line
      group_hbox = gtk.HBox(False, 8)
      group_hbox.show()
      top_box.add(group_hbox)

      left_box = gtk.VBox(False, 8)
      left_box.show()
      group_hbox.add(left_box)

      right_box = gtk.VBox(False, 8)
      right_box.show()
      group_hbox.add(right_box)


      # Create the table
      sw = gtk.ScrolledWindow()
      sw.show()
      sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
      sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

      # create tree view
      self.treeview_class = gtk.TreeView(model)
      self.treeview_class.show()
      self.treeview_class.set_rules_hint(True)
      self.treeview_class.set_search_column(COLUMN_NAME)

      sw.add(self.treeview_class)

      left_box.pack_start(sw, True, True, 0)

      # add columns to the tree view
      self.__add_columns_class(self.treeview_class)

      # Add buttons

      button = gtk.Button(stock='gtk-add')
      button.connect("clicked", self.on_add_class_clicked, self.treeview_class)
      right_box.pack_start(button, False, False, 0)
      button.show()

      self.button_edit = gtk.Button(stock='gtk-edit')
      self.button_edit.connect("clicked", self.on_edit_class_clicked, self.treeview_class)
      right_box.pack_start(self.button_edit, False, False, 0)
      self.button_edit.show()
      # Not editable until one class is selected
      self.button_edit.set_sensitive(False)

      self.button_remove = gtk.Button(stock='gtk-remove')
      self.button_remove.connect("clicked", self.on_remove_class_clicked, self.treeview_class)
      right_box.pack_start(self.button_remove, False, False, 0)
      self.button_remove.show()
      # Not removable until one class is selected
      self.button_remove.set_sensitive(False)

      # User list for the group
      user_hbox = gtk.HBox(False, 8)
      user_hbox.show()
      top_box.add(user_hbox)


      self.list_user = user_list.User_list(user_hbox,
                                           self.con, self.cur)

      # Missing callbacks
      selection = self.treeview_class.get_selection()
      selection.connect('changed', self.class_changed_cb, self.list_user)


  # -------------------
  # Class Management
  # -------------------

  def __create_model_class(self):
    model = gtk.ListStore(
      gobject.TYPE_INT,
      gobject.TYPE_STRING,
      gobject.TYPE_STRING,
      gobject.TYPE_BOOLEAN)

    # Grab the class data
    self.cur.execute('select * from class')
    self.class_data = self.cur.fetchall()

    for item in self.class_data:
      iter = model.append()
      model.set(iter,
                 COLUMN_CLASSID,   item[COLUMN_CLASSID],
                 COLUMN_NAME,      item[COLUMN_NAME],
                 COLUMN_TEACHER,   item[COLUMN_TEACHER])
    return model


  def __add_columns_class(self, treeview):
    model = treeview.get_model()

    # Total column length must be 400

    # columns for name
    renderer = gtk.CellRendererText()
    renderer.set_data("column", COLUMN_NAME)
    column = gtk.TreeViewColumn(_('Class'), renderer,
                                text=COLUMN_NAME)
    column.set_sort_column_id(COLUMN_NAME)
    column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    column.set_fixed_width(constants.COLUMN_WIDTH_CLASSNAME)
    treeview.append_column(column)

    # columns for teacher
    renderer = gtk.CellRendererText()
    renderer.set_data("column", COLUMN_TEACHER)
    column = gtk.TreeViewColumn(_('Teacher'), renderer,
                                text=COLUMN_TEACHER)
    column.set_sort_column_id(COLUMN_TEACHER)
    column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    column.set_fixed_width(constants.COLUMN_WIDTH_TEACHER)
    treeview.append_column(column)


  # Add class in the model
  def add_class_in_model(self, model, aclass):
    iter = model.append()
    model.set (iter,
               COLUMN_CLASSID,    aclass[COLUMN_CLASSID],
               COLUMN_NAME,       aclass[COLUMN_NAME],
               COLUMN_TEACHER,    aclass[COLUMN_TEACHER]
               )


  def on_remove_class_clicked(self, button, treeview):
    selection = treeview.get_selection()
    model, iter = selection.get_selected()

    if iter:
      path = model.get_path(iter)[0]
      class_id = model.get_value(iter, COLUMN_CLASSID)
      model.remove(iter)

      #
      # Remove it from the base (Triggers maintains other tables)
      #
      print "Deleting class_id=" + str(class_id)
      self.cur.execute('DELETE FROM class WHERE class_id=?', (class_id,))
      self.con.commit()

      print "class_list:remove_class_clicked calling reload"
      self.list_user.reload(class_id)


  def on_edit_class_clicked(self, button, treeview):
    selection = treeview.get_selection()
    model, iter = selection.get_selected()

    if iter:
      path = model.get_path(iter)[0]
      class_id      = model.get_value(iter, COLUMN_CLASSID)
      class_name    = model.get_value(iter, COLUMN_NAME)
      teacher_name  = model.get_value(iter, COLUMN_TEACHER)
      class_edit.ClassEdit(self.con, self.cur,
                           class_id, class_name, teacher_name,
                           self)


  def on_add_class_clicked(self, button, treeview):
    model = treeview.get_model()
    class_id = constants.get_next_class_id(self.con, self.cur)

    class_edit.ClassEdit(self.con, self.cur,
                         class_id, "", "",
                         self)


  # The class is changed ...
  def class_changed_cb(self, selection, user_list):
    model, iter = selection.get_selected()

    if iter:
      path = model.get_path(iter)[0]
      class_id = model.get_value(iter, COLUMN_CLASSID)
      print "class_changed_cb %d" %class_id
      user_list.reload(class_id)

      # The Unaffected class is not editable.
      if class_id == 1:
        self.button_edit.set_sensitive(False)
        self.button_remove.set_sensitive(False)
      else:
        self.button_edit.set_sensitive(True)
        self.button_remove.set_sensitive(True)


  # Reload data (class data and users)
  def reload(self, class_id, class_name, class_teacher):
    print ">> class_list:reload %d" %class_id
    print "class_name %s" %class_name
    # We need to find the row matching this class_id.
    # If not found, it's a new class to create
    model = self.treeview_class.get_model()
    iter = model.get_iter_first()

    updated = False
    # Loop over each class raw
    while(iter):
      path = model.get_path(iter)[0]
      tmp_class_id = model.get_value(iter, COLUMN_CLASSID)

      if(tmp_class_id == class_id):

        # Now update the class_name and class_teacher if provided
        if class_name:
          print "updating %s" %class_name
          print dir(class_name)
          model.set(iter, COLUMN_NAME, class_name)

        if class_teacher:
          model.set(iter, COLUMN_TEACHER, class_teacher)

        updated = True
        # It's updated now
        break

      iter = model.iter_next(iter)



    # The job not done yet, it's a new class.
    if(not updated):
      new_class = [class_id, class_name, class_teacher]
      self.add_class_in_model(model, new_class)

    # Reload the selected class
    selection = self.treeview_class.get_selection()
    if(selection):
      print "got selection"
      model, iter = selection.get_selected()
      if iter:
        path = model.get_path(iter)[0]
        sel_class_id = model.get_value(iter, COLUMN_CLASSID)
        print "got selection %d" %sel_class_id
        self.list_user.reload(sel_class_id)

    print "class_list reload DONE"