*/
public function has_awards() {
global $DB;
- if ($DB->record_exists('badge_issued', array('badgeid' => $this->id))) {
- return true;
- }
- return false;
+ $awarded = $DB->record_exists_sql('SELECT b.uniquehash
+ FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
+ WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $this->id));
+
+ return $awarded;
}
/**
'SELECT b.userid, b.dateissued, b.uniquehash, u.firstname, u.lastname
FROM {badge_issued} b INNER JOIN {user} u
ON b.userid = u.id
- WHERE b.badgeid = :badgeid', array('badgeid' => $this->id));
+ WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $this->id));
return $awards;
}
}
/**
- * Marks the badge as archived.
- * For reporting and historical purposed we cannot completely delete badges.
- * We will just change their status to BADGE_STATUS_ARCHIVED.
+ * Fully deletes the badge or marks it as archived.
+ *
+ * @param $archive bool Achive a badge without actual deleting of any data.
*/
- public function delete() {
- $this->status = BADGE_STATUS_ARCHIVED;
- $this->save();
+ public function delete($archive = true) {
+ global $DB;
+
+ if ($archive) {
+ $this->status = BADGE_STATUS_ARCHIVED;
+ $this->save();
+ return;
+ }
+
+ $fs = get_file_storage();
+
+ // Remove all issued badge image files and badge awards.
+ // Cannot bulk remove area files here because they are issued in user context.
+ $awards = $this->get_awards();
+ foreach ($awards as $award) {
+ $usercontext = context_user::instance($award->userid);
+ $fs->delete_area_files($usercontext->id, 'badges', 'userbadge', $this->id);
+ }
+ $DB->delete_records('badge_issued', array('badgeid' => $this->id));
+
+ // Remove all badge criteria.
+ $criteria = $this->get_criteria();
+ foreach ($criteria as $criterion) {
+ $criterion->delete();
+ }
+
+ // Delete badge images.
+ $badgecontext = $this->get_context();
+ $fs->delete_area_files($badgecontext->id, 'badges', 'badgeimage', $this->id);
+
+ // Finally, remove badge itself.
+ $DB->delete_records('badge', array('id' => $this->id));
}
}
$badges[$r->id]->dateissued = $r->dateissued;
$badges[$r->id]->uniquehash = $r->uniquehash;
} else {
- $badges[$r->id]->awards = $DB->count_records('badge_issued', array('badgeid' => $badge->id));
+ $badges[$r->id]->awards = $DB->count_records_sql('SELECT COUNT(b.userid)
+ FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
+ WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badge->id));
$badges[$r->id]->statstring = $badge->get_status_name();
}
}