original_string: public function enrol_self(stdClass $instance, $data = null) {
          global $DB, $USER, $CFG;
  
          // Don't enrol user if password is not passed when required.
          if ($instance->password && !isset($data->enrolpassword)) {
              return;
          }
  
          $timestart = time();
          if ($instance->enrolperiod) {
              $timeend = $timestart + $instance->enrolperiod;
          } else {
              $timeend = 0;
          }
  
          $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
  
          if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
              // It must be a group enrolment, let's assign group too.
              $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
              foreach ($groups as $group) {
                  if (empty($group->enrolmentkey)) {
                      continue;
                  }
                  if ($group->enrolmentkey === $data->enrolpassword) {
                      // Add user to group.
                      require_once($CFG->dirroot.'/group/lib.php');
                      groups_add_member($group->id, $USER->id);
                      break;
                  }
              }
          }
          // Send welcome message.
          if ($instance->customint4 != ENROL_DO_NOT_SEND_EMAIL) {
              $this->email_welcome_message($instance, $USER);
          }
      }
code: public function enrol_self(stdClass $instance, $data = null) {
          global $DB, $USER, $CFG;
  
          // Don't enrol user if password is not passed when required.
          if ($instance->password && !isset($data->enrolpassword)) {
              return;
          }
  
          $timestart = time();
          if ($instance->enrolperiod) {
              $timeend = $timestart + $instance->enrolperiod;
          } else {
              $timeend = 0;
          }
  
          $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
  
          if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
              // It must be a group enrolment, let's assign group too.
              $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
              foreach ($groups as $group) {
                  if (empty($group->enrolmentkey)) {
                      continue;
                  }
                  if ($group->enrolmentkey === $data->enrolpassword) {
                      // Add user to group.
                      require_once($CFG->dirroot.'/group/lib.php');
                      groups_add_member($group->id, $USER->id);
                      break;
                  }
              }
          }
          // Send welcome message.
          if ($instance->customint4 != ENROL_DO_NOT_SEND_EMAIL) {
              $this->email_welcome_message($instance, $USER);
          }
      }
code_tokens: public function enrol_self ( stdClass $ instance , $ data = null ) { global $ DB , $ USER , $ CFG ; // Don't enrol user if password is not passed when required. if ( $ instance -> password && ! isset ( $ data -> enrolpassword ) ) { return ; } $ timestart = time ( ) ; if ( $ instance -> enrolperiod ) { $ timeend = $ timestart + $ instance -> enrolperiod ; } else { $ timeend = 0 ; } $ this -> enrol_user ( $ instance , $ USER -> id , $ instance -> roleid , $ timestart , $ timeend ) ; if ( $ instance -> password and $ instance -> customint1 and $ data -> enrolpassword !== $ instance -> password ) { // It must be a group enrolment, let's assign group too. $ groups = $ DB -> get_records ( 'groups' , array ( 'courseid' => $ instance -> courseid ) , 'id' , 'id, enrolmentkey' ) ; foreach ( $ groups as $ group ) { if ( empty ( $ group -> enrolmentkey ) ) { continue ; } if ( $ group -> enrolmentkey === $ data -> enrolpassword ) { // Add user to group. require_once ( $ CFG -> dirroot . '/group/lib.php' ) ; groups_add_member ( $ group -> id , $ USER -> id ) ; break ; } } } // Send welcome message. if ( $ instance -> customint4 != ENROL_DO_NOT_SEND_EMAIL ) { $ this -> email_welcome_message ( $ instance , $ USER ) ; } }
docstring: Self enrol user to course
  
  @param stdClass $instance enrolment instance
  @param stdClass $data data needed for enrolment.
  @return bool|array true if enroled else eddor code and messege
docstring_tokens: Self enrol user to course