Skip to content
Merged

#2763 #424

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modules/access_badges/access_badges.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ type: module
core_version_requirement: ^10 || ^11
package: Custom
dependencies:
- access
- ultimate_cron

- drupal:access
- drupal:ultimate_cron
27 changes: 16 additions & 11 deletions modules/access_badges/access_badges.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* Access Badges updates.
*/

use Drupal\user\Entity\User;

/**
* Add Badges to taxonomy.
*/
function access_badges_install() {
function access_badges_install(): void {
$terms = [
'New to CSSN',
'New to ACCESS',
Expand Down Expand Up @@ -79,7 +81,6 @@ function access_badges_install() {
// Set badges for users.
$badge_tools->setBadges($scipe, $users);


// Set Knowledge Base Resource Contributor badges.
$query = \Drupal::database()->select('webform_submission', 'ws');
$query->fields('ws', ['sid', 'uid']);
Expand All @@ -91,7 +92,8 @@ function access_badges_install() {
foreach ($wf_submissions as $submission) {
if (!isset($authors[$submission->uid])) {
$authors[$submission->uid] = 1;
} else {
}
else {
$authors[$submission->uid]++;
}
}
Expand All @@ -101,18 +103,20 @@ function access_badges_install() {
$kb_ten = \Drupal::service('access_badges.badgeTools')->getBadgeTid('KB Resource Contributor (10)');

foreach ($authors as $uid => $count) {
$user = \Drupal\user\Entity\User::load($uid);
$user = User::load($uid);
$badgetid = $user->get('field_user_badges')->getValue();

if ($count < 4) {
$badgetid[] = ['target_id' => $kb_one];
$user->set('field_user_badges', $badgetid);
$user->save();
} elseif ($count > 5 && $count < 10) {
}
elseif ($count > 5 && $count < 10) {
$badgetid[] = ['target_id' => $kb_five];
$user->set('field_user_badges', $badgetid);
$user->save();
} elseif ($count >= 10) {
}
elseif ($count >= 10) {
$badgetid[] = ['target_id' => $kb_ten];
$user->set('field_user_badges', $badgetid);
$user->save();
Expand All @@ -129,7 +133,6 @@ function access_badges_install() {
$badge_tools->fieldToBadge('field_mentor', 'CCMNet Engagement Participant', 'mentorship_engagement');
}


/**
* Sort all user badges to match taxonomy term weight/name order.
*/
Expand All @@ -141,7 +144,7 @@ function access_badges_update_10001(): string {
/**
* Create access_badges_pending table for CSV badge assignment queue.
*/
function access_badges_update_10002() {
function access_badges_update_10002(): void {
$schema = \Drupal::database()->schema();
if (!$schema->tableExists('access_badges_pending')) {
$schema->createTable('access_badges_pending', _access_badges_pending_schema());
Expand All @@ -150,8 +153,11 @@ function access_badges_update_10002() {

/**
* Returns the schema definition for the access_badges_pending table.
*
* @return array<string, mixed>
* The schema definition.
*/
function _access_badges_pending_schema() {
function _access_badges_pending_schema(): array {
return [
'description' => 'Pending badge assignments from CSV uploads.',
'fields' => [
Expand Down Expand Up @@ -234,13 +240,12 @@ function _access_badges_pending_schema() {
/**
* Remove Badges from taxonomy.
*/
function access_badges_uninstall() {
function access_badges_uninstall(): void {
// Drop the pending badges table.
\Drupal::database()->schema()->dropTable('access_badges_pending');

\Drupal::database()->truncate('user__field_user_badges')->execute();


$tids = \Drupal::entityQuery('taxonomy_term')
->condition('vid', 'badges')
->accessCheck(FALSE)
Expand Down
24 changes: 16 additions & 8 deletions modules/access_badges/access_badges.module
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

/**
* @file
* Access Badges hooks.
*/

use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\UserInterface;

/**
* Implements hook_form_alter().
*
* @phpstan-param array<string, mixed> $form
*/
function access_badges_form_alter(&$form, FormStateInterface $form_state, $form_id) {
function access_badges_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
if ($form_id == 'user_form' || $form_id == 'user_register_form') {
// Check if user is an administator.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$user = User::load(\Drupal::currentUser()->id());
$roles = $user->getRoles();
if (!in_array('administrator', $roles)) {
$form['#attached']['library'][] = 'access_badges/hide_ief_button';
Expand All @@ -26,20 +31,20 @@ function access_badges_form_alter(&$form, FormStateInterface $form_state, $form_
/**
* Implements hook_user_presave().
*/
function access_badges_user_presave(UserInterface $user) {
function access_badges_user_presave(UserInterface $user): void {
\Drupal::service('access_badges.badge_sorter')->sortUserBadges($user);
}

/**
* Implements hook_cron().
*/
function access_badges_cron() {
function access_badges_cron(): void {
// Run this cron job every day at 4am.
$currentTime = \Drupal::time()->getCurrentTime();
$hour = date('H', $currentTime);
$min = date('i', $currentTime);

// Cron runs every 10 minutes
// Cron runs every 10 minutes.
if ($hour == 4 && $min < 20) {
$badge_tools = \Drupal::service('access_badges.badgeTools');
// Get 'New to ACCESS' term id.
Expand Down Expand Up @@ -79,7 +84,7 @@ function access_badges_cron() {
* Auto-assigns badges from pending queue when a new user registers
* with a matching email address (exact match, case-insensitive via collation).
*/
function access_badges_user_insert(UserInterface $account) {
function access_badges_user_insert(UserInterface $account): void {
$email = $account->getEmail();
if (empty($email)) {
return;
Expand Down Expand Up @@ -121,8 +126,11 @@ function access_badges_user_insert(UserInterface $account) {

/**
* Implements hook_mail().
*
* @phpstan-param array<string, mixed> $message
* @phpstan-param array<string, mixed> $params
*/
function access_badges_mail($key, &$message, $params) {
function access_badges_mail(string $key, array &$message, array $params): void {
if ($key === 'pending_match_digest') {
$message['subject'] = t('Badge Assignments: New matches need review');

Expand All @@ -140,7 +148,7 @@ function access_badges_mail($key, &$message, $params) {
}

$lines[] = '';
$review_url = \Drupal\Core\Url::fromRoute('access_badges.review', [], ['absolute' => TRUE])->toString();
$review_url = Url::fromRoute('access_badges.review', [], ['absolute' => TRUE])->toString();
$lines[] = t('Review these matches at: @url', ['@url' => $review_url]);

$message['body'] = $lines;
Expand Down
5 changes: 5 additions & 0 deletions modules/access_badges/access_badges.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
services:
access_badges.badgeTools:
class: Drupal\access_badges\Plugin\BadgeTools
arguments:
- '@entity_type.manager'
- '@database'
- '@datetime.time'

access_badges.badge_sorter:
class: Drupal\access_badges\Service\BadgeSorter
Expand All @@ -17,3 +21,4 @@ services:
- '@database'
- '@entity_type.manager'
- '@access_badges.badgeTools'
- '@datetime.time'
48 changes: 34 additions & 14 deletions modules/access_badges/src/Controller/BadgePendingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
namespace Drupal\access_badges\Controller;

use Drupal\access_badges\Plugin\BadgeTools;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Controller for badge pending action routes.
*/
class BadgePendingController extends ControllerBase {
final class BadgePendingController extends ControllerBase {

/**
* The database connection.
Expand All @@ -28,35 +30,53 @@ class BadgePendingController extends ControllerBase {
*/
protected $badgeTools;

/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* Constructs a BadgePendingController.
*/
public function __construct(Connection $database, BadgeTools $badge_tools) {
public function __construct(Connection $database, BadgeTools $badge_tools, TimeInterface $time, RequestStack $request_stack) {
$this->database = $database;
$this->badgeTools = $badge_tools;
$this->time = $time;
$this->requestStack = $request_stack;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container): static {
return new static(
$container->get('database'),
$container->get('access_badges.badgeTools')
$container->get('access_badges.badgeTools'),
$container->get('datetime.time'),
$container->get('request_stack')
);
}

/**
* Redirects the Badge Assignments landing page to the Pending sub-tab.
*/
public function badgeAssignments() {
public function badgeAssignments(): RedirectResponse {
return new RedirectResponse(Url::fromRoute('access_badges.pending')->toString());
}

/**
* Deletes a pending row.
*/
public function delete($id) {
public function delete(string $id): RedirectResponse {
$this->database->delete('access_badges_pending')
->condition('id', $id)
->execute();
Expand All @@ -67,8 +87,8 @@ public function delete($id) {
/**
* Assigns a badge from an inline possible match.
*/
public function assign($uid, $badge_tid, $vocabulary, $email) {
$assigned = $this->badgeTools->assignBadgeToUser($uid, $badge_tid, $vocabulary);
public function assign(string $uid, string $badge_tid, string $vocabulary, string $email): RedirectResponse {
$assigned = $this->badgeTools->assignBadgeToUser((int) $uid, (int) $badge_tid, $vocabulary);
if ($assigned) {
$this->messenger()->addStatus($this->t('Badge assigned successfully.'));
}
Expand All @@ -88,11 +108,11 @@ public function assign($uid, $badge_tid, $vocabulary, $email) {
/**
* Sends an inline possible match to the pending queue.
*/
public function sendToPending($email, $first_name, $last_name, $organization = '') {
public function sendToPending(string $email, string $first_name, string $last_name, string $organization = ''): RedirectResponse {
// This route requires badge_tid and vocabulary from the query string.
$request = \Drupal::request();
$badge_tid = $request->query->get('badge_tid', 0);
$vocabulary = $request->query->get('vocabulary', 'badges');
$request = $this->requestStack->getCurrentRequest();
$badge_tid = $request ? $request->query->get('badge_tid', 0) : 0;
$vocabulary = $request ? $request->query->get('vocabulary', 'badges') : 'badges';

if (!empty($email) && !empty($badge_tid)) {
// Check for duplicate.
Expand All @@ -112,7 +132,7 @@ public function sendToPending($email, $first_name, $last_name, $organization = '
'organization' => $organization,
'badge_tid' => $badge_tid,
'vocabulary' => $vocabulary,
'created' => \Drupal::time()->getRequestTime(),
'created' => $this->time->getRequestTime(),
'status' => 'pending',
])
->execute();
Expand Down
Loading