| 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 |
* Policy actions.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_policy/policyactions
|
|
|
20 |
* @copyright 2018 Sara Arjona (sara@moodle.com)
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define([
|
|
|
24 |
'jquery',
|
|
|
25 |
'core/ajax',
|
|
|
26 |
'core/notification',
|
|
|
27 |
'core/modal',
|
|
|
28 |
], function($, Ajax, Notification, Modal) {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* PolicyActions class.
|
|
|
32 |
*
|
|
|
33 |
* @param {jQuery} root
|
|
|
34 |
*/
|
|
|
35 |
var PolicyActions = function(root) {
|
|
|
36 |
this.registerEvents(root);
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Register event listeners.
|
|
|
41 |
*
|
|
|
42 |
* @param {jQuery} root
|
|
|
43 |
*/
|
|
|
44 |
PolicyActions.prototype.registerEvents = function(root) {
|
|
|
45 |
root.on("click", function(e) {
|
|
|
46 |
e.preventDefault();
|
|
|
47 |
|
|
|
48 |
var versionid = $(this).data('versionid');
|
|
|
49 |
var behalfid = $(this).data('behalfid');
|
|
|
50 |
|
|
|
51 |
var params = {
|
|
|
52 |
'versionid': versionid,
|
|
|
53 |
'behalfid': behalfid
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
var request = {
|
|
|
57 |
methodname: 'tool_policy_get_policy_version',
|
|
|
58 |
args: params
|
|
|
59 |
};
|
|
|
60 |
|
|
|
61 |
var modalTitle = $.Deferred();
|
|
|
62 |
var modalBody = $.Deferred();
|
|
|
63 |
|
|
|
64 |
var modal = Modal.create({
|
|
|
65 |
title: modalTitle,
|
|
|
66 |
body: modalBody,
|
|
|
67 |
large: true,
|
|
|
68 |
removeOnClose: true,
|
|
|
69 |
show: true,
|
|
|
70 |
})
|
|
|
71 |
.catch(Notification.exception);
|
|
|
72 |
|
|
|
73 |
// Make the request now that the modal is configured.
|
|
|
74 |
var promises = Ajax.call([request]);
|
|
|
75 |
$.when(promises[0]).then(function(data) {
|
|
|
76 |
if (data.result.policy) {
|
|
|
77 |
modalTitle.resolve(data.result.policy.name);
|
|
|
78 |
modalBody.resolve(data.result.policy.content);
|
|
|
79 |
|
|
|
80 |
return data;
|
|
|
81 |
} else {
|
|
|
82 |
throw new Error(data.warnings[0].message);
|
|
|
83 |
}
|
|
|
84 |
}).catch(function(message) {
|
|
|
85 |
modal.then(function(modal) {
|
|
|
86 |
modal.hide();
|
|
|
87 |
|
|
|
88 |
return modal;
|
|
|
89 |
})
|
|
|
90 |
.catch(Notification.exception);
|
|
|
91 |
|
|
|
92 |
return Notification.addNotification({
|
|
|
93 |
message: message,
|
|
|
94 |
type: 'error'
|
|
|
95 |
});
|
|
|
96 |
});
|
|
|
97 |
});
|
|
|
98 |
|
|
|
99 |
};
|
|
|
100 |
|
|
|
101 |
return /** @alias module:tool_policy/policyactions */ {
|
|
|
102 |
// Public variables and functions.
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Initialise the actions helper.
|
|
|
106 |
*
|
|
|
107 |
* @method init
|
|
|
108 |
* @param {object} root
|
|
|
109 |
* @return {PolicyActions}
|
|
|
110 |
*/
|
|
|
111 |
'init': function(root) {
|
|
|
112 |
root = $(root);
|
|
|
113 |
return new PolicyActions(root);
|
|
|
114 |
}
|
|
|
115 |
};
|
|
|
116 |
});
|