Skip to content

Commit 370a24f

Browse files
committed
MOBILE-1465 addons: Hide not available options in user and courses
1 parent 5a57fdc commit 370a24f

File tree

6 files changed

+113
-13
lines changed

6 files changed

+113
-13
lines changed

www/addons/coursecompletion/services/coursecompletion.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ angular.module('mm.addons.coursecompletion')
106106
preSets = {
107107
cacheKey: getCompletionCacheKey(courseid, userid)
108108
};
109-
110109
return $mmSite.read('core_completion_get_course_completion_status', data, preSets).then(function(data) {
111110
if (data.completionstatus) {
112111
return data.completionstatus;
@@ -184,6 +183,24 @@ angular.module('mm.addons.coursecompletion')
184183
});
185184
};
186185

186+
/**
187+
* Returns whether or not the view course completion plugin is enabled for a certain user.
188+
*
189+
* @module mm.addons.coursecompletion
190+
* @ngdoc method
191+
* @name $mmaCourseCompletion#isPluginViewEnabledForCourse
192+
* @param {Number} courseId Course ID.
193+
* @param {Number} userId User ID.
194+
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
195+
*/
196+
self.isPluginViewEnabledForUser = function(courseId, userId) {
197+
return self.getCompletion(courseId, userId).then(function() {
198+
return true;
199+
}).catch(function() {
200+
return false;
201+
});
202+
};
203+
187204
/**
188205
* Returns whether or not the self completion is available in current site.
189206
*

www/addons/coursecompletion/services/handlers.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ angular.module('mm.addons.coursecompletion')
5555
* @return {Boolean} True if handler is enabled, false otherwise.
5656
*/
5757
self.isEnabledForUser = function(user, courseId) {
58-
return $mmaCourseCompletion.isPluginViewEnabledForCourse(courseId);
58+
return $mmaCourseCompletion.isPluginViewEnabledForCourse(courseId).then(function() {
59+
return $mmaCourseCompletion.isPluginViewEnabledForUser(courseId, user.id);
60+
});
5961
};
6062

6163
/**
@@ -126,7 +128,10 @@ angular.module('mm.addons.coursecompletion')
126128
if (accessData && accessData.type == mmCoursesAccessMethods.guest) {
127129
return false; // Not enabled for guests.
128130
}
129-
return $mmaCourseCompletion.isPluginViewEnabledForCourse(courseId);
131+
return $mmaCourseCompletion.isPluginViewEnabledForCourse(courseId).then(function() {
132+
// Check if the user can see his own report, teachers can't.
133+
return $mmaCourseCompletion.isPluginViewEnabledForUser(courseId);
134+
});
130135
};
131136

132137
/**

www/addons/grades/services/grades.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ angular.module('mm.addons.grades')
224224
});
225225
};
226226

227+
/**
228+
* Returns whether or not the grade addon is enabled for a certain user.
229+
*
230+
* @module mm.addons.grades
231+
* @ngdoc method
232+
* @name $mmaGrades#isPluginEnabledForUser
233+
* @param {Number} courseId Course ID.
234+
* @param {Number} userId User ID.
235+
* @param {String} [siteId] Site ID. If not defined, current site.
236+
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
237+
*/
238+
self.isPluginEnabledForUser = function(courseId, userId) {
239+
// We don't use the getGradesTable function to prevent formatting the table.
240+
var data = {
241+
courseid: courseId,
242+
userid: userId
243+
};
244+
return $mmSite.read('gradereport_user_get_grades_table', data, {}).then(function() {
245+
return true;
246+
}).catch(function() {
247+
return false;
248+
});
249+
};
250+
227251
/**
228252
* Get the grades for a certain course.
229253
* For now we only support gradereport_user_get_grades_table. It returns the complete grades table.

www/addons/grades/services/handlers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ angular.module('mm.addons.grades')
118118
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
119119
*/
120120
self.isEnabledForUser = function(user, courseId) {
121-
return $mmaGrades.isPluginEnabledForCourse(courseId);
121+
return $mmaGrades.isPluginEnabledForCourse(courseId).then(function() {
122+
return $mmaGrades.isPluginEnabledForUser(courseId, user.id);
123+
});
122124
};
123125

124126
/**

www/addons/notes/services/handlers.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ angular.module('mm.addons.notes')
2323
* @ngdoc service
2424
* @name $mmaNotesHandlers
2525
*/
26-
.factory('$mmaNotesHandlers', function($mmaNotes, $mmSite, $mmApp, $ionicModal, $mmUtil, mmCoursesAccessMethods) {
26+
.factory('$mmaNotesHandlers', function($mmaNotes, $mmSite, $mmApp, $ionicModal, $mmUtil, $q, mmCoursesAccessMethods) {
2727

2828
var self = {};
2929

@@ -52,11 +52,14 @@ angular.module('mm.addons.notes')
5252
*
5353
* @param {Object} user User to check.
5454
* @param {Number} courseId Course ID.
55-
* @return {Boolean} True if handler is enabled, false otherwise.
55+
* @return {Promise} Promise resolved with true if enabled, resolved with false otherwise.
5656
*/
5757
self.isEnabledForUser = function(user, courseId) {
5858
// Active course required.
59-
return courseId && user.id != $mmSite.getUserId();
59+
if (!courseId || user.id == $mmSite.getUserId()) {
60+
return $q.when(false);
61+
}
62+
return $mmaNotes.isPluginAddNoteEnabledForCourse(courseId);
6063
};
6164

6265
/**
@@ -161,7 +164,7 @@ angular.module('mm.addons.notes')
161164
if (accessData && accessData.type == mmCoursesAccessMethods.guest) {
162165
return false; // Not enabled for guests.
163166
}
164-
return true;
167+
return $mmaNotes.isPluginViewNotesEnabledForCourse(courseId);
165168
};
166169

167170
/**

www/addons/notes/services/notes.js

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ angular.module('mm.addons.notes')
4646
"notes[0][text]": noteText,
4747
"notes[0][format]": 1
4848
};
49-
return $mmSite.write('core_notes_create_notes', data);
49+
return $mmSite.write('core_notes_create_notes', data).then(function(response) {
50+
if (!response || !response.length) {
51+
return $q.reject();
52+
}
53+
54+
if (response[0].noteid == -1) {
55+
return $q.reject(response[0].errormessage);
56+
}
57+
return response;
58+
});
5059
};
5160

5261
/**
@@ -61,8 +70,6 @@ angular.module('mm.addons.notes')
6170
* @return {Boolean}
6271
*/
6372
self.isPluginAddNoteEnabled = function() {
64-
var infos;
65-
6673
if (!$mmSite.isLoggedIn()) {
6774
return false;
6875
} else if (!$mmSite.canUseAdvancedFeature('enablenotes')) {
@@ -74,6 +81,33 @@ angular.module('mm.addons.notes')
7481
return true;
7582
};
7683

84+
/**
85+
* Returns whether or not the add note plugin is enabled for a certain course.
86+
*
87+
* @module mm.addons.notes
88+
* @ngdoc method
89+
* @name $mmaNotes#isPluginAddNoteEnabledForCourse
90+
* @param {Number} courseId ID of the course.
91+
* @return {Promise} Promise resolved with true if enabled, resolved with false otherwise.
92+
*/
93+
self.isPluginAddNoteEnabledForCourse = function(courseId) {
94+
// The only way to detect if it's enabled is to perform a WS call.
95+
// We use an invalid user ID (-1) and check if the error is due to permissions or to invalid ID.
96+
var data = {
97+
"notes[0][userid]" : -1,
98+
"notes[0][courseid]": courseId,
99+
"notes[0][publishstate]": 'personal',
100+
"notes[0][text]": '',
101+
"notes[0][format]": 1
102+
};
103+
return $mmSite.read('core_notes_create_notes', data).then(function() {
104+
// User can add notes.
105+
return true;
106+
}).catch(function() {
107+
return false;
108+
});
109+
};
110+
77111
/**
78112
* Returns whether or not the read notes plugin is enabled for the current site.
79113
*
@@ -86,8 +120,6 @@ angular.module('mm.addons.notes')
86120
* @return {Boolean}
87121
*/
88122
self.isPluginViewNotesEnabled = function() {
89-
var infos;
90-
91123
if (!$mmSite.isLoggedIn()) {
92124
return false;
93125
} else if (!$mmSite.canUseAdvancedFeature('enablenotes')) {
@@ -99,6 +131,23 @@ angular.module('mm.addons.notes')
99131
return true;
100132
};
101133

134+
/**
135+
* Returns whether or not the read notes plugin is enabled for a certain course.
136+
*
137+
* @module mm.addons.notes
138+
* @ngdoc method
139+
* @name $mmaNotes#isPluginViewNotesEnabledForCourse
140+
* @param {Number} courseId ID of the course.
141+
* @return {Promise} Promise resolved with true if enabled, resolved with false otherwise.
142+
*/
143+
self.isPluginViewNotesEnabledForCourse = function(courseId) {
144+
return self.getNotes(courseId).then(function() {
145+
return true;
146+
}).catch(function() {
147+
return false;
148+
});
149+
};
150+
102151
/**
103152
* Get users notes for a certain site, course and personal notes.
104153
*

0 commit comments

Comments
 (0)