Moodle
  1. Moodle
  2. MDL-26250

Create a web service function that enrols users to a certain course

    Details

    • Testing Instructions:
      Hide

      This web service function is for developer so it needs to be tested by developer.

      In order to test, create a web service client and call the web service function. A good test is to try to to enrol two users in two differents courses.

      Look at http://docs.moodle.org/en/Web_Services for information how to create a web service client.

      Show
      This web service function is for developer so it needs to be tested by developer. In order to test, create a web service client and call the web service function. A good test is to try to to enrol two users in two differents courses. Look at http://docs.moodle.org/en/Web_Services for information how to create a web service client.
    • Affected Branches:
      MOODLE_20_STABLE
    • Fixed Branches:
      MOODLE_20_STABLE
    • Pull from Repository:
      git@github.com:mouneyrac/moodle.git
    • Pull Master Branch:
      MDL-26250-final6
    • Rank:
      15960

      Description

      Something like that (I'm just writing this on the fly, need to finish):

      moodle_enrol_users:
      ------------------

      public static function enrol_users_parameters() {
              return new external_function_parameters(
                  array(
                      'enrolments' => new external_multiple_structure(
                          new external_single_structure(
                              array(
                                  'roleid'    => new external_value(PARAM_INT, 'Role to assign to the user'),
                                  'userid'    => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
                                  'contextid' => new external_value(PARAM_INT, 'The context to enrol the user role in'),
                              )
                          )
                      )
                  )
              );
          }
      
      /**
           * Manual enrolment of users
           *
           * @param array $enrolments  An array of manual user enrolment
           * @return null
           */
          public static function enrol_users($enrolments) {
              global $DB, $USER;
      
              $params = self::validate_parameters(self::enrol_users_parameters(), array('enrolments'=>$enrolments));
      
              $transaction = $DB->start_delegated_transaction();
      
              foreach ($params['enrolments'] as $enrolment) {
                  // Ensure the current user is allowed to run this function in the enrolment context
                  $context = get_context_instance_by_id($enrolment['contextid']);
                  self::validate_context($context);
                  require_capability('moodle/role:assign', $context); //TODO: there is probably a capability related to enrolment, look for it and check on it it.
      
                  $instance = $DB->get_record('enrol', array('courseid' => $context->instanceid, 'enrol' => 'manual'));
                  $enrol = enrol_get_plugin('manual');
                  $enrol->enrol_user($instance, $enrolment['userid'], $enrolment['roleid']);
      
                  //TODO: maybe return a array of success/fail
              }
      
              $transaction->allow_commit();
          }
      
       /**
           * Returns description of method result value
           * @return external_description
           */
          public static function enrol_users_returns() {
              return null;
          }
      
      

        Issue Links

          Activity

            People

            • Votes:
              6 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: