Skip to content

Commit

Permalink
MDL-63064 block_starredcourses: remove pagination from main module
Browse files Browse the repository at this point in the history
This commit also removes the userid attribute handling on templates, JS and from the webservices
setting the logged user as user id on the webservice side.
  • Loading branch information
lameze committed Oct 31, 2018
1 parent 07fdb5a commit 8730c06
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
2 changes: 1 addition & 1 deletion blocks/starredcourses/amd/build/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 14 additions & 37 deletions blocks/starredcourses/amd/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ define(
'jquery',
'core/notification',
'block_starredcourses/repository',
'core/paged_content_factory',
'core/pubsub',
'core/templates',
'core_course/events'
Expand All @@ -34,7 +33,6 @@ function(
$,
Notification,
Repository,
PagedContentFactory,
PubSub,
Templates,
CourseEvents
Expand All @@ -61,7 +59,7 @@ function(
courses: courses
});
} else {
var nocoursesimg = $(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg');
var nocoursesimg =root.find(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg');
return Templates.render('block_starredcourses/no-courses', {
nocoursesimg: nocoursesimg
});
Expand All @@ -72,37 +70,18 @@ function(
* Fetch user's starred courses and reload the content of the block.
*
* @param {object} root The root element for the starred view.
* @param {Number} userid The user id.
*/
var reloadContent = function(root, userid) {
var reloadContent = function(root) {
var content = root.find(SELECTORS.STARRED_COURSES_REGION);

PagedContentFactory.createWithLimit(
NUM_COURSES_TOTAL,
function(pagesData, actions) {
var promises = [];
var args = {
limit: NUM_COURSES_TOTAL,
offset: 0,
};

pagesData.forEach(function(pageData) {
var args = {
limit: NUM_COURSES_TOTAL,
offset: pageData.offset,
userid: userid
};

// Load the page data.
var pagePromise = Repository.getStarredCourses(args).then(function(courses) {
if (courses.length > 0) {
return renderCourses(root, courses);
} else {
actions.allItemsLoaded(pageData.pageNumber);
return renderCourses(root, courses);
}
});

promises.push(pagePromise);
});

return promises;
return Repository.getStarredCourses(args)
.then(function(courses) {
return renderCourses(root, courses);
}).then(function(html, js) {
return Templates.replaceNodeContents(content, html, js);
}).catch(Notification.exception);
Expand All @@ -112,15 +91,14 @@ function(
* Register event listeners for the block.
*
* @param {object} root The calendar root element
* @param {Number} userid The user id.
*/
var registerEventListeners = function(root, userid) {
var registerEventListeners = function(root) {
PubSub.subscribe(CourseEvents.favourited, function() {
reloadContent(root, userid);
reloadContent(root);
});

PubSub.subscribe(CourseEvents.unfavorited, function() {
reloadContent(root, userid);
reloadContent(root);
});
};

Expand All @@ -131,10 +109,9 @@ function(
*/
var init = function(root) {
root = $(root);
var userid = root.data('userid');

registerEventListeners(root, userid);
reloadContent(root, userid);
registerEventListeners(root);
reloadContent(root);
};

return {
Expand Down
1 change: 1 addition & 0 deletions blocks/starredcourses/amd/src/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
*
* Valid args are:
* int limit number of records to retrieve
* int offset the offset of records to retrieve
*
* @method getStarredCourses
* @param {object} args The request arguments
Expand Down
23 changes: 5 additions & 18 deletions blocks/starredcourses/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ public static function get_starred_courses_parameters() {
}

/**
* Get users starred courses appending additional course information like images.
* Get users starred courses.
*
* @param int $limit Limit
* @param int $limit Limit
* @param int $offset Offset
* @param int|null $userid The user's userid to fetch the favourite courses.
*
* @return array list of courses and warnings
*/
public static function get_starred_courses($limit, $offset, $userid = null) {
public static function get_starred_courses($limit, $offset) {
global $USER, $PAGE;

if (!$userid) {
$userid = $USER->id;
}
$userid = $USER->id;

$params = self::validate_parameters(self::get_starred_courses_parameters(), [
'limit' => $limit,
Expand Down Expand Up @@ -95,18 +93,7 @@ public static function get_starred_courses($limit, $offset, $userid = null) {
if (!isset($results[$courseid])) {
$exporter = new course_summary_exporter(get_course($courseid),
['context' => \context_course::instance($courseid)]);
$courseinlist = new \core_course_list_element(get_course($courseid));
foreach ($courseinlist->get_course_overviewfiles() as $file) {
if ($file->is_valid_image()) {
$url = new moodle_url("/pluginfile.php".'/'.$file->get_contextid(). '/'. $file->get_component(). '/'.
$file->get_filearea(). $file->get_filepath(). $file->get_filename());
$courseimage = $url->__toString();
}
}
$results[$courseid] = $exporter->export($output);
if (!empty($courseimage)) {
$results[$courseid]['courseimage'] = $courseimage;
}
}
}

Expand Down

0 comments on commit 8730c06

Please sign in to comment.