| 1 | 
           efrain | 
           1 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           2 | 
           //
  | 
        
        
            | 
            | 
           3 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           4 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           5 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           6 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           7 | 
           //
  | 
        
        
            | 
            | 
           8 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           9 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           10 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           11 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           12 | 
           //
  | 
        
        
            | 
            | 
           13 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           14 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           15 | 
              | 
        
        
            | 
            | 
           16 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * Javascript to initialise the starred courses block.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module block_starredcourses/main
  | 
        
        
            | 
            | 
           20 | 
            * @copyright   2018 Simey Lameze <simey@moodle.com>
  | 
        
        
            | 
            | 
           21 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           define(
  | 
        
        
            | 
            | 
           25 | 
           [
  | 
        
        
            | 
            | 
           26 | 
               'jquery',
  | 
        
        
            | 
            | 
           27 | 
               'core/notification',
  | 
        
        
            | 
            | 
           28 | 
               'block_starredcourses/repository',
  | 
        
        
            | 
            | 
           29 | 
               'core/pubsub',
  | 
        
        
            | 
            | 
           30 | 
               'core/templates',
  | 
        
        
            | 
            | 
           31 | 
               'core_course/events'
  | 
        
        
            | 
            | 
           32 | 
           ],
  | 
        
        
            | 
            | 
           33 | 
           function(
  | 
        
        
            | 
            | 
           34 | 
               $,
  | 
        
        
            | 
            | 
           35 | 
               Notification,
  | 
        
        
            | 
            | 
           36 | 
               Repository,
  | 
        
        
            | 
            | 
           37 | 
               PubSub,
  | 
        
        
            | 
            | 
           38 | 
               Templates,
  | 
        
        
            | 
            | 
           39 | 
               CourseEvents
  | 
        
        
            | 
            | 
           40 | 
           ) {
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               var SELECTORS = {
  | 
        
        
            | 
            | 
           43 | 
                   BLOCK_CONTAINER: '[data-region="starred-courses"]',
  | 
        
        
            | 
            | 
           44 | 
                   STARRED_COURSES_REGION_VIEW: '[data-region="starred-courses-view"]',
  | 
        
        
            | 
            | 
           45 | 
                   STARRED_COURSES_REGION: '[data-region="starred-courses-view-content"]'
  | 
        
        
            | 
            | 
           46 | 
               };
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /**
  | 
        
        
            | 
            | 
           49 | 
                * Render the starred courses.
  | 
        
        
            | 
            | 
           50 | 
                *
  | 
        
        
            | 
            | 
           51 | 
                * @method renderCourses
  | 
        
        
            | 
            | 
           52 | 
                * @param {object} root The root element for the starred view.
  | 
        
        
            | 
            | 
           53 | 
                * @param {array} courses containing array of returned courses.
  | 
        
        
            | 
            | 
           54 | 
                * @returns {promise} Resolved with HTML and JS strings
  | 
        
        
            | 
            | 
           55 | 
                */
  | 
        
        
            | 
            | 
           56 | 
               var renderCourses = function(root, courses) {
  | 
        
        
            | 
            | 
           57 | 
                   if (courses.length > 0) {
  | 
        
        
            | 
            | 
           58 | 
                       return Templates.render('core_course/view-cards', {
  | 
        
        
            | 
            | 
           59 | 
                           courses: courses
  | 
        
        
            | 
            | 
           60 | 
                       });
  | 
        
        
            | 
            | 
           61 | 
                   } else {
  | 
        
        
            | 
            | 
           62 | 
                       var nocoursesimg = root.find(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg');
  | 
        
        
            | 
            | 
           63 | 
                       return Templates.render('block_starredcourses/no-courses', {
  | 
        
        
            | 
            | 
           64 | 
                           nocoursesimg: nocoursesimg
  | 
        
        
            | 
            | 
           65 | 
                       });
  | 
        
        
            | 
            | 
           66 | 
                   }
  | 
        
        
            | 
            | 
           67 | 
               };
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Fetch user's starred courses and reload the content of the block.
  | 
        
        
            | 
            | 
           71 | 
                *
  | 
        
        
            | 
            | 
           72 | 
                * @param {object} root The root element for the starred view.
  | 
        
        
            | 
            | 
           73 | 
                * @returns {promise} The updated content for the block.
  | 
        
        
            | 
            | 
           74 | 
                */
  | 
        
        
            | 
            | 
           75 | 
               var reloadContent = function(root) {
  | 
        
        
            | 
            | 
           76 | 
                   var content = root.find(SELECTORS.STARRED_COURSES_REGION);
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   var args = {
  | 
        
        
            | 
            | 
           79 | 
                       limit: 0,
  | 
        
        
            | 
            | 
           80 | 
                       offset: 0,
  | 
        
        
            | 
            | 
           81 | 
                   };
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   return Repository.getStarredCourses(args)
  | 
        
        
            | 
            | 
           84 | 
                       .then(function(courses) {
  | 
        
        
            | 
            | 
           85 | 
                           // Whether the course category should be displayed in the course item.
  | 
        
        
            | 
            | 
           86 | 
                           var showcoursecategory = $(SELECTORS.BLOCK_CONTAINER).data('displaycoursecategory');
  | 
        
        
            | 
            | 
           87 | 
                           courses = courses.map(function(course) {
  | 
        
        
            | 
            | 
           88 | 
                               course.showcoursecategory = showcoursecategory;
  | 
        
        
            | 
            | 
           89 | 
                               return course;
  | 
        
        
            | 
            | 
           90 | 
                           });
  | 
        
        
            | 
            | 
           91 | 
                           return renderCourses(root, courses);
  | 
        
        
            | 
            | 
           92 | 
                       }).then(function(html, js) {
  | 
        
        
            | 
            | 
           93 | 
                           return Templates.replaceNodeContents(content, html, js);
  | 
        
        
            | 
            | 
           94 | 
                       }).catch(Notification.exception);
  | 
        
        
            | 
            | 
           95 | 
               };
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
               /**
  | 
        
        
            | 
            | 
           98 | 
                * Register event listeners for the block.
  | 
        
        
            | 
            | 
           99 | 
                *
  | 
        
        
            | 
            | 
           100 | 
                * @param {object} root The calendar root element
  | 
        
        
            | 
            | 
           101 | 
                */
  | 
        
        
            | 
            | 
           102 | 
               var registerEventListeners = function(root) {
  | 
        
        
            | 
            | 
           103 | 
                   PubSub.subscribe(CourseEvents.favourited, function() {
  | 
        
        
            | 
            | 
           104 | 
                       reloadContent(root);
  | 
        
        
            | 
            | 
           105 | 
                   });
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
                   PubSub.subscribe(CourseEvents.unfavorited, function() {
  | 
        
        
            | 
            | 
           108 | 
                       reloadContent(root);
  | 
        
        
            | 
            | 
           109 | 
                   });
  | 
        
        
            | 
            | 
           110 | 
               };
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
               /**
  | 
        
        
            | 
            | 
           113 | 
                * Initialise all of the modules for the starred courses block.
  | 
        
        
            | 
            | 
           114 | 
                *
  | 
        
        
            | 
            | 
           115 | 
                * @param {object} root The root element for the block.
  | 
        
        
            | 
            | 
           116 | 
                */
  | 
        
        
            | 
            | 
           117 | 
               var init = function(root) {
  | 
        
        
            | 
            | 
           118 | 
                   root = $(root);
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   registerEventListeners(root);
  | 
        
        
            | 
            | 
           121 | 
                   reloadContent(root);
  | 
        
        
            | 
            | 
           122 | 
               };
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               return {
  | 
        
        
            | 
            | 
           125 | 
                   init: init
  | 
        
        
            | 
            | 
           126 | 
               };
  | 
        
        
            | 
            | 
           127 | 
           });
  |