admin/tool/lp/amd/src/user_competency_info.js

  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. * Module to refresh a user competency summary in a page.
  17. *
  18. * @module tool_lp/user_competency_info
  19. * @copyright 2015 Damyon Wiese
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/notification', 'core/ajax', 'core/templates'], function($, notification, ajax, templates) {
  23. /**
  24. * Info
  25. *
  26. * @param {JQuery} rootElement Selector to replace when the information needs updating.
  27. * @param {Number} competencyId The id of the competency.
  28. * @param {Number} userId The id of the user.
  29. * @param {Number} planId The id of the plan.
  30. * @param {Number} courseId The id of the course.
  31. * @param {Boolean} displayuser If we should display the user info.
  32. */
  33. var Info = function(rootElement, competencyId, userId, planId, courseId, displayuser) {
  34. this._rootElement = rootElement;
  35. this._competencyId = competencyId;
  36. this._userId = userId;
  37. this._planId = planId;
  38. this._courseId = courseId;
  39. this._valid = true;
  40. this._displayuser = (typeof displayuser !== 'undefined') ? displayuser : false;
  41. if (this._planId) {
  42. this._methodName = 'tool_lp_data_for_user_competency_summary_in_plan';
  43. this._args = {competencyid: this._competencyId, planid: this._planId};
  44. this._templateName = 'tool_lp/user_competency_summary_in_plan';
  45. } else if (this._courseId) {
  46. this._methodName = 'tool_lp_data_for_user_competency_summary_in_course';
  47. this._args = {userid: this._userId, competencyid: this._competencyId, courseid: this._courseId};
  48. this._templateName = 'tool_lp/user_competency_summary_in_course';
  49. } else {
  50. this._methodName = 'tool_lp_data_for_user_competency_summary';
  51. this._args = {userid: this._userId, competencyid: this._competencyId};
  52. this._templateName = 'tool_lp/user_competency_summary';
  53. }
  54. };
  55. /**
  56. * Reload the info for this user competency.
  57. *
  58. * @method reload
  59. */
  60. Info.prototype.reload = function() {
  61. var self = this,
  62. promises = [];
  63. if (!this._valid) {
  64. return;
  65. }
  66. promises = ajax.call([{
  67. methodname: this._methodName,
  68. args: this._args
  69. }]);
  70. promises[0].done(function(context) {
  71. // Check if we should also the user info.
  72. if (self._displayuser) {
  73. context.displayuser = true;
  74. }
  75. templates.render(self._templateName, context).done(function(html, js) {
  76. templates.replaceNode(self._rootElement, html, js);
  77. }).fail(notification.exception);
  78. }).fail(notification.exception);
  79. };
  80. /** @property {JQuery} The root element to replace in the DOM. */
  81. Info.prototype._rootElement = null;
  82. /** @property {Number} The id of the course. */
  83. Info.prototype._courseId = null;
  84. /** @property {Boolean} Is this module valid? */
  85. Info.prototype._valid = null;
  86. /** @property {Number} The id of the plan. */
  87. Info.prototype._planId = null;
  88. /** @property {Number} The id of the competency. */
  89. Info.prototype._competencyId = null;
  90. /** @property {Number} The id of the user. */
  91. Info.prototype._userId = null;
  92. /** @property {String} The method name to load the data. */
  93. Info.prototype._methodName = null;
  94. /** @property {Object} The arguments to load the data. */
  95. Info.prototype._args = null;
  96. /** @property {String} The template to reload the fragment. */
  97. Info.prototype._templateName = null;
  98. /** @property {Boolean} If we should display the user info? */
  99. Info.prototype._displayuser = false;
  100. return /** @alias module:tool_lp/user_competency_info */ Info;
  101. });