Merge branch 'MDL-69819-master' of https://github.com/davewoloszyn/moodle

This commit is contained in:
Ilya Tregubov 2023-11-23 10:35:07 +08:00
commit 61c53f00d2
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
5 changed files with 83 additions and 8 deletions

View File

@ -553,6 +553,8 @@ class mod_feedback_completion extends mod_feedback_structure {
global $SESSION, $DB, $USER;
$feedbackcompleted = $this->find_last_completed();
// If no record is found, change false to null for safe use in feedback_save_tmp_values.
$feedbackcompleted = !$feedbackcompleted ? null : $feedbackcompleted;
$feedbackcompletedtmp = $this->get_current_completed_tmp();
if (feedback_check_is_switchrole()) {

View File

@ -102,9 +102,10 @@ class feedback_item_textarea extends feedback_item_base {
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @param bool $excel Indicate if being used for Excel
* @return stdClass
*/
protected function get_analysed($item, $groupid = false, $courseid = false) {
protected function get_analysed($item, $groupid = false, $courseid = false, bool $excel = false) {
global $DB;
$analysed_val = new stdClass();
@ -115,7 +116,8 @@ class feedback_item_textarea extends feedback_item_base {
if ($values) {
$data = array();
foreach ($values as $value) {
$data[] = str_replace("\n", '<br />', $value->value);
// Convert line breaks except for Excel.
$data[] = $excel ? $value->value : str_replace("\n", '<br />', $value->value);
}
$analysed_val->data = $data;
}
@ -158,11 +160,11 @@ class feedback_item_textarea extends feedback_item_base {
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
$analyseditem = $this->get_analysed($item, $groupid, $courseid, true);
$worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
$worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
$data = $analysed_item->data;
$data = $analyseditem->data;
if (is_array($data)) {
if (isset($data[0])) {
$worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);

View File

@ -1878,10 +1878,10 @@ function feedback_set_tmp_values($feedbackcompleted) {
*
* @global object
* @param object $feedbackcompletedtmp the temporary completed
* @param object $feedbackcompleted the target completed
* @param stdClass|null $feedbackcompleted the target completed
* @return int the id of the completed
*/
function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted) {
function feedback_save_tmp_values($feedbackcompletedtmp, ?stdClass $feedbackcompleted = null) {
global $DB;
$tmpcplid = $feedbackcompletedtmp->id;

View File

@ -212,7 +212,7 @@ class events_test extends \advanced_testcase {
// Save the feedback.
$sink = $this->redirectEvents();
$id = feedback_save_tmp_values($completed, false);
$id = feedback_save_tmp_values($completed);
$events = $sink->get_events();
$event = array_pop($events); // Response submitted feedback event.
$sink->close();
@ -248,7 +248,7 @@ class events_test extends \advanced_testcase {
// Save the feedback.
$sink = $this->redirectEvents();
feedback_save_tmp_values($completed, false);
feedback_save_tmp_values($completed);
$events = $sink->get_events();
$event = array_pop($events); // Response submitted feedback event.
$sink->close();

View File

@ -0,0 +1,71 @@
<?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/>.
namespace mod_feedback;
use advanced_testcase;
use ReflectionClass;
/**
* This file contains unit tests for the mod_feedback items.
*
* @package mod_feedback
* @copyright 2020 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
* @author 2023 David Woloszyn <david.woloszyn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_test extends advanced_testcase {
/**
* Test that get_analysed() for textarea item returns correctly formatted data for exporting to Excel.
*
* @covers ::get_analysed
*/
public function test_get_analysed_textarea_for_excel_export(): void {
global $DB;
$this->resetAfterTest();
// Create a course, a feedback activity and an item.
$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course]);
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
$item = $feedbackgenerator->create_item_textfield($feedback);
// Expected text.
$valuetext = "First line\nSecond line";
// Create a temporary response.
$completedid = $DB->insert_record('feedback_completedtmp', (object)['feedback' => $feedback->id]);
$completed = $DB->get_record('feedback_completedtmp', ['id' => $completedid], '*', MUST_EXIST);
$value = (object)['course_id' => $course->id, 'item' => $item->id, 'completed' => $completedid, 'value' => $valuetext];
$DB->insert_record('feedback_valuetmp', $value);
feedback_save_tmp_values($completed);
// Set get_analysed() method accessibility.
$itemclass = feedback_get_item_class('textarea');
$reflection = new ReflectionClass($itemclass);
$method = $reflection->getMethod('get_analysed');
$method->setAccessible(true);
// Call the method and indicate it is being used for Excel.
$actual = $method->invoke(new $itemclass(), $item, false, $course->id, true);
// Check returned data maintains the line break.
$this->assertCount(1, $actual->data);
$datum = reset($actual->data);
$this->assertEquals($valuetext, $datum);
}
}