1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2012 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
27 from random import sample
28 from tools.translate import _
30 class event_moodle(osv.osv):
31 _name = 'event.moodle.config.wiz'
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"),
40 _order = 'create_date desc'
43 'server_moodle': 'http://127.0.0.1',
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'}
51 def find(self, cr, uid, context=None):
53 Find the config wizard containing the configuration and raise and error if none is available.
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]
60 def make_url(self, cr, uid, ids, context=None):
62 create the good url with the information of the configuration
63 @return url for moodle connexion
65 def _encode_password(password):
66 for i in range(len(password)):
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)
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:
81 url = config_moodle.server_moodle + '/moodle/webservice/xmlrpc/server.php?wstoken=' + config_moodle.moodle_token
84 def create_moodle_user(self, cr, uid, id, dic_user, context=None):
87 @param dic_user : is a list of dictonnaries with the moodle information
88 @return a liste of dictonaries with the create user id
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)
96 def create_moodle_courses(self, cr, uid, id, courses, context=None):
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
103 url = self.browse(cr, uid, id, context=context).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)
109 def moodle_enrolled(self, cr, uid, id, enrolled, context=None):
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
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)
120 def create_password(self):
122 create a random password
124 rand = string.ascii_letters + string.digits
126 passwd = ''.join(sample(rand, length))
127 passwd = passwd + '+'
130 def check_email(self, email):
133 check if email is correct
136 if (email.count('@') != 1 and email.count('.') < 1):
137 raise osv.except_osv(_('Error!'),_("Your email '%s' is wrong.") % (email))
139 def make_username(self, username, response_courses):
141 create a moodle username with a random number for the uniqueness
142 @return the moodle username
145 #remove space in the name
146 username = username.replace(" ","_")
148 name_user = username + "%d" % (response_courses,) + "%d" % (random.randint(1,999999),)
150 name_user = "moodle_" + "%d" % (response_courses,) + "%d" % (random.randint(1,999999),)
155 class event_event(osv.osv):
156 _inherit = "event.event"
159 'moodle_id': fields.integer('Moodle ID', help='The identifier of this event in Moodle'),
162 def check_registration_limits(self, cr, uid, ids, context=None):
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
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)
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
178 'fullname': event.name,
181 'summary': event.note,
182 'categoryid':1, #the category hardcoded is 'Miscellaneous'
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']})
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()
198 'username' : name_user,
200 'city' : registration.city,
201 'firstname' : registration.name ,
203 'email': registration.email
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'])
211 moodle_uids.append(registration.moodle_uid)
213 #link the course with users
215 for moodle_user in moodle_uids:
217 'roleid' :'5', #mark as 'Student'
218 'userid' : moodle_user,
219 'courseid' :response_courses[0]['id']
221 moodle_pool.moodle_enrolled(cr, uid, moodle_config_wiz_id, enrolled, context=context)
226 class event_registration(osv.osv):
228 _inherit = "event.registration"
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'),
236 def confirm_registration(self, cr, uid, ids, context=None):
238 create a user and match to a course if the event is already confirmed
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()
251 'username': name_user,
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,
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})
263 moodle_user_id = register.moodle_uid
265 'roleid': '5', #mark as student
266 'userid': moodle_user_id,
267 'courseid': register.event_id.moodle_id
269 moodle_pool.moodle_enrolled(cr, uid, moodle_config_wiz_id, enrolled, context=context)
272 def onchange_moodle_name(self, cr, uid, ids, moodle_username, context=None):
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
278 reg_ids = self.search(cr, uid, [('moodle_username', '=', moodle_username)], order='create_date desc', context=context)
280 reg = self.browse(cr, uid, reg_ids[0], context=context)
282 'moodle_uid': reg.moodle_uid,
287 'street': reg.street}}