original_string: public static function assign_roles($assignments) {
          global $DB;
  
          // 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::assign_roles_parameters(), array('assignments'=>$assignments));
  
          $transaction = $DB->start_delegated_transaction();
  
          foreach ($params['assignments'] as $assignment) {
              // Ensure correct context level with a instance id or contextid is passed.
              $context = self::get_context_from_params($assignment);
  
              // Ensure the current user is allowed to run this function in the enrolment context.
              self::validate_context($context);
              require_capability('moodle/role:assign', $context);
  
              // throw an exception if user is not able to assign the role in this context
              $roles = get_assignable_roles($context, ROLENAME_SHORT);
  
              if (!array_key_exists($assignment['roleid'], $roles)) {
                  throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
              }
  
              role_assign($assignment['roleid'], $assignment['userid'], $context->id);
          }
  
          $transaction->allow_commit();
      }
code: public static function assign_roles($assignments) {
          global $DB;
  
          // 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::assign_roles_parameters(), array('assignments'=>$assignments));
  
          $transaction = $DB->start_delegated_transaction();
  
          foreach ($params['assignments'] as $assignment) {
              // Ensure correct context level with a instance id or contextid is passed.
              $context = self::get_context_from_params($assignment);
  
              // Ensure the current user is allowed to run this function in the enrolment context.
              self::validate_context($context);
              require_capability('moodle/role:assign', $context);
  
              // throw an exception if user is not able to assign the role in this context
              $roles = get_assignable_roles($context, ROLENAME_SHORT);
  
              if (!array_key_exists($assignment['roleid'], $roles)) {
                  throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
              }
  
              role_assign($assignment['roleid'], $assignment['userid'], $context->id);
          }
  
          $transaction->allow_commit();
      }
code_tokens: public static function assign_roles ( $ assignments ) { global $ DB ; // 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 :: assign_roles_parameters ( ) , array ( 'assignments' => $ assignments ) ) ; $ transaction = $ DB -> start_delegated_transaction ( ) ; foreach ( $ params [ 'assignments' ] as $ assignment ) { // Ensure correct context level with a instance id or contextid is passed. $ context = self :: get_context_from_params ( $ assignment ) ; // Ensure the current user is allowed to run this function in the enrolment context. self :: validate_context ( $ context ) ; require_capability ( 'moodle/role:assign' , $ context ) ; // throw an exception if user is not able to assign the role in this context $ roles = get_assignable_roles ( $ context , ROLENAME_SHORT ) ; if ( ! array_key_exists ( $ assignment [ 'roleid' ] , $ roles ) ) { throw new invalid_parameter_exception ( 'Can not assign roleid=' . $ assignment [ 'roleid' ] . ' in contextid=' . $ assignment [ 'contextid' ] ) ; } role_assign ( $ assignment [ 'roleid' ] , $ assignment [ 'userid' ] , $ context -> id ) ; } $ transaction -> allow_commit ( ) ; }
docstring: Manual role assignments to users
  
  @param array $assignments An array of manual role assignment
docstring_tokens: Manual role assignments to users