Merge branch 'MDL-49499-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 7 Apr 2015 15:29:15 +0000 (17:29 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 7 Apr 2015 15:29:15 +0000 (17:29 +0200)
1  2 
lib/db/services.php
user/externallib.php
user/profile.php
version.php

@@@ -503,15 -513,15 +503,24 @@@ $functions = array
          'capabilities'  => '',
      ),
  
 +    'core_user_view_user_list' => array(
 +        'classname'     => 'core_user_external',
 +        'methodname'    => 'view_user_list',
 +        'classpath'     => 'user/externallib.php',
 +        'description'   => 'Simulates the web-interface view of user/index.php (triggering events).',
 +        'type'          => 'write',
 +        'capabilities'  => 'moodle/course:viewparticipants',
 +    ),
 +
+     'core_user_view_user_profile' => array(
+         'classname'     => 'core_user_external',
+         'methodname'    => 'view_user_profile',
+         'classpath'     => 'user/externallib.php',
+         'description'   => 'Simulates the web-interface view of user/view.php and user/profile.php (triggering events).',
+         'type'          => 'write',
+         'capabilities'  => 'moodle/user:viewdetails',
+     ),
      // === enrol related functions ===
  
      'core_enrol_get_enrolled_users_with_capability' => array(
@@@ -1101,11 -1077,7 +1110,12 @@@ $services = array
              'core_course_view_course',
              'core_completion_get_activities_completion_status',
              'core_notes_get_course_notes',
 +            'core_completion_get_course_completion_status',
 +            'core_user_view_user_list',
 +            'core_message_mark_message_read',
 +            'core_notes_view_notes',
 +            'mod_forum_view_forum_discussion',
+             'core_user_view_user_profile',
              ),
          'enabled' => 0,
          'restrictedusers' => 0,
@@@ -1243,81 -1243,110 +1243,185 @@@ class core_user_external extends extern
          );
      }
  
 +    /**
 +     * Returns description of method parameters
 +     *
 +     * @return external_function_parameters
 +     * @since Moodle 2.9
 +     */
 +    public static function view_user_list_parameters() {
 +        return new external_function_parameters(
 +            array(
 +                'courseid' => new external_value(PARAM_INT, 'id of the course, 0 for site')
 +            )
 +        );
 +    }
 +
 +    /**
 +     * Simulate the /user/index.php web interface page triggering events
 +     *
 +     * @param int $courseid id of course
 +     * @return array of warnings and status result
 +     * @since Moodle 2.9
 +     * @throws moodle_exception
 +     */
 +    public static function view_user_list($courseid) {
 +        global $CFG;
 +        require_once($CFG->dirroot . "/user/lib.php");
 +
 +        $params = self::validate_parameters(self::view_user_list_parameters(),
 +                                            array(
 +                                                'courseid' => $courseid
 +                                            ));
 +
 +        $warnings = array();
 +
 +        if (empty($params['courseid'])) {
 +            $params['courseid'] = SITEID;
 +        }
 +
 +        $course = get_course($params['courseid']);
 +
 +        if ($course->id == SITEID) {
 +            $context = context_system::instance();
 +        } else {
 +            $context = context_course::instance($course->id);
 +        }
 +        self::validate_context($context);
 +
 +        if ($course->id == SITEID) {
 +            require_capability('moodle/site:viewparticipants', $context);
 +        } else {
 +            require_capability('moodle/course:viewparticipants', $context);
 +        }
 +
 +        user_list_view($course, $context);
 +
 +        $result = array();
 +        $result['status'] = true;
 +        $result['warnings'] = $warnings;
 +        return $result;
 +    }
 +
 +    /**
 +     * Returns description of method result value
 +     *
 +     * @return external_description
 +     * @since Moodle 2.9
 +     */
 +    public static function view_user_list_returns() {
 +        return new external_single_structure(
 +            array(
 +                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 +                'warnings' => new external_warnings()
 +            )
 +        );
 +    }
 +
+     /**
+      * Returns description of method parameters
+      *
+      * @return external_function_parameters
+      * @since Moodle 2.9
+      */
+     public static function view_user_profile_parameters() {
+         return new external_function_parameters(
+             array(
+                 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED),
+                 'courseid' => new external_value(PARAM_INT, 'id of the course, default site course', VALUE_DEFAULT, 0)
+             )
+         );
+     }
+     /**
+      * Simulate the /user/index.php and /user/profile.php web interface page triggering events
+      *
+      * @param int $userid id of user
+      * @param int $courseid id of course
+      * @return array of warnings and status result
+      * @since Moodle 2.9
+      * @throws moodle_exception
+      */
+     public static function view_user_profile($userid, $courseid = 0) {
+         global $CFG, $USER;
+         require_once($CFG->dirroot . "/user/profile/lib.php");
+         $params = self::validate_parameters(self::view_user_profile_parameters(),
+                                             array(
+                                                 'userid' => $userid,
+                                                 'courseid' => $courseid
+                                             ));
+         $warnings = array();
+         if (empty($params['userid'])) {
+             $params['userid'] = $USER->id;
+         }
+         if (empty($params['courseid'])) {
+             $params['courseid'] = SITEID;
+         }
+         $course = get_course($params['courseid']);
+         $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
+         if ($user->deleted) {
+             throw new moodle_exception('userdeleted');
+         }
+         if (isguestuser($user)) {
+             // Can not view profile of guest - thre is nothing to see there.
+             throw new moodle_exception('invaliduserid');
+         }
+         if ($course->id == SITEID) {
+             $coursecontext = context_system::instance();;
+         } else {
+             $coursecontext = context_course::instance($course->id);
+         }
+         self::validate_context($coursecontext);
+         $currentuser = $USER->id == $user->id;
+         $usercontext = context_user::instance($user->id);
+         if (!$currentuser and
+                 !has_capability('moodle/user:viewdetails', $coursecontext) and
+                 !has_capability('moodle/user:viewdetails', $usercontext)) {
+             throw new moodle_exception('cannotviewprofile');
+         }
+         // Case like user/profile.php.
+         if ($course->id == SITEID) {
+             profile_view($user, $usercontext);
+         } else {
+             // Case like user/view.php.
+             if (!$currentuser and !is_enrolled($coursecontext, $user->id)) {
+                 throw new moodle_exception('notenrolledprofile');
+             }
+             profile_view($user, $coursecontext, $course);
+         }
+         $result = array();
+         $result['status'] = true;
+         $result['warnings'] = $warnings;
+         return $result;
+     }
+     /**
+      * Returns description of method result value
+      *
+      * @return external_description
+      * @since Moodle 2.9
+      */
+     public static function view_user_profile_returns() {
+         return new external_single_structure(
+             array(
+                 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+                 'warnings' => new external_warnings()
+             )
+         );
+     }
  }
  
   /**
Simple merge
diff --cc version.php
@@@ -29,7 -29,7 +29,7 @@@
  
  defined('MOODLE_INTERNAL') || die();
  
- $version  = 2015040700.03;              // YYYYMMDD      = weekly release date of this DEV branch.
 -$version  = 2015040200.01;              // YYYYMMDD      = weekly release date of this DEV branch.
++$version  = 2015040200.04;              // YYYYMMDD      = weekly release date of this DEV branch.
                                          //         RR    = release increments - 00 in DEV branches.
                                          //           .XX = incremental changes.