MDL-63062 block_recentlyaccessedcourses: add starred icon
authorVíctor Déniz Falcón <victor@moodle.com>
Mon, 5 Nov 2018 23:51:49 +0000 (23:51 +0000)
committerVíctor Déniz Falcón <victor@moodle.com>
Mon, 5 Nov 2018 23:51:49 +0000 (23:51 +0000)
blocks/recentlyaccessedcourses/amd/build/main.min.js
blocks/recentlyaccessedcourses/amd/src/main.js
blocks/recentlyaccessedcourses/templates/view-cards.mustache
course/amd/build/repository.min.js
course/externallib.php
course/lib.php

index e6d471c..73692b5 100644 (file)
Binary files a/blocks/recentlyaccessedcourses/amd/build/main.min.js and b/blocks/recentlyaccessedcourses/amd/build/main.min.js differ
index 8c84aa8..70e679e 100644 (file)
@@ -27,13 +27,17 @@ define(
         'jquery',
         'core_course/repository',
         'core/templates',
-        'core/notification'
+        'core/notification',
+        'core/pubsub',
+        'core_course/events'
     ],
     function(
         $,
         CoursesRepository,
         Templates,
-        Notification
+        Notification,
+        PubSub,
+        CourseEvents
     ) {
 
         var SELECTORS = {
@@ -77,19 +81,20 @@ define(
         };
 
         /**
-         * Get and show the recent courses into the block.
+         * Fetch user's recently accessed courses and reload the content of the block.
          *
-         * @param {int} userid User from which the courses will be obtained
-         * @param {object} root The root element for the recentlyaccessedcourses block.
+         * @param {int} userid User whose courses will be shown
+         * @param {object} root The root element for the recentlyaccessedcourses view.
+         * @returns {promise} The updated content for the block.
          */
-        var init = function(userid, root) {
-            root = $(root);
+        var reloadContent = function(userid, root) {
+
             var recentcoursesViewRoot = root.find(SELECTORS.COURSES_VIEW);
             var recentcoursesViewContent = root.find(SELECTORS.COURSES_VIEW_CONTENT);
 
             var coursesPromise = getRecentCourses(userid, NUM_COURSES_TOTAL);
 
-            coursesPromise.then(function(courses) {
+            return coursesPromise.then(function(courses) {
                 var pagedContentPromise = renderCourses(recentcoursesViewRoot, courses);
 
                 pagedContentPromise.then(function(html, js) {
@@ -99,6 +104,35 @@ define(
             }).catch(Notification.exception);
         };
 
+        /**
+         * Register event listeners for the block.
+         *
+         * @param {int} userid User whose courses will be shown
+         * @param {object} root The root element for the recentlyaccessedcourses block.
+         */
+        var registerEventListeners = function(userid, root) {
+            PubSub.subscribe(CourseEvents.favourited, function() {
+                reloadContent(userid, root);
+            });
+
+            PubSub.subscribe(CourseEvents.unfavorited, function() {
+                reloadContent(userid, root);
+            });
+        };
+
+        /**
+         * Get and show the recent courses into the block.
+         *
+         * @param {int} userid User from which the courses will be obtained
+         * @param {object} root The root element for the recentlyaccessedcourses block.
+         */
+        var init = function(userid, root) {
+            root = $(root);
+
+            registerEventListeners(userid, root);
+            reloadContent(userid, root);
+        };
+
         return {
             init: init
         };
index b54c6c7..57dba61 100644 (file)
@@ -26,7 +26,8 @@
                 "name": "Assignment due 1",
                 "viewurl": "https://moodlesite/course/view.php?id=2",
                 "courseimageurl": "https://moodlesite/pluginfile/123/course/overviewfiles/123.jpg",
-                "fullname": "course 3"
+                "fullname": "course 3",
+                "isfavourite": true
             }
         ]
     }
 
 <div class="card-deck dashboard-card-deck" role="list">
     {{#courses}}
-        <a href="{{viewurl}}" title="{{fullname}}" class="card dashboard-card" role="list-item">
-            <div class="card-img-top dashboard-card-img" style='background-image: url("{{{courseimage}}}");'>
-            </div>
-            <div class="card-body pr-1 course-info-container">
-                <div class="d-flex">
-                    <div class="card-title d-inline-block text-truncate">
-                        {{{fullname}}}
+        <div class="card dashboard-card" role="listitem">
+            <a href="{{viewurl}}" title="{{fullname}}">
+                <div class="card-img-top dashboard-card-img" style='background-image: url("{{{courseimage}}}");'>
+                    <span class="sr-only">{{#str}}aria:courseimage, core_course{{/str}}</span>
+                    {{>core_course/favouriteicon }}
+                </div>
+                <div class="card-body pr-1 course-info-container">
+                    <div class="d-flex">
+                        <div class="card-title d-inline-block text-truncate">
+                            {{{fullname}}}
+                        </div>
                     </div>
                 </div>
-            </div>
-        </a>
+            </a>
+        </div>
     {{/courses}}
 </div>
\ No newline at end of file
index a402038..5ea972f 100644 (file)
Binary files a/course/amd/build/repository.min.js and b/course/amd/build/repository.min.js differ
index 3d35042..8d59976 100644 (file)
@@ -3951,7 +3951,8 @@ class core_course_external extends external_api {
         $recentcourses = array_map(function($course) use ($renderer) {
             context_helper::preload_from_record($course);
             $context = context_course::instance($course->id);
-            $exporter = new course_summary_exporter($course, ['context' => $context]);
+            $isfavourite = !empty($course->component);
+            $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => $isfavourite]);
             return $exporter->export($renderer);
         }, $courses);
 
index f28f29a..b4ed03b 100644 (file)
@@ -4548,7 +4548,7 @@ function course_get_recent_courses(int $userid = null, int $limit = 0, int $offs
     }
 
     $basefields = array('id', 'idnumber', 'summary', 'summaryformat', 'startdate', 'enddate', 'category',
-            'shortname', 'fullname', 'userid', 'timeaccess');
+            'shortname', 'fullname', 'timeaccess', 'component');
 
     $sort = trim($sort);
     if (empty($sort)) {
@@ -4571,9 +4571,16 @@ function course_get_recent_courses(int $userid = null, int $limit = 0, int $offs
 
     $sql = "SELECT $ctxfields, $coursefields
               FROM {course} c
-              JOIN {context} ctx ON ctx.contextlevel = :contextlevel
+              JOIN {context} ctx
+                   ON ctx.contextlevel = :contextlevel
                    AND ctx.instanceid = c.id
-              JOIN {user_lastaccess} ul ON ul.courseid = c.id
+              JOIN {user_lastaccess} ul
+                   ON ul.courseid = c.id
+         LEFT JOIN {favourite} f
+                   ON f.component = 'core_course'
+                   AND f.itemtype = 'courses'
+                   AND f.userid = ul.userid
+                   AND f.itemid = ul.courseid
              WHERE ul.userid = :userid
           $orderby";
     $params = ['userid' => $userid, 'contextlevel' => CONTEXT_COURSE];