[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/amd/src/ -> grading_navigation_user_info.js (source)

   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 controller for the "User summary" panel at the top of the page.
  18   *
  19   * @module     mod_assign/grading_navigation_user_info
  20   * @package    mod_assign
  21   * @class      UserInfo
  22   * @copyright  2016 Damyon Wiese <damyon@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      3.1
  25   */
  26  define(['jquery', 'core/notification', 'core/ajax', 'core/templates'], function($, notification, ajax, templates) {
  27  
  28      /**
  29       * UserInfo class.
  30       *
  31       * @class UserInfo
  32       * @param {String} selector The selector for the page region containing the user navigation.
  33       */
  34      var UserInfo = function(selector) {
  35          this._regionSelector = selector;
  36          this._region = $(selector);
  37          this._userCache = {};
  38  
  39          $(document).on('user-changed', this._refreshUserInfo.bind(this));
  40      };
  41  
  42      /** @type {String} Selector for the page region containing the user navigation. */
  43      UserInfo.prototype._regionSelector = null;
  44  
  45      /** @type {Array} Cache of user info contexts. */
  46      UserInfo.prototype._userCache = null;
  47  
  48      /** @type {JQuery} JQuery node for the page region containing the user navigation. */
  49      UserInfo.prototype._region = null;
  50  
  51      /** @type {Integer} Remember the last user id to prevent unnessecary reloads. */
  52      UserInfo.prototype._lastUserId = 0;
  53  
  54      /**
  55       * Get the assignment id
  56       *
  57       * @private
  58       * @method _getAssignmentId
  59       * @return {Integer} assignment id
  60       */
  61      UserInfo.prototype._getAssignmentId = function() {
  62          return this._region.attr('data-assignmentid');
  63      };
  64  
  65      /**
  66       * Get the user context - re-render the template in the page.
  67       *
  68       * @private
  69       * @method _refreshUserInfo
  70       * @param {Event} event
  71       * @param {Number} userid
  72       */
  73      UserInfo.prototype._refreshUserInfo = function(event, userid) {
  74          var promise = $.Deferred();
  75  
  76          // Skip reloading if it is the same user.
  77          if (this._lastUserId == userid) {
  78              return;
  79          }
  80          this._lastUserId = userid;
  81  
  82          // First insert the loading template.
  83          templates.render('mod_assign/loading', {}).done(function(html, js) {
  84              // Update the page.
  85              this._region.fadeOut("fast", function() {
  86                  templates.replaceNodeContents(this._region, html, js);
  87                  this._region.fadeIn("fast");
  88              }.bind(this));
  89  
  90              if (userid < 0) {
  91                  // Render the template.
  92                  templates.render('mod_assign/grading_navigation_no_users', {}).done(function(html, js) {
  93                      // Update the page.
  94                      this._region.fadeOut("fast", function() {
  95                          templates.replaceNodeContents(this._region, html, js);
  96                          this._region.fadeIn("fast");
  97                      }.bind(this));
  98                  }.bind(this)).fail(notification.exception);
  99                  return;
 100              }
 101  
 102              if (typeof this._userCache[userid] !== "undefined") {
 103                  promise.resolve(this._userCache[userid]);
 104              } else {
 105                  // Load context from ajax.
 106                  var assignmentId = this._getAssignmentId();
 107                  var requests = ajax.call([{
 108                      methodname: 'mod_assign_get_participant',
 109                      args: {
 110                          userid: userid,
 111                          assignid: assignmentId,
 112                          embeduser: true
 113                      }
 114                  }]);
 115  
 116                  requests[0].done(function(participant) {
 117                      if (!participant.hasOwnProperty('id')) {
 118                          promise.reject('No users');
 119                      } else {
 120                          this._userCache[userid] = participant;
 121                          promise.resolve(this._userCache[userid]);
 122                      }
 123                  }.bind(this)).fail(notification.exception);
 124              }
 125  
 126              promise.done(function(context) {
 127                  var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(','),
 128                      identity = [];
 129                  // Render the template.
 130                  context.courseid = $('[data-region="grading-navigation-panel"]').attr('data-courseid');
 131  
 132                  if (context.user) {
 133                      // Build a string for the visible identity fields listed in showuseridentity config setting.
 134                      $.each(identityfields, function(i, k) {
 135                          if (typeof context.user[k] !== 'undefined' && context.user[k] !== '') {
 136                              context.hasidentity = true;
 137                              identity.push(context.user[k]);
 138                          }
 139                      });
 140                      context.identity = identity.join(', ');
 141  
 142                      // Add profile image url to context.
 143                      if (context.user.profileimageurl) {
 144                          context.profileimageurl = context.user.profileimageurl;
 145                      }
 146                  }
 147  
 148                  templates.render('mod_assign/grading_navigation_user_summary', context).done(function(html, js) {
 149                      // Update the page.
 150                      this._region.fadeOut("fast", function() {
 151                          templates.replaceNodeContents(this._region, html, js);
 152                          this._region.fadeIn("fast");
 153                      }.bind(this));
 154                  }.bind(this)).fail(notification.exception);
 155              }.bind(this)).fail(function() {
 156                  // Render the template.
 157                  templates.render('mod_assign/grading_navigation_no_users', {}).done(function(html, js) {
 158                      // Update the page.
 159                      this._region.fadeOut("fast", function() {
 160                          templates.replaceNodeContents(this._region, html, js);
 161                          this._region.fadeIn("fast");
 162                      }.bind(this));
 163                  }.bind(this)).fail(notification.exception);
 164              }
 165              .bind(this));
 166          }.bind(this)).fail(notification.exception);
 167      };
 168  
 169      return UserInfo;
 170  });


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1