Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sushkov committed Feb 1, 2012
1 parent 5e1c8e0 commit 98ed5d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
74 changes: 44 additions & 30 deletions gradebook/gradebook.class.php
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 );
}

}
}
}
Expand Down
1 change: 1 addition & 0 deletions notifications/notifications.class.php
Expand Up @@ -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' ) );
}
Expand Down

0 comments on commit 98ed5d2

Please sign in to comment.