[MERGE] Merge lp:openerp-web.
[odoo/odoo.git] / addons / event_moodle / event_moodle.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2012 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23 import xmlrpclib
24 import string
25 import time
26 import random
27 from random import sample
28 from openerp.tools.translate import _
29
30 class event_moodle(osv.osv):
31     _name = 'event.moodle.config.wiz'
32     _columns = {
33         'moodle_username' : fields.char('Moodle Username', 128, help="You can also connect with your username that you define when you create a token"),
34         'moodle_password' : fields.char('Moodle Password', 128),
35         'moodle_token' : fields.char('Moodle Token', 128, help="Put your token that you created in your moodle server"),
36         'server_moodle': fields.char('Moodle Server', 128, required=True,help="URL where you have your moodle server. For exemple: 'http://127.0.0.1' or 'http://localhost'"),
37         'url': fields.char('URL to Moodle Server', size=256, help="The url that will be used for the connection with moodle in xml-rpc"),
38     }
39
40     _order = 'create_date desc'
41
42     _defaults = {
43         'server_moodle': 'http://127.0.0.1',
44     }
45
46     def configure_moodle(self, cr, uid, ids, context=None):
47         url = self.make_url(cr, uid, ids, context=context)
48         self.write(cr, uid, ids, {'url': url})
49         return {'type': 'ir.actions.act_window_close'}
50
51     def find(self, cr, uid, context=None):
52         """
53         Find the config wizard containing the configuration and raise and error if none is available.
54         """
55         moodle_config_wiz_ids = self.search(cr, uid, [], context=context)
56         if not moodle_config_wiz_ids:
57             raise osv.except_osv(_('Error!'),_("First configure your moodle connection."))
58         return moodle_config_wiz_ids[0]
59
60     def make_url(self, cr, uid, ids, context=None):
61         """
62         create the good url with the information of the configuration
63         @return url for moodle connexion
64         """
65         def _encode_password(password):
66             for i in range(len(password)):
67                 x = password[i]
68                 if x not in string.ascii_letters + string.digits:
69                     unicode_car = (hex(ord(x)))
70                     hex_car = '%'+str(unicode_car[2:])
71                     password = password.replace(x,hex_car)
72             return password
73         url=""
74         config_moodle = self.browse(cr, uid, ids[0], context=context)
75         if config_moodle.moodle_username and config_moodle.moodle_password:
76             #connexion with password and username
77             password = _encode_password(config_moodle.moodle_password)
78             url = config_moodle.server_moodle + '/moodle/webservice/xmlrpc/simpleserver.php?wsusername=' + config_moodle.moodle_username + '&wspassword=' + password
79         if config_moodle.moodle_token:
80             #connexion with token
81             url = config_moodle.server_moodle + '/moodle/webservice/xmlrpc/server.php?wstoken=' + config_moodle.moodle_token
82         return url
83
84     def create_moodle_user(self, cr, uid, id, dic_user, context=None):
85         """
86         create a moodle user
87         @param dic_user : is a list of dictonnaries with the moodle information
88         @return a liste of dictonaries with the create user id
89         """
90         #connect to moodle
91         url = self.browse(cr, uid, id, context=context).url
92         sock = xmlrpclib.ServerProxy(url)
93         #add user in moodle and return list of id and username
94         return sock.core_user_create_users(dic_user)
95
96     def create_moodle_courses(self, cr, uid, id, courses, context=None):
97         """
98         create a mmodle course
99         @param courses : is a list of dictionaries with the moodle course information
100         @return a list of dictionaries with the create course id
101         """
102         #connect to moodle
103         url = self.browse(cr, uid, id, context=context).url
104         if not url:
105             raise osv.except_osv(_('Error!'),_("You must configure your moodle connection."))
106         sock = xmlrpclib.ServerProxy(url)
107         return sock.core_course_create_courses(courses)
108
109     def moodle_enrolled(self, cr, uid, id, enrolled, context=None):
110         """
111         this method is used to match a course with users
112         @param enrolled : list of dictonaries with the course id and the user id
113         """
114         #connect to moodle
115         url = self.browse(cr, uid, id, context=context).url
116         sock = xmlrpclib.ServerProxy(url)
117         #add enrolled in moodle
118         sock.enrol_manual_enrol_users(enrolled)
119
120     def create_password(self):
121         """
122         create a random password
123         """
124         rand = string.ascii_letters + string.digits
125         length = 8
126         passwd = ''.join(sample(rand, length))
127         passwd = passwd + '+'
128         return passwd
129
130     def check_email(self, email):
131     
132         """
133         check if email is correct
134         """
135         if email:
136             if (email.count('@') != 1 and email.count('.') < 1):
137                 raise osv.except_osv(_('Error!'),_("Your email '%s' is wrong.") % (email))
138
139     def make_username(self, username, response_courses):
140         """
141         create a moodle username with a random number for the uniqueness
142         @return the moodle username
143         """
144         if username:
145             #remove space in the name
146             username = username.replace(" ","_")
147             #give an user name
148             name_user = username + "%d" % (response_courses,) + "%d" % (random.randint(1,999999),)
149         else:
150             name_user = "moodle_" + "%d" % (response_courses,) + "%d" % (random.randint(1,999999),)
151         return name_user
152
153 event_moodle()
154
155 class event_event(osv.osv):
156     _inherit = "event.event"
157
158     _columns={
159         'moodle_id': fields.integer('Moodle ID', help='The identifier of this event in Moodle'),
160     }
161
162     def check_registration_limits(self, cr, uid, ids, context=None):
163         """
164         create moodle courses ,users and match them when an event is confirmed
165         if the event_registration is not confirmed then it doesn t nothing
166         """
167         res = super(event_event, self).check_registration_limits(cr, uid, ids, context=context)
168         moodle_pool = self.pool.get('event.moodle.config.wiz')
169         moodle_config_wiz_id = moodle_pool.find(cr, uid, context=context)
170         list_users=[]
171         userid = []
172         for event in self.browse(cr, uid, ids, context=context):
173             #moodle use time() to store the date
174             date = time.strptime(event.date_begin, '%Y-%m-%d %H:%M:%S')
175             date = int (time.mktime(date))
176             #create the dict of values to create the course in Moodle
177             dic_courses= [{
178                 'fullname': event.name,
179                 'shortname': '',
180                 'startdate': date,
181                 'summary': event.note,
182                 'categoryid':1, #the category hardcoded is 'Miscellaneous'
183                 }]
184             #create a course in moodle and keep the id
185             response_courses = moodle_pool.create_moodle_courses(cr, uid, moodle_config_wiz_id, dic_courses, context=context)
186             self.write(cr, uid, event.id, {'moodle_id': response_courses[0]['id']})
187
188             moodle_uids = []
189             for registration in event.registration_ids:
190                 if registration.state == 'open':
191                     #enroll the registration in Moodle as it is confirmed
192                     if not registration.moodle_uid:
193                         #create a dictionary for an user
194                         name_user = moodle_pool.make_username(registration.name, response_courses[0]['id'])
195                         moodle_pool.check_email(registration.email)
196                         passwd = moodle_pool.create_password()
197                         dic_users={
198                             'username' : name_user,
199                             'password' : passwd,
200                             'city' : registration.city,
201                             'firstname' : registration.name ,
202                             'lastname': '',
203                             'email': registration.email
204                         }
205                         #create the user in moodle
206                         response_user = moodle_pool.create_moodle_user(cr, uid, moodle_config_wiz_id, [dic_users], context=context)
207                         for user in response_user:
208                             self.pool.get('event.registration').write(cr,uid,[registration.id],{'moodle_uid': user['id'], 'moodle_user_password': passwd, 'moodle_username': name_user})
209                             moodle_uids.append(user['id'])
210                     else:
211                         moodle_uids.append(registration.moodle_uid)
212
213             #link the course with users
214             enrolled = []
215             for moodle_user in moodle_uids:
216                 enrolled.append({
217                 'roleid' :'5', #mark as 'Student'
218                 'userid' : moodle_user,
219                 'courseid' :response_courses[0]['id']
220                 })
221             moodle_pool.moodle_enrolled(cr, uid, moodle_config_wiz_id, enrolled, context=context)
222         return res
223
224 event_event()
225
226 class event_registration(osv.osv):
227
228     _inherit = "event.registration"
229
230     _columns={
231         'moodle_user_password': fields.char('Password for Moodle User', 128),
232         'moodle_username': fields.char('Moodle Username', 128),
233         'moodle_uid': fields.integer('Moodle User ID'),
234     }
235
236     def confirm_registration(self, cr, uid, ids, context=None):
237         """
238         create a user and match to a course if the event is already confirmed
239         """
240         res = super(event_registration, self).confirm_registration(cr, uid, ids, context=context)
241         moodle_pool = self.pool.get('event.moodle.config.wiz')
242         moodle_config_wiz_id = moodle_pool.find(cr, uid, context=context)
243         for register in self.browse(cr, uid, ids, context=context):
244             if register.event_id.state == 'confirm' and register.event_id.moodle_id:
245                 if not register.moodle_uid:
246                     #create the user in moodle
247                     name_user = moodle_pool.make_username(register.name, register.event_id.moodle_id)
248                     moodle_pool.check_email(register.email)
249                     passwd = moodle_pool.create_password()
250                     dic_users = [{
251                         'username': name_user,
252                         'password': passwd,
253                         'city': register.city,
254                         'firstname': register.name,
255                         'lastname': '', #we could make a split of the register.name on ' ' but it would be inaccurate, so it seems better to let it empty as it's not really useful
256                         'email': register.email,
257                     }]
258                     response_user = moodle_pool.create_moodle_user(cr, uid, moodle_config_wiz_id, dic_users, context=context)
259                     #write in database the password and the username
260                     moodle_user_id = response_user[0]['id']
261                     self.pool.get('event.registration').write(cr, uid, ids, {'moodle_uid': moodle_user_id, 'moodle_user_password': passwd, 'moodle_username': name_user})
262                 else:
263                     moodle_user_id = register.moodle_uid
264                 enrolled=[{
265                     'roleid': '5', #mark as student
266                     'userid': moodle_user_id,
267                     'courseid': register.event_id.moodle_id
268                 }]
269                 moodle_pool.moodle_enrolled(cr, uid, moodle_config_wiz_id, enrolled, context=context)
270         return res
271
272     def onchange_moodle_name(self, cr, uid, ids, moodle_username, context=None):
273         """
274         This onchange receive as parameter a username moddle and will fill the moodle_uid and password fields if existing records with this username are found
275             @param moodle_username: the existing moodle username
276         """
277         res = {}
278         reg_ids = self.search(cr, uid, [('moodle_username', '=', moodle_username)], order='create_date desc', context=context)
279         if reg_ids:
280             reg = self.browse(cr, uid, reg_ids[0], context=context)
281             res = {'value' :{
282                     'moodle_uid': reg.moodle_uid,
283                     'name': reg.name,
284                     'email':reg.email,
285                     'phone':reg.phone,
286                     'city': reg.city,
287                     'street': reg.street}}
288         return res
289 event_registration()