| 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 |  * Module to open user competency plan in popup
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     tool_lp/user_competency_plan_popup
 | 
        
           |  |  | 20 |  * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
 | 
        
           |  |  | 21 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  */
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/templates', 'tool_lp/dialogue'],
 | 
        
           |  |  | 25 |        function($, notification, str, ajax, templates, Dialogue) {
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     /**
 | 
        
           |  |  | 28 |      * UserCompetencyPopup
 | 
        
           |  |  | 29 |      *
 | 
        
           |  |  | 30 |      * @param {String} regionSelector The regionSelector
 | 
        
           |  |  | 31 |      * @param {String} userCompetencySelector The userCompetencySelector
 | 
        
           |  |  | 32 |      * @param {Number} planId The plan ID
 | 
        
           |  |  | 33 |      */
 | 
        
           |  |  | 34 |     var UserCompetencyPopup = function(regionSelector, userCompetencySelector, planId) {
 | 
        
           |  |  | 35 |         this._regionSelector = regionSelector;
 | 
        
           |  |  | 36 |         this._userCompetencySelector = userCompetencySelector;
 | 
        
           |  |  | 37 |         this._planId = planId;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |         $(this._regionSelector).on('click', this._userCompetencySelector, this._handleClick.bind(this));
 | 
        
           |  |  | 40 |     };
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |     /**
 | 
        
           |  |  | 43 |      * Get the data from the closest TR and open the popup.
 | 
        
           |  |  | 44 |      *
 | 
        
           |  |  | 45 |      * @method _handleClick
 | 
        
           |  |  | 46 |      * @param {Event} e
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     UserCompetencyPopup.prototype._handleClick = function(e) {
 | 
        
           |  |  | 49 |         e.preventDefault();
 | 
        
           |  |  | 50 |         var tr = $(e.target).closest('tr');
 | 
        
           |  |  | 51 |         var competencyId = $(tr).data('competencyid');
 | 
        
           |  |  | 52 |         var userId = $(tr).data('userid');
 | 
        
           |  |  | 53 |         var planId = this._planId;
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |         var requests = ajax.call([{
 | 
        
           |  |  | 56 |             methodname: 'tool_lp_data_for_user_competency_summary_in_plan',
 | 
        
           |  |  | 57 |             args: {competencyid: competencyId, planid: planId},
 | 
        
           |  |  | 58 |             done: this._contextLoaded.bind(this),
 | 
        
           |  |  | 59 |             fail: notification.exception
 | 
        
           |  |  | 60 |         }]);
 | 
        
           |  |  | 61 |         // Log the user competency viewed in plan event.
 | 
        
           |  |  | 62 |         requests[0].then(function(result) {
 | 
        
           |  |  | 63 |             var eventMethodName = 'core_competency_user_competency_viewed_in_plan';
 | 
        
           |  |  | 64 |             // Trigger core_competency_user_competency_plan_viewed event instead if plan is already completed.
 | 
        
           |  |  | 65 |             if (result.plan.iscompleted) {
 | 
        
           |  |  | 66 |                 eventMethodName = 'core_competency_user_competency_plan_viewed';
 | 
        
           |  |  | 67 |             }
 | 
        
           |  |  | 68 |             return ajax.call([{
 | 
        
           |  |  | 69 |                 methodname: eventMethodName,
 | 
        
           |  |  | 70 |                 args: {competencyid: competencyId, userid: userId, planid: planId}
 | 
        
           |  |  | 71 |             }])[0];
 | 
        
           |  |  | 72 |         }).catch(notification.exception);
 | 
        
           |  |  | 73 |     };
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * We loaded the context, now render the template.
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @method _contextLoaded
 | 
        
           |  |  | 79 |      * @param {Object} context
 | 
        
           |  |  | 80 |      */
 | 
        
           |  |  | 81 |     UserCompetencyPopup.prototype._contextLoaded = function(context) {
 | 
        
           |  |  | 82 |         var self = this;
 | 
        
           |  |  | 83 |         templates.render('tool_lp/user_competency_summary_in_plan', context).done(function(html, js) {
 | 
        
           |  |  | 84 |             str.get_string('usercompetencysummary', 'report_competency').done(function(title) {
 | 
        
           |  |  | 85 |                 (new Dialogue(title, html, templates.runTemplateJS.bind(templates, js), self._refresh.bind(self), true));
 | 
        
           |  |  | 86 |             }).fail(notification.exception);
 | 
        
           |  |  | 87 |         }).fail(notification.exception);
 | 
        
           |  |  | 88 |     };
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |     /**
 | 
        
           |  |  | 91 |      * Refresh the page.
 | 
        
           |  |  | 92 |      *
 | 
        
           |  |  | 93 |      * @method _refresh
 | 
        
           |  |  | 94 |      */
 | 
        
           |  |  | 95 |     UserCompetencyPopup.prototype._refresh = function() {
 | 
        
           |  |  | 96 |         var planId = this._planId;
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         ajax.call([{
 | 
        
           |  |  | 99 |             methodname: 'tool_lp_data_for_plan_page',
 | 
        
           |  |  | 100 |             args: {planid: planId},
 | 
        
           |  |  | 101 |             done: this._pageContextLoaded.bind(this),
 | 
        
           |  |  | 102 |             fail: notification.exception
 | 
        
           |  |  | 103 |         }]);
 | 
        
           |  |  | 104 |     };
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     /**
 | 
        
           |  |  | 107 |      * We loaded the context, now render the template.
 | 
        
           |  |  | 108 |      *
 | 
        
           |  |  | 109 |      * @method _pageContextLoaded
 | 
        
           |  |  | 110 |      * @param {Object} context
 | 
        
           |  |  | 111 |      */
 | 
        
           |  |  | 112 |     UserCompetencyPopup.prototype._pageContextLoaded = function(context) {
 | 
        
           |  |  | 113 |         var self = this;
 | 
        
           |  |  | 114 |         templates.render('tool_lp/plan_page', context).done(function(html, js) {
 | 
        
           |  |  | 115 |             templates.replaceNode(self._regionSelector, html, js);
 | 
        
           |  |  | 116 |         }).fail(notification.exception);
 | 
        
           |  |  | 117 |     };
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /** @property {String} The selector for the region with the user competencies */
 | 
        
           |  |  | 120 |     UserCompetencyPopup.prototype._regionSelector = null;
 | 
        
           |  |  | 121 |     /** @property {String} The selector for the region with a single user competencies */
 | 
        
           |  |  | 122 |     UserCompetencyPopup.prototype._userCompetencySelector = null;
 | 
        
           |  |  | 123 |     /** @property {Number} The plan Id */
 | 
        
           |  |  | 124 |     UserCompetencyPopup.prototype._planId = null;
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |     return /** @alias module:tool_lp/user_competency_plan_popup */ UserCompetencyPopup;
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 | });
 |