func CheckCourses(url string, username string) (result CheckCoursesReturn) {
	moodle, err := SearchMoodle(helperPrettifyURL(url), username)
	if err != nil {
		result.Error = errors.DBError
		return
	}

	// Get the host courses
	hostCourses, body, errorResponse, err := GetCourses(moodle.URL, moodle.Token, moodle.Userid)

	// Check for errors
	if err != nil {
		result.Error = errors.InternetError
		return
	}
	if errorResponse.ErrorCode != "" {
		if errorResponse.ErrorCode == "invalidtoken" {
			// Token isn't valid anymore
			errorCode := helperUpdateToken(&moodle)
			if errorCode == errors.NoError {
				// A new token was found, try again
				return CheckCourses(moodle.URL, moodle.Username)
			}
			result.Error = errorCode
			return result
		}
		result.Error = errors.ConvertMoodleError(errorResponse.ErrorCode)
		return
	}

	// Check if there were any changes (https://gist.github.com/sergiotapia/8263278)
	hasher := md5.New()
	hasher.Write(body)
	hash := hex.EncodeToString(hasher.Sum(nil))

	if hash == moodle.Previoushash {
		// Nothing changed
		result.Error = errors.NoError
		return
	}

	// Get the local courses
	localCourses, err := SearchCourses(moodle.ID)
	if err != nil {
		result.Error = errors.DBError
		return
	}

	// Everything is ok, compare
	result.Add, result.Mod, result.Del = helperCourseListComparer(localCourses, hostCourses)

	// Apply changes
	if InsertAddedCourses(moodle.ID, result.Add) != nil || UpdateModifiedCourses(moodle.ID, result.Mod) != nil || UpdateDeletedCourses(moodle.ID, result.Del) != nil {
		result.Error = errors.DBError
		return
	}

	result.Error = errors.NoError
	return
}
// CheckCourses checks if the courses available in Moodle have been added,
// changed or deleted. If that was the case, it will find out which courses were
// altered and handle them appropriately.
// The return values are:
// 1: All the new courses
// 2: All the changed courses
// 3: All the deleted courses
// 4: Error
// NOTE: This function doesn't check if the course's contents have been changed.
CheckCourses checks if the courses available in Moodle have been added,
changed or deleted. If that was the case, it will find out which courses were
altered and handle them appropriately.
The return values are:
1: All the new courses
2: All the changed courses
3: All the deleted courses
4: Error
NOTE: This function doesn't check if the course's contents have been changed.
// CheckCourses checks if the courses available in Moodle have been added // changed or deleted . If that was the case it will find out which courses were // altered and handle them appropriately . // The return values are : // 1 : All the new courses // 2 : All the changed courses // 3 : All the deleted courses // 4 : Error // NOTE : This function doesn ' t check if the course ' s contents have been changed .
func CheckCourses(url string, username string) (result CheckCoursesReturn) {
	moodle, err := SearchMoodle(helperPrettifyURL(url), username)
	if err != nil {
		result.Error = errors.DBError
		return
	}
	hostCourses, body, errorResponse, err := GetCourses(moodle.URL, moodle.Token, moodle.Userid)
	if err != nil {
		result.Error = errors.InternetError
		return
	}
	if errorResponse.ErrorCode != "" {
		if errorResponse.ErrorCode == "invalidtoken" {
			errorCode := helperUpdateToken(&moodle)
			if errorCode == errors.NoError {
				return CheckCourses(moodle.URL, moodle.Username)
			}
			result.Error = errorCode
			return result
		}
		result.Error = errors.ConvertMoodleError(errorResponse.ErrorCode)
		return
	}
	hasher := md5.New()
	hasher.Write(body)
	hash := hex.EncodeToString(hasher.Sum(nil))
	if hash == moodle.Previoushash {
		result.Error = errors.NoError
		return
	}
	localCourses, err := SearchCourses(moodle.ID)
	if err != nil {
		result.Error = errors.DBError
		return
	}
	result.Add, result.Mod, result.Del = helperCourseListComparer(localCourses, hostCourses)
	if InsertAddedCourses(moodle.ID, result.Add) != nil || UpdateModifiedCourses(moodle.ID, result.Mod) != nil || UpdateDeletedCourses(moodle.ID, result.Del) != nil {
		result.Error = errors.DBError
		return
	}
	result.Error = errors.NoError
	return
}
func CheckCourses ( url string , username string ) ( result CheckCoursesReturn ) { moodle , err := SearchMoodle ( helperPrettifyURL ( url ) , username ) if err != nil { result . Error = errors . DBError return } hostCourses , body , errorResponse , err := GetCourses ( moodle . URL , moodle . Token , moodle . Userid ) if err != nil { result . Error = errors . InternetError return } if errorResponse . ErrorCode != " " { if errorResponse . ErrorCode == " " { errorCode := helperUpdateToken ( & moodle ) if errorCode == errors . NoError { return CheckCourses ( moodle . URL , moodle . Username ) } result . Error = errorCode return result } result . Error = errors . ConvertMoodleError ( errorResponse . ErrorCode ) return } hasher := md5 . New ( ) hasher . Write ( body ) hash := hex . EncodeToString ( hasher . Sum ( nil ) ) if hash == moodle . Previoushash { result . Error = errors . NoError return } localCourses , err := SearchCourses ( moodle . ID ) if err != nil { result . Error = errors . DBError return } result . Add , result . Mod , result . Del = helperCourseListComparer ( localCourses , hostCourses ) if InsertAddedCourses ( moodle . ID , result . Add ) != nil || UpdateModifiedCourses ( moodle . ID , result . Mod ) != nil || UpdateDeletedCourses ( moodle . ID , result . Del ) != nil { result . Error = errors . DBError return } result . Error = errors . NoError return }
CheckCourses checks if the courses available in Moodle have been added,
changed or deleted.
CheckCourses checks if the courses available in Moodle have been added changed or deleted .
// Get the host courses // Check for errors // Token isn't valid anymore // A new token was found, try again // Check if there were any changes (https://gist.github.com/sergiotapia/8263278) // Nothing changed // Get the local courses // Everything is ok, compare // Apply changes