import { User } from '../src/models/User' ; import { Course , ICourseModel } from '../src/models/Course' ; import { IUserModel } from '../src/models/User' ; import { Lecture , ILectureModel } from '../src/models/Lecture' ; import { Unit , IUnitModel } from '../src/models/units/Unit' ; import { ICourse } from '../../shared/models/ICourse' ; import { ILecture } from '../../shared/models/ILecture' ; import { IUnit } from '../../shared/models/units/IUnit' ; import { IUser } from '../../shared/models/IUser' ; import { ITaskUnit } from '../../shared/models/units/ITaskUnit' ; import { ITaskUnitModel } from '../src/models/units/TaskUnit' ; import { IChatRoom } from '../../shared/models/IChatRoom' ; import { extractSingleMongoId } from '../src/utilities/ExtractMongoId' ; import * as mongoose from 'mongoose' ; import ObjectId = mongoose . Types . ObjectId ; export class FixtureUtils { public static async getRandomUser ( hash ? ) { const array = await this . getUser ( ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomUsers ( min , max , hash ? ) { const array = await this . getUser ( ) ; return this . getRandomArray < IUser > ( array , min , max , hash ) ; } public static async getRandomAdmin ( hash ? ) { const array = await this . getAdmins ( ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomAdmins ( min , max , hash ? ) { const array = await this . getAdmins ( ) ; return this . getRandomArray < IUser > ( array , min , max , hash ) ; } public static async getRandomTeacher ( hash ? ) { const array = await this . getTeacher ( ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomTeacherForCourse ( course , hash ? ) { const user = await this . getRandom < IUser > ( [ course . courseAdmin , ... course . teachers ] , hash ) ; return await User . findById ( user ) ; } public static async getRandomStudentForCourse ( course , hash ? ) { const user = await this . getRandom < IUser > ( [ ... course . students ] , hash ) ; return await User . findById ( user ) ; } public static async getRandomTeachers ( min , max , hash ? ) { const array = await this . getTeacher ( ) ; return this . getRandomArray < IUser > ( array , min , max , hash ) ; } public static async getRandomStudent ( hash ? ) { const array = await this . getStudents ( ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomActiveStudent ( hash ? ) { const array = await User . find ( { role : 'student' , isActive : true } ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomInactiveStudent ( hash ? ) { const array = await User . find ( { role : 'student' , isActive : false } ) ; return this . getRandom < IUser > ( array , hash ) ; } public static async getRandomStudents ( min , max , hash ? ) { const array = await this . getStudents ( ) ; return this . getRandomArray < IUser > ( array , min , max , hash ) ; } public static async getRandomWhitelistUsers ( students , course , hash ? ) { const randomArray = students . splice ( 0 , this . getRandomNumber ( 0 , students . length - 1 ) ) ; const array = await this . getRandomArray ( randomArray , 0 , students . length - 1 , hash ) ; return array . map ( ( stud ) => { return { firstName : stud . profile . firstName , lastName : stud . profile . lastName , uid : stud . uid , courseId : new ObjectId ( course . _id ) } ; } ) ; } public static async getRandomCourse ( hash ? ) { const array = < ICourseModel [ ] > await this . getCourses ( ) ; return this . getRandom < ICourseModel > ( array , hash ) ; } public static async getRandomCourseWithAllUnitTypes ( hash ? ) { const array = await this . getCourses ( ) ; const coursesWithAllUnitTypes = [ ] ; for ( const course of array ) { let hasFreeText = false ; let hasTask = false ; let hasCodeKata = false ; for ( const lecId of course . lectures ) { const lec = await Lecture . findById ( lecId ) ; for ( const unitId of lec . units ) { const unit = await Unit . findById ( unitId ) ; if ( unit . __t === 'free-text' ) { hasFreeText = true ; } else if ( unit . __t === 'task' ) { hasTask = true ; } else if ( unit . __t === 'code-kata' ) { hasCodeKata = true ; } } } if ( hasFreeText && hasTask && hasCodeKata ) { coursesWithAllUnitTypes . push ( course ) ; } } return this . getRandom < ICourse > ( coursesWithAllUnitTypes , hash ) ; } public static async getCourseFromLecture ( lecture ) { return Course . findOne ( { lectures : { $in : [ lecture . _id ] } } ) ; } public static async getCourseFromUnit ( unit ) { return Course . findById ( unit . _course ) ; } public static async getRandomLecture ( hash ? ) { const array = await this . getLectures ( ) ; return this . getRandom < ILectureModel > ( array , hash ) ; } public static async getRandomLectureFromCourse ( course , hash ? ) { const lectureId = await this . getRandom < ILecture > ( course . lectures , hash ) ; return Lecture . findById ( lectureId ) ; } public static async getLectureFromUnit ( unit ) { return Lecture . findOne ( { units : { $in : [ unit . _id ] } } ) ; } public static async getRandomUnit ( hash ? ) { const array = await this . getUnits ( ) ; return this . getRandom < IUnitModel > ( array , hash ) ; } public static async getRandomUnitFromLecture ( lecture , hash ? ) { const unitId = await this . getRandom < IUnit > ( lecture . units , hash ) ; return Unit . findById ( unitId ) ; } public static async getRandomUnitFromCourse ( course , hash ? ) { const units = await Unit . find ( { _course : course } ) ; return await this . getRandom < IUnitModel > ( units , hash ) ; } param hash returns public static async getSimpleChatRoomSetup ( hash ? ) { const course = await FixtureUtils . getRandomCourse ( ) ; const room = await FixtureUtils . getRandom < IChatRoom > ( course . chatRooms , hash ) ; const roomId = extractSingleMongoId ( room ) ; return { course , roomId } ; } param course returns public static async getUnauthorizedTeacherForCourse ( course ) { return await User . findOne ( { _id : { $nin : [ course . courseAdmin , ... course . teachers ] } , role : 'teacher' } ) ; } param course returns public static async getUnauthorizedStudentForCourse ( course ) { return await User . findOne ( { _id : { $nin : course . students } , role : 'student' } ) ; } private static async getAdmins ( ) { return this . getUser ( 'admin' ) ; } private static async getTeacher ( ) { return this . getUser ( 'teacher' ) ; } private static async getStudents ( ) { return this . getUser ( 'student' ) ; } public static async getUserCount ( ) { return User . countDocuments ( { } ) ; } public static async getCourses ( ) { return Course . find ( ) . populate ( 'students' ) . populate ( 'whitelist' ) ; } public static async getLectures ( ) { return Lecture . find ( ) ; } public static async getUnits ( ) { return Unit . find ( ) ; } private static async getUser ( role ? ) { if ( ! role ) { return User . find ( ) ; } return User . find ( { role : role } ) ; } private static async getRandom < T > ( array , hash ? ) { if ( hash ) { return array [ this . getNumberFromString ( hash , 0 , array . length ) ] ; } else { return array [ this . getRandomNumber ( 0 , array . length ) ] ; } } private static async getRandomArray < T > ( array , min , max , hash ? ) { if ( hash ) { const count = this . getNumberFromString ( hash , min , max ) ; const start = this . getNumberFromString ( hash , 0 , array . length - count ) ; return array . slice ( start , start + count ) ; } else { const shuffeledArray = this . shuffleArray ( array ) ; const count = this . getRandomNumber ( min , max ) ; const start = this . getRandomNumber ( 0 , shuffeledArray . length - count ) ; return shuffeledArray . slice ( start , start + count ) ; } } public static getRandomNumber ( start , end ) { return Math . floor ( Math . random ( ) * end ) + start ; } public static getNumberFromString ( str , start , end ) { let hash = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { const char = str . charCodeAt ( i ) ; hash += char ; } return start + ( hash % ( end - start ) ) ; } private static shuffleArray < T > ( array ) { let tmp ; let current ; let top = array . length ; if ( top ) { while ( -- top ) { current = Math . floor ( Math . random ( ) * ( top + 1 ) ) ; tmp = array [ current ] ; array [ current ] = array [ top ] ; array [ top ] = tmp ; } } return array ; } public static getAnswersFromTaskUnit ( unit , success ) { const answers = { } ; const unitObj = < ITaskUnit > unit . toObject ( ) ; unitObj . tasks . forEach ( ( task ) => { answers [ task . _id . toString ( ) ] = { } ; task . answers . forEach ( ( answer ) => { if ( success ) { if ( answer . hasOwnProperty ( 'value' ) ) { answers [ task . _id . toString ( ) ] [ answer . _id . toString ( ) ] = true ; } else { answers [ task . _id . toString ( ) ] [ answer . _id . toString ( ) ] = false ; } } else { if ( answer . hasOwnProperty ( 'value' ) ) { answers [ task . _id . toString ( ) ] [ answer . _id . toString ( ) ] = false ; } else { answers [ task . _id . toString ( ) ] [ answer . _id . toString ( ) ] = true ; } } } ) ; } ) ; return answers ; } }