Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk upload suppress emails 40 #169

Merged
merged 4 commits into from
Jul 8, 2024
Merged
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
12 changes: 11 additions & 1 deletion classes/booking_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class booking_manager {
/** @var bool Whether or not the bookings are loaded from a file. */
private $usefile = true;

/** @var bool When true, confirmation emails are not sent. */
private $suppressemail = false;

/**
* Constructor for the booking manager.
* @param int $f The facetoface module ID.
Expand Down Expand Up @@ -357,7 +360,7 @@ public function process() {
$this->transform_notification_type($entry->notificationtype),
$statuscode,
$user->id,
true
!$this->suppressemail,
);

continue;
Expand Down Expand Up @@ -390,4 +393,11 @@ public function process() {

return true;
}

/**
* Stops confirmation emails from being sent
*/
public function suppress_email() {
$this->suppressemail = true;
}
}
9 changes: 7 additions & 2 deletions classes/form/confirm_bookings_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public function definition() {
global $OUTPUT;

$mform = $this->_form;
$fileid = $this->_customdata['fileid'] ?: 0;
$f = $this->_customdata['f'] ?: 0;
$fileid = $this->_customdata['fileid'] ?? 0;
$f = $this->_customdata['f'] ?? 0;

// Suppress email checkbox.
$mform->addElement('advcheckbox', 'suppressemail', get_string('suppressemail', 'facetoface'), '', [], [0, 1]);
$mform->addHelpButton('suppressemail', 'suppressemail', 'facetoface');
$mform->setType('supressemail', PARAM_BOOL);

// The facetoface module ID.
$mform->addElement('hidden', 'f');
Expand Down
3 changes: 3 additions & 0 deletions classes/form/upload_bookings_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use moodle_url;
use html_writer;

defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');

/**
* Upload bookings form class
*
Expand Down
75 changes: 75 additions & 0 deletions tests/upload_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,79 @@ public function test_updates_for_previous_sessions() {
$this->assertEquals($update->grade_expected, $grade->grade);
}
}

/**
* Provides values to test_email_suppression
* @return array
*/
public static function email_suppression_provider(): array {
return [
'no suppression' => [
'shouldsuppress' => false,
],
'suppressed emails' => [
'shouldsuppress' => true,
],
];
}

/**
* Tests that the email suppression property is considered when signing up users.
* @param bool $shouldsuppress
* @dataProvider email_suppression_provider
*/
public function test_email_suppression($shouldsuppress) {
/** @var \mod_facetoface_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('mod_facetoface');

// Create f2f in course.
// Note the f2f must have a confirmation message & subject set, otherwise no emails will be sent ever.
$course = $this->getDataGenerator()->create_course();
$facetoface = $generator->create_instance(['course' => $course->id, 'confirmationmessage' => 'test',
'confirmationsubject' => 'test']);
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');

$this->setCurrentTimeStart();
$now = time();
$session = $generator->create_session([
'facetoface' => $facetoface->id,
'capacity' => '10',
'allowoverbook' => '0',
'details' => 'xyzabc',
'duration' => '1.5', // One and half hours.
'normalcost' => '123',
'discountcost' => '12',
'allowcancellations' => '0',
'sessiondates' => [
['timestart' => $now + 5 * DAYSECS, 'timefinish' => $now + 2 * DAYSECS],
],
]);

$bm = new booking_manager($facetoface->id);
$record = (object) [
'email' => $student->email,
'session' => $session->id,
'status' => 'booked',
];

$records = [$record];
$bm->load_from_array($records);

// Set the email suppression value.
if ($shouldsuppress) {
$bm->suppress_email();
}

// Setup email sink and process.
$sink = $this->redirectEmails();
$bm->process();

// Check emails got sent per the config.
$emails = $sink->get_messages();
if ($shouldsuppress) {
$this->assertEmpty($emails);
} else {
$this->assertNotEmpty($emails);
}
}
}
15 changes: 10 additions & 5 deletions upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use mod_facetoface\form\upload_bookings_form;
use mod_facetoface\form\confirm_bookings_form;
use mod_facetoface\booking_manager;
use mod_facetoface\event\csv_processed;

$f = optional_param('f', 0, PARAM_INT); // The facetoface module ID.
$fileid = optional_param('fileid', 0, PARAM_INT); // The fileid of the file uploaded.
Expand All @@ -54,7 +53,6 @@
require_capability('mod/facetoface:editsessions', $context);
require_capability('mod/facetoface:uploadbookings', $context);

echo $OUTPUT->header();

// Render form, which should only consist of an upload element.
if ($validate) {
Expand Down Expand Up @@ -97,6 +95,13 @@
$bm = new booking_manager($f);
$bm->load_from_file($fileid);

// Get the options selected by the user at confirm time.
$confirmdata = (new confirm_bookings_form(null))->get_data();

if (!empty($confirmdata->suppressemail)) {
$bm->suppress_email();
}

// Validate entries.
$errors = $bm->validate();
if (empty($errors)) {
Expand All @@ -109,7 +114,6 @@
'objectid' => $f,
];
$event = \mod_facetoface\event\csv_processed::create($params);
$event->add_record_snapshot('facetoface_sessions', $session);
$event->add_record_snapshot('facetoface', $facetoface);
$event->trigger();

Expand All @@ -135,12 +139,13 @@
$heading = get_string('facetoface:uploadbookings', 'facetoface');
}

$PAGE->set_url(new moodle_url('/mod/facetoface/upload.php', ['courseid' => $courseid, 'cmid' => $cm->id]));
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/mod/facetoface/upload.php', ['courseid' => $course->id, 'cmid' => $cm->id]));
$PAGE->set_pagelayout('standard');
$PAGE->set_title($heading);
$PAGE->set_heading($heading);

echo $OUTPUT->header();

$mform->display();

// Display footer.
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023101904;
$plugin->release = 2022101904;
$plugin->version = 2023101905;
$plugin->release = 2022101905;
$plugin->requires = 2022031500; // Requires 4.0.
$plugin->component = 'mod_facetoface';
$plugin->maturity = MATURITY_STABLE;
Expand Down
Loading