original_string: public function generate_user_ws_tokens($userid) {
          global $CFG, $DB;
  
          // generate a token for non admin if web service are enable and the user has the capability to create a token
          if (!is_siteadmin() && has_capability('moodle/webservice:createtoken', context_system::instance(), $userid) && !empty($CFG->enablewebservices)) {
              // for every service than the user is authorised on, create a token (if it doesn't already exist)
  
              // get all services which are set to all user (no restricted to specific users)
              $norestrictedservices = $DB->get_records('external_services', array('restrictedusers' => 0));
              $serviceidlist = array();
              foreach ($norestrictedservices as $service) {
                  $serviceidlist[] = $service->id;
              }
  
              // get all services which are set to the current user (the current user is specified in the restricted user list)
              $servicesusers = $DB->get_records('external_services_users', array('userid' => $userid));
              foreach ($servicesusers as $serviceuser) {
                  if (!in_array($serviceuser->externalserviceid,$serviceidlist)) {
                       $serviceidlist[] = $serviceuser->externalserviceid;
                  }
              }
  
              // get all services which already have a token set for the current user
              $usertokens = $DB->get_records('external_tokens', array('userid' => $userid, 'tokentype' => EXTERNAL_TOKEN_PERMANENT));
              $tokenizedservice = array();
              foreach ($usertokens as $token) {
                      $tokenizedservice[]  = $token->externalserviceid;
              }
  
              // create a token for the service which have no token already
              foreach ($serviceidlist as $serviceid) {
                  if (!in_array($serviceid, $tokenizedservice)) {
                      // create the token for this service
                      $newtoken = new stdClass();
                      $newtoken->token = md5(uniqid(rand(),1));
                      // check that the user has capability on this service
                      $newtoken->tokentype = EXTERNAL_TOKEN_PERMANENT;
                      $newtoken->userid = $userid;
                      $newtoken->externalserviceid = $serviceid;
                      // TODO MDL-31190 find a way to get the context - UPDATE FOLLOWING LINE
                      $newtoken->contextid = context_system::instance()->id;
                      $newtoken->creatorid = $userid;
                      $newtoken->timecreated = time();
                      $newtoken->privatetoken = null;
  
                      $DB->insert_record('external_tokens', $newtoken);
                  }
              }
  
  
          }
      }
code: public function generate_user_ws_tokens($userid) {
          global $CFG, $DB;
  
          // generate a token for non admin if web service are enable and the user has the capability to create a token
          if (!is_siteadmin() && has_capability('moodle/webservice:createtoken', context_system::instance(), $userid) && !empty($CFG->enablewebservices)) {
              // for every service than the user is authorised on, create a token (if it doesn't already exist)
  
              // get all services which are set to all user (no restricted to specific users)
              $norestrictedservices = $DB->get_records('external_services', array('restrictedusers' => 0));
              $serviceidlist = array();
              foreach ($norestrictedservices as $service) {
                  $serviceidlist[] = $service->id;
              }
  
              // get all services which are set to the current user (the current user is specified in the restricted user list)
              $servicesusers = $DB->get_records('external_services_users', array('userid' => $userid));
              foreach ($servicesusers as $serviceuser) {
                  if (!in_array($serviceuser->externalserviceid,$serviceidlist)) {
                       $serviceidlist[] = $serviceuser->externalserviceid;
                  }
              }
  
              // get all services which already have a token set for the current user
              $usertokens = $DB->get_records('external_tokens', array('userid' => $userid, 'tokentype' => EXTERNAL_TOKEN_PERMANENT));
              $tokenizedservice = array();
              foreach ($usertokens as $token) {
                      $tokenizedservice[]  = $token->externalserviceid;
              }
  
              // create a token for the service which have no token already
              foreach ($serviceidlist as $serviceid) {
                  if (!in_array($serviceid, $tokenizedservice)) {
                      // create the token for this service
                      $newtoken = new stdClass();
                      $newtoken->token = md5(uniqid(rand(),1));
                      // check that the user has capability on this service
                      $newtoken->tokentype = EXTERNAL_TOKEN_PERMANENT;
                      $newtoken->userid = $userid;
                      $newtoken->externalserviceid = $serviceid;
                      // TODO MDL-31190 find a way to get the context - UPDATE FOLLOWING LINE
                      $newtoken->contextid = context_system::instance()->id;
                      $newtoken->creatorid = $userid;
                      $newtoken->timecreated = time();
                      $newtoken->privatetoken = null;
  
                      $DB->insert_record('external_tokens', $newtoken);
                  }
              }
  
  
          }
      }
code_tokens: public function generate_user_ws_tokens ( $ userid ) { global $ CFG , $ DB ; // generate a token for non admin if web service are enable and the user has the capability to create a token if ( ! is_siteadmin ( ) && has_capability ( 'moodle/webservice:createtoken' , context_system :: instance ( ) , $ userid ) && ! empty ( $ CFG -> enablewebservices ) ) { // for every service than the user is authorised on, create a token (if it doesn't already exist) // get all services which are set to all user (no restricted to specific users) $ norestrictedservices = $ DB -> get_records ( 'external_services' , array ( 'restrictedusers' => 0 ) ) ; $ serviceidlist = array ( ) ; foreach ( $ norestrictedservices as $ service ) { $ serviceidlist [ ] = $ service -> id ; } // get all services which are set to the current user (the current user is specified in the restricted user list) $ servicesusers = $ DB -> get_records ( 'external_services_users' , array ( 'userid' => $ userid ) ) ; foreach ( $ servicesusers as $ serviceuser ) { if ( ! in_array ( $ serviceuser -> externalserviceid , $ serviceidlist ) ) { $ serviceidlist [ ] = $ serviceuser -> externalserviceid ; } } // get all services which already have a token set for the current user $ usertokens = $ DB -> get_records ( 'external_tokens' , array ( 'userid' => $ userid , 'tokentype' => EXTERNAL_TOKEN_PERMANENT ) ) ; $ tokenizedservice = array ( ) ; foreach ( $ usertokens as $ token ) { $ tokenizedservice [ ] = $ token -> externalserviceid ; } // create a token for the service which have no token already foreach ( $ serviceidlist as $ serviceid ) { if ( ! in_array ( $ serviceid , $ tokenizedservice ) ) { // create the token for this service $ newtoken = new stdClass ( ) ; $ newtoken -> token = md5 ( uniqid ( rand ( ) , 1 ) ) ; // check that the user has capability on this service $ newtoken -> tokentype = EXTERNAL_TOKEN_PERMANENT ; $ newtoken -> userid = $ userid ; $ newtoken -> externalserviceid = $ serviceid ; // TODO MDL-31190 find a way to get the context - UPDATE FOLLOWING LINE $ newtoken -> contextid = context_system :: instance ( ) -> id ; $ newtoken -> creatorid = $ userid ; $ newtoken -> timecreated = time ( ) ; $ newtoken -> privatetoken = null ; $ DB -> insert_record ( 'external_tokens' , $ newtoken ) ; } } } }
docstring: Generate all tokens of a specific user
  
  @param int $userid user id
docstring_tokens: Generate all tokens of a specific user