Skip to content

Commit

Permalink
Filter by student role by default if not specified. Fetch defaults fr…
Browse files Browse the repository at this point in the history
…om the base schema to avoid duplication.
  • Loading branch information
brianhogg committed Jul 17, 2024
1 parent f9bfd1c commit 574a47a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
6 changes: 1 addition & 5 deletions includes/server/class-llms-rest-instructors-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected function check_read_item_permissions( $item_id ) {
return current_user_can( 'list_users', $item_id );
}


/**
* Format query arguments to retrieve a collection of objects
*
Expand All @@ -70,17 +69,14 @@ protected function prepare_collection_query_args( $request ) {
$query_args = array_merge(
$query_args,
array(
'roles' => array(
'instructor',
),
'roles' => $this->get_item_schema_base()['properties']['roles']['default'],
)
);
}

return $query_args;
}


/**
* Determine if current user has permission to create a user.
*
Expand Down
40 changes: 27 additions & 13 deletions includes/server/class-llms-rest-students-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected function check_read_item_permissions( $item_id ) {
}

return current_user_can( 'view_students', $item_id );

}

/**
Expand All @@ -76,7 +75,6 @@ public function create_item_permissions_check( $request ) {
}

return $this->check_roles_permissions( $request );

}

/**
Expand All @@ -94,7 +92,6 @@ public function delete_item_permissions_check( $request ) {
}

return true;

}

/**
Expand Down Expand Up @@ -125,7 +122,6 @@ public function get_collection_params() {
);

return $params;

}

/**
Expand All @@ -141,7 +137,6 @@ public function get_item_schema_base() {
$schema['properties']['roles']['default'] = array( 'student' );

return $schema;

}

/**
Expand All @@ -159,7 +154,6 @@ public function get_item_permissions_check( $request ) {
}

return true;

}

/**
Expand All @@ -181,7 +175,6 @@ public function get_items_permissions_check( $request ) {
}

return true;

}

/**
Expand All @@ -197,7 +190,6 @@ protected function get_object( $id ) {

$student = llms_get_student( $id, false );
return $student ? $student : llms_rest_not_found_error();

}

/**
Expand Down Expand Up @@ -230,7 +222,34 @@ protected function get_objects_query( $prepared, $request ) {
}

return $query;
}

/**
* Format query arguments to retrieve a collection of objects
*
* @since [version]
*
* @param WP_REST_Request $request Full details about the request.
* @return array|WP_Error
*/
protected function prepare_collection_query_args( $request ) {

$query_args = parent::prepare_collection_query_args( $request );
if ( is_wp_error( $query_args ) ) {
return $query_args;
}

if ( empty( $request['roles'] ) ) {
$query_args = array_merge(
$query_args,
array(
'roles' =>
$this->get_item_schema_base()['properties']['roles']['default'],
)
);
}

return $query_args;
}

/**
Expand Down Expand Up @@ -264,7 +283,6 @@ public function get_objects_query_pre( $query ) {
$query->query_where .= " AND ( p_{$post_id}_stat IS NULL OR p_{$post_id}_stat != 'enrolled' )";
}
}

}

/**
Expand All @@ -288,7 +306,6 @@ private function get_objects_query_status_subquery( $post_id ) {
ORDER BY updated_date DESC
LIMIT 1
) AS p_{$post_id}_stat";

}

/**
Expand Down Expand Up @@ -356,7 +373,6 @@ protected function prepare_links( $object, $request ) {
);

return $links;

}

/**
Expand All @@ -378,7 +394,5 @@ public function update_item_permissions_check( $request ) {
}

return $this->check_roles_permissions( $request );

}

}

0 comments on commit 574a47a

Please sign in to comment.