Completed
Push — master ( 1a4e84...4efef7 )
by Dwain
04:36
created
includes/class-sensei-grading.php 2 patches
Indentation   +419 added lines, -419 removed lines patch added patch discarded remove patch
@@ -20,8 +20,8 @@  discard block
 block discarded – undo
20 20
 	/**
21 21
 	 * Constructor
22 22
 	 * @since  1.3.0
23
-     *
24
-     * @param $file
23
+	 *
24
+	 * @param $file
25 25
 	 */
26 26
 	public function __construct ( $file ) {
27 27
 		$this->name = __( 'Grading', 'woothemes-sensei' );
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
 	/**
231 231
 	 * Outputs Grading general headers
232 232
 	 * @since  1.3.0
233
-     * @param array $args
233
+	 * @param array $args
234 234
 	 * @return void
235 235
 	 */
236 236
 	public function grading_headers( $args = array( 'nav' => 'default' ) ) {
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 		}
283 283
 		if ( isset( $_GET['user_id'] ) && 0 < intval( $_GET['user_id'] ) ) {
284 284
 
285
-            $user_name = Sensei_Learner::get_full_name( $_GET['user_id'] );
285
+			$user_name = Sensei_Learner::get_full_name( $_GET['user_id'] );
286 286
 			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;' . $user_name . '</span>';
287 287
 
288 288
 		} // End If Statement
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
 		}
317 317
 		if ( isset( $_GET['user'] ) && 0 < intval( $_GET['user'] ) ) {
318 318
 
319
-            $user_name = Sensei_Learner::get_full_name( $_GET['user'] );
319
+			$user_name = Sensei_Learner::get_full_name( $_GET['user'] );
320 320
 			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;' . $user_name . '</span>';
321 321
 
322 322
 		} // End If Statement
@@ -365,15 +365,15 @@  discard block
 block discarded – undo
365 365
 	public function count_statuses( $args = array() ) {
366 366
 		global  $wpdb;
367 367
 
368
-        /**
369
-         * Filter fires inside Sensei_Grading::count_statuses
370
-         *
371
-         * Alter the the post_in array to determine which posts the
372
-         * comment query should be limited to.
373
-         * @since 1.8.0
374
-         * @param array $args
375
-         */
376
-        $args = apply_filters( 'sensei_count_statuses_args', $args );
368
+		/**
369
+		 * Filter fires inside Sensei_Grading::count_statuses
370
+		 *
371
+		 * Alter the the post_in array to determine which posts the
372
+		 * comment query should be limited to.
373
+		 * @since 1.8.0
374
+		 * @param array $args
375
+		 */
376
+		$args = apply_filters( 'sensei_count_statuses_args', $args );
377 377
 
378 378
 		if ( 'course' == $args['type'] ) {
379 379
 			$type = 'sensei_course_status';
@@ -385,7 +385,7 @@  discard block
 block discarded – undo
385 385
 
386 386
 		$query = "SELECT comment_approved, COUNT( * ) AS total FROM {$wpdb->comments} WHERE comment_type = %s ";
387 387
 
388
-        // Restrict to specific posts
388
+		// Restrict to specific posts
389 389
 		if ( isset( $args['post__in'] ) && !empty( $args['post__in'] ) && is_array( $args['post__in'] ) ) {
390 390
 			$query .= ' AND comment_post_ID IN (' . implode( ',', array_map( 'absint', $args['post__in'] ) ) . ')';
391 391
 		}
@@ -520,148 +520,148 @@  discard block
 block discarded – undo
520 520
 		return $html;
521 521
 	} // End lessons_drop_down_html()
522 522
 
523
-    /**
524
-     * The process grading function handles admin grading submissions.
525
-     *
526
-     * This function is hooked on to admin_init. It simply accepts
527
-     * the grades as the Grader selected theme and saves the total grade and
528
-     * individual question grades.
529
-     *
530
-     * @return bool
531
-     */
532
-    public function admin_process_grading_submission() {
523
+	/**
524
+	 * The process grading function handles admin grading submissions.
525
+	 *
526
+	 * This function is hooked on to admin_init. It simply accepts
527
+	 * the grades as the Grader selected theme and saves the total grade and
528
+	 * individual question grades.
529
+	 *
530
+	 * @return bool
531
+	 */
532
+	public function admin_process_grading_submission() {
533 533
 
534
-        // NEEDS REFACTOR/OPTIMISING, such as combining the various meta data stored against the sensei_user_answer entry
535
-        if( ! isset( $_POST['sensei_manual_grade'] )
536
-            || ! wp_verify_nonce( $_POST['_wp_sensei_manual_grading_nonce'], 'sensei_manual_grading' )
537
-            || ! isset( $_GET['quiz_id'] )
538
-            || $_GET['quiz_id'] != $_POST['sensei_manual_grade'] ) {
534
+		// NEEDS REFACTOR/OPTIMISING, such as combining the various meta data stored against the sensei_user_answer entry
535
+		if( ! isset( $_POST['sensei_manual_grade'] )
536
+			|| ! wp_verify_nonce( $_POST['_wp_sensei_manual_grading_nonce'], 'sensei_manual_grading' )
537
+			|| ! isset( $_GET['quiz_id'] )
538
+			|| $_GET['quiz_id'] != $_POST['sensei_manual_grade'] ) {
539 539
 
540
-            return false; //exit and do not grade
540
+			return false; //exit and do not grade
541 541
 
542
-        }
542
+		}
543 543
 
544
-        $quiz_id = $_GET['quiz_id'];
545
-        $user_id = $_GET['user'];
544
+		$quiz_id = $_GET['quiz_id'];
545
+		$user_id = $_GET['user'];
546 546
 
547 547
 
548
-        $questions = Sensei_Utils::sensei_get_quiz_questions( $quiz_id );
549
-        $quiz_lesson_id =  Sensei()->quiz->get_lesson_id( $quiz_id );
550
-        $quiz_grade = 0;
551
-        $count = 0;
552
-        $quiz_grade_total = $_POST['quiz_grade_total'];
553
-        $all_question_grades = array();
554
-        $all_answers_feedback = array();
548
+		$questions = Sensei_Utils::sensei_get_quiz_questions( $quiz_id );
549
+		$quiz_lesson_id =  Sensei()->quiz->get_lesson_id( $quiz_id );
550
+		$quiz_grade = 0;
551
+		$count = 0;
552
+		$quiz_grade_total = $_POST['quiz_grade_total'];
553
+		$all_question_grades = array();
554
+		$all_answers_feedback = array();
555 555
 
556
-        foreach( $questions as $question ) {
556
+		foreach( $questions as $question ) {
557 557
 
558
-            ++$count;
559
-            $question_id = $question->ID;
558
+			++$count;
559
+			$question_id = $question->ID;
560 560
 
561
-            if( isset( $_POST[ 'question_' . $question_id ] ) ) {
561
+			if( isset( $_POST[ 'question_' . $question_id ] ) ) {
562 562
 
563
-                $question_grade = 0;
564
-                if( $_POST[ 'question_' . $question_id ] == 'right' ) {
563
+				$question_grade = 0;
564
+				if( $_POST[ 'question_' . $question_id ] == 'right' ) {
565 565
 
566
-                    $question_grade = $_POST[ 'question_' . $question_id . '_grade' ];
566
+					$question_grade = $_POST[ 'question_' . $question_id . '_grade' ];
567 567
 
568
-                }
568
+				}
569 569
 
570
-                // add data to the array that will, after the loop, be stored on the lesson status
571
-                $all_question_grades[ $question_id ] = $question_grade;
570
+				// add data to the array that will, after the loop, be stored on the lesson status
571
+				$all_question_grades[ $question_id ] = $question_grade;
572 572
 
573
-                // tally up the total quiz grade
574
-                $quiz_grade += $question_grade;
573
+				// tally up the total quiz grade
574
+				$quiz_grade += $question_grade;
575 575
 
576
-            } // endif
576
+			} // endif
577 577
 
578
-            // Question answer feedback / notes
579
-            $question_feedback = '';
580
-            if( isset( $_POST[ 'questions_feedback' ][ $question_id ] ) ){
578
+			// Question answer feedback / notes
579
+			$question_feedback = '';
580
+			if( isset( $_POST[ 'questions_feedback' ][ $question_id ] ) ){
581 581
 
582
-                $question_feedback = wp_unslash( $_POST[ 'questions_feedback' ][ $question_id ] );
582
+				$question_feedback = wp_unslash( $_POST[ 'questions_feedback' ][ $question_id ] );
583 583
 
584
-            }
585
-            $all_answers_feedback[ $question_id ] = $question_feedback;
584
+			}
585
+			$all_answers_feedback[ $question_id ] = $question_feedback;
586 586
 
587
-        } // end for each $questions
587
+		} // end for each $questions
588 588
 
589
-        //store all question grades on the lesson status
590
-        Sensei()->quiz->set_user_grades( $all_question_grades, $quiz_lesson_id , $user_id );
589
+		//store all question grades on the lesson status
590
+		Sensei()->quiz->set_user_grades( $all_question_grades, $quiz_lesson_id , $user_id );
591 591
 
592
-        //store the feedback from grading
593
-        Sensei()->quiz->save_user_answers_feedback( $all_answers_feedback, $quiz_lesson_id , $user_id );
592
+		//store the feedback from grading
593
+		Sensei()->quiz->save_user_answers_feedback( $all_answers_feedback, $quiz_lesson_id , $user_id );
594 594
 
595
-        // $_POST['all_questions_graded'] is set when all questions have been graded
596
-        // in the class sensei grading user quiz -> display()
597
-        if( $_POST['all_questions_graded'] == 'yes' ) {
595
+		// $_POST['all_questions_graded'] is set when all questions have been graded
596
+		// in the class sensei grading user quiz -> display()
597
+		if( $_POST['all_questions_graded'] == 'yes' ) {
598 598
 
599
-            // set the users total quiz grade
599
+			// set the users total quiz grade
600 600
 			if ( 0 < intval( $quiz_grade_total ) ) {
601
-            $grade = abs( round( ( doubleval( $quiz_grade ) * 100 ) / ( $quiz_grade_total ), 2 ) );
601
+			$grade = abs( round( ( doubleval( $quiz_grade ) * 100 ) / ( $quiz_grade_total ), 2 ) );
602 602
 			}
603 603
 			else {
604 604
 				$grade = 0;
605 605
 			}
606
-            Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id );
607
-
608
-            // Duplicating what Frontend->sensei_complete_quiz() does
609
-            $pass_required = get_post_meta( $quiz_id, '_pass_required', true );
610
-            $quiz_passmark = abs( round( doubleval( get_post_meta( $quiz_id, '_quiz_passmark', true ) ), 2 ) );
611
-            $lesson_metadata = array();
612
-            if ( $pass_required ) {
613
-                // Student has reached the pass mark and lesson is complete
614
-                if ( $quiz_passmark <= $grade ) {
615
-                    $lesson_status = 'passed';
616
-                }
617
-                else {
618
-                    $lesson_status = 'failed';
619
-                } // End If Statement
620
-            }
621
-            // Student only has to partake the quiz
622
-            else {
623
-                $lesson_status = 'graded';
624
-            }
625
-            $lesson_metadata['grade'] = $grade; // Technically already set as part of "WooThemes_Sensei_Utils::sensei_grade_quiz()" above
606
+			Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id );
607
+
608
+			// Duplicating what Frontend->sensei_complete_quiz() does
609
+			$pass_required = get_post_meta( $quiz_id, '_pass_required', true );
610
+			$quiz_passmark = abs( round( doubleval( get_post_meta( $quiz_id, '_quiz_passmark', true ) ), 2 ) );
611
+			$lesson_metadata = array();
612
+			if ( $pass_required ) {
613
+				// Student has reached the pass mark and lesson is complete
614
+				if ( $quiz_passmark <= $grade ) {
615
+					$lesson_status = 'passed';
616
+				}
617
+				else {
618
+					$lesson_status = 'failed';
619
+				} // End If Statement
620
+			}
621
+			// Student only has to partake the quiz
622
+			else {
623
+				$lesson_status = 'graded';
624
+			}
625
+			$lesson_metadata['grade'] = $grade; // Technically already set as part of "WooThemes_Sensei_Utils::sensei_grade_quiz()" above
626 626
 
627
-            Sensei_Utils::update_lesson_status( $user_id, $quiz_lesson_id, $lesson_status, $lesson_metadata );
627
+			Sensei_Utils::update_lesson_status( $user_id, $quiz_lesson_id, $lesson_status, $lesson_metadata );
628 628
 
629
-            if(  in_array( $lesson_status, array( 'passed', 'graded'  ) ) ) {
629
+			if(  in_array( $lesson_status, array( 'passed', 'graded'  ) ) ) {
630 630
 
631
-                /**
632
-                 * Summary.
633
-                 *
634
-                 * Description.
635
-                 *
636
-                 * @since 1.7.0
637
-                 *
638
-                 * @param int  $user_id
639
-                 * @param int $quiz_lesson_id
640
-                 */
641
-                do_action( 'sensei_user_lesson_end', $user_id, $quiz_lesson_id );
631
+				/**
632
+				 * Summary.
633
+				 *
634
+				 * Description.
635
+				 *
636
+				 * @since 1.7.0
637
+				 *
638
+				 * @param int  $user_id
639
+				 * @param int $quiz_lesson_id
640
+				 */
641
+				do_action( 'sensei_user_lesson_end', $user_id, $quiz_lesson_id );
642 642
 
643
-            } // end if in_array
643
+			} // end if in_array
644 644
 
645
-        }// end if $_POST['all_que...
645
+		}// end if $_POST['all_que...
646 646
 
647
-        if( isset( $_POST['sensei_grade_next_learner'] ) && strlen( $_POST['sensei_grade_next_learner'] ) > 0 ) {
647
+		if( isset( $_POST['sensei_grade_next_learner'] ) && strlen( $_POST['sensei_grade_next_learner'] ) > 0 ) {
648 648
 
649
-            $load_url = add_query_arg( array( 'message' => 'graded' ) );
649
+			$load_url = add_query_arg( array( 'message' => 'graded' ) );
650 650
 
651
-        } elseif ( isset( $_POST['_wp_http_referer'] ) ) {
651
+		} elseif ( isset( $_POST['_wp_http_referer'] ) ) {
652 652
 
653
-            $load_url = add_query_arg( array( 'message' => 'graded' ), $_POST['_wp_http_referer'] );
653
+			$load_url = add_query_arg( array( 'message' => 'graded' ), $_POST['_wp_http_referer'] );
654 654
 
655
-        } else {
655
+		} else {
656 656
 
657
-            $load_url = add_query_arg( array( 'message' => 'graded' ) );
657
+			$load_url = add_query_arg( array( 'message' => 'graded' ) );
658 658
 
659
-        }
659
+		}
660 660
 
661
-        wp_safe_redirect( esc_url_raw( $load_url ) );
662
-        exit;
661
+		wp_safe_redirect( esc_url_raw( $load_url ) );
662
+		exit;
663 663
 
664
-    } // end admin_process_grading_submission
664
+	} // end admin_process_grading_submission
665 665
 
666 666
 	public function get_redirect_url() {
667 667
 		// Parse POST data
@@ -706,398 +706,398 @@  discard block
 block discarded – undo
706 706
 		} // End If Statement
707 707
 	} // End sensei_grading_notices()
708 708
 
709
-    /**
710
-     * Grade quiz automatically
711
-     *
712
-     * This function grades each question automatically if there all questions are auto gradable. If not
713
-     * the quiz will not be auto gradable.
714
-     *
715
-     * @since 1.7.4
716
-     *
717
-     * @param  integer $quiz_id         ID of quiz
718
-     * @param  array $submitted questions id ans answers {
719
-     *          @type int $question_id
720
-     *          @type mixed $answer
721
-     * }
722
-     * @param  integer $total_questions Total questions in quiz (not used)
723
-     * @param string $quiz_grade_type Optional defaults to auto
724
-     *
725
-     * @return int $quiz_grade total sum of all question grades
726
-     */
727
-    public static function grade_quiz_auto( $quiz_id = 0, $submitted = array(), $total_questions = 0, $quiz_grade_type = 'auto' ) {
728
-
729
-        if( ! ( intval( $quiz_id ) > 0 )  || ! $submitted
730
-            || $quiz_grade_type != 'auto' ) {
731
-            return false; // exit early
732
-        }
733
-
734
-
735
-        $user_id = get_current_user_id();
736
-        $lesson_id =  Sensei()->quiz->get_lesson_id(  $quiz_id ) ;
737
-        $quiz_autogradable = true;
738
-
739
-        /**
740
-         * Filter the types of question types that can be automatically graded.
741
-         *
742
-         * This filter fires inside the auto grade quiz function and provides you with the default list.
743
-         *
744
-         * @param array {
745
-         *      'multiple-choice',
746
-         *      'boolean',
747
-         *      'gap-fill'.
748
-         * }
749
-         */
750
-        $autogradable_question_types = apply_filters( 'sensei_autogradable_question_types', array( 'multiple-choice', 'boolean', 'gap-fill' ) );
751
-
752
-        $grade_total = 0;
753
-        $all_question_grades = array();
754
-        foreach( $submitted as $question_id => $answer ) {
755
-
756
-            // check if the question is autogradable, either by type, or because the grade is 0
757
-            $question_type = Sensei()->question->get_question_type( $question_id );
709
+	/**
710
+	 * Grade quiz automatically
711
+	 *
712
+	 * This function grades each question automatically if there all questions are auto gradable. If not
713
+	 * the quiz will not be auto gradable.
714
+	 *
715
+	 * @since 1.7.4
716
+	 *
717
+	 * @param  integer $quiz_id         ID of quiz
718
+	 * @param  array $submitted questions id ans answers {
719
+	 *          @type int $question_id
720
+	 *          @type mixed $answer
721
+	 * }
722
+	 * @param  integer $total_questions Total questions in quiz (not used)
723
+	 * @param string $quiz_grade_type Optional defaults to auto
724
+	 *
725
+	 * @return int $quiz_grade total sum of all question grades
726
+	 */
727
+	public static function grade_quiz_auto( $quiz_id = 0, $submitted = array(), $total_questions = 0, $quiz_grade_type = 'auto' ) {
728
+
729
+		if( ! ( intval( $quiz_id ) > 0 )  || ! $submitted
730
+			|| $quiz_grade_type != 'auto' ) {
731
+			return false; // exit early
732
+		}
733
+
734
+
735
+		$user_id = get_current_user_id();
736
+		$lesson_id =  Sensei()->quiz->get_lesson_id(  $quiz_id ) ;
737
+		$quiz_autogradable = true;
738
+
739
+		/**
740
+		 * Filter the types of question types that can be automatically graded.
741
+		 *
742
+		 * This filter fires inside the auto grade quiz function and provides you with the default list.
743
+		 *
744
+		 * @param array {
745
+		 *      'multiple-choice',
746
+		 *      'boolean',
747
+		 *      'gap-fill'.
748
+		 * }
749
+		 */
750
+		$autogradable_question_types = apply_filters( 'sensei_autogradable_question_types', array( 'multiple-choice', 'boolean', 'gap-fill' ) );
751
+
752
+		$grade_total = 0;
753
+		$all_question_grades = array();
754
+		foreach( $submitted as $question_id => $answer ) {
755
+
756
+			// check if the question is autogradable, either by type, or because the grade is 0
757
+			$question_type = Sensei()->question->get_question_type( $question_id );
758 758
 			$achievable_grade = Sensei()->question->get_question_grade( $question_id );
759 759
 			// Question has a zero grade, so skip grading
760 760
 			if ( 0 == $achievable_grade ) {
761 761
 				$all_question_grades[ $question_id ] = $achievable_grade;
762 762
 			}
763
-            elseif ( in_array( $question_type, $autogradable_question_types ) ) {
764
-                // Get user question grade
765
-                $question_grade = Sensei_Utils::sensei_grade_question_auto( $question_id, $question_type, $answer, $user_id );
766
-                $all_question_grades[ $question_id ] = $question_grade;
767
-                $grade_total += $question_grade;
763
+			elseif ( in_array( $question_type, $autogradable_question_types ) ) {
764
+				// Get user question grade
765
+				$question_grade = Sensei_Utils::sensei_grade_question_auto( $question_id, $question_type, $answer, $user_id );
766
+				$all_question_grades[ $question_id ] = $question_grade;
767
+				$grade_total += $question_grade;
768 768
 
769
-            } else {
769
+			} else {
770 770
 
771
-                // There is a question that cannot be autograded
772
-                $quiz_autogradable = false;
771
+				// There is a question that cannot be autograded
772
+				$quiz_autogradable = false;
773 773
 
774
-            } // end if in_array( $question_type...
774
+			} // end if in_array( $question_type...
775 775
 
776
-        }// end for each question
776
+		}// end for each question
777 777
 
778
-        // Only if the whole quiz was autogradable do we set a grade
779
-        if ( $quiz_autogradable ) {
778
+		// Only if the whole quiz was autogradable do we set a grade
779
+		if ( $quiz_autogradable ) {
780 780
 
781
-            $quiz_total = Sensei_Utils::sensei_get_quiz_total( $quiz_id );
781
+			$quiz_total = Sensei_Utils::sensei_get_quiz_total( $quiz_id );
782 782
 			// Check for zero total from grades
783 783
 			if ( 0 < $quiz_total ) {
784
-            $grade = abs( round( ( doubleval( $grade_total ) * 100 ) / ( $quiz_total ), 2 ) );
784
+			$grade = abs( round( ( doubleval( $grade_total ) * 100 ) / ( $quiz_total ), 2 ) );
785 785
 			}
786 786
 			else {
787 787
 				$grade = 0;
788 788
 			}
789
-            Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id, $quiz_grade_type );
789
+			Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id, $quiz_grade_type );
790 790
 
791
-        } else {
791
+		} else {
792 792
 
793
-            $grade = new WP_Error( 'autograde', __( 'This quiz is not able to be automatically graded.', 'woothemes-sensei' ) );
793
+			$grade = new WP_Error( 'autograde', __( 'This quiz is not able to be automatically graded.', 'woothemes-sensei' ) );
794 794
 
795
-        }
796
-
797
-        // store the auto gradable grades. If the quiz is not auto gradable the grades can be use as the default
798
-        // when doing manual grading.
799
-        Sensei()->quiz-> set_user_grades( $all_question_grades, $lesson_id, $user_id );
795
+		}
800 796
 
801
-        return $grade;
797
+		// store the auto gradable grades. If the quiz is not auto gradable the grades can be use as the default
798
+		// when doing manual grading.
799
+		Sensei()->quiz-> set_user_grades( $all_question_grades, $lesson_id, $user_id );
802 800
 
803
-    } // End grade_quiz_auto()
801
+		return $grade;
804 802
 
805
-    /**
806
-     * Grade question automatically
807
-     *
808
-     * This function checks the question typ and then grades it accordingly.
809
-     *
810
-     * @since 1.7.4
811
-     *
812
-     * @param integer $question_id
813
-     * @param string $question_type of the standard Sensei question types
814
-     * @param string $answer
815
-     * @param int $user_id
816
-     *
817
-     * @return int $question_grade
818
-     */
819
-    public static function grade_question_auto( $question_id = 0, $question_type = '', $answer = '', $user_id = 0 ) {
820
-
821
-        if( intval( $user_id ) == 0 ) {
822
-
823
-            $user_id = get_current_user_id();
824
-
825
-        }
826
-
827
-        if( ! ( intval( $question_id ) > 0 ) ) {
828
-
829
-            return false;
830
-
831
-        }
832
-
833
-
834
-        Sensei()->question->get_question_type( $question_id );
835
-
836
-        /**
837
-         * Applying a grade before the auto grading takes place.
838
-         *
839
-         * This filter is applied just before the question is auto graded. It fires in the context of a single question
840
-         * in the sensei_grade_question_auto function. It fires irrespective of the question type. If you return a value
841
-         * other than false the auto grade functionality will be ignored and your supplied grade will be user for this question.
842
-         *
843
-         * @param int $question_grade default false
844
-         * @param int $question_id
845
-         * @param string $question_type one of the Sensei question type.
846
-         * @param string $answer user supplied question answer
847
-         */
848
-        $question_grade = apply_filters( 'sensei_pre_grade_question_auto', false, $question_id, $question_type, $answer );
803
+	} // End grade_quiz_auto()
849 804
 
850
-        if ( false !== $question_grade ) {
805
+	/**
806
+	 * Grade question automatically
807
+	 *
808
+	 * This function checks the question typ and then grades it accordingly.
809
+	 *
810
+	 * @since 1.7.4
811
+	 *
812
+	 * @param integer $question_id
813
+	 * @param string $question_type of the standard Sensei question types
814
+	 * @param string $answer
815
+	 * @param int $user_id
816
+	 *
817
+	 * @return int $question_grade
818
+	 */
819
+	public static function grade_question_auto( $question_id = 0, $question_type = '', $answer = '', $user_id = 0 ) {
851 820
 
852
-            return $question_grade;
821
+		if( intval( $user_id ) == 0 ) {
853 822
 
854
-        }
823
+			$user_id = get_current_user_id();
855 824
 
856
-        // auto grading core
857
-        if( in_array( $question_type ,  array( 'multiple-choice'  , 'boolean'  ) )   ){
825
+		}
858 826
 
859
-            $right_answer = (array) get_post_meta( $question_id, '_question_right_answer', true );
827
+		if( ! ( intval( $question_id ) > 0 ) ) {
860 828
 
861
-            if( 0 == get_magic_quotes_gpc() ) {
862
-                $answer = wp_unslash( $answer );
863
-            }
864
-            $answer = (array) $answer;
865
-            if ( is_array( $right_answer ) && count( $right_answer ) == count( $answer ) ) {
866
-                // Loop through all answers ensure none are 'missing'
867
-                $all_correct = true;
868
-                foreach ( $answer as $check_answer ) {
869
-                    if ( !in_array( $check_answer, $right_answer ) ) {
870
-                        $all_correct = false;
871
-                    }
872
-                }
873
-                // If all correct then grade
874
-                if ( $all_correct ) {
875
-                    $question_grade = Sensei()->question->get_question_grade( $question_id );
876
-                }
877
-            }
829
+			return false;
878 830
 
879
-        } elseif( 'gap-fill' == $question_type ){
831
+		}
880 832
 
881
-            $question_grade = self::grade_gap_fill_question( $question_id ,$answer );
882 833
 
883
-        } else{
834
+		Sensei()->question->get_question_type( $question_id );
884 835
 
885
-            /**
886
-             * Grading questions that are not auto gradable.
887
-             *
888
-             * This filter is applied the context of ta single question within the sensei_grade_question_auto function.
889
-             * It fires for all other questions types. It does not apply to 'multiple-choice'  , 'boolean' and gap-fill.
890
-             *
891
-             * @param int $question_grade default zero
892
-             * @param int $question_id
893
-             * @param string $question_type one of the Sensei question type.
894
-             * @param string $answer user supplied question answer
895
-             */
896
-            $question_grade = ( int ) apply_filters( 'sensei_grade_question_auto', $question_grade, $question_id, $question_type, $answer );
836
+		/**
837
+		 * Applying a grade before the auto grading takes place.
838
+		 *
839
+		 * This filter is applied just before the question is auto graded. It fires in the context of a single question
840
+		 * in the sensei_grade_question_auto function. It fires irrespective of the question type. If you return a value
841
+		 * other than false the auto grade functionality will be ignored and your supplied grade will be user for this question.
842
+		 *
843
+		 * @param int $question_grade default false
844
+		 * @param int $question_id
845
+		 * @param string $question_type one of the Sensei question type.
846
+		 * @param string $answer user supplied question answer
847
+		 */
848
+		$question_grade = apply_filters( 'sensei_pre_grade_question_auto', false, $question_id, $question_type, $answer );
897 849
 
898
-        } // end if $question_type
850
+		if ( false !== $question_grade ) {
899 851
 
900
-        return $question_grade;
901
-    } // end grade_question_auto
852
+			return $question_grade;
902 853
 
903
-    /**
904
-     * Grading logic specifically for the gap fill questions
905
-     *
906
-     * @since 1.9.0
907
-     * @param $question_id
908
-     * @param $user_answer
909
-     *
910
-     * @return bool | int false or the grade given to the user answer
911
-     */
912
-    public static function grade_gap_fill_question( $question_id, $user_answer ){
854
+		}
913 855
 
914
-        $right_answer = get_post_meta( $question_id, '_question_right_answer', true );
915
-        $gapfill_array = explode( '||', $right_answer );
856
+		// auto grading core
857
+		if( in_array( $question_type ,  array( 'multiple-choice'  , 'boolean'  ) )   ){
916 858
 
917
-        if( 0 == get_magic_quotes_gpc() ) { // deprecated from PHP 5.4 but we still support PHP 5.2
918
-            $user_answer = wp_unslash( $user_answer );
919
-        }
859
+			$right_answer = (array) get_post_meta( $question_id, '_question_right_answer', true );
920 860
 
921
-        /**
922
-         * case sensitive grading filter
923
-         *
924
-         * alter the value simply use this code in your plugin or the themes functions.php
925
-         * add_filter( 'sensei_gap_fill_case_sensitive_grading','__return_true' );
926
-         *
927
-         * @param bool $do_case_sensitive_comparison default false.
928
-         *
929
-         * @since 1.9.0
930
-         */
931
-        $do_case_sensitive_comparison = apply_filters('sensei_gap_fill_case_sensitive_grading', false );
861
+			if( 0 == get_magic_quotes_gpc() ) {
862
+				$answer = wp_unslash( $answer );
863
+			}
864
+			$answer = (array) $answer;
865
+			if ( is_array( $right_answer ) && count( $right_answer ) == count( $answer ) ) {
866
+				// Loop through all answers ensure none are 'missing'
867
+				$all_correct = true;
868
+				foreach ( $answer as $check_answer ) {
869
+					if ( !in_array( $check_answer, $right_answer ) ) {
870
+						$all_correct = false;
871
+					}
872
+				}
873
+				// If all correct then grade
874
+				if ( $all_correct ) {
875
+					$question_grade = Sensei()->question->get_question_grade( $question_id );
876
+				}
877
+			}
932 878
 
933
-        if( $do_case_sensitive_comparison ){
879
+		} elseif( 'gap-fill' == $question_type ){
934 880
 
935
-            // Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
936
-            if ( trim(($gapfill_array[1])) == trim( $user_answer ) ) {
881
+			$question_grade = self::grade_gap_fill_question( $question_id ,$answer );
937 882
 
938
-                return Sensei()->question->get_question_grade( $question_id );
883
+		} else{
939 884
 
940
-            } else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
885
+			/**
886
+			 * Grading questions that are not auto gradable.
887
+			 *
888
+			 * This filter is applied the context of ta single question within the sensei_grade_question_auto function.
889
+			 * It fires for all other questions types. It does not apply to 'multiple-choice'  , 'boolean' and gap-fill.
890
+			 *
891
+			 * @param int $question_grade default zero
892
+			 * @param int $question_id
893
+			 * @param string $question_type one of the Sensei question type.
894
+			 * @param string $answer user supplied question answer
895
+			 */
896
+			$question_grade = ( int ) apply_filters( 'sensei_grade_question_auto', $question_grade, $question_id, $question_type, $answer );
941 897
 
942
-                if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
898
+		} // end if $question_type
943 899
 
944
-                    return Sensei()->question->get_question_grade($question_id);
900
+		return $question_grade;
901
+	} // end grade_question_auto
945 902
 
946
-                }else{
903
+	/**
904
+	 * Grading logic specifically for the gap fill questions
905
+	 *
906
+	 * @since 1.9.0
907
+	 * @param $question_id
908
+	 * @param $user_answer
909
+	 *
910
+	 * @return bool | int false or the grade given to the user answer
911
+	 */
912
+	public static function grade_gap_fill_question( $question_id, $user_answer ){
947 913
 
948
-                    return false;
914
+		$right_answer = get_post_meta( $question_id, '_question_right_answer', true );
915
+		$gapfill_array = explode( '||', $right_answer );
949 916
 
950
-                }
917
+		if( 0 == get_magic_quotes_gpc() ) { // deprecated from PHP 5.4 but we still support PHP 5.2
918
+			$user_answer = wp_unslash( $user_answer );
919
+		}
951 920
 
952
-            }else{
921
+		/**
922
+		 * case sensitive grading filter
923
+		 *
924
+		 * alter the value simply use this code in your plugin or the themes functions.php
925
+		 * add_filter( 'sensei_gap_fill_case_sensitive_grading','__return_true' );
926
+		 *
927
+		 * @param bool $do_case_sensitive_comparison default false.
928
+		 *
929
+		 * @since 1.9.0
930
+		 */
931
+		$do_case_sensitive_comparison = apply_filters('sensei_gap_fill_case_sensitive_grading', false );
953 932
 
954
-                return false;
933
+		if( $do_case_sensitive_comparison ){
955 934
 
956
-            }
935
+			// Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
936
+			if ( trim(($gapfill_array[1])) == trim( $user_answer ) ) {
957 937
 
958
-        }else{
938
+				return Sensei()->question->get_question_grade( $question_id );
959 939
 
960
-            // Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
961
-            if ( trim(strtolower($gapfill_array[1])) == trim(strtolower( $user_answer )) ) {
940
+			} else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
962 941
 
963
-               return Sensei()->question->get_question_grade( $question_id );
942
+				if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
964 943
 
965
-            } else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
944
+					return Sensei()->question->get_question_grade($question_id);
966 945
 
967
-                if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
946
+				}else{
968 947
 
969
-                    return  Sensei()->question->get_question_grade( $question_id );
948
+					return false;
970 949
 
971
-                }else{
950
+				}
972 951
 
973
-                    return false;
952
+			}else{
974 953
 
975
-                }
954
+				return false;
976 955
 
977
-            }else{
956
+			}
957
+
958
+		}else{
959
+
960
+			// Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
961
+			if ( trim(strtolower($gapfill_array[1])) == trim(strtolower( $user_answer )) ) {
962
+
963
+			   return Sensei()->question->get_question_grade( $question_id );
964
+
965
+			} else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
966
+
967
+				if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
968
+
969
+					return  Sensei()->question->get_question_grade( $question_id );
970
+
971
+				}else{
972
+
973
+					return false;
974
+
975
+				}
978 976
 
979
-                return false;
977
+			}else{
978
+
979
+				return false;
980
+
981
+			}
982
+
983
+		}
984
+
985
+	}
986
+
987
+	/**
988
+	 * Counts the lessons that have been graded manually and automatically
989
+	 *
990
+	 * @since 1.9.0
991
+	 * @return int $number_of_graded_lessons
992
+	 */
993
+	public static function get_graded_lessons_count(){
980 994
 
981
-            }
995
+		global $wpdb;
982 996
 
983
-        }
997
+		$comment_query_piece[ 'select']  = "SELECT   COUNT(*) AS total";
998
+		$comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
999
+		$comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->wp_commentmeta}.meta_key = 'grade')";
1000
+		$comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
984 1001
 
985
-    }
1002
+		$comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1003
+		$number_of_graded_lessons = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
986 1004
 
987
-    /**
988
-     * Counts the lessons that have been graded manually and automatically
989
-     *
990
-     * @since 1.9.0
991
-     * @return int $number_of_graded_lessons
992
-     */
993
-    public static function get_graded_lessons_count(){
1005
+		return $number_of_graded_lessons;
1006
+	}
994 1007
 
995
-        global $wpdb;
1008
+	/**
1009
+	 * Add together all the graded lesson grades
1010
+	 *
1011
+	 * @since 1.9.0
1012
+	 * @return double $sum_of_all_grades
1013
+	 */
1014
+	public static function get_graded_lessons_sum(){
996 1015
 
997
-        $comment_query_piece[ 'select']  = "SELECT   COUNT(*) AS total";
998
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
999
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->wp_commentmeta}.meta_key = 'grade')";
1000
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1016
+		global $wpdb;
1001 1017
 
1002
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1003
-        $number_of_graded_lessons = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1018
+		$comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1019
+		$comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1020
+		$comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade')";
1021
+		$comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1004 1022
 
1005
-        return $number_of_graded_lessons;
1006
-    }
1023
+		$comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1024
+		$sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1007 1025
 
1008
-    /**
1009
-     * Add together all the graded lesson grades
1010
-     *
1011
-     * @since 1.9.0
1012
-     * @return double $sum_of_all_grades
1013
-     */
1014
-    public static function get_graded_lessons_sum(){
1026
+		return $sum_of_all_grades;
1015 1027
 
1016
-        global $wpdb;
1028
+	}
1017 1029
 
1018
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1019
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1020
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade')";
1021
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1030
+	/**
1031
+	 * Get the sum of all grades for the given user.
1032
+	 *
1033
+	 * @since 1.9.0
1034
+	 * @param $user_id
1035
+	 * @return double
1036
+	 */
1037
+	public static function get_user_graded_lessons_sum( $user_id ){
1038
+		global $wpdb;
1022 1039
 
1023
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1024
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1040
+		$clean_user_id = esc_sql( $user_id);
1041
+		$comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1042
+		$comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1043
+		$comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.user_id = {$clean_user_id} ";
1044
+		$comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1025 1045
 
1026
-        return $sum_of_all_grades;
1046
+		$comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1047
+		$sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1027 1048
 
1028
-    }
1049
+		return $sum_of_all_grades;
1050
+	}
1029 1051
 
1030
-    /**
1031
-     * Get the sum of all grades for the given user.
1032
-     *
1033
-     * @since 1.9.0
1034
-     * @param $user_id
1035
-     * @return double
1036
-     */
1037
-    public static function get_user_graded_lessons_sum( $user_id ){
1038
-        global $wpdb;
1052
+	/**
1053
+	 * Get the sum of all user grades for the given lesson.
1054
+	 *
1055
+	 * @since 1.9.0
1056
+	 *
1057
+	 * @param int lesson_id
1058
+	 * @return double
1059
+	 */
1060
+	public static function get_lessons_users_grades_sum( $lesson_id ){
1039 1061
 
1040
-        $clean_user_id = esc_sql( $user_id);
1041
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1042
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1043
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.user_id = {$clean_user_id} ";
1044
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1062
+		global $wpdb;
1045 1063
 
1046
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1047
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1064
+		$clean_lesson_id = esc_sql( $lesson_id);
1065
+		$comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1066
+		$comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1067
+		$comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.comment_post_ID = {$clean_lesson_id} ";
1068
+		$comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1048 1069
 
1049
-        return $sum_of_all_grades;
1050
-    }
1070
+		$comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1071
+		$sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1051 1072
 
1052
-    /**
1053
-     * Get the sum of all user grades for the given lesson.
1054
-     *
1055
-     * @since 1.9.0
1056
-     *
1057
-     * @param int lesson_id
1058
-     * @return double
1059
-     */
1060
-    public static function get_lessons_users_grades_sum( $lesson_id ){
1073
+		return $sum_of_all_grades;
1061 1074
 
1062
-        global $wpdb;
1075
+	}//get_lessons_user_grades_sum
1063 1076
 
1064
-        $clean_lesson_id = esc_sql( $lesson_id);
1065
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1066
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1067
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.comment_post_ID = {$clean_lesson_id} ";
1068
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1077
+	/**
1078
+	 * Get the sum of all user grades for the given course.
1079
+	 *
1080
+	 * @since 1.9.0
1081
+	 *
1082
+	 * @param int $course_id
1083
+	 * @return double
1084
+	 */
1085
+	public static function get_course_users_grades_sum( $course_id ){
1069 1086
 
1070
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1071
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1087
+		global $wpdb;
1072 1088
 
1073
-        return $sum_of_all_grades;
1089
+		$clean_course_id = esc_sql( $course_id);
1090
+		$comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1091
+		$comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1092
+		$comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_course_status') AND ( {$wpdb->commentmeta}.meta_key = 'percent') AND {$wpdb->comments}.comment_post_ID = {$clean_course_id} ";
1093
+		$comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1074 1094
 
1075
-    }//get_lessons_user_grades_sum
1095
+		$comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1096
+		$sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1076 1097
 
1077
-    /**
1078
-     * Get the sum of all user grades for the given course.
1079
-     *
1080
-     * @since 1.9.0
1081
-     *
1082
-     * @param int $course_id
1083
-     * @return double
1084
-     */
1085
-    public static function get_course_users_grades_sum( $course_id ){
1086
-
1087
-        global $wpdb;
1088
-
1089
-        $clean_course_id = esc_sql( $course_id);
1090
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1091
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1092
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_course_status') AND ( {$wpdb->commentmeta}.meta_key = 'percent') AND {$wpdb->comments}.comment_post_ID = {$clean_course_id} ";
1093
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1098
+		return $sum_of_all_grades;
1094 1099
 
1095
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1096
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1097
-
1098
-        return $sum_of_all_grades;
1099
-
1100
-    }//get_lessons_user_grades_sum
1100
+	}//get_lessons_user_grades_sum
1101 1101
 
1102 1102
 } // End Class
1103 1103
 
Please login to merge, or discard this patch.
Spacing   +280 added lines, -280 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2
+if ( ! defined('ABSPATH')) exit; // Exit if accessed directly
3 3
 
4 4
 /**
5 5
  * Sensei Grading Class
@@ -23,30 +23,30 @@  discard block
 block discarded – undo
23 23
      *
24 24
      * @param $file
25 25
 	 */
26
-	public function __construct ( $file ) {
27
-		$this->name = __( 'Grading', 'woothemes-sensei' );
26
+	public function __construct($file) {
27
+		$this->name = __('Grading', 'woothemes-sensei');
28 28
 		$this->file = $file;
29 29
 		$this->page_slug = 'sensei_grading';
30 30
 
31 31
 		// Admin functions
32
-		if ( is_admin() ) {
33
-			add_action( 'admin_menu', array( $this, 'grading_admin_menu' ), 20);
34
-			add_action( 'grading_wrapper_container', array( $this, 'wrapper_container'  ) );
35
-			if ( isset( $_GET['page'] ) && ( $_GET['page'] == $this->page_slug ) ) {
36
-				add_action( 'admin_print_scripts', array( $this, 'enqueue_scripts' ) );
37
-				add_action( 'admin_print_styles', array( $this, 'enqueue_styles' ) );
32
+		if (is_admin()) {
33
+			add_action('admin_menu', array($this, 'grading_admin_menu'), 20);
34
+			add_action('grading_wrapper_container', array($this, 'wrapper_container'));
35
+			if (isset($_GET['page']) && ($_GET['page'] == $this->page_slug)) {
36
+				add_action('admin_print_scripts', array($this, 'enqueue_scripts'));
37
+				add_action('admin_print_styles', array($this, 'enqueue_styles'));
38 38
 			}
39 39
 
40
-			add_action( 'admin_init', array( $this, 'admin_process_grading_submission' ) );
40
+			add_action('admin_init', array($this, 'admin_process_grading_submission'));
41 41
 
42
-			add_action( 'admin_notices', array( $this, 'add_grading_notices' ) );
42
+			add_action('admin_notices', array($this, 'add_grading_notices'));
43 43
 //			add_action( 'sensei_grading_notices', array( $this, 'sensei_grading_notices' ) );
44 44
 		} // End If Statement
45 45
 
46 46
 		// Ajax functions
47
-		if ( is_admin() ) {
48
-			add_action( 'wp_ajax_get_lessons_dropdown', array( $this, 'get_lessons_dropdown' ) );
49
-			add_action( 'wp_ajax_get_redirect_url', array( $this, 'get_redirect_url' ) );
47
+		if (is_admin()) {
48
+			add_action('wp_ajax_get_lessons_dropdown', array($this, 'get_lessons_dropdown'));
49
+			add_action('wp_ajax_get_redirect_url', array($this, 'get_redirect_url'));
50 50
 		} // End If Statement
51 51
 	} // End __construct()
52 52
 
@@ -59,8 +59,8 @@  discard block
 block discarded – undo
59 59
 	public function grading_admin_menu() {
60 60
 		global $menu;
61 61
 
62
-		if ( current_user_can( 'manage_sensei_grades' ) ) {
63
-			$grading_page = add_submenu_page('sensei', __('Grading', 'woothemes-sensei'),  __('Grading', 'woothemes-sensei') , 'manage_sensei_grades', $this->page_slug, array( $this, 'grading_page' ) );
62
+		if (current_user_can('manage_sensei_grades')) {
63
+			$grading_page = add_submenu_page('sensei', __('Grading', 'woothemes-sensei'), __('Grading', 'woothemes-sensei'), 'manage_sensei_grades', $this->page_slug, array($this, 'grading_page'));
64 64
 		}
65 65
 
66 66
 	} // End grading_admin_menu()
@@ -73,12 +73,12 @@  discard block
 block discarded – undo
73 73
 	 * @since 1.3.0
74 74
 	 * @return void
75 75
 	 */
76
-	public function enqueue_scripts () {
76
+	public function enqueue_scripts() {
77 77
 
78
-		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
78
+		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
79 79
 
80 80
 		// Load Grading JS
81
-		wp_enqueue_script( 'sensei-grading-general', Sensei()->plugin_url . 'assets/js/grading-general' . $suffix . '.js', array( 'jquery' ), Sensei()->version );
81
+		wp_enqueue_script('sensei-grading-general', Sensei()->plugin_url.'assets/js/grading-general'.$suffix.'.js', array('jquery'), Sensei()->version);
82 82
 
83 83
 	} // End enqueue_scripts()
84 84
 
@@ -90,11 +90,11 @@  discard block
 block discarded – undo
90 90
 	 * @since 1.0.0
91 91
 	 * @return void
92 92
 	 */
93
-	public function enqueue_styles () {
93
+	public function enqueue_styles() {
94 94
 
95
-		wp_enqueue_style( Sensei()->token . '-admin' );
95
+		wp_enqueue_style(Sensei()->token.'-admin');
96 96
 
97
-		wp_enqueue_style( 'woothemes-sensei-settings-api', Sensei()->plugin_url . 'assets/css/settings.css', '', Sensei()->version );
97
+		wp_enqueue_style('woothemes-sensei-settings-api', Sensei()->plugin_url.'assets/css/settings.css', '', Sensei()->version);
98 98
 
99 99
 	} // End enqueue_styles()
100 100
 
@@ -106,12 +106,12 @@  discard block
 block discarded – undo
106 106
 	public function load_data_table_files() {
107 107
 
108 108
 		// Load Grading Classes
109
-		$classes_to_load = array(	'list-table',
109
+		$classes_to_load = array('list-table',
110 110
 									'grading-main',
111 111
 									'grading-user-quiz'
112 112
 									);
113
-		foreach ( $classes_to_load as $class_file ) {
114
-			Sensei()->load_class( $class_file );
113
+		foreach ($classes_to_load as $class_file) {
114
+			Sensei()->load_class($class_file);
115 115
 		} // End For Loop
116 116
 	} // End load_data_table_files()
117 117
 
@@ -123,16 +123,16 @@  discard block
 block discarded – undo
123 123
 	 * @param  undefined  $optional_data optional constructor arguments
124 124
 	 * @return object                 class instance object
125 125
 	 */
126
-	public function load_data_object( $name = '', $data = 0, $optional_data = null ) {
126
+	public function load_data_object($name = '', $data = 0, $optional_data = null) {
127 127
 		// Load Analysis data
128
-		$object_name = 'WooThemes_Sensei_Grading_' . $name;
129
-		if ( is_null($optional_data) ) {
130
-			$sensei_grading_object = new $object_name( $data );
128
+		$object_name = 'WooThemes_Sensei_Grading_'.$name;
129
+		if (is_null($optional_data)) {
130
+			$sensei_grading_object = new $object_name($data);
131 131
 		}
132 132
 		else {
133
-			$sensei_grading_object = new $object_name( $data, $optional_data );
133
+			$sensei_grading_object = new $object_name($data, $optional_data);
134 134
 		} // End If Statement
135
-		if ( 'Main' == $name ) {
135
+		if ('Main' == $name) {
136 136
 			$sensei_grading_object->prepare_items();
137 137
 		} // End If Statement
138 138
 		return $sensei_grading_object;
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 	 */
147 147
 	public function grading_page() {
148 148
 
149
-		if ( isset( $_GET['quiz_id'] ) && 0 < intval( $_GET['quiz_id'] ) && isset( $_GET['user'] ) && 0 < intval( $_GET['user'] ) ) {
149
+		if (isset($_GET['quiz_id']) && 0 < intval($_GET['quiz_id']) && isset($_GET['user']) && 0 < intval($_GET['user'])) {
150 150
 			$this->grading_user_quiz_view();
151 151
 		}
152 152
 		else {
@@ -162,23 +162,23 @@  discard block
 block discarded – undo
162 162
 	public function grading_default_view() {
163 163
 
164 164
 		// Load Grading data
165
-		if( !empty( $_GET['course_id'] ) ) {
166
-			$course_id = intval( $_GET['course_id'] );
165
+		if ( ! empty($_GET['course_id'])) {
166
+			$course_id = intval($_GET['course_id']);
167 167
 		}
168
-		if( !empty( $_GET['lesson_id'] ) ) {
169
-			$lesson_id = intval( $_GET['lesson_id'] );
168
+		if ( ! empty($_GET['lesson_id'])) {
169
+			$lesson_id = intval($_GET['lesson_id']);
170 170
 		}
171
-		if( !empty( $_GET['user_id'] ) ) {
172
-			$user_id = intval( $_GET['user_id'] );
171
+		if ( ! empty($_GET['user_id'])) {
172
+			$user_id = intval($_GET['user_id']);
173 173
 		}
174
-		if( !empty( $_GET['view'] ) ) {
175
-			$view = esc_html( $_GET['view'] );
174
+		if ( ! empty($_GET['view'])) {
175
+			$view = esc_html($_GET['view']);
176 176
 		}
177
-		$sensei_grading_overview = $this->load_data_object( 'Main', compact( 'course_id', 'lesson_id', 'user_id', 'view' ) );
177
+		$sensei_grading_overview = $this->load_data_object('Main', compact('course_id', 'lesson_id', 'user_id', 'view'));
178 178
 
179 179
 		// Wrappers
180
-		do_action( 'grading_before_container' );
181
-		do_action( 'grading_wrapper_container', 'top' );
180
+		do_action('grading_before_container');
181
+		do_action('grading_wrapper_container', 'top');
182 182
 		$this->grading_headers();
183 183
 		?>
184 184
 		<div id="poststuff" class="sensei-grading-wrap">
@@ -186,12 +186,12 @@  discard block
 block discarded – undo
186 186
 				<?php $sensei_grading_overview->display(); ?>
187 187
 			</div>
188 188
 			<div class="sensei-grading-extra">
189
-				<?php do_action( 'sensei_grading_extra' ); ?>
189
+				<?php do_action('sensei_grading_extra'); ?>
190 190
 			</div>
191 191
 		</div>
192 192
 		<?php
193
-		do_action( 'grading_wrapper_container', 'bottom' );
194
-		do_action( 'grading_after_container' );
193
+		do_action('grading_wrapper_container', 'bottom');
194
+		do_action('grading_after_container');
195 195
 	} // End grading_default_view()
196 196
 
197 197
 	/**
@@ -204,17 +204,17 @@  discard block
 block discarded – undo
204 204
 		// Load Grading data
205 205
 		$user_id = 0;
206 206
 		$quiz_id = 0;
207
-		if( isset( $_GET['user'] ) ) {
208
-			$user_id = intval( $_GET['user'] );
207
+		if (isset($_GET['user'])) {
208
+			$user_id = intval($_GET['user']);
209 209
 		}
210
-		if( isset( $_GET['quiz_id'] ) ) {
211
-			$quiz_id = intval( $_GET['quiz_id'] );
210
+		if (isset($_GET['quiz_id'])) {
211
+			$quiz_id = intval($_GET['quiz_id']);
212 212
 		}
213
-		$sensei_grading_user_profile = $this->load_data_object( 'User_Quiz', $user_id, $quiz_id );
213
+		$sensei_grading_user_profile = $this->load_data_object('User_Quiz', $user_id, $quiz_id);
214 214
 		// Wrappers
215
-		do_action( 'grading_before_container' );
216
-		do_action( 'grading_wrapper_container', 'top' );
217
-		$this->grading_headers( array( 'nav' => 'user_quiz' ) );
215
+		do_action('grading_before_container');
216
+		do_action('grading_wrapper_container', 'top');
217
+		$this->grading_headers(array('nav' => 'user_quiz'));
218 218
 		?>
219 219
 		<div id="poststuff" class="sensei-grading-wrap user-profile">
220 220
 			<div class="sensei-grading-main">
@@ -223,8 +223,8 @@  discard block
 block discarded – undo
223 223
 			</div>
224 224
 		</div>
225 225
 		<?php
226
-		do_action( 'grading_wrapper_container', 'bottom' );
227
-		do_action( 'grading_after_container' );
226
+		do_action('grading_wrapper_container', 'bottom');
227
+		do_action('grading_after_container');
228 228
 	} // End grading_user_quiz_view()
229 229
 
230 230
 	/**
@@ -233,14 +233,14 @@  discard block
 block discarded – undo
233 233
      * @param array $args
234 234
 	 * @return void
235 235
 	 */
236
-	public function grading_headers( $args = array( 'nav' => 'default' ) ) {
236
+	public function grading_headers($args = array('nav' => 'default')) {
237 237
 
238
-		$function = 'grading_' . $args['nav'] . '_nav';
238
+		$function = 'grading_'.$args['nav'].'_nav';
239 239
 		$this->$function();
240 240
 		?>
241
-			<p class="powered-by-woo"><?php _e( 'Powered by', 'woothemes-sensei' ); ?><a href="http://www.woothemes.com/" title="WooThemes"><img src="<?php echo Sensei()->plugin_url; ?>assets/images/woothemes.png" alt="WooThemes" /></a></p>
241
+			<p class="powered-by-woo"><?php _e('Powered by', 'woothemes-sensei'); ?><a href="http://www.woothemes.com/" title="WooThemes"><img src="<?php echo Sensei()->plugin_url; ?>assets/images/woothemes.png" alt="WooThemes" /></a></p>
242 242
 		<?php
243
-		do_action( 'sensei_grading_after_headers' );
243
+		do_action('sensei_grading_after_headers');
244 244
 	} // End grading_headers()
245 245
 
246 246
 	/**
@@ -249,10 +249,10 @@  discard block
 block discarded – undo
249 249
 	 * @param $which string
250 250
 	 * @return void
251 251
 	 */
252
-	public function wrapper_container( $which ) {
253
-		if ( 'top' == $which ) {
252
+	public function wrapper_container($which) {
253
+		if ('top' == $which) {
254 254
 			?><div id="woothemes-sensei" class="wrap woothemes-sensei"><?php
255
-		} elseif ( 'bottom' == $which ) {
255
+		} elseif ('bottom' == $which) {
256 256
 			?></div><!--/#woothemes-sensei--><?php
257 257
 		} // End If Statement
258 258
 	} // End wrapper_container()
@@ -265,29 +265,29 @@  discard block
 block discarded – undo
265 265
 	public function grading_default_nav() {
266 266
 		global  $wp_version;
267 267
 
268
-		$title = sprintf( '<a href="%s">%s</a>', esc_url(add_query_arg( array( 'page' => $this->page_slug ), admin_url( 'admin.php' ) ) ), esc_html( $this->name ) );
269
-		if ( isset( $_GET['course_id'] ) ) { 
270
-			$course_id = intval( $_GET['course_id'] );
271
-			if ( version_compare($wp_version, '4.1', '>=') ) {
272
-				$url = add_query_arg( array( 'page' => $this->page_slug, 'course_id' => $course_id ), admin_url( 'admin.php' ) );
273
-				$title .= sprintf( '&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url( $url ), get_the_title( $course_id ) );
268
+		$title = sprintf('<a href="%s">%s</a>', esc_url(add_query_arg(array('page' => $this->page_slug), admin_url('admin.php'))), esc_html($this->name));
269
+		if (isset($_GET['course_id'])) { 
270
+			$course_id = intval($_GET['course_id']);
271
+			if (version_compare($wp_version, '4.1', '>=')) {
272
+				$url = add_query_arg(array('page' => $this->page_slug, 'course_id' => $course_id), admin_url('admin.php'));
273
+				$title .= sprintf('&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url($url), get_the_title($course_id));
274 274
 			}
275 275
 			else {
276
-				$title .= sprintf( '&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;%s</span>', get_the_title( $course_id ) ); 
276
+				$title .= sprintf('&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;%s</span>', get_the_title($course_id)); 
277 277
 			}
278 278
 		}
279
-		if ( isset( $_GET['lesson_id'] ) ) { 
280
-			$lesson_id = intval( $_GET['lesson_id'] );
281
-			$title .= '&nbsp;&nbsp;<span class="lesson-title">&gt;&nbsp;&nbsp;' . get_the_title( intval( $lesson_id ) ) . '</span>'; 
279
+		if (isset($_GET['lesson_id'])) { 
280
+			$lesson_id = intval($_GET['lesson_id']);
281
+			$title .= '&nbsp;&nbsp;<span class="lesson-title">&gt;&nbsp;&nbsp;'.get_the_title(intval($lesson_id)).'</span>'; 
282 282
 		}
283
-		if ( isset( $_GET['user_id'] ) && 0 < intval( $_GET['user_id'] ) ) {
283
+		if (isset($_GET['user_id']) && 0 < intval($_GET['user_id'])) {
284 284
 
285
-            $user_name = Sensei_Learner::get_full_name( $_GET['user_id'] );
286
-			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;' . $user_name . '</span>';
285
+            $user_name = Sensei_Learner::get_full_name($_GET['user_id']);
286
+			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;'.$user_name.'</span>';
287 287
 
288 288
 		} // End If Statement
289 289
 		?>
290
-			<h2><?php echo apply_filters( 'sensei_grading_nav_title', $title ); ?></h2>
290
+			<h2><?php echo apply_filters('sensei_grading_nav_title', $title); ?></h2>
291 291
 		<?php
292 292
 	} // End grading_default_nav()
293 293
 
@@ -299,29 +299,29 @@  discard block
 block discarded – undo
299 299
 	public function grading_user_quiz_nav() {
300 300
 		global  $wp_version;
301 301
 
302
-		$title = sprintf( '<a href="%s">%s</a>', add_query_arg( array( 'page' => $this->page_slug ), admin_url( 'admin.php' ) ), esc_html( $this->name ) );
303
-		if ( isset( $_GET['quiz_id'] ) ) { 
304
-			$quiz_id = intval( $_GET['quiz_id'] );
305
-			$lesson_id = get_post_meta( $quiz_id, '_quiz_lesson', true );
306
-			$course_id = get_post_meta( $lesson_id, '_lesson_course', true );
307
-			if ( version_compare($wp_version, '4.1', '>=') ) {
308
-				$url = add_query_arg( array( 'page' => $this->page_slug, 'course_id' => $course_id ), admin_url( 'admin.php' ) );
309
-				$title .= sprintf( '&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url( $url ), get_the_title( $course_id ) );
302
+		$title = sprintf('<a href="%s">%s</a>', add_query_arg(array('page' => $this->page_slug), admin_url('admin.php')), esc_html($this->name));
303
+		if (isset($_GET['quiz_id'])) { 
304
+			$quiz_id = intval($_GET['quiz_id']);
305
+			$lesson_id = get_post_meta($quiz_id, '_quiz_lesson', true);
306
+			$course_id = get_post_meta($lesson_id, '_lesson_course', true);
307
+			if (version_compare($wp_version, '4.1', '>=')) {
308
+				$url = add_query_arg(array('page' => $this->page_slug, 'course_id' => $course_id), admin_url('admin.php'));
309
+				$title .= sprintf('&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url($url), get_the_title($course_id));
310 310
 			}
311 311
 			else {
312
-				$title .= sprintf( '&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;%s</span>', get_the_title( $course_id ) ); 
312
+				$title .= sprintf('&nbsp;&nbsp;<span class="course-title">&gt;&nbsp;&nbsp;%s</span>', get_the_title($course_id)); 
313 313
 			}
314
-			$url = add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $lesson_id ), admin_url( 'admin.php' ) );
315
-			$title .= sprintf( '&nbsp;&nbsp;<span class="lesson-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url( $url ), get_the_title( $lesson_id ) );
314
+			$url = add_query_arg(array('page' => $this->page_slug, 'lesson_id' => $lesson_id), admin_url('admin.php'));
315
+			$title .= sprintf('&nbsp;&nbsp;<span class="lesson-title">&gt;&nbsp;&nbsp;<a href="%s">%s</a></span>', esc_url($url), get_the_title($lesson_id));
316 316
 		}
317
-		if ( isset( $_GET['user'] ) && 0 < intval( $_GET['user'] ) ) {
317
+		if (isset($_GET['user']) && 0 < intval($_GET['user'])) {
318 318
 
319
-            $user_name = Sensei_Learner::get_full_name( $_GET['user'] );
320
-			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;' . $user_name . '</span>';
319
+            $user_name = Sensei_Learner::get_full_name($_GET['user']);
320
+			$title .= '&nbsp;&nbsp;<span class="user-title">&gt;&nbsp;&nbsp;'.$user_name.'</span>';
321 321
 
322 322
 		} // End If Statement
323 323
 		?>
324
-			<h2><?php echo apply_filters( 'sensei_grading_nav_title', $title ); ?></h2>
324
+			<h2><?php echo apply_filters('sensei_grading_nav_title', $title); ?></h2>
325 325
 		<?php
326 326
 	} // End grading_user_quiz_nav()
327 327
 
@@ -330,9 +330,9 @@  discard block
 block discarded – undo
330 330
 	 * @since  1.7.0
331 331
 	 * @return array
332 332
 	 */
333
-	public function get_stati( $type ) {
333
+	public function get_stati($type) {
334 334
 		$statuses = array();
335
-		switch( $type ) {
335
+		switch ($type) {
336 336
 			case 'course' :
337 337
 				$statuses = array(
338 338
 					'in-progress',
@@ -362,7 +362,7 @@  discard block
 block discarded – undo
362 362
 	 * @param  array $args (default: array())
363 363
 	 * @return object
364 364
 	 */
365
-	public function count_statuses( $args = array() ) {
365
+	public function count_statuses($args = array()) {
366 366
 		global  $wpdb;
367 367
 
368 368
         /**
@@ -373,71 +373,71 @@  discard block
 block discarded – undo
373 373
          * @since 1.8.0
374 374
          * @param array $args
375 375
          */
376
-        $args = apply_filters( 'sensei_count_statuses_args', $args );
376
+        $args = apply_filters('sensei_count_statuses_args', $args);
377 377
 
378
-		if ( 'course' == $args['type'] ) {
378
+		if ('course' == $args['type']) {
379 379
 			$type = 'sensei_course_status';
380 380
 		}
381 381
 		else {
382 382
 			$type = 'sensei_lesson_status';
383 383
 		}
384
-		$cache_key = 'sensei-' . $args['type'] . '-statuses';
384
+		$cache_key = 'sensei-'.$args['type'].'-statuses';
385 385
 
386 386
 		$query = "SELECT comment_approved, COUNT( * ) AS total FROM {$wpdb->comments} WHERE comment_type = %s ";
387 387
 
388 388
         // Restrict to specific posts
389
-		if ( isset( $args['post__in'] ) && !empty( $args['post__in'] ) && is_array( $args['post__in'] ) ) {
390
-			$query .= ' AND comment_post_ID IN (' . implode( ',', array_map( 'absint', $args['post__in'] ) ) . ')';
389
+		if (isset($args['post__in']) && ! empty($args['post__in']) && is_array($args['post__in'])) {
390
+			$query .= ' AND comment_post_ID IN ('.implode(',', array_map('absint', $args['post__in'])).')';
391 391
 		}
392
-		elseif ( !empty( $args['post_id'] ) ) {
393
-			$query .= $wpdb->prepare( ' AND comment_post_ID = %d', $args['post_id'] );
392
+		elseif ( ! empty($args['post_id'])) {
393
+			$query .= $wpdb->prepare(' AND comment_post_ID = %d', $args['post_id']);
394 394
 		}
395 395
 		// Restrict to specific users
396
-		if ( isset( $args['user_id'] ) && is_array( $args['user_id'] ) ) {
397
-			$query .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $args['user_id'] ) ) . ')';
396
+		if (isset($args['user_id']) && is_array($args['user_id'])) {
397
+			$query .= ' AND user_id IN ('.implode(',', array_map('absint', $args['user_id'])).')';
398 398
 		}
399
-		elseif ( !empty( $args['user_id'] ) ) {
400
-			$query .= $wpdb->prepare( ' AND user_id = %d', $args['user_id'] );
399
+		elseif ( ! empty($args['user_id'])) {
400
+			$query .= $wpdb->prepare(' AND user_id = %d', $args['user_id']);
401 401
 		}
402 402
 		$query .= ' GROUP BY comment_approved';
403 403
 
404
-		$counts = wp_cache_get( $cache_key, 'counts' );
405
-		if ( false === $counts ) {
406
-			$sql = $wpdb->prepare( $query, $type );
407
-			$results = (array) $wpdb->get_results( $sql, ARRAY_A );
408
-			$counts = array_fill_keys( $this->get_stati( $type ), 0 );
404
+		$counts = wp_cache_get($cache_key, 'counts');
405
+		if (false === $counts) {
406
+			$sql = $wpdb->prepare($query, $type);
407
+			$results = (array) $wpdb->get_results($sql, ARRAY_A);
408
+			$counts = array_fill_keys($this->get_stati($type), 0);
409 409
 
410
-			foreach ( $results as $row ) {
411
-				$counts[ $row['comment_approved'] ] = $row['total'];
410
+			foreach ($results as $row) {
411
+				$counts[$row['comment_approved']] = $row['total'];
412 412
 			}
413
-			wp_cache_set( $cache_key, $counts, 'counts' );
413
+			wp_cache_set($cache_key, $counts, 'counts');
414 414
 		}
415 415
 
416
-		if( ! isset( $counts['graded'] ) ) {
416
+		if ( ! isset($counts['graded'])) {
417 417
 			$counts['graded'] = 0;
418 418
 		}
419 419
 
420
-		if( ! isset( $counts['ungraded'] ) ) {
420
+		if ( ! isset($counts['ungraded'])) {
421 421
 			$counts['ungraded'] = 0;
422 422
 		}
423 423
 
424
-		if( ! isset( $counts['passed'] ) ) {
424
+		if ( ! isset($counts['passed'])) {
425 425
 			$counts['passed'] = 0;
426 426
 		}
427 427
 
428
-		if( ! isset( $counts['failed'] ) ) {
428
+		if ( ! isset($counts['failed'])) {
429 429
 			$counts['failed'] = 0;
430 430
 		}
431 431
 
432
-		if( ! isset( $counts['in-progress'] ) ) {
432
+		if ( ! isset($counts['in-progress'])) {
433 433
 			$counts['in-progress'] = 0;
434 434
 		}
435 435
 
436
-		if( ! isset( $counts['complete'] ) ) {
436
+		if ( ! isset($counts['complete'])) {
437 437
 			$counts['complete'] = 0;
438 438
 		}
439 439
 
440
-		return apply_filters( 'sensei_count_statuses', $counts, $type );
440
+		return apply_filters('sensei_count_statuses', $counts, $type);
441 441
 	} // End sensei_count_statuses()
442 442
 
443 443
 	/**
@@ -445,11 +445,11 @@  discard block
 block discarded – undo
445 445
 	 * @since  1.7.0
446 446
 	 * @return string
447 447
 	 */
448
-	public function courses_drop_down_html( $selected_course_id = 0 ) {
448
+	public function courses_drop_down_html($selected_course_id = 0) {
449 449
 
450 450
 		$html = '';
451 451
 
452
-		$course_args = array(   'post_type'         => 'course',
452
+		$course_args = array('post_type'         => 'course',
453 453
 								'posts_per_page'       => -1,
454 454
 								'orderby'           => 'title',
455 455
 								'order'             => 'ASC',
@@ -457,12 +457,12 @@  discard block
 block discarded – undo
457 457
 								'suppress_filters'  => 0,
458 458
 								'fields'            => 'ids',
459 459
 							);
460
-		$courses = get_posts( apply_filters( 'sensei_grading_filter_courses', $course_args ) );
460
+		$courses = get_posts(apply_filters('sensei_grading_filter_courses', $course_args));
461 461
 
462
-		$html .= '<option value="">' . __( 'Select a course', 'woothemes-sensei' ) . '</option>';
463
-		if ( count( $courses ) > 0 ) {
464
-			foreach ($courses as $course_id){
465
-				$html .= '<option value="' . esc_attr( absint( $course_id ) ) . '" ' . selected( $course_id, $selected_course_id, false ) . '>' . esc_html( get_the_title( $course_id ) ) . '</option>' . "\n";
462
+		$html .= '<option value="">'.__('Select a course', 'woothemes-sensei').'</option>';
463
+		if (count($courses) > 0) {
464
+			foreach ($courses as $course_id) {
465
+				$html .= '<option value="'.esc_attr(absint($course_id)).'" '.selected($course_id, $selected_course_id, false).'>'.esc_html(get_the_title($course_id)).'</option>'."\n";
466 466
 			} // End For Loop
467 467
 		} // End If Statement
468 468
 
@@ -483,20 +483,20 @@  discard block
 block discarded – undo
483 483
 		$course_data = array();
484 484
 		parse_str($data, $course_data);
485 485
 
486
-		$course_id = intval( $course_data['course_id'] );
486
+		$course_id = intval($course_data['course_id']);
487 487
 
488
-		$html = $this->lessons_drop_down_html( $course_id );
488
+		$html = $this->lessons_drop_down_html($course_id);
489 489
 
490 490
 		echo $html;
491 491
 		die(); // WordPress may print out a spurious zero without this can be particularly bad if using JSON
492 492
 	}
493 493
 
494
-	public function lessons_drop_down_html( $course_id = 0, $selected_lesson_id = 0 ) {
494
+	public function lessons_drop_down_html($course_id = 0, $selected_lesson_id = 0) {
495 495
 
496 496
 		$html = '';
497
-		if ( 0 < intval( $course_id ) ) {
497
+		if (0 < intval($course_id)) {
498 498
 
499
-			$lesson_args = array( 'post_type'       => 'lesson',
499
+			$lesson_args = array('post_type'       => 'lesson',
500 500
 								'posts_per_page'       => -1,
501 501
 								'orderby'           => 'title',
502 502
 								'order'             => 'ASC',
@@ -506,12 +506,12 @@  discard block
 block discarded – undo
506 506
 								'suppress_filters'  => 0,
507 507
 								'fields'            => 'ids',
508 508
 								);
509
-			$lessons = get_posts( apply_filters( 'sensei_grading_filter_lessons', $lesson_args ) );
509
+			$lessons = get_posts(apply_filters('sensei_grading_filter_lessons', $lesson_args));
510 510
 
511
-			$html .= '<option value="">' . __( 'Select a lesson', 'woothemes-sensei' ) . '</option>';
512
-			if ( count( $lessons ) > 0 ) {
513
-				foreach ( $lessons as $lesson_id ){
514
-					$html .= '<option value="' . esc_attr( absint( $lesson_id ) ) . '" ' . selected( $lesson_id, $selected_lesson_id, false ) . '>' . esc_html( get_the_title( $lesson_id ) ) . '</option>' . "\n";
511
+			$html .= '<option value="">'.__('Select a lesson', 'woothemes-sensei').'</option>';
512
+			if (count($lessons) > 0) {
513
+				foreach ($lessons as $lesson_id) {
514
+					$html .= '<option value="'.esc_attr(absint($lesson_id)).'" '.selected($lesson_id, $selected_lesson_id, false).'>'.esc_html(get_the_title($lesson_id)).'</option>'."\n";
515 515
 				} // End For Loop
516 516
 			} // End If Statement
517 517
 
@@ -532,10 +532,10 @@  discard block
 block discarded – undo
532 532
     public function admin_process_grading_submission() {
533 533
 
534 534
         // NEEDS REFACTOR/OPTIMISING, such as combining the various meta data stored against the sensei_user_answer entry
535
-        if( ! isset( $_POST['sensei_manual_grade'] )
536
-            || ! wp_verify_nonce( $_POST['_wp_sensei_manual_grading_nonce'], 'sensei_manual_grading' )
537
-            || ! isset( $_GET['quiz_id'] )
538
-            || $_GET['quiz_id'] != $_POST['sensei_manual_grade'] ) {
535
+        if ( ! isset($_POST['sensei_manual_grade'])
536
+            || ! wp_verify_nonce($_POST['_wp_sensei_manual_grading_nonce'], 'sensei_manual_grading')
537
+            || ! isset($_GET['quiz_id'])
538
+            || $_GET['quiz_id'] != $_POST['sensei_manual_grade']) {
539 539
 
540 540
             return false; //exit and do not grade
541 541
 
@@ -545,30 +545,30 @@  discard block
 block discarded – undo
545 545
         $user_id = $_GET['user'];
546 546
 
547 547
 
548
-        $questions = Sensei_Utils::sensei_get_quiz_questions( $quiz_id );
549
-        $quiz_lesson_id =  Sensei()->quiz->get_lesson_id( $quiz_id );
548
+        $questions = Sensei_Utils::sensei_get_quiz_questions($quiz_id);
549
+        $quiz_lesson_id = Sensei()->quiz->get_lesson_id($quiz_id);
550 550
         $quiz_grade = 0;
551 551
         $count = 0;
552 552
         $quiz_grade_total = $_POST['quiz_grade_total'];
553 553
         $all_question_grades = array();
554 554
         $all_answers_feedback = array();
555 555
 
556
-        foreach( $questions as $question ) {
556
+        foreach ($questions as $question) {
557 557
 
558 558
             ++$count;
559 559
             $question_id = $question->ID;
560 560
 
561
-            if( isset( $_POST[ 'question_' . $question_id ] ) ) {
561
+            if (isset($_POST['question_'.$question_id])) {
562 562
 
563 563
                 $question_grade = 0;
564
-                if( $_POST[ 'question_' . $question_id ] == 'right' ) {
564
+                if ($_POST['question_'.$question_id] == 'right') {
565 565
 
566
-                    $question_grade = $_POST[ 'question_' . $question_id . '_grade' ];
566
+                    $question_grade = $_POST['question_'.$question_id.'_grade'];
567 567
 
568 568
                 }
569 569
 
570 570
                 // add data to the array that will, after the loop, be stored on the lesson status
571
-                $all_question_grades[ $question_id ] = $question_grade;
571
+                $all_question_grades[$question_id] = $question_grade;
572 572
 
573 573
                 // tally up the total quiz grade
574 574
                 $quiz_grade += $question_grade;
@@ -577,41 +577,41 @@  discard block
 block discarded – undo
577 577
 
578 578
             // Question answer feedback / notes
579 579
             $question_feedback = '';
580
-            if( isset( $_POST[ 'questions_feedback' ][ $question_id ] ) ){
580
+            if (isset($_POST['questions_feedback'][$question_id])) {
581 581
 
582
-                $question_feedback = wp_unslash( $_POST[ 'questions_feedback' ][ $question_id ] );
582
+                $question_feedback = wp_unslash($_POST['questions_feedback'][$question_id]);
583 583
 
584 584
             }
585
-            $all_answers_feedback[ $question_id ] = $question_feedback;
585
+            $all_answers_feedback[$question_id] = $question_feedback;
586 586
 
587 587
         } // end for each $questions
588 588
 
589 589
         //store all question grades on the lesson status
590
-        Sensei()->quiz->set_user_grades( $all_question_grades, $quiz_lesson_id , $user_id );
590
+        Sensei()->quiz->set_user_grades($all_question_grades, $quiz_lesson_id, $user_id);
591 591
 
592 592
         //store the feedback from grading
593
-        Sensei()->quiz->save_user_answers_feedback( $all_answers_feedback, $quiz_lesson_id , $user_id );
593
+        Sensei()->quiz->save_user_answers_feedback($all_answers_feedback, $quiz_lesson_id, $user_id);
594 594
 
595 595
         // $_POST['all_questions_graded'] is set when all questions have been graded
596 596
         // in the class sensei grading user quiz -> display()
597
-        if( $_POST['all_questions_graded'] == 'yes' ) {
597
+        if ($_POST['all_questions_graded'] == 'yes') {
598 598
 
599 599
             // set the users total quiz grade
600
-			if ( 0 < intval( $quiz_grade_total ) ) {
601
-            $grade = abs( round( ( doubleval( $quiz_grade ) * 100 ) / ( $quiz_grade_total ), 2 ) );
600
+			if (0 < intval($quiz_grade_total)) {
601
+            $grade = abs(round((doubleval($quiz_grade) * 100) / ($quiz_grade_total), 2));
602 602
 			}
603 603
 			else {
604 604
 				$grade = 0;
605 605
 			}
606
-            Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id );
606
+            Sensei_Utils::sensei_grade_quiz($quiz_id, $grade, $user_id);
607 607
 
608 608
             // Duplicating what Frontend->sensei_complete_quiz() does
609
-            $pass_required = get_post_meta( $quiz_id, '_pass_required', true );
610
-            $quiz_passmark = abs( round( doubleval( get_post_meta( $quiz_id, '_quiz_passmark', true ) ), 2 ) );
609
+            $pass_required = get_post_meta($quiz_id, '_pass_required', true);
610
+            $quiz_passmark = abs(round(doubleval(get_post_meta($quiz_id, '_quiz_passmark', true)), 2));
611 611
             $lesson_metadata = array();
612
-            if ( $pass_required ) {
612
+            if ($pass_required) {
613 613
                 // Student has reached the pass mark and lesson is complete
614
-                if ( $quiz_passmark <= $grade ) {
614
+                if ($quiz_passmark <= $grade) {
615 615
                     $lesson_status = 'passed';
616 616
                 }
617 617
                 else {
@@ -624,9 +624,9 @@  discard block
 block discarded – undo
624 624
             }
625 625
             $lesson_metadata['grade'] = $grade; // Technically already set as part of "WooThemes_Sensei_Utils::sensei_grade_quiz()" above
626 626
 
627
-            Sensei_Utils::update_lesson_status( $user_id, $quiz_lesson_id, $lesson_status, $lesson_metadata );
627
+            Sensei_Utils::update_lesson_status($user_id, $quiz_lesson_id, $lesson_status, $lesson_metadata);
628 628
 
629
-            if(  in_array( $lesson_status, array( 'passed', 'graded'  ) ) ) {
629
+            if (in_array($lesson_status, array('passed', 'graded'))) {
630 630
 
631 631
                 /**
632 632
                  * Summary.
@@ -638,27 +638,27 @@  discard block
 block discarded – undo
638 638
                  * @param int  $user_id
639 639
                  * @param int $quiz_lesson_id
640 640
                  */
641
-                do_action( 'sensei_user_lesson_end', $user_id, $quiz_lesson_id );
641
+                do_action('sensei_user_lesson_end', $user_id, $quiz_lesson_id);
642 642
 
643 643
             } // end if in_array
644 644
 
645 645
         }// end if $_POST['all_que...
646 646
 
647
-        if( isset( $_POST['sensei_grade_next_learner'] ) && strlen( $_POST['sensei_grade_next_learner'] ) > 0 ) {
647
+        if (isset($_POST['sensei_grade_next_learner']) && strlen($_POST['sensei_grade_next_learner']) > 0) {
648 648
 
649
-            $load_url = add_query_arg( array( 'message' => 'graded' ) );
649
+            $load_url = add_query_arg(array('message' => 'graded'));
650 650
 
651
-        } elseif ( isset( $_POST['_wp_http_referer'] ) ) {
651
+        } elseif (isset($_POST['_wp_http_referer'])) {
652 652
 
653
-            $load_url = add_query_arg( array( 'message' => 'graded' ), $_POST['_wp_http_referer'] );
653
+            $load_url = add_query_arg(array('message' => 'graded'), $_POST['_wp_http_referer']);
654 654
 
655 655
         } else {
656 656
 
657
-            $load_url = add_query_arg( array( 'message' => 'graded' ) );
657
+            $load_url = add_query_arg(array('message' => 'graded'));
658 658
 
659 659
         }
660 660
 
661
-        wp_safe_redirect( esc_url_raw( $load_url ) );
661
+        wp_safe_redirect(esc_url_raw($load_url));
662 662
         exit;
663 663
 
664 664
     } // end admin_process_grading_submission
@@ -669,13 +669,13 @@  discard block
 block discarded – undo
669 669
 		$lesson_data = array();
670 670
 		parse_str($data, $lesson_data);
671 671
 
672
-		$lesson_id = intval( $lesson_data['lesson_id'] );
673
-		$course_id = intval( $lesson_data['course_id'] );
674
-		$grading_view = sanitize_text_field( $lesson_data['view'] );
672
+		$lesson_id = intval($lesson_data['lesson_id']);
673
+		$course_id = intval($lesson_data['course_id']);
674
+		$grading_view = sanitize_text_field($lesson_data['view']);
675 675
 
676 676
 		$redirect_url = '';
677
-		if ( 0 < $lesson_id && 0 < $course_id ) {
678
-			$redirect_url = esc_url_raw( apply_filters( 'sensei_ajax_redirect_url', add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $lesson_id, 'course_id' => $course_id, 'view' => $grading_view ), admin_url( 'admin.php' ) ) ) );
677
+		if (0 < $lesson_id && 0 < $course_id) {
678
+			$redirect_url = esc_url_raw(apply_filters('sensei_ajax_redirect_url', add_query_arg(array('page' => $this->page_slug, 'lesson_id' => $lesson_id, 'course_id' => $course_id, 'view' => $grading_view), admin_url('admin.php'))));
679 679
 		} // End If Statement
680 680
 
681 681
 		echo $redirect_url;
@@ -683,11 +683,11 @@  discard block
 block discarded – undo
683 683
 	}
684 684
 
685 685
 	public function add_grading_notices() {
686
-		if( isset( $_GET['page'] ) && $this->page_slug == $_GET['page'] && isset( $_GET['message'] ) && $_GET['message'] ) {
687
-			if( 'graded' == $_GET['message'] ) {
686
+		if (isset($_GET['page']) && $this->page_slug == $_GET['page'] && isset($_GET['message']) && $_GET['message']) {
687
+			if ('graded' == $_GET['message']) {
688 688
 				$msg = array(
689 689
 					'updated',
690
-					__( 'Quiz Graded Successfully!', 'woothemes-sensei' ),
690
+					__('Quiz Graded Successfully!', 'woothemes-sensei'),
691 691
 				);
692 692
 			}
693 693
 			?>
@@ -699,9 +699,9 @@  discard block
 block discarded – undo
699 699
 	}
700 700
 
701 701
 	public function sensei_grading_notices() {
702
-		if ( isset( $_GET['action'] ) && 'graded' == $_GET['action'] ) {
702
+		if (isset($_GET['action']) && 'graded' == $_GET['action']) {
703 703
 			echo '<div class="grading-notice updated">';
704
-				echo '<p>' . __( 'Quiz Graded Successfully!', 'woothemes-sensei' ) . '</p>';
704
+				echo '<p>'.__('Quiz Graded Successfully!', 'woothemes-sensei').'</p>';
705 705
 			echo '</div>';
706 706
 		} // End If Statement
707 707
 	} // End sensei_grading_notices()
@@ -724,16 +724,16 @@  discard block
 block discarded – undo
724 724
      *
725 725
      * @return int $quiz_grade total sum of all question grades
726 726
      */
727
-    public static function grade_quiz_auto( $quiz_id = 0, $submitted = array(), $total_questions = 0, $quiz_grade_type = 'auto' ) {
727
+    public static function grade_quiz_auto($quiz_id = 0, $submitted = array(), $total_questions = 0, $quiz_grade_type = 'auto') {
728 728
 
729
-        if( ! ( intval( $quiz_id ) > 0 )  || ! $submitted
730
-            || $quiz_grade_type != 'auto' ) {
729
+        if ( ! (intval($quiz_id) > 0) || ! $submitted
730
+            || $quiz_grade_type != 'auto') {
731 731
             return false; // exit early
732 732
         }
733 733
 
734 734
 
735 735
         $user_id = get_current_user_id();
736
-        $lesson_id =  Sensei()->quiz->get_lesson_id(  $quiz_id ) ;
736
+        $lesson_id = Sensei()->quiz->get_lesson_id($quiz_id);
737 737
         $quiz_autogradable = true;
738 738
 
739 739
         /**
@@ -747,23 +747,23 @@  discard block
 block discarded – undo
747 747
          *      'gap-fill'.
748 748
          * }
749 749
          */
750
-        $autogradable_question_types = apply_filters( 'sensei_autogradable_question_types', array( 'multiple-choice', 'boolean', 'gap-fill' ) );
750
+        $autogradable_question_types = apply_filters('sensei_autogradable_question_types', array('multiple-choice', 'boolean', 'gap-fill'));
751 751
 
752 752
         $grade_total = 0;
753 753
         $all_question_grades = array();
754
-        foreach( $submitted as $question_id => $answer ) {
754
+        foreach ($submitted as $question_id => $answer) {
755 755
 
756 756
             // check if the question is autogradable, either by type, or because the grade is 0
757
-            $question_type = Sensei()->question->get_question_type( $question_id );
758
-			$achievable_grade = Sensei()->question->get_question_grade( $question_id );
757
+            $question_type = Sensei()->question->get_question_type($question_id);
758
+			$achievable_grade = Sensei()->question->get_question_grade($question_id);
759 759
 			// Question has a zero grade, so skip grading
760
-			if ( 0 == $achievable_grade ) {
761
-				$all_question_grades[ $question_id ] = $achievable_grade;
760
+			if (0 == $achievable_grade) {
761
+				$all_question_grades[$question_id] = $achievable_grade;
762 762
 			}
763
-            elseif ( in_array( $question_type, $autogradable_question_types ) ) {
763
+            elseif (in_array($question_type, $autogradable_question_types)) {
764 764
                 // Get user question grade
765
-                $question_grade = Sensei_Utils::sensei_grade_question_auto( $question_id, $question_type, $answer, $user_id );
766
-                $all_question_grades[ $question_id ] = $question_grade;
765
+                $question_grade = Sensei_Utils::sensei_grade_question_auto($question_id, $question_type, $answer, $user_id);
766
+                $all_question_grades[$question_id] = $question_grade;
767 767
                 $grade_total += $question_grade;
768 768
 
769 769
             } else {
@@ -776,27 +776,27 @@  discard block
 block discarded – undo
776 776
         }// end for each question
777 777
 
778 778
         // Only if the whole quiz was autogradable do we set a grade
779
-        if ( $quiz_autogradable ) {
779
+        if ($quiz_autogradable) {
780 780
 
781
-            $quiz_total = Sensei_Utils::sensei_get_quiz_total( $quiz_id );
781
+            $quiz_total = Sensei_Utils::sensei_get_quiz_total($quiz_id);
782 782
 			// Check for zero total from grades
783
-			if ( 0 < $quiz_total ) {
784
-            $grade = abs( round( ( doubleval( $grade_total ) * 100 ) / ( $quiz_total ), 2 ) );
783
+			if (0 < $quiz_total) {
784
+            $grade = abs(round((doubleval($grade_total) * 100) / ($quiz_total), 2));
785 785
 			}
786 786
 			else {
787 787
 				$grade = 0;
788 788
 			}
789
-            Sensei_Utils::sensei_grade_quiz( $quiz_id, $grade, $user_id, $quiz_grade_type );
789
+            Sensei_Utils::sensei_grade_quiz($quiz_id, $grade, $user_id, $quiz_grade_type);
790 790
 
791 791
         } else {
792 792
 
793
-            $grade = new WP_Error( 'autograde', __( 'This quiz is not able to be automatically graded.', 'woothemes-sensei' ) );
793
+            $grade = new WP_Error('autograde', __('This quiz is not able to be automatically graded.', 'woothemes-sensei'));
794 794
 
795 795
         }
796 796
 
797 797
         // store the auto gradable grades. If the quiz is not auto gradable the grades can be use as the default
798 798
         // when doing manual grading.
799
-        Sensei()->quiz-> set_user_grades( $all_question_grades, $lesson_id, $user_id );
799
+        Sensei()->quiz-> set_user_grades($all_question_grades, $lesson_id, $user_id);
800 800
 
801 801
         return $grade;
802 802
 
@@ -816,22 +816,22 @@  discard block
 block discarded – undo
816 816
      *
817 817
      * @return int $question_grade
818 818
      */
819
-    public static function grade_question_auto( $question_id = 0, $question_type = '', $answer = '', $user_id = 0 ) {
819
+    public static function grade_question_auto($question_id = 0, $question_type = '', $answer = '', $user_id = 0) {
820 820
 
821
-        if( intval( $user_id ) == 0 ) {
821
+        if (intval($user_id) == 0) {
822 822
 
823 823
             $user_id = get_current_user_id();
824 824
 
825 825
         }
826 826
 
827
-        if( ! ( intval( $question_id ) > 0 ) ) {
827
+        if ( ! (intval($question_id) > 0)) {
828 828
 
829 829
             return false;
830 830
 
831 831
         }
832 832
 
833 833
 
834
-        Sensei()->question->get_question_type( $question_id );
834
+        Sensei()->question->get_question_type($question_id);
835 835
 
836 836
         /**
837 837
          * Applying a grade before the auto grading takes place.
@@ -845,42 +845,42 @@  discard block
 block discarded – undo
845 845
          * @param string $question_type one of the Sensei question type.
846 846
          * @param string $answer user supplied question answer
847 847
          */
848
-        $question_grade = apply_filters( 'sensei_pre_grade_question_auto', false, $question_id, $question_type, $answer );
848
+        $question_grade = apply_filters('sensei_pre_grade_question_auto', false, $question_id, $question_type, $answer);
849 849
 
850
-        if ( false !== $question_grade ) {
850
+        if (false !== $question_grade) {
851 851
 
852 852
             return $question_grade;
853 853
 
854 854
         }
855 855
 
856 856
         // auto grading core
857
-        if( in_array( $question_type ,  array( 'multiple-choice'  , 'boolean'  ) )   ){
857
+        if (in_array($question_type, array('multiple-choice', 'boolean'))) {
858 858
 
859
-            $right_answer = (array) get_post_meta( $question_id, '_question_right_answer', true );
859
+            $right_answer = (array) get_post_meta($question_id, '_question_right_answer', true);
860 860
 
861
-            if( 0 == get_magic_quotes_gpc() ) {
862
-                $answer = wp_unslash( $answer );
861
+            if (0 == get_magic_quotes_gpc()) {
862
+                $answer = wp_unslash($answer);
863 863
             }
864 864
             $answer = (array) $answer;
865
-            if ( is_array( $right_answer ) && count( $right_answer ) == count( $answer ) ) {
865
+            if (is_array($right_answer) && count($right_answer) == count($answer)) {
866 866
                 // Loop through all answers ensure none are 'missing'
867 867
                 $all_correct = true;
868
-                foreach ( $answer as $check_answer ) {
869
-                    if ( !in_array( $check_answer, $right_answer ) ) {
868
+                foreach ($answer as $check_answer) {
869
+                    if ( ! in_array($check_answer, $right_answer)) {
870 870
                         $all_correct = false;
871 871
                     }
872 872
                 }
873 873
                 // If all correct then grade
874
-                if ( $all_correct ) {
875
-                    $question_grade = Sensei()->question->get_question_grade( $question_id );
874
+                if ($all_correct) {
875
+                    $question_grade = Sensei()->question->get_question_grade($question_id);
876 876
                 }
877 877
             }
878 878
 
879
-        } elseif( 'gap-fill' == $question_type ){
879
+        } elseif ('gap-fill' == $question_type) {
880 880
 
881
-            $question_grade = self::grade_gap_fill_question( $question_id ,$answer );
881
+            $question_grade = self::grade_gap_fill_question($question_id, $answer);
882 882
 
883
-        } else{
883
+        } else {
884 884
 
885 885
             /**
886 886
              * Grading questions that are not auto gradable.
@@ -893,7 +893,7 @@  discard block
 block discarded – undo
893 893
              * @param string $question_type one of the Sensei question type.
894 894
              * @param string $answer user supplied question answer
895 895
              */
896
-            $question_grade = ( int ) apply_filters( 'sensei_grade_question_auto', $question_grade, $question_id, $question_type, $answer );
896
+            $question_grade = (int) apply_filters('sensei_grade_question_auto', $question_grade, $question_id, $question_type, $answer);
897 897
 
898 898
         } // end if $question_type
899 899
 
@@ -909,13 +909,13 @@  discard block
 block discarded – undo
909 909
      *
910 910
      * @return bool | int false or the grade given to the user answer
911 911
      */
912
-    public static function grade_gap_fill_question( $question_id, $user_answer ){
912
+    public static function grade_gap_fill_question($question_id, $user_answer) {
913 913
 
914
-        $right_answer = get_post_meta( $question_id, '_question_right_answer', true );
915
-        $gapfill_array = explode( '||', $right_answer );
914
+        $right_answer = get_post_meta($question_id, '_question_right_answer', true);
915
+        $gapfill_array = explode('||', $right_answer);
916 916
 
917
-        if( 0 == get_magic_quotes_gpc() ) { // deprecated from PHP 5.4 but we still support PHP 5.2
918
-            $user_answer = wp_unslash( $user_answer );
917
+        if (0 == get_magic_quotes_gpc()) { // deprecated from PHP 5.4 but we still support PHP 5.2
918
+            $user_answer = wp_unslash($user_answer);
919 919
         }
920 920
 
921 921
         /**
@@ -928,53 +928,53 @@  discard block
 block discarded – undo
928 928
          *
929 929
          * @since 1.9.0
930 930
          */
931
-        $do_case_sensitive_comparison = apply_filters('sensei_gap_fill_case_sensitive_grading', false );
931
+        $do_case_sensitive_comparison = apply_filters('sensei_gap_fill_case_sensitive_grading', false);
932 932
 
933
-        if( $do_case_sensitive_comparison ){
933
+        if ($do_case_sensitive_comparison) {
934 934
 
935 935
             // Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
936
-            if ( trim(($gapfill_array[1])) == trim( $user_answer ) ) {
936
+            if (trim(($gapfill_array[1])) == trim($user_answer)) {
937 937
 
938
-                return Sensei()->question->get_question_grade( $question_id );
938
+                return Sensei()->question->get_question_grade($question_id);
939 939
 
940
-            } else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
940
+            } else if (@preg_match('/'.$gapfill_array[1].'/i', null) !== FALSE) {
941 941
 
942
-                if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
942
+                if (preg_match('/'.$gapfill_array[1].'/i', $user_answer)) {
943 943
 
944 944
                     return Sensei()->question->get_question_grade($question_id);
945 945
 
946
-                }else{
946
+                } else {
947 947
 
948 948
                     return false;
949 949
 
950 950
                 }
951 951
 
952
-            }else{
952
+            } else {
953 953
 
954 954
                 return false;
955 955
 
956 956
             }
957 957
 
958
-        }else{
958
+        } else {
959 959
 
960 960
             // Case Sensitive Check that the 'gap' is "exactly" equal to the given answer
961
-            if ( trim(strtolower($gapfill_array[1])) == trim(strtolower( $user_answer )) ) {
961
+            if (trim(strtolower($gapfill_array[1])) == trim(strtolower($user_answer))) {
962 962
 
963
-               return Sensei()->question->get_question_grade( $question_id );
963
+               return Sensei()->question->get_question_grade($question_id);
964 964
 
965
-            } else if (@preg_match('/' . $gapfill_array[1] . '/i', null) !== FALSE) {
965
+            } else if (@preg_match('/'.$gapfill_array[1].'/i', null) !== FALSE) {
966 966
 
967
-                if (preg_match('/' . $gapfill_array[1] . '/i', $user_answer)) {
967
+                if (preg_match('/'.$gapfill_array[1].'/i', $user_answer)) {
968 968
 
969
-                    return  Sensei()->question->get_question_grade( $question_id );
969
+                    return  Sensei()->question->get_question_grade($question_id);
970 970
 
971
-                }else{
971
+                } else {
972 972
 
973 973
                     return false;
974 974
 
975 975
                 }
976 976
 
977
-            }else{
977
+            } else {
978 978
 
979 979
                 return false;
980 980
 
@@ -990,17 +990,17 @@  discard block
 block discarded – undo
990 990
      * @since 1.9.0
991 991
      * @return int $number_of_graded_lessons
992 992
      */
993
-    public static function get_graded_lessons_count(){
993
+    public static function get_graded_lessons_count() {
994 994
 
995 995
         global $wpdb;
996 996
 
997
-        $comment_query_piece[ 'select']  = "SELECT   COUNT(*) AS total";
998
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
999
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->wp_commentmeta}.meta_key = 'grade')";
1000
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
997
+        $comment_query_piece['select']  = "SELECT   COUNT(*) AS total";
998
+        $comment_query_piece['from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
999
+        $comment_query_piece['where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->wp_commentmeta}.meta_key = 'grade')";
1000
+        $comment_query_piece['orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1001 1001
 
1002
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1003
-        $number_of_graded_lessons = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1002
+        $comment_query = $comment_query_piece['select'].$comment_query_piece['from'].$comment_query_piece['where'].$comment_query_piece['orderby'];
1003
+        $number_of_graded_lessons = intval($wpdb->get_var($comment_query, 0, 0));
1004 1004
 
1005 1005
         return $number_of_graded_lessons;
1006 1006
     }
@@ -1011,17 +1011,17 @@  discard block
 block discarded – undo
1011 1011
      * @since 1.9.0
1012 1012
      * @return double $sum_of_all_grades
1013 1013
      */
1014
-    public static function get_graded_lessons_sum(){
1014
+    public static function get_graded_lessons_sum() {
1015 1015
 
1016 1016
         global $wpdb;
1017 1017
 
1018
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1019
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1020
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade')";
1021
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1018
+        $comment_query_piece['select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1019
+        $comment_query_piece['from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1020
+        $comment_query_piece['where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade')";
1021
+        $comment_query_piece['orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1022 1022
 
1023
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1024
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1023
+        $comment_query = $comment_query_piece['select'].$comment_query_piece['from'].$comment_query_piece['where'].$comment_query_piece['orderby'];
1024
+        $sum_of_all_grades = intval($wpdb->get_var($comment_query, 0, 0));
1025 1025
 
1026 1026
         return $sum_of_all_grades;
1027 1027
 
@@ -1034,17 +1034,17 @@  discard block
 block discarded – undo
1034 1034
      * @param $user_id
1035 1035
      * @return double
1036 1036
      */
1037
-    public static function get_user_graded_lessons_sum( $user_id ){
1037
+    public static function get_user_graded_lessons_sum($user_id) {
1038 1038
         global $wpdb;
1039 1039
 
1040
-        $clean_user_id = esc_sql( $user_id);
1041
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1042
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1043
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.user_id = {$clean_user_id} ";
1044
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1040
+        $clean_user_id = esc_sql($user_id);
1041
+        $comment_query_piece['select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1042
+        $comment_query_piece['from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1043
+        $comment_query_piece['where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.user_id = {$clean_user_id} ";
1044
+        $comment_query_piece['orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1045 1045
 
1046
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1047
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1046
+        $comment_query = $comment_query_piece['select'].$comment_query_piece['from'].$comment_query_piece['where'].$comment_query_piece['orderby'];
1047
+        $sum_of_all_grades = intval($wpdb->get_var($comment_query, 0, 0));
1048 1048
 
1049 1049
         return $sum_of_all_grades;
1050 1050
     }
@@ -1057,18 +1057,18 @@  discard block
 block discarded – undo
1057 1057
      * @param int lesson_id
1058 1058
      * @return double
1059 1059
      */
1060
-    public static function get_lessons_users_grades_sum( $lesson_id ){
1060
+    public static function get_lessons_users_grades_sum($lesson_id) {
1061 1061
 
1062 1062
         global $wpdb;
1063 1063
 
1064
-        $clean_lesson_id = esc_sql( $lesson_id);
1065
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1066
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1067
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.comment_post_ID = {$clean_lesson_id} ";
1068
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1064
+        $clean_lesson_id = esc_sql($lesson_id);
1065
+        $comment_query_piece['select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1066
+        $comment_query_piece['from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1067
+        $comment_query_piece['where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_lesson_status') AND ( {$wpdb->commentmeta}.meta_key = 'grade') AND {$wpdb->comments}.comment_post_ID = {$clean_lesson_id} ";
1068
+        $comment_query_piece['orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1069 1069
 
1070
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1071
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1070
+        $comment_query = $comment_query_piece['select'].$comment_query_piece['from'].$comment_query_piece['where'].$comment_query_piece['orderby'];
1071
+        $sum_of_all_grades = intval($wpdb->get_var($comment_query, 0, 0));
1072 1072
 
1073 1073
         return $sum_of_all_grades;
1074 1074
 
@@ -1082,18 +1082,18 @@  discard block
 block discarded – undo
1082 1082
      * @param int $course_id
1083 1083
      * @return double
1084 1084
      */
1085
-    public static function get_course_users_grades_sum( $course_id ){
1085
+    public static function get_course_users_grades_sum($course_id) {
1086 1086
 
1087 1087
         global $wpdb;
1088 1088
 
1089
-        $clean_course_id = esc_sql( $course_id);
1090
-        $comment_query_piece[ 'select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1091
-        $comment_query_piece[ 'from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1092
-        $comment_query_piece[ 'where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_course_status') AND ( {$wpdb->commentmeta}.meta_key = 'percent') AND {$wpdb->comments}.comment_post_ID = {$clean_course_id} ";
1093
-        $comment_query_piece[ 'orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1089
+        $clean_course_id = esc_sql($course_id);
1090
+        $comment_query_piece['select']  = "SELECT SUM({$wpdb->commentmeta}.meta_value) AS meta_sum";
1091
+        $comment_query_piece['from']    = " FROM {$wpdb->comments}  INNER JOIN {$wpdb->commentmeta}  ON ( {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id ) ";
1092
+        $comment_query_piece['where']   = " WHERE {$wpdb->comments}.comment_type IN ('sensei_course_status') AND ( {$wpdb->commentmeta}.meta_key = 'percent') AND {$wpdb->comments}.comment_post_ID = {$clean_course_id} ";
1093
+        $comment_query_piece['orderby'] = " ORDER BY {$wpdb->comments}.comment_date_gmt DESC ";
1094 1094
 
1095
-        $comment_query = $comment_query_piece['select'] . $comment_query_piece['from'] . $comment_query_piece['where'] . $comment_query_piece['orderby'];
1096
-        $sum_of_all_grades = intval( $wpdb->get_var( $comment_query, 0, 0 ) );
1095
+        $comment_query = $comment_query_piece['select'].$comment_query_piece['from'].$comment_query_piece['where'].$comment_query_piece['orderby'];
1096
+        $sum_of_all_grades = intval($wpdb->get_var($comment_query, 0, 0));
1097 1097
 
1098 1098
         return $sum_of_all_grades;
1099 1099
 
@@ -1106,4 +1106,4 @@  discard block
 block discarded – undo
1106 1106
  * @ignore only for backward compatibility
1107 1107
  * @since 1.9.0
1108 1108
  */
1109
-class WooThemes_Sensei_Grading extends Sensei_Grading{}
1109
+class WooThemes_Sensei_Grading extends Sensei_Grading {}
Please login to merge, or discard this patch.