diff --git a/classes/course_evasys_courses_allocation.php b/classes/course_evasys_courses_allocation.php index ac9e279..5e688d5 100644 --- a/classes/course_evasys_courses_allocation.php +++ b/classes/course_evasys_courses_allocation.php @@ -44,4 +44,26 @@ protected static function define_properties() { ), ); } + + /** Returns array of evasysids that belong to this course. + * @param $courseid int id of moodle course + * @return array of evasysids + * @throws \dml_exception + */ + public static function raw_get_evasyscourses($courseid) { + global $DB; + $courses = $DB->get_field(self::TABLE, 'evasyscourses', array('course' => $courseid)); + $idcourse = $DB->get_field('course', 'idnumber', array('id' => $courseid)); + $coursearray = array(); + if ($courses) { + $coursearray = explode('#', $courses); + } + if ($idcourse) { + if (!in_array($idcourse, $coursearray)) { + $coursearray[] = $idcourse; + } + } + array_unique($coursearray); + return $coursearray; + } } \ No newline at end of file diff --git a/classes/evasys_inviter.php b/classes/evasys_inviter.php index 81edff4..6aa1495 100644 --- a/classes/evasys_inviter.php +++ b/classes/evasys_inviter.php @@ -331,4 +331,36 @@ public static function getmode($category) { $default = get_config('block_evasys_sync', 'default_evasys_mode'); return (bool)$default; } + + /** + * Alerts the Evaluation-coordinator of a given course, that a teacher has set a timeframe for the evaluation. + * @param $courseid int + * @param $startdate \DateTime Startdate + * @param $enddate \DateTime + * @throws \coding_exception + * @throws \dml_exception + */ + public static function alert_coordinator($courseid, $startdate, $enddate) { + global $USER; + $course = get_course($courseid); + $evacourses = course_evasys_courses_allocation::raw_get_evasyscourses($courseid); + $coursestring = ''; + foreach ($evacourses as $evacourse) { + // Add courses with two tabulators. + $coursestring .= "\t\t". $evacourse . "\n"; + } + $usercoordinator = evasys_synchronizer::get_assigned_user($course); + $data = array( + 'name' => $course->fullname, + 'teacher' => $USER->firstname . " " . $USER->lastname, + 'start' => $startdate->format('d.m.Y H:i:s'), + 'end' => $enddate->format('d.m.Y H:i:s'), + 'evasyscourses' => $coursestring + ); + $subject = get_string("alert_email_subject", "block_evasys_sync", $course->fullname); + $message = get_string("alert_email_body", "block_evasys_sync", $data); + + email_to_user($usercoordinator, $USER, $subject, $message, '', '' , '', + true, $USER->email, $USER->firstname . " " . $USER->lastname); + } } \ No newline at end of file diff --git a/invite.php b/invite.php index b0882d3..8bfc8d6 100644 --- a/invite.php +++ b/invite.php @@ -112,7 +112,7 @@ ) ); $event->trigger(); - +\block_evasys_sync\evasys_inviter::alert_coordinator($courseid, $startdate, $enddate); if ($invitedirect) { $invitedusers = count_enrolled_users(context_course::instance($courseid), 'block/evasys_sync:mayevaluate'); diff --git a/lang/de/block_evasys_sync.php b/lang/de/block_evasys_sync.php index f90ca83..ea3107d 100755 --- a/lang/de/block_evasys_sync.php +++ b/lang/de/block_evasys_sync.php @@ -73,6 +73,18 @@ $string['content_send_invalid'] = "Eine Evaluationsperiode beginnt nachdem sie endet!
" . "Alle anderen Evaluationsperioden wurden wie gewohnt geändert."; +// Alert Coordinator mail. +$string['alert_email_subject'] = 'Evaluationszeitraum gesetzt für {$a}'; +$string['alert_email_body'] = 'Sehr geehrte*r Evaluationskoordinator*in, ' . "\n" . + 'Sie erhalten diese E-Mail da im Learnweb-Kurs "{$a->name}" der Evaluationszeitraum wie folgt festgesetzt wurde:' . "\n\n" . + "\t".'Start: {$a->start}' . "\n" . + "\t".'Ende: {$a->end}' . "\n" . + "\t".'Verantwortliche*r: {$a->teacher}' . "\n" . + "\t".'EvaSys-IDs:' . "\n" . + '{$a->evasyscourses}' . "\n" . + 'Mit freundlichen Grüßen' . "\n" . + 'Ihr Learnweb-Support'; + // New invite strings. $string['title_success'] = "Erfolgreich"; $string['content_success_invite'] = "Die Evaluation wurde erfolgreich gestartet"; diff --git a/lang/en/block_evasys_sync.php b/lang/en/block_evasys_sync.php index b74b43c..737fa54 100755 --- a/lang/en/block_evasys_sync.php +++ b/lang/en/block_evasys_sync.php @@ -105,6 +105,18 @@ "This is not allowed. Some evaluationperiods may have been altered.
"; $string['content_confirm_reactivate'] = "Are you sure you want to reactivate the invite options? This will restart all surveys with at least one new participant"; +// Alert Coordinator mail. +$string['alert_email_subject'] = 'Evaluationszeitraum gesetzt für {$a}'; +$string['alert_email_body'] = 'Sehr geehrte*r Evaluationskoordinator*in, ' . "\n" . + 'Sie erhalten diese E-Mail da im Learnweb-Kurs "{$a->name}" der Evaluationszeitraum wie folgt festgesetzt wurde:' . "\n\n" . + "\t".'Start: {$a->start}' . "\n" . + "\t".'Ende: {$a->end}' . "\n" . + "\t".'Verantwortliche*r: {$a->teacher}' . "\n" . + "\t".'EvaSys-IDs:' . "\n" . + '{$a->evasyscourses}' . "\n" . + 'Mit freundlichen Grüßen' . "\n" . + 'Ihr Learnweb-Support'; + // New invite strings. $string['title_success'] = "Success"; $string['content_success_invite'] = "Evaluation has been started"; diff --git a/tests/courses_allocation_test.php b/tests/courses_allocation_test.php new file mode 100755 index 0000000..e96f036 --- /dev/null +++ b/tests/courses_allocation_test.php @@ -0,0 +1,143 @@ +. +/** + * Tests the class evasys_courses_allocation + * @package block_evasys_sync + * @category test + * @copyright 2019 T Reischmann + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); + +/** + * PHPUnit evasys testcase + * @package block_evasys_sync + * @category test + * @copyright 2019 T Reischmann + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_evasys_courses_allocation_testcase extends advanced_testcase { + + /** + * Tests if a course has a idnumber, but no allocation entry. + */ + public function test_idnumber_only() { + $this->resetAfterTest(true); + + $idnumber = '123456789'; + + $courseone = $this->getDataGenerator()->create_course(array('name' => 'First course', 'idnumber' => $idnumber)); + + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(1, $result); + $this->assertEquals($idnumber, array_pop($result)); + } + + /** + * Tests if a course has neither an idnumber, nor an allocation entry. + */ + public function test_no_idnumber() { + $this->resetAfterTest(true); + + $courseone = $this->getDataGenerator()->create_course(array('name' => 'First course')); + + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(0, $result); + } + + /** + * Tests if a course has an idnumber and an allocation entry. + */ + public function test_idnumber_and_allocation_entries() { + $this->resetAfterTest(true); + + $idnumber = 'idnumber'; + $evasysid1 = 'evasysid1'; + $evasysid2 = 'evasysid2'; + + $courseone = $this->getDataGenerator()->create_course(array('name' => 'First course', 'idnumber' => $idnumber)); + + $allocation = new \block_evasys_sync\course_evasys_courses_allocation(); + + // Add first evasysid. + $evasysids = array(); + $evasysids[] = $evasysid1; + $allocation->set('course', $courseone->id); + $allocation->set('evasyscourses', implode('#', $evasysids)); + $allocation->save(); + + // Query evasyscourses and test them. + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(2, $result); + $this->assertContains($idnumber, $result); + $this->assertContains($evasysid1, $result); + + // Add a second evasysid. + $evasysids[] = $evasysid2; + $allocation->set('evasyscourses', implode('#', $evasysids)); + $allocation->save(); + + // Query evasyscourses and test them. + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(3, $result); + $this->assertContains($idnumber, $result); + $this->assertContains($evasysid1, $result); + $this->assertContains($evasysid2, $result); + } + + /** + * Tests if a course has no idnumber, but an allocation entry. + */ + public function test_only_allocation_entries() { + $this->resetAfterTest(true); + + $evasysid1 = 'evasysid1'; + $evasysid2 = 'evasysid2'; + + $courseone = $this->getDataGenerator()->create_course(array('name' => 'First course')); + + $allocation = new \block_evasys_sync\course_evasys_courses_allocation(); + + // Add first evasysid. + $evasysids = array(); + $evasysids[] = $evasysid1; + $allocation->set('course', $courseone->id); + $allocation->set('evasyscourses', implode('#', $evasysids)); + $allocation->save(); + + // Query evasyscourses and test them. + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(1, $result); + $this->assertContains($evasysid1, $result); + + // Add a second evasysid. + $evasysids[] = $evasysid2; + $allocation->set('evasyscourses', implode('#', $evasysids)); + $allocation->save(); + + // Query evasyscourses and test them. + $result = \block_evasys_sync\course_evasys_courses_allocation::raw_get_evasyscourses($courseone->id); + + $this->assertCount(2, $result); + $this->assertContains($evasysid1, $result); + $this->assertContains($evasysid2, $result); + } +} \ No newline at end of file