2012-01-31 10:34:39 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields , osv
import xmlrpclib
import string
2012-02-06 09:47:14 +00:00
import time
2012-02-01 12:35:17 +00:00
import random
2012-01-31 10:34:39 +00:00
from random import sample
2012-02-20 15:21:01 +00:00
2012-01-31 10:34:39 +00:00
class event_moodle ( osv . osv ) :
_name = ' event.moodle '
_columns = {
2012-02-02 09:51:55 +00:00
' moodle_username ' : fields . char ( ' Moodle username ' , 128 , help = " You can also connect with your username that you define when you create a tokken " ) ,
2012-01-31 10:34:39 +00:00
' moodle_password ' : fields . char ( ' Moodle password ' , 128 ) ,
2012-02-02 09:51:55 +00:00
' moodle_token ' : fields . char ( ' Moodle token ' , 128 , help = " Put your token that you created in your moodle serveur " ) ,
2012-02-21 15:50:40 +00:00
' serveur_moodle ' : fields . char ( ' Moodle server ' , 128 , required = True , help = " URL where you have your moodle server without http:// for exemple:127.0.0.1 or localhost " ) ,
2012-01-31 10:34:39 +00:00
}
2012-02-02 09:51:55 +00:00
2012-02-03 08:59:48 +00:00
url = ' '
2012-01-31 10:34:39 +00:00
def configure_moodle ( self , cr , uid , ids , context = None ) :
2012-02-06 10:50:22 +00:00
"""
Use to configure moodle
"""
2012-02-21 09:43:42 +00:00
self . write ( cr , uid , ids , { ' id ' : 1 } )
2012-01-31 16:01:23 +00:00
#save information that you need to create the url
2012-02-03 08:59:48 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
#use to quit the wizard
2012-02-06 10:50:22 +00:00
2012-02-01 12:35:17 +00:00
def make_url ( self , cr , uid , ids , context = None ) :
2012-02-06 10:50:22 +00:00
"""
create the good url with the information of the configuration
@return url for moodle connexion
"""
2012-02-24 16:16:38 +00:00
reg_ids = self . pool . get ( ' event.moodle ' ) . search ( cr , uid , [ ] , context = context )
if reg_ids == [ ] :
2012-02-21 15:50:40 +00:00
raise osv . except_osv ( ( ' Error! ' ) , ( " Configure moodle connexion before " ) )
#change this code and take directly the good ids
2012-02-20 15:21:01 +00:00
url = " "
2012-02-24 16:16:38 +00:00
config_moodle = self . browse ( cr , uid , [ reg_ids [ - 1 ] ] , context = context )
2012-01-31 16:01:23 +00:00
if config_moodle [ 0 ] . moodle_username and config_moodle [ 0 ] . moodle_password :
url = ' http:// ' + config_moodle [ 0 ] . serveur_moodle + ' /moodle/webservice/xmlrpc/simpleserver.php?wsusername= ' + config_moodle [ 0 ] . moodle_username + ' &wspassword= ' + config_moodle [ 0 ] . moodle_password
2012-02-02 13:55:01 +00:00
#connexion with password and username
#to do warning on special char
2012-01-31 16:01:23 +00:00
if config_moodle [ 0 ] . moodle_token :
url = ' http:// ' + config_moodle [ 0 ] . serveur_moodle + ' /moodle/webservice/xmlrpc/server.php?wstoken= ' + config_moodle [ 0 ] . moodle_token
2012-02-02 13:55:01 +00:00
#connexion with token
2012-02-01 12:35:17 +00:00
self . url = url
2012-01-31 10:34:39 +00:00
return url
2012-02-20 15:21:01 +00:00
#create a good url for xmlrpc connect
2012-02-06 10:50:22 +00:00
2012-02-03 08:59:48 +00:00
def create_moodle_user ( self , cr , uid , ids , dic_user ) :
2012-02-06 10:50:22 +00:00
"""
create a moodle user
@param dic_user : is a list of dictonnaries with the moodle information
@return a liste of dictonaries with the create user id
"""
2012-02-03 08:59:48 +00:00
self . make_url ( cr , uid , ids , context = None )
2012-02-02 13:55:01 +00:00
sock = xmlrpclib . ServerProxy ( self . url )
2012-01-31 10:34:39 +00:00
#connect to moodle
return sock . core_user_create_users ( dic_user )
2012-02-02 09:51:55 +00:00
#add user un moodle and return list of id and username
2012-01-31 10:34:39 +00:00
2012-02-03 08:59:48 +00:00
def create_moodle_courses ( self , cr , uid , ids , courses ) :
2012-02-06 10:50:22 +00:00
"""
create a mmodle course
@param courses : is a list of dictionaries with the moodle course information
@return a list of dictionaries with the create course id
"""
2012-02-03 08:59:48 +00:00
self . make_url ( cr , uid , ids , context = None )
2012-02-02 13:55:01 +00:00
sock = xmlrpclib . ServerProxy ( self . url )
2012-01-31 10:34:39 +00:00
#connect to moodle
2012-01-31 16:01:23 +00:00
return sock . core_course_create_courses ( courses )
2012-01-31 10:34:39 +00:00
#add course un moodle
2012-02-02 13:55:01 +00:00
2012-02-03 08:59:48 +00:00
def moodle_enrolled ( self , cr , uid , ids , enrolled ) :
2012-02-06 10:50:22 +00:00
"""
this method is used to match a course with users
@param enrolled : list of dictonaries with the course id and the user id
"""
2012-02-03 08:59:48 +00:00
self . make_url ( cr , uid , ids , context = None )
2012-02-02 13:55:01 +00:00
sock = xmlrpclib . ServerProxy ( self . url )
2012-01-31 10:34:39 +00:00
#connect to moodle
sock . enrol_manual_enrol_users ( enrolled )
#add enrolled un moodle
2012-02-02 13:55:01 +00:00
2012-02-01 12:53:55 +00:00
def create_password ( self ) :
2012-02-06 10:50:22 +00:00
"""
create a random password
"""
2012-02-01 12:53:55 +00:00
rand = string . ascii_letters + string . digits
length = 8
while length > len ( rand ) :
rand * = 2
passwd = ' ' . join ( sample ( rand , length ) )
passwd = passwd + ' + '
2012-02-02 13:55:01 +00:00
return passwd
# create a random password
2012-02-02 09:51:55 +00:00
def check_email ( self , email ) :
2012-02-06 10:59:51 +00:00
"""
check is email is true
"""
2012-02-03 08:59:48 +00:00
if email :
if ( email . count ( ' @ ' ) != 1 and email . count ( ' . ' ) < 1 ) :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( " Your email ' %s ' is wrong " ) % ( email ) )
2012-02-02 13:55:01 +00:00
2012-02-02 09:51:55 +00:00
def make_username ( self , username , response_courses ) :
2012-02-06 10:50:22 +00:00
"""
create a moodle username with a random number for the uniqueness
@return the moodle username
"""
2012-02-02 09:51:55 +00:00
if username :
username = username . replace ( " " , " _ " )
#remove space in the name
2012-02-02 13:55:01 +00:00
name_user = username + " %d " % ( response_courses , ) + " %d " % ( random . randint ( 1 , 999999 ) , )
2012-02-02 09:51:55 +00:00
#give an user name
else :
name_user = " moodle_ " + " %d " % ( response_courses , ) + " %d " % ( random . randint ( 1 , 999999 ) , )
2012-02-02 13:55:01 +00:00
return name_user
2012-02-01 13:49:33 +00:00
event_moodle ( )
2012-02-02 13:55:01 +00:00
2012-01-31 10:34:39 +00:00
class event_event ( osv . osv ) :
_inherit = " event.event "
2012-02-01 13:49:33 +00:00
_columns = {
' moodle_id ' : fields . integer ( ' Moodle id ' ) ,
}
2012-01-31 16:01:23 +00:00
def button_confirm ( self , cr , uid , ids , context = None ) :
2012-02-06 10:59:51 +00:00
"""
create moodle courses , users and match them when an event is confirmed
if the event_registration is not confirmed then it doesn t nothing
"""
2012-01-31 16:01:23 +00:00
list_users = [ ]
2012-02-07 09:34:25 +00:00
userid = [ ]
2012-02-02 13:55:01 +00:00
event = self . browse ( cr , uid , ids , context = context )
2012-02-06 09:47:14 +00:00
name_event = event [ 0 ] . name
date = event [ 0 ] . date_begin
date = time . strptime ( date , ' % Y- % m- %d % H: % M: % S ' )
date = int ( time . mktime ( date ) )
#moodle use time() to store the date
dic_courses = [ { ' fullname ' : name_event , ' shortname ' : ' ' , ' startdate ' : date , ' summary ' : event [ 0 ] . note , ' categoryid ' : 1 } ]
2012-01-31 16:01:23 +00:00
#create a dict course
moodle_pool = self . pool . get ( ' event.moodle ' )
2012-02-21 15:50:40 +00:00
response_courses = moodle_pool . create_moodle_courses ( cr , uid , ids , dic_courses )
2012-02-01 13:49:33 +00:00
self . write ( cr , uid , ids , { ' moodle_id ' : response_courses [ 0 ] [ ' id ' ] } )
#create a course in moodle and keep the id
2012-01-31 16:01:23 +00:00
for registration in event [ 0 ] . registration_ids :
2012-02-02 09:51:55 +00:00
name_user = moodle_pool . make_username ( registration . name , response_courses [ 0 ] [ ' id ' ] )
2012-02-02 13:55:01 +00:00
moodle_pool . check_email ( registration . email )
passwd = moodle_pool . create_password ( )
2012-02-06 12:02:10 +00:00
if registration . state == ' open ' :
2012-02-06 10:50:22 +00:00
#confirm if the registrator is confirmed
2012-02-07 09:34:25 +00:00
if registration . moodle_users_id == 0 :
dic_users = {
' username ' : name_user ,
' password ' : passwd ,
' city ' : registration . city ,
' firstname ' : registration . name ,
' lastname ' : ' ' ,
' email ' : registration . email
}
#create a dictionary for an user
list_users . append ( dic_users )
#add the dictionary in a list
else :
userid = [ ]
userid . append ( registration . moodle_users_id )
2012-02-21 15:50:40 +00:00
response_user = moodle_pool . create_moodle_user ( cr , uid , ids , list_users )
2012-01-31 16:01:23 +00:00
#create users in moodle
enrolled = [ ]
2012-02-07 09:34:25 +00:00
for list in userid :
self . pool . get ( ' event.registration ' ) . write ( cr , uid , [ registration . id ] , { ' moodle_users_id ' : list , ' moodle_user_password ' : passwd , ' moodle_users ' : name_user } )
#write in database the password and the username and the id
enrolled = [ {
' roleid ' : ' 5 ' ,
' userid ' : list ,
' courseid ' : response_courses [ 0 ] [ ' id ' ]
} ]
2012-02-21 15:50:40 +00:00
moodle_pool . moodle_enrolled ( cr , uid , ids , enrolled )
2012-02-07 09:34:25 +00:00
#link a course with users
2012-01-31 16:01:23 +00:00
for dic in response_user :
2012-02-07 09:34:25 +00:00
self . pool . get ( ' event.registration ' ) . write ( cr , uid , [ registration . id ] , { ' moodle_users_id ' : dic [ ' id ' ] , ' moodle_user_password ' : passwd , ' moodle_users ' : name_user } )
#write in database the password and the username and the id
2012-01-31 16:01:23 +00:00
enrolled = [ {
2012-02-01 12:35:17 +00:00
' roleid ' : ' 5 ' ,
2012-01-31 16:01:23 +00:00
' userid ' : dic [ ' id ' ] ,
' courseid ' : response_courses [ 0 ] [ ' id ' ]
} ]
2012-02-21 15:50:40 +00:00
moodle_pool . moodle_enrolled ( cr , uid , ids , enrolled )
2012-02-02 09:51:55 +00:00
#link a course with users
2012-01-31 16:01:23 +00:00
return super ( event_event , self ) . button_confirm ( cr , uid , ids , context )
2012-02-02 13:55:01 +00:00
event_event ( )
2012-01-31 16:01:23 +00:00
2012-02-01 12:35:17 +00:00
class event_registration ( osv . osv ) :
_inherit = " event.registration "
_columns = {
' moodle_user_password ' : fields . char ( ' password for moodle user ' , 128 ) ,
2012-02-06 09:47:14 +00:00
' moodle_users ' : fields . char ( ' moodle username ' , 128 ) ,
2012-02-07 09:34:25 +00:00
' moodle_users_id ' : fields . integer ( ' moodle uid ' ) ,
2012-02-21 15:50:40 +00:00
' moodle_check_user ' : fields . char ( ' Check Moodle Username ' , 128 , help = ' Try to find an existing moodle username ' )
2012-02-07 09:34:25 +00:00
}
_defaults = {
' moodle_check_user ' : ' '
2012-02-01 12:35:17 +00:00
}
2012-02-06 12:02:10 +00:00
def case_open ( self , cr , uid , ids , context = None ) :
2012-02-06 10:59:51 +00:00
"""
create a user and match to a course if the event is already confirmed
"""
2012-02-01 13:49:33 +00:00
register = self . browse ( cr , uid , ids , context = context )
2012-02-07 09:34:25 +00:00
if register [ 0 ] . event_id . state == ' confirm ' :
2012-02-02 08:02:08 +00:00
moodle_pool = self . pool . get ( ' event.moodle ' )
2012-02-07 09:34:25 +00:00
if register [ 0 ] . moodle_users_id == 0 :
moodle_pool = self . pool . get ( ' event.moodle ' )
name_user = moodle_pool . make_username ( register [ 0 ] . name , register [ 0 ] . event_id . moodle_id )
passwd = moodle_pool . create_password ( )
dic_users = [ {
' username ' : name_user ,
' password ' : passwd ,
' city ' : register [ 0 ] . city ,
' firstname ' : register [ 0 ] . name ,
2012-02-21 15:50:40 +00:00
' lastname ' : ' ' , #no need and difficult to now the difference between firstname and lastname
2012-02-07 09:34:25 +00:00
' email ' : register [ 0 ] . email
} ]
#create a dictionary for an use
2012-02-21 15:50:40 +00:00
response_user = moodle_pool . create_moodle_user ( cr , uid , ids , dic_users )
2012-02-07 09:34:25 +00:00
self . pool . get ( ' event.registration ' ) . write ( cr , uid , ids , { ' moodle_users_id ' : response_user [ 0 ] [ ' id ' ] , ' moodle_user_password ' : passwd , ' moodle_users ' : name_user } )
#write in database the password and the username
enrolled = [ {
' roleid ' : ' 5 ' ,
' userid ' : response_user [ 0 ] [ ' id ' ] , #use the response of the create user
' courseid ' : register [ 0 ] . event_id . moodle_id
} ]
else :
enrolled = [ {
' roleid ' : ' 5 ' ,
' userid ' : register [ 0 ] . moodle_users_id ,
' courseid ' : register [ 0 ] . event_id . moodle_id
} ]
2012-02-21 15:50:40 +00:00
moodle_pool . moodle_enrolled ( cr , uid , ids , enrolled )
2012-02-07 11:23:04 +00:00
return super ( event_registration , self ) . case_open ( cr , uid , ids , context )
2012-02-06 09:47:14 +00:00
2012-02-07 09:34:25 +00:00
def onchange_moodle_name ( self , cr , uid , ids , moodle_check_user , context = None ) :
2012-02-21 15:50:40 +00:00
"""
@param moodle_check_user : the moodle id
search the moodle id if exist you fill in the field
"""
2012-02-07 11:23:04 +00:00
req_sql = " select name,email,phone,city,street,moodle_users,moodle_users_id from event_registration "
2012-02-07 09:34:25 +00:00
cr . execute ( req_sql )
sql_res = cr . dictfetchall ( )
res = { }
for username in sql_res :
if username [ ' moodle_users ' ] == moodle_check_user :
2012-02-07 11:23:04 +00:00
res = { ' value ' : { ' moodle_users_id ' : username [ ' moodle_users_id ' ] , ' name ' : username [ ' name ' ] , ' email ' : username [ ' email ' ] , ' phone ' : username [ ' phone ' ] , ' city ' : username [ ' city ' ] , ' street ' : username [ ' street ' ] } }
return res
2012-02-07 09:34:25 +00:00
else :
res = { ' value ' : { ' moodle_users_id ' : 0 } }
return res