Lesson.js 11.1 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/**
 * Lesson Model
 *
 * @since 3.13.0
 * @version 4.20.0
 */
define( [ 'Models/Quiz', 'Models/_Relationships', 'Models/_Utilities', 'Schemas/Lesson' ], function( Quiz, Relationships, Utilities, LessonSchema ) {

	return Backbone.Model.extend( _.defaults( {

		/**
		 * Model relationships
		 *
		 * @type {Object}
		 */
		relationships: {
			parents: {
				model: 'section',
				type: 'model',
			},
			children: {
				quiz: {
					class: 'Quiz',
					conditional: function( model ) {
						// if quiz is enabled OR not enabled but we have some quiz data as an obj
						return ( 'yes' === model.get( 'quiz_enabled' ) || ! _.isEmpty( model.get( 'quiz' ) ) );
					},
					model: 'llms_quiz',
					type: 'model',
				},
			},
		},

		/**
		 * Lesson Settings Schema
		 *
		 * @type {Object}
		 */
		schema: LessonSchema,

		/**
		 * New lesson defaults
		 *
		 * @since 3.13.0
		 * @since 3.24.0 Unknown.
		 *
		 * @return {Object} Default options associative array (js object).
		 */
		defaults: function() {
			return {
				id: _.uniqueId( 'temp_' ),
				title: LLMS.l10n.translate( 'New Lesson' ),
				type: 'lesson',
				order: this.collection ? this.collection.length + 1 : 1,
				parent_course: window.llms_builder.course.id,
				parent_section: '',

				// Urls.
				edit_url: '',
				view_url: '',

				// Editable fields.
				content: '',
				audio_embed: '',
				has_prerequisite: 'no',
				require_passing_grade: 'yes',
				require_assignment_passing_grade: 'yes',
				video_embed: '',
				free_lesson: '',
				points: 1,

				// Other fields.
				assignment: {}, // Assignment model/data.
				assignment_enabled: 'no',

				quiz: {}, // Quiz model/data.
				quiz_enabled: 'no',

				_forceSync: false,

			};
		},

		/**
		 * Initializer
		 *
		 * @since 3.16.0
		 * @since 3.17.0 Unknown
		 *
		 * @return {void}
		 */
		initialize: function() {

			this.init_custom_schema();
			this.startTracking();
			this.maybe_init_assignments();
			this.init_relationships();

			// If the lesson ID isn't set on a quiz, set it.
			var quiz = this.get( 'quiz' );
			if ( ! _.isEmpty( quiz ) && ! quiz.get( 'lesson_id' ) ) {
				quiz.set( 'lesson_id', this.get( 'id' ) );
			}

			window.llms.hooks.doAction( 'llms_lesson_model_init', this );

		},

		/**
		 * Retrieve a reference to the parent course of the lesson
		 *
		 * @since 3.16.0
		 * @since 4.14.0 Use Section.get_course() in favor of Section.get_parent().
		 *
		 * @return {Object} The parent course model of the lesson.
		 */
		get_course: function() {
			return this.get_parent().get_course();
		},

		/**
		 * Retrieve the translated post type name for the model's type
		 *
		 * @since  3.16.12
		 *
		 * @param bool plural If true, returns the plural, otherwise returns singular.
		 * @return string The translated post type name.
		 */
		get_l10n_type: function( plural ) {

			if ( plural ) {
				return LLMS.l10n.translate( 'lessons' );
			}

			return LLMS.l10n.translate( 'lesson' );
		},

		/**
		 * Override default get_parent to grab from collection if models parent isn't set
		 *
		 * @since 3.17.0
		 *
		 * @return {Object}|false The parent model or false if not available.
		 */
		get_parent: function() {

			var rels = this.get_relationships();
			if ( rels.parent && rels.parent.reference ) {
				return rels.parent.reference;
			} else if ( this.collection && this.collection.parent ) {
				return this.collection.parent;
			}
			return false;

		},

		/**
		 * Retrieve the questions percentage value within the quiz
		 *
		 * @since 3.24.0
		 *
		 * @return {String} Questions percentage value within the quiz.
		 */
		get_points_percentage: function() {

			var total  = this.get_course().get_total_points(),
				points = this.get( 'points' ) * 1;

			if ( ! _.isNumber( points ) ) {
				points = 0;
			}

			if ( 0 === total ) {
				return '0%';
			}

			return ( ( points / total ) * 100 ).toFixed( 2 ) + '%';

		},

		/**
		 * Retrieve an array of prerequisite options available for the current lesson
		 *
		 * @since 3.17.0
		 *
		 * @return {Object} Prerequisite options.
		 */
		get_available_prereq_options: function() {

			var parent_section_index    = this.get_parent().collection.indexOf( this.get_parent() ),
				lesson_index_in_section = this.collection.indexOf( this ),
				options                 = [];

			this.get_course().get( 'sections' ).each( function( section, curr_sec_index ) {
				if ( curr_sec_index <= parent_section_index ) {
					var group = {
							// Translators: %1$d = section order number, %2$s = section title.
						label: LLMS.l10n.replace( 'Section %1$d: %2$s', {
							'%1$d': section.get( 'order' ),
							'%2$s': section.get( 'title' )
						} ),
					options: [],
					};

					section.get( 'lessons' ).each( function( lesson, curr_les_index ) {
						if ( curr_sec_index !== parent_section_index || curr_les_index < lesson_index_in_section ) {
							// Translators: %1$d = lesson order number, %2$s = lesson title.
							group.options.push( {
								key: lesson.get( 'id' ),
								val: LLMS.l10n.replace( 'Lesson %1$d: %2$s', {
									'%1$d': lesson.get( 'order' ),
									'%2$s': lesson.get( 'title' )
								} ),
							} );
						}
					}, this );

					options.push( group );
				}
			}, this );

			return options;

		},

		/**
		 * Add a new quiz to the lesson
		 *
		 * @since 3.16.0
		 * @since 3.27.0 Unknown.
		 *
		 * @param {Object} data Object of quiz data used to construct a new quiz model.
		 * @return {Object} Model for the created quiz.
		 */
		add_quiz: function( data ) {

			data = data || {};

			data.lesson_id         = this.id;
			data._questions_loaded = true;

			if ( ! data.title ) {

				data.title = LLMS.l10n.replace( '%1$s Quiz', {
					'%1$s': this.get( 'title' ),
				} );

			}

			this.set( 'quiz', data );
			this.init_relationships();

			var quiz = this.get( 'quiz' );
			this.set( 'quiz_enabled', 'yes' );

			window.llms.hooks.doAction( 'llms_lesson_add_quiz', quiz, this );

			return quiz;

		},

		/**
		 * Determine if this is the first lesson
		 *
		 * @since 3.17.0
		 * @since 4.20.0 Use is_first_in_section() new method.
		 *
		 * @return {Boolean} Whether this is the first lesson of its course.
		 */
		is_first_in_course: function() {

			// If it's not the first item in the section it cant be the first lesson.
			if ( ! this.is_first_in_section() ) {
				return false;
			}

			// If it's not the first section it cant' be first lesson.
			var section = this.get_parent();
			if ( section.collection.indexOf( section ) ) {
				return false;
			}

			// It's first lesson in first section.
			return true;

		},

		/**
		 * Determine if this is the last lesson of the course
		 *
		 * @since 4.20.0
		 *
		 * @return {Boolean} Whether this is the last lesson of its course.
		 */
		 is_last_in_course: function() {

			// If it's not last item in the section it cant be the last lesson.
			if ( ! this.is_last_in_section() ) {
				return false;
			}

			// If it's not the last section it cant' be last lesson.
			var section = this.get_parent();
			if ( section.collection.indexOf( section ) < ( section.collection.size() - 1 ) ) {
				return false;
			}

			// It's last lesson in last section.
			return true;

		},

		/**
		 * Determine if this is the first lesson within its section
		 *
		 * @since 4.20.0
		 *
		 * @return {Boolean} Whether this is the first lesson of its section.
		 */
		is_first_in_section: function() {
			return 0 === this.collection.indexOf( this );
		},

		/**
		 * Determine if this is the last lesson within its section
		 *
		 * @since 4.20.0
		 *
		 * @return {Boolean} Whether this is the last lesson of its section.
		 */
		is_last_in_section: function() {
			return this.collection.indexOf( this ) === ( this.collection.size() - 1 );
		},

		/**
		 * Get prev lesson in a course
		 *
		 * @since 4.20.0
		 *
		 * @param {String} status Prev lesson post status. If not specified any status will be taken into account.
		 * @return {Object}|false Previous lesson model or `false` if no previous lesson could be found.
		 */
		get_prev: function( status ) {
			return this.get_sibling( 'prev', status );
		},

		/**
		 * Get next lesson in a course
		 *
		 * @since 4.20.0
		 *
		 * @param {String} status Next lesson post status. If not specified any status will be taken into account.
		 * @return {Object}|false Next lesson model or `false` if no next lesson could be found.
		 */
		get_next: function( status ) {
			return this.get_sibling( 'next', status );
		},

		/**
		 * Get a sibling lesson
		 *
		 * @param {String} direction Siblings direction [next|prev]. If not specified will fall back on 'prev'.
		 * @param {String} status    Sibling lesson post status. If not specified any status will be taken into account.
		 * @return {Object}|false Sibling lesson model, in the specified direction, or `false` if no sibling lesson could be found.
		 */
		get_sibling: function( direction, status ) {

			direction = 'next' === direction ? direction : 'prev';

			// Functions and vars to use when direction is 'prev' (default).
			var is_course_limit_reached_f               = 'is_first_in_course',
				is_section_limit_reached_f              = 'is_first_in_section',
				sibling_index_increment                 = -1,
				get_sibling_lesson_in_sibling_section_f = 'last';

			if ( 'next' === direction ) {
				is_course_limit_reached_f               = 'is_last_in_course';
				is_section_limit_reached_f              = 'is_last_in_section';
				sibling_index_increment                 = 1,
				get_sibling_lesson_in_sibling_section_f = 'first';
			}

			if ( this[ is_course_limit_reached_f ]() ) {
				return false;
			}

			var sibling_index  = this.collection.indexOf( this ) + sibling_index_increment,
				sibling_lesson = this.collection.at( sibling_index );

			if ( this[ 'next' === direction ? 'is_last_in_section' : 'is_first_in_section' ]() ) {
				var cur_section     = this.get_parent(),
					sibling_section = cur_section[ 'get_' + direction ]( false );

				// Skip sibling empty sections.
				while ( sibling_section && ! sibling_section.get( 'lessons' ).size() ) {
					sibling_section = sibling_section[ 'get_' + direction ]( false );
				}

				// Couldn't find any suitable lesson.
				if ( ! sibling_section || ! sibling_section.get( 'lessons' ).size() ) {
					return false;
				}

				sibling_lesson = sibling_section.get( 'lessons' )[ get_sibling_lesson_in_sibling_section_f ]();

			}

			// If we need a specific lesson status.
			if ( status && status !== sibling_lesson.get( 'status' ) ) {
				return sibling_lesson.get_sibling( direction, status );
			}

			return sibling_lesson;

		},

		/**
		 * Initialize lesson assignments *if* the assignments addon is available and enabled
		 *
		 * @since 3.17.0
		 *
		 * @return {Void}
		 */
		maybe_init_assignments: function() {

			if ( ! window.llms_builder.assignments ) {
				return;
			}

			this.relationships.children.assignment = {
				class: 'Assignment',
				conditional: function( model ) {
					// If assignment is enabled OR not enabled but we have some assignment data as an obj.
					return ( 'yes' === model.get( 'assignment_enabled' ) || ! _.isEmpty( model.get( 'assignment' ) ) );
				},
				model: 'llms_assignment',
				type: 'model',
			};

		},

	}, Relationships, Utilities ) );

} );