AssignmentsReportsGenerator
Extends report generator for assignments reports
Description
Source
File: bp-integrations/learndash/buddypress/generators/AssignmentsReportsGenerator.php
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | class AssignmentsReportsGenerator extends ReportsGenerator { /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct() { $this ->completed_table_title = __( 'Marked Assignments' , 'buddyboss' ); $this ->incompleted_table_title = __( 'Unmarked Assignments' , 'buddyboss' ); parent::__construct(); } /** * Custom fetcher to load the assigments from database and setup the pagination * * @since BuddyBoss 1.0.0 */ public function fetch() { $assignmentQuery = $this ->getGroupAssignments( $this ->args); // print_r($assignmentQuery->request);die(); $this ->results = $assignmentQuery ->posts; $this ->pager = [ 'total_items' => $assignmentQuery ->found_posts, 'per_page' => $assignmentQuery ->query_vars[ 'posts_per_page' ], 'total_pages' => $assignmentQuery ->max_num_pages ]; } /** * Returns the columns and their settings * * @since BuddyBoss 1.0.0 */ protected function columns() { return [ 'user_id' => $this ->column( 'user_id' ), 'user' => $this ->column( 'user' ), 'course_id' => $this ->column( 'course_id' ), 'course' => $this ->column( 'course' ), 'assignment' => [ 'label' => __( 'Assignment' , 'buddyboss' ), 'sortable' => false, 'order_key' => '' , ], 'completion_date' => [ 'label' => __( 'Graded Date' , 'buddyboss' ), 'sortable' => true, 'order_key' => 'assignment_modify_date' , ], 'updated_date' => [ 'label' => __( 'Uploaded Date' , 'buddyboss' ), 'sortable' => true, 'order_key' => 'assignment_post_date' , ], 'score' => [ 'label' => __( 'Score' , 'buddyboss' ), 'sortable' => false, 'order_key' => '' , ], ]; } /** * Format the activity results for each column * * @since BuddyBoss 1.0.0 */ protected function formatData( $activity ) { return [ 'user_id' => $activity ->user_id, 'user' => $activity ->user_display_name, 'course_id' => $activity ->activity_course_id, 'course' => $activity ->activity_course_title, 'assignment' => $activity ->assignment_title, 'completion_date' => get_date_from_gmt( $activity ->assignment_modify_date, $this ->args[ 'date_format' ]), 'updated_date' => get_date_from_gmt( $activity ->assignment_post_date, $this ->args[ 'date_format' ]), 'score' => $this ->getAssignmentScore( $activity ) ]; } /** * Load all the assignments from the courses belong to the group * * @since BuddyBoss 1.0.0 */ protected function getGroupAssignments() { if ( $this ->hasArg( 'course' ) && ! $this ->args[ 'course' ]) { $courseIds = learndash_group_enrolled_courses( bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( $this ->args[ 'group' ]) ); } else { $courseIds = [ $this ->args[ 'course' ]]; } $args = [ 'posts_per_page' => $this ->args[ 'length' ], 'page' => $this ->args[ 'start' ] / $this ->args[ 'length' ] + 1, 'post_type' => learndash_get_post_type_slug( 'assignment' ), 'post_status' => 'publish' , 'meta_query' => [ [ 'key' => 'course_id' , 'value' => $courseIds ] ] ]; if ( $this ->args[ 'completed' ]) { $args [ 'meta_query' ][] = [ 'key' => 'approval_status' , 'value' => 1 ]; } else { $args [ 'meta_query' ][] = [ 'key' => 'approval_status' , 'compare' => 'NOT EXISTS' ]; } if ( $this ->hasArg( 'user' ) && $this ->args[ 'user' ]) { $args [ 'author' ] = $this ->args[ 'user' ]; } $this ->registerQueryHooks(); $query = new WP_Query( $args ); $this ->unregisterQueryHooks(); return $query ; } /** * Add additional sql statement to fetch data * * @since BuddyBoss 1.0.0 */ protected function registerQueryHooks() { add_filter( 'posts_fields' , [ $this , 'addAdditionalFields' ]); add_filter( 'posts_join_paged' , [ $this , 'addAdditionalJoins' ]); add_filter( 'posts_orderby' , [ $this , 'addAdditionalOrderBy' ]); } /** * Remove additional sql statement to fetch data * * @since BuddyBoss 1.0.0 */ protected function unregisterQueryHooks() { remove_filter( 'posts_fields' , [ $this , 'addAdditionalFields' ]); remove_filter( 'posts_join_paged' , [ $this , 'addAdditionalJoins' ]); remove_filter( 'posts_orderby' , [ $this , 'addAdditionalOrderBy' ]); } /** * Add additional field sql statement * * @since BuddyBoss 1.0.0 */ public function addAdditionalFields( $strFields ) { global $wpdb ; $quizPostType = learndash_get_post_type_slug( 'quiz' ); $fields = " users.ID as user_id, users.display_name as user_display_name, users.user_email as user_email, { $wpdb ->posts}.ID as assignment_id, { $wpdb ->posts}.post_title as assignment_title, { $wpdb ->posts}.post_date_gmt as assignment_post_date, { $wpdb ->posts}.post_modified_gmt as assignment_modify_date, ( SELECT meta_value FROM { $wpdb ->postmeta} as course_meta WHERE course_meta.post_id = { $wpdb ->posts}.ID AND course_meta.meta_key = 'course_id' ) as activity_course_id, ( SELECT post_title FROM { $wpdb ->posts} as courses WHERE activity_course_id = courses.ID ) as activity_course_title "; return $fields ; } /** * Add additional joins sql statement * * @since BuddyBoss 1.0.0 */ public function addAdditionalJoins( $strJoins ) { global $wpdb ; $strJoins .= " INNER JOIN { $wpdb ->users} as users ON users.ID = { $wpdb ->posts}.post_author "; return $strJoins ; } /** * Add additional order sql statement * * @since BuddyBoss 1.0.0 */ public function addAdditionalOrderBy( $strOrder ) { $strOrder = 'GREATEST(assignment_modify_date, assignment_post_date) DESC' ; if ( $this ->hasArg( 'order' )) { $columns = $this ->columns(); $columnIndex = $this ->args[ 'order' ][0][ 'column' ]; $column = $columns [ $this ->args[ 'columns' ][ $columnIndex ][ 'name' ]]; $strOrder = "{$column['order_key']} {$this->args['order'][0]['dir']}, {$strOrder}" ; } return $strOrder ; } /** * Return the assignment score if available * * @since BuddyBoss 1.0.0 */ protected function getAssignmentScore( $activity ) { $postId = $activity ->assignment_id; if (! get_post_meta( $postId , 'approval_status' , true)) { return '-' ; } $assignmentSettingId = intval ( get_post_meta( $postId , 'lesson_id' , true ) ); if ( empty ( $assignmentSettingId )) { return '-' ; } $maxPoints = learndash_get_setting( $assignmentSettingId , 'lesson_assignment_points_amount' ); return sprintf( _x( '%1$s / %2$s' , 'placeholders: current points / maximum point' , 'buddyboss' ), get_post_meta( $postId , 'points' , true), $maxPoints ); } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- addAdditionalFields — Add additional field sql statement
- addAdditionalJoins — Add additional joins sql statement
- addAdditionalOrderBy — Add additional order sql statement
- columns — Returns the columns and their settings
- fetch — Custom fetcher to load the assignments from database and setup the pagination
- formatData — Format the activity results for each column
- getAssignmentScore — Return the assignment score if available
- getGroupAssignments — Load all the assignments from the courses belong to the group
- registerQueryHooks — Add additional sql statement to fetch data
- unregisterQueryHooks — Remove additional sql statement to fetch data
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.