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

Email: add ##shortcourses## placeholder #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions step/email/lang/de/lifecyclestep_email.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
. '<br>' . 'Nachname des Empfängers: ##lastname##'
. '<br>' . 'Link zur Antwortseite: ##link##'
. '<br>' . 'Betroffene Kurse: ##courses##'
. '<br>' . 'Kurznamen betroffener Kurse: ##shortcourses##'
. '</p>';
$string['email_content_html'] = 'HTML-Vorlage für Emails';
$string['email_content_html_help'] = 'Stellen sie die HTML-Vorlage für Emails ein. (in HTML-Format; falls gesetzt, wird es an Stelle der Klartext-Vorlage benutzt!)' . '<p>' . 'Sie können die folgenden Platzhalter benutzen:'
Expand All @@ -45,6 +46,7 @@
. '<br>' . 'Nachname des Empfängers: ##lastname##'
. '<br>' . 'Link zur Antwortseite: ##link##'
. '<br>' . 'Betroffene Kurse: ##courses##'
. '<br>' . 'Kurznamen betroffener Kurse: ##shortcourses-html##'
. '</p>';
$string['keep_course'] = 'Kurs behalten';
$string['pluginname'] = 'Email-Schritt';
Expand Down
2 changes: 2 additions & 0 deletions step/email/lang/en/lifecyclestep_email.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
. '<br>' . 'Last name of recipient: ##lastname##'
. '<br>' . 'Link to response page: ##link##'
. '<br>' . 'Impacted courses: ##courses##'
. '<br>' . 'Short names of impacted courses: ##shortcourses##'
. '</p>';
$string['email_content_html'] = 'Content HTML Template';
$string['email_content_html_help'] = 'Set the html template for the content of the email (HTML email, will be used instead of plaintext field if not empty!)' . '<p>' . 'You can use the following placeholders:'
. '<br>' . 'First name of recipient: ##firstname##'
. '<br>' . 'Last name of recipient: ##lastname##'
. '<br>' . 'Link to response page: ##link-html##'
. '<br>' . 'Impacted courses: ##courses-html##'
. '<br>' . 'Short names of impacted courses: ##shortcourses-html##'
. '</p>';
$string['email_responsetimeout'] = 'Time the user has for the response';
$string['email_subject'] = 'Subject template';
Expand Down
60 changes: 20 additions & 40 deletions step/email/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use tool_lifecycle\local\manager\settings_manager;
use tool_lifecycle\local\response\step_response;
use tool_lifecycle\local\manager\step_manager;
use tool_lifecycle\local\manager\process_data_manager;
use tool_lifecycle\settings_type;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -169,53 +168,34 @@ private function replace_placeholders($strings, $user, $stepid, $mailentries) {
$patterns[] = '##link-html##';
$replacements[] = \html_writer::link($url, $url);

// Replace courses list.
$patterns[] = '##courses##';
$courses = $mailentries;
$coursesstring = '';
$coursesstring .= $this->parse_course(array_pop($courses)->courseid);
foreach ($courses as $entry) {
$coursesstring .= "\n" . $this->parse_course($entry->courseid);
$courses = [];
foreach ($mailentries as $entry) {
$courses[] = get_course($entry->courseid);
}
$replacements[] = $coursesstring;

// Replace courses html.
// Replace courses list.
$coursesstrings = [];
foreach ($courses as $course) {
$coursesstrings[] = $course->fullname;
}
$patterns[] = '##courses##';
$replacements[] = join("\n", $coursesstrings);
$patterns[] = '##courses-html##';
$courses = $mailentries;
$coursestabledata = [];
foreach ($courses as $entry) {
$coursestabledata[$entry->courseid] = $this->parse_course_row_data($entry->courseid);
$replacements[] = join("<br>", $coursesstrings);

// Replace short courses list.
$coursesstrings = [];
foreach ($courses as $course) {
$coursesstrings[] = $course->shortname;
}
$coursestable = new \html_table();
$coursestable->data = $coursestabledata;
$replacements[] = \html_writer::table($coursestable);
$patterns[] = '##shortcourses##';
$replacements[] = join("\n", $coursesstrings);
$patterns[] = '##shortcourses-html##';
$replacements[] = join("<br>", $coursesstrings);

return str_ireplace($patterns, $replacements, $strings);
}

/**
* Parses a course for the non html format.
* @param int $courseid id of the course
* @return string
* @throws \dml_exception
*/
private function parse_course($courseid) {
$course = get_course($courseid);
$result = $course->fullname;
return $result;
}

/**
* Parses a course for the html format.
* @param int $courseid id of the course
* @return array column of a course
* @throws \dml_exception
*/
private function parse_course_row_data($courseid) {
$course = get_course($courseid);
return [$course->fullname];
}

/**
* Defines which settings each instance of the subplugin offers for the user to define.
* @return instance_setting[] containing settings keys and PARAM_TYPES
Expand Down
58 changes: 58 additions & 0 deletions step/email/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the lifecyclestep_email lib.php.
*
* @package lifecyclestep_email
* @copyright 2024 Justus Dieckmann, University of Münster.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace lifecyclestep_email;

/**
* Unit tests for the lifecyclestep_email lib.php.
*
* @package lifecyclestep_email
* @copyright 2024 Justus Dieckmann, University of Münster.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class lib_test extends \advanced_testcase {

/**
* Tests \tool_lifecycle\step\email::replace_placeholders.
*
* @covers \tool_lifecycle\step\email::replace_placeholders
*/
public function test_replace_placeholders(): void {
$this->resetAfterTest();

$user1 = $this->getDataGenerator()->create_user(['firstname' => 'Jane', 'lastname' => 'Doe']);
$course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1']);
$course2 = $this->getDataGenerator()->create_course(['fullname' => 'Course 2', 'shortname' => 'C2']);
$lib = new \tool_lifecycle\step\email();
$callreplaceplaceholders = function($strings, $user, $stepid, $mailentries) {
return $this->replace_placeholders($strings, $user, $stepid, $mailentries);
};
$response = $callreplaceplaceholders->call($lib, [
"##firstname##\n##lastname##\n##courses##\n##shortcourses##",
"##firstname##<br>##lastname##<br>##courses-html##<br>##shortcourses-html##",
], $user1, 0, [(object) ['courseid' => $course1->id], (object) ['courseid' => $course2->id]]);

$this->assertCount(2, $response);
$this->assertEquals("Jane\nDoe\nCourse 1\nCourse 2\nC1\nC2", $response[0]);
$this->assertEquals("Jane<br>Doe<br>Course 1<br>Course 2<br>C1<br>C2", $response[1]);
}
}
Loading