Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-26250

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

    XMLWordPrintable

Details

    • MOODLE_20_STABLE
    • MOODLE_20_STABLE
    • git@github.com:mouneyrac/moodle.git
    • MDL-26250-final6
    • 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.

    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;
          }
       

      Attachments

        Issue Links

          Activity

            People

              jerome Jérôme Mouneyrac
              jerome Jérôme Mouneyrac
              Rossiani Wijaya Rossiani Wijaya
              Sam Hemelryk Sam Hemelryk
              Rossiani Wijaya Rossiani Wijaya
              Juan Leyva, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo
              Votes:
              6 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                5/May/11