From 98ed5d223e3f81af49080f4560a95e8849d55ada Mon Sep 17 00:00:00 2001 From: sushkov Date: Wed, 1 Feb 2012 21:30:29 +0000 Subject: [PATCH] Skip notifications when grade is not updated. Closes #72. git-svn-id: https://svn.wp-plugins.org/buddypress-courseware/trunk@498790 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- gradebook/gradebook.class.php | 74 ++++++++++++++++----------- notifications/notifications.class.php | 1 + 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/gradebook/gradebook.class.php b/gradebook/gradebook.class.php index 2b9196c..1ccf056 100644 --- a/gradebook/gradebook.class.php +++ b/gradebook/gradebook.class.php @@ -310,40 +310,45 @@ function load_grade_by_user_id( $assignment = null, $user_id = null ) { * * @param Int $gradebook_id, the ID of the gradebook * @param Mixed $grade, information about grade - * @return True on success and false on failure. + * @return True when entry is added, False when is updated */ function save_grade( $gradebook_id, $grade ) { - $grade_saved = false; + $grade_saved = null; if( empty( $gradebook_id ) ) - return false; + return null; $grade_shortcode = $this->gen_grade_shortcode( $grade ); $grades = $this->load_grades( $gradebook_id ); - - if( empty( $grades ) ) { + + // If this is the first grade, just add it! + if( count( $grades ) == 0 ) { add_post_meta( $gradebook_id, 'grade', $grade_shortcode ); - $grade_saved = true; - } else { - foreach( $grades as $g ) { - $g_data = shortcode_parse_atts( $g ); - // Check if we need to update an existing grade - if( $g_data['uid'] == $grade['uid'] ) { + return true; + } + + // Cycle to find the user grade + foreach( $grades as $g ) { + $g_data = shortcode_parse_atts( $g ); + // Check if we need to update an existing grade + if( $g_data['uid'] == $grade['uid'] ) { + // If the grade changed, update it + if ( $g_data['value'] != $grade['value'] ) { $new_grade = array_merge( $g_data, $grade ); $new_grade = array_unique( $new_grade ); unset( $new_grade[0] ); // start of shortcode unset( $new_grade[1] ); // end of shortcode $new_grade_shortcode = $this->gen_grade_shortcode( $new_grade ); update_post_meta( $gradebook_id, 'grade', $new_grade_shortcode, $g ); - $grade_saved = true; + return false; } + return; } - // If no previous entry was found, just add a new one - if( !$grade_saved ) - add_post_meta( $gradebook_id, 'grade', $grade_shortcode ); } - - return $grade_saved; + + // If no previous entry was found, just add a new one + add_post_meta( $gradebook_id, 'grade', $grade_shortcode ); + return true; } /** @@ -383,17 +388,24 @@ function gradebook_screen( $vars ) { } if( !empty( $_POST['grade'] ) ){ - foreach( $_POST['grade'] as $grade ) - if( !empty( $grade ) && !empty( $grade['uid'] ) && !empty( $grade['value'] ) ) - if( $this->save_grade( $gradebook_id, $grade ) ) { + foreach( $_POST['grade'] as $grade ) { + if( !empty( $grade ) && !empty( $grade['uid'] ) && !empty( $grade['value'] ) ) { + # If grade was added or updated! + $status = $this->save_grade( $gradebook_id, $grade ); + if( $status !== null ) { $data = array( 'grade' => $grade, 'teacher' => $bp->loggedin_user->userdata, 'assignment' => $this->current_assignment, ); - do_action( 'courseware_grade_updated', $data ); - $vars['message'] = __( 'Gradebook saved.', 'bpsp' ); + if ( $status == true ) + do_action( 'courseware_grade_added', $data ); + else + do_action( 'courseware_grade_updated', $data ); } + } + } + $vars['message'] = __( 'Gradebook saved.', 'bpsp' ); } $vars['name'] = 'gradebook'; @@ -521,15 +533,17 @@ function save_quiz_coverage( $results ) { ); if( $gradebook ) { - $saved = $this->save_grade( $gradebook, $grade ); - if( $saved ) { - $data = array( - 'grade' => $grade, - 'teacher' => bp_core_get_core_userdata( $bp->groups->current_group->creator_id ), - 'assignment' => $this->current_assignment, - ); + $status = $this->save_grade( $gradebook, $grade ); + $data = array( + 'grade' => $grade, + 'teacher' => bp_core_get_core_userdata( $bp->groups->current_group->creator_id ), + 'assignment' => $this->current_assignment, + ); + if( $status == true ) + do_action( 'courseware_grade_added', $data ); + else do_action( 'courseware_grade_updated', $data ); - } + } } } diff --git a/notifications/notifications.class.php b/notifications/notifications.class.php index 0e7c2c4..984339b 100644 --- a/notifications/notifications.class.php +++ b/notifications/notifications.class.php @@ -10,6 +10,7 @@ class BPSP_Notifications { * Constructor, adds hooks to existing actions */ function BPSP_Notifications() { + add_action( 'courseware_grade_added', array( &$this, 'send_message' ) ); add_action( 'courseware_grade_updated', array( &$this, 'send_message' ) ); add_action( 'courseware_response_added', array( &$this, 'send_message' ) ); }