Permalink
Browse files

MDL-26249 external enrol functions: get list of courses user is enrol…

…led in
  • Loading branch information...
1 parent 4452ed6 commit 3364dbb5a477e4362ddcc0afe1d485152fcad003 @skodak skodak committed Apr 23, 2011
Showing with 78 additions and 0 deletions.
  1. +69 −0 enrol/externallib.php
  2. +9 −0 lib/db/services.php
View
69 enrol/externallib.php
@@ -34,6 +34,7 @@
class moodle_enrol_external extends external_api {
+
/**
* Returns description of method parameters
* @return external_function_parameters
@@ -138,6 +139,74 @@ public static function get_enrolled_users_returns() {
* Returns description of method parameters
* @return external_function_parameters
*/
+ public static function get_users_courses_parameters() {
+ return new external_function_parameters(
+ array(
+ 'userid' => new external_value(PARAM_INT, 'user id'),
+ )
+ );
+ }
+
+ /**
+ * Get list of courses user is enrolled in (only active enrolments are returned).
+ *
+ * Please note the current user must be able to access the course, otherwise the course is not included.
+ *
+ * @param int $userid
+ * @return array of courses
+ */
+ public static function get_users_courses($userid) {
+ global $USER;
+
+ // Do basic automatic PARAM checks on incoming data, using params description
+ // If any problems are found then exceptions are thrown with helpful error messages
+ $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid));
+
+ $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible');
+ $result = array();
+
+ foreach ($courses as $course) {
+ $context = get_context_instance(CONTEXT_COURSE, $course->id);
+ try {
+ self::validate_context($context);
+ } catch (Exception $e) {
+ // current user can not access this course, sorry we can not disclose who is enrolled in this course!
+ continue;
+ }
+ if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
+ // we need capability to view participants
+ continue;
+ }
+
+ $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Returns description of method result value
+ * @return external_description
+ */
+ public static function get_users_courses_returns() {
+ return new external_multiple_structure(
+ new external_single_structure(
+ array(
+ 'id' => new external_value(PARAM_INT, 'id of course'),
+ 'shortname' => new external_value(PARAM_RAW, 'short name of course'),
+ 'fullname' => new external_value(PARAM_RAW, 'long name of course'),
+ 'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
+ 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'),
+ )
+ )
+ );
+ }
+
+
+ /**
+ * Returns description of method parameters
+ * @return external_function_parameters
+ */
public static function role_assign_parameters() {
return new external_function_parameters(
array(
View
9 lib/db/services.php
@@ -159,6 +159,15 @@
moodle/role:review, moodle/site:accessallgroups, moodle/course:enrolreview',
),
+ 'moodle_enrol_get_users_courses' => array(
+ 'classname' => 'moodle_enrol_external',
+ 'methodname' => 'get_users_courses',
+ 'classpath' => 'enrol/externallib.php',
+ 'description' => 'Get list of courses user is enrolled in',
+ 'type' => 'read',
+ 'capabilities'=> 'moodle/course:viewparticipants',
+ ),
+
'moodle_role_assign' => array(
'classname' => 'moodle_enrol_external',
'methodname' => 'role_assign',

0 comments on commit 3364dbb

Please sign in to comment.