original_string: public static function enrol_user($tool, $userid) {
          global $DB;
  
          // Check if the user enrolment exists.
          if (!$DB->record_exists('user_enrolments', array('enrolid' => $tool->enrolid, 'userid' => $userid))) {
              // Check if the maximum enrolled limit has been met.
              if ($tool->maxenrolled) {
                  if ($DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)) >= $tool->maxenrolled) {
                      return self::ENROLMENT_MAX_ENROLLED;
                  }
              }
              // Check if the enrolment has not started.
              if ($tool->enrolstartdate && time() < $tool->enrolstartdate) {
                  return self::ENROLMENT_NOT_STARTED;
              }
              // Check if the enrolment has finished.
              if ($tool->enrolenddate && time() > $tool->enrolenddate) {
                  return self::ENROLMENT_FINISHED;
              }
  
              $timeend = 0;
              if ($tool->enrolperiod) {
                  $timeend = time() + $tool->enrolperiod;
              }
  
              // Finally, enrol the user.
              $instance = new \stdClass();
              $instance->id = $tool->enrolid;
              $instance->courseid = $tool->courseid;
              $instance->enrol = 'lti';
              $instance->status = $tool->status;
              $ltienrol = enrol_get_plugin('lti');
  
              // Hack - need to do this to workaround DB caching hack. See MDL-53977.
              $timestart = intval(substr(time(), 0, 8) . '00') - 1;
              $ltienrol->enrol_user($instance, $userid, null, $timestart, $timeend);
          }
  
          return self::ENROLMENT_SUCCESSFUL;
      }
code: public static function enrol_user($tool, $userid) {
          global $DB;
  
          // Check if the user enrolment exists.
          if (!$DB->record_exists('user_enrolments', array('enrolid' => $tool->enrolid, 'userid' => $userid))) {
              // Check if the maximum enrolled limit has been met.
              if ($tool->maxenrolled) {
                  if ($DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)) >= $tool->maxenrolled) {
                      return self::ENROLMENT_MAX_ENROLLED;
                  }
              }
              // Check if the enrolment has not started.
              if ($tool->enrolstartdate && time() < $tool->enrolstartdate) {
                  return self::ENROLMENT_NOT_STARTED;
              }
              // Check if the enrolment has finished.
              if ($tool->enrolenddate && time() > $tool->enrolenddate) {
                  return self::ENROLMENT_FINISHED;
              }
  
              $timeend = 0;
              if ($tool->enrolperiod) {
                  $timeend = time() + $tool->enrolperiod;
              }
  
              // Finally, enrol the user.
              $instance = new \stdClass();
              $instance->id = $tool->enrolid;
              $instance->courseid = $tool->courseid;
              $instance->enrol = 'lti';
              $instance->status = $tool->status;
              $ltienrol = enrol_get_plugin('lti');
  
              // Hack - need to do this to workaround DB caching hack. See MDL-53977.
              $timestart = intval(substr(time(), 0, 8) . '00') - 1;
              $ltienrol->enrol_user($instance, $userid, null, $timestart, $timeend);
          }
  
          return self::ENROLMENT_SUCCESSFUL;
      }
code_tokens: public static function enrol_user ( $ tool , $ userid ) { global $ DB ; // Check if the user enrolment exists. if ( ! $ DB -> record_exists ( 'user_enrolments' , array ( 'enrolid' => $ tool -> enrolid , 'userid' => $ userid ) ) ) { // Check if the maximum enrolled limit has been met. if ( $ tool -> maxenrolled ) { if ( $ DB -> count_records ( 'user_enrolments' , array ( 'enrolid' => $ tool -> enrolid ) ) >= $ tool -> maxenrolled ) { return self :: ENROLMENT_MAX_ENROLLED ; } } // Check if the enrolment has not started. if ( $ tool -> enrolstartdate && time ( ) < $ tool -> enrolstartdate ) { return self :: ENROLMENT_NOT_STARTED ; } // Check if the enrolment has finished. if ( $ tool -> enrolenddate && time ( ) > $ tool -> enrolenddate ) { return self :: ENROLMENT_FINISHED ; } $ timeend = 0 ; if ( $ tool -> enrolperiod ) { $ timeend = time ( ) + $ tool -> enrolperiod ; } // Finally, enrol the user. $ instance = new \ stdClass ( ) ; $ instance -> id = $ tool -> enrolid ; $ instance -> courseid = $ tool -> courseid ; $ instance -> enrol = 'lti' ; $ instance -> status = $ tool -> status ; $ ltienrol = enrol_get_plugin ( 'lti' ) ; // Hack - need to do this to workaround DB caching hack. See MDL-53977. $ timestart = intval ( substr ( time ( ) , 0 , 8 ) . '00' ) - 1 ; $ ltienrol -> enrol_user ( $ instance , $ userid , null , $ timestart , $ timeend ) ; } return self :: ENROLMENT_SUCCESSFUL ; }
docstring: Enrol a user in a course.
  
  @param \stdclass $tool The tool object (retrieved using self::get_lti_tool() or self::get_lti_tools())
  @param int $userid The user id
  @return bool|string returns true if successful, else an error code
docstring_tokens: Enrol a user in a course .