MDL-48493 admin: Add icon to repeat permission check in plugin installer
[moodle.git] / admin / tool / installaddon / yui / permcheck / permcheck.js
CommitLineData
ddab904b
DM
1/**
2 * Check for write permission for the selected plugin type
3 *
4 * @module moodle-tool_installaddon-permcheck
5 * @author David Mudrak <david@moodle.com>
6 */
7YUI.add('moodle-tool_installaddon-permcheck', function(Y) {
8
9 M.tool_installaddon = M.tool_installaddon || {};
10
11 /**
12 * @class permcheck
13 * @static
14 */
15 M.tool_installaddon.permcheck = {
16
17 /**
18 * @method init
19 * @param {Object} config Configuration passed from the PHP
20 */
21 init : function(config) {
22 this.config = config;
23 var plugintypesel = Y.one('#tool_installaddon_installfromzip_plugintype');
24 if (plugintypesel) {
1f6a9391
DM
25 plugintypesel.on('change', function() {
26 this.check_for_permission(plugintypesel);
27 }, this);
28 this.repeat_permcheck_icon = Y.Node.create('<div><a href="#repeat-permcheck"><img src="' + M.util.image_url('i/reload') + '" /> ' +
29 M.util.get_string('permcheckrepeat', 'tool_installaddon') + '</a></div>');
30 this.repeat_permcheck_icon.one('a').on('click', function(e) {
31 e.preventDefault();
32 this.check_for_permission(plugintypesel);
33 }, this);
ddab904b
DM
34 }
35 },
36
37 /**
38 * @method check_for_permission
1f6a9391 39 * @param {Node} plugintypesel Plugin type selector node
ddab904b 40 */
1f6a9391
DM
41 check_for_permission : function(plugintypesel) {
42 var plugintype = plugintypesel.get('value');
43
ddab904b
DM
44 if (plugintype == '') {
45 return;
46 }
47 Y.log('Selected plugin type: ' + plugintype, 'debug', 'moodle-tool_installaddon-permcheck');
48 Y.io(this.config.permcheckurl, {
49 'method' : 'GET',
50 'data' : {
51 'sesskey' : M.cfg.sesskey,
52 'plugintype' : plugintype
53 },
1f6a9391 54 'context' : this,
ddab904b
DM
55 'on' : {
56 'start' : function(transid, args) {
1f6a9391 57 this.showresult(M.util.get_string('permcheckprogress', 'tool_installaddon'), 'progress');
ddab904b
DM
58 },
59 'success': function(transid, outcome, args) {
60 var response;
61 try {
62 response = Y.JSON.parse(outcome.responseText);
63 if (response.error) {
64 Y.log(response.error, 'error', 'moodle-tool_installaddon-permcheck');
1f6a9391 65 this.showresult(M.util.get_string('permcheckerror', 'tool_installaddon', response), 'error');
ddab904b 66 } else if (response.path && response.writable == 1) {
1f6a9391 67 this.showresult(M.util.get_string('permcheckresultyes', 'tool_installaddon', response), 'success');
ddab904b 68 } else if (response.path && response.writable == 0) {
1f6a9391 69 this.showresult(M.util.get_string('permcheckresultno', 'tool_installaddon', response), 'error');
ddab904b
DM
70 } else {
71 Y.log(response, 'debug', 'moodle-tool_installaddon-permcheck');
1f6a9391 72 this.showresult(M.util.get_string('permcheckerror', 'tool_installaddon', response), 'error');
ddab904b
DM
73 }
74
75 } catch (e) {
76 Y.log(e, 'error', 'moodle-tool_installaddon-permcheck');
1f6a9391 77 this.showresult(M.util.get_string('permcheckerror', 'tool_installaddon'), 'error');
ddab904b
DM
78 }
79 },
80 'failure': function(transid, outcome, args) {
81 Y.log(outcome.statusText, 'error', 'moodle-tool_installaddon-permcheck');
1f6a9391 82 this.showresult(M.util.get_string('permcheckerror', 'tool_installaddon'));
ddab904b
DM
83 }
84 }
85 });
86 },
87
1f6a9391
DM
88 /**
89 * @method showresult
90 * @param {String} msg Message to display
91 * @param {String} status Message status
92 */
93 showresult : function(msg, status) {
94 var resultline = Y.one('#tool_installaddon_installfromzip_permcheck');
95 if (resultline) {
96 if (status === 'success') {
97 resultline.setHTML('<span class="success"><img src="' + M.util.image_url('i/valid') + '" /> ' +
98 msg + '</span>');
99 } else if (status === 'progress') {
100 resultline.setHTML('<span class="progress"><img src="' + M.cfg.loadingicon + '" /> ' +
101 msg + '</span>');
102 } else {
103 resultline.setHTML('<span class="error"><img src="' + M.util.image_url('i/invalid') + '" /> ' +
104 msg + '</span>').append(this.repeat_permcheck_icon);
105 }
106 }
107 },
108
109 /**
110 * @property
111 * @type {Y.Node}
112 */
113 repeat_permcheck_icon : null,
114
ddab904b
DM
115 /**
116 * @property
117 * @type {Object}
118 */
119 config : null
120 };
121
122}, '@VERSION@', {
123 requires:['node', 'event', 'io-base']
124});