public function issue($userid, $nobake = false) {
        global $DB, $CFG;

        $now = time();
        $issued = new stdClass();
        $issued->badgeid = $this->id;
        $issued->userid = $userid;
        $issued->uniquehash = sha1(rand() . $userid . $this->id . $now);
        $issued->dateissued = $now;

        if ($this->can_expire()) {
            $issued->dateexpire = $this->calculate_expiry($now);
        } else {
            $issued->dateexpire = null;
        }

        // Take into account user badges privacy settings.
        // If none set, badges default visibility is set to public.
        $issued->visible = get_user_preferences('badgeprivacysetting', 1, $userid);

        $result = $DB->insert_record('badge_issued', $issued, true);

        if ($result) {
            // Trigger badge awarded event.
            $eventdata = array (
                'context' => $this->get_context(),
                'objectid' => $this->id,
                'relateduserid' => $userid,
                'other' => array('dateexpire' => $issued->dateexpire, 'badgeissuedid' => $result)
            );
            \core\event\badge_awarded::create($eventdata)->trigger();

            // Lock the badge, so that its criteria could not be changed any more.
            if ($this->status == BADGE_STATUS_ACTIVE) {
                $this->set_status(BADGE_STATUS_ACTIVE_LOCKED);
            }

            // Update details in criteria_met table.
            $compl = $this->get_criteria_completions($userid);
            foreach ($compl as $c) {
                $obj = new stdClass();
                $obj->id = $c->id;
                $obj->issuedid = $result;
                $DB->update_record('badge_criteria_met', $obj, true);
            }

            if (!$nobake) {
                // Bake a badge image.
                $pathhash = badges_bake($issued->uniquehash, $this->id, $userid, true);

                // Notify recipients and badge creators.
                badges_notify_badge_award($this, $userid, $issued->uniquehash, $pathhash);
            }
        }
    }
public function issue($userid, $nobake = false) {
        global $DB, $CFG;

        $now = time();
        $issued = new stdClass();
        $issued->badgeid = $this->id;
        $issued->userid = $userid;
        $issued->uniquehash = sha1(rand() . $userid . $this->id . $now);
        $issued->dateissued = $now;

        if ($this->can_expire()) {
            $issued->dateexpire = $this->calculate_expiry($now);
        } else {
            $issued->dateexpire = null;
        }

        // Take into account user badges privacy settings.
        // If none set, badges default visibility is set to public.
        $issued->visible = get_user_preferences('badgeprivacysetting', 1, $userid);

        $result = $DB->insert_record('badge_issued', $issued, true);

        if ($result) {
            // Trigger badge awarded event.
            $eventdata = array (
                'context' => $this->get_context(),
                'objectid' => $this->id,
                'relateduserid' => $userid,
                'other' => array('dateexpire' => $issued->dateexpire, 'badgeissuedid' => $result)
            );
            \core\event\badge_awarded::create($eventdata)->trigger();

            // Lock the badge, so that its criteria could not be changed any more.
            if ($this->status == BADGE_STATUS_ACTIVE) {
                $this->set_status(BADGE_STATUS_ACTIVE_LOCKED);
            }

            // Update details in criteria_met table.
            $compl = $this->get_criteria_completions($userid);
            foreach ($compl as $c) {
                $obj = new stdClass();
                $obj->id = $c->id;
                $obj->issuedid = $result;
                $DB->update_record('badge_criteria_met', $obj, true);
            }

            if (!$nobake) {
                // Bake a badge image.
                $pathhash = badges_bake($issued->uniquehash, $this->id, $userid, true);

                // Notify recipients and badge creators.
                badges_notify_badge_award($this, $userid, $issued->uniquehash, $pathhash);
            }
        }
    }
public function issue ( $ userid , $ nobake = false ) { global $ DB , $ CFG ; $ now = time ( ) ; $ issued = new stdClass ( ) ; $ issued -> badgeid = $ this -> id ; $ issued -> userid = $ userid ; $ issued -> uniquehash = sha1 ( rand ( ) . $ userid . $ this -> id . $ now ) ; $ issued -> dateissued = $ now ; if ( $ this -> can_expire ( ) ) { $ issued -> dateexpire = $ this -> calculate_expiry ( $ now ) ; } else { $ issued -> dateexpire = null ; } // Take into account user badges privacy settings. // If none set, badges default visibility is set to public. $ issued -> visible = get_user_preferences ( 'badgeprivacysetting' , 1 , $ userid ) ; $ result = $ DB -> insert_record ( 'badge_issued' , $ issued , true ) ; if ( $ result ) { // Trigger badge awarded event. $ eventdata = array ( 'context' => $ this -> get_context ( ) , 'objectid' => $ this -> id , 'relateduserid' => $ userid , 'other' => array ( 'dateexpire' => $ issued -> dateexpire , 'badgeissuedid' => $ result ) ) ; \ core \ event \ badge_awarded :: create ( $ eventdata ) -> trigger ( ) ; // Lock the badge, so that its criteria could not be changed any more. if ( $ this -> status == BADGE_STATUS_ACTIVE ) { $ this -> set_status ( BADGE_STATUS_ACTIVE_LOCKED ) ; } // Update details in criteria_met table. $ compl = $ this -> get_criteria_completions ( $ userid ) ; foreach ( $ compl as $ c ) { $ obj = new stdClass ( ) ; $ obj -> id = $ c -> id ; $ obj -> issuedid = $ result ; $ DB -> update_record ( 'badge_criteria_met' , $ obj , true ) ; } if ( ! $ nobake ) { // Bake a badge image. $ pathhash = badges_bake ( $ issued -> uniquehash , $ this -> id , $ userid , true ) ; // Notify recipients and badge creators. badges_notify_badge_award ( $ this , $ userid , $ issued -> uniquehash , $ pathhash ) ; } } }
Issue a badge to user.

@param int $userid User who earned the badge
@param bool $nobake Not baking actual badges (for testing purposes)
Issue a badge to user .