func NewUser(url string, username string, password string, location string) errors.ErrorCode {
	// Check if the URL has the last /
	url = helperPrettifyURL(url)

	// Send and receive the authentication request
	response, err := RequestAuthentication(url, username, password)
	if err != nil {
		return errors.InternetError
	}

	// Check if the request was unsuccessful
	if _, ok := response["error"]; ok {
		return errors.ConvertMoodleError(response["errorcode"].(string))
	}

	var token string = response["token"].(string)

	// The request was successful
	// Get the rest of the information from moodle and store it
	info, err := GetSiteInfo(url, token)
	if err != nil {
		return errors.InternetError
	}

	info.Token = token
	info.URL = url
	info.Username = username
	info.Password = password

	// Check if the location exists, if it doesn't try to create it
	if location == "" {
		// Create the default location
		hash := md5.Sum([]byte(info.Sitename + info.Username))
		folder := hex.EncodeToString(hash[:])
		location = files.GetSettingsPath() + folder
	}
	err = helpers.CreateLocation(location)

	if err != nil {
		return errors.IOError
	}

	info.Location = location

	err = SaveSiteInfo(info, url, username)
	if err != nil {
		return errors.UnableToSaveSiteInfo
	}

	return errors.NoError
}
// NewUser authenticates the user, stores its information in the DB
// and returns the token if it was successfull
// If location is an empty string, it will automatically store to the default
// location
// If it wasn't able to authenticate, then it will return an eror message and
// the corresponding error code
NewUser authenticates the user, stores its information in the DB
and returns the token if it was successfull
If location is an empty string, it will automatically store to the default
location
If it wasn't able to authenticate, then it will return an eror message and
the corresponding error code
// NewUser authenticates the user stores its information in the DB // and returns the token if it was successfull // If location is an empty string it will automatically store to the default // location // If it wasn ' t able to authenticate then it will return an eror message and // the corresponding error code
func NewUser(url string, username string, password string, location string) errors.ErrorCode {
	url = helperPrettifyURL(url)
	response, err := RequestAuthentication(url, username, password)
	if err != nil {
		return errors.InternetError
	}
	if _, ok := response["error"]; ok {
		return errors.ConvertMoodleError(response["errorcode"].(string))
	}
	var token string = response["token"].(string)
	info, err := GetSiteInfo(url, token)
	if err != nil {
		return errors.InternetError
	}
	info.Token = token
	info.URL = url
	info.Username = username
	info.Password = password
	if location == "" {
		hash := md5.Sum([]byte(info.Sitename + info.Username))
		folder := hex.EncodeToString(hash[:])
		location = files.GetSettingsPath() + folder
	}
	err = helpers.CreateLocation(location)
	if err != nil {
		return errors.IOError
	}
	info.Location = location
	err = SaveSiteInfo(info, url, username)
	if err != nil {
		return errors.UnableToSaveSiteInfo
	}
	return errors.NoError
}
func NewUser ( url string , username string , password string , location string ) errors . ErrorCode { url = helperPrettifyURL ( url ) response , err := RequestAuthentication ( url , username , password ) if err != nil { return errors . InternetError } if _ , ok := response [ " " ] ; ok { return errors . ConvertMoodleError ( response [ " " ] . ( string ) ) } var token string = response [ " " ] . ( string ) info , err := GetSiteInfo ( url , token ) if err != nil { return errors . InternetError } info . Token = token info . URL = url info . Username = username info . Password = password if location == " " { hash := md5 . Sum ( [ ] byte ( info . Sitename + info . Username ) ) folder := hex . EncodeToString ( hash [ : ] ) location = files . GetSettingsPath ( ) + folder } err = helpers . CreateLocation ( location ) if err != nil { return errors . IOError } info . Location = location err = SaveSiteInfo ( info , url , username ) if err != nil { return errors . UnableToSaveSiteInfo } return errors . NoError }
NewUser authenticates the user, stores its information in the DB
and returns the token if it was successfull
If location is an empty string, it will automatically store to the default
location
If it wasn't able to authenticate, then it will return an eror message and
the corresponding error code
NewUser authenticates the user stores its information in the DB and returns the token if it was successfull If location is an empty string it will automatically store to the default location If it wasn ' t able to authenticate then it will return an eror message and the corresponding error code
// Check if the URL has the last / // Send and receive the authentication request // Check if the request was unsuccessful // The request was successful // Get the rest of the information from moodle and store it // Check if the location exists, if it doesn't try to create it // Create the default location