Skip to content

Commit

Permalink
Workaround bug in core tablelib that strips all html characters and n…
Browse files Browse the repository at this point in the history
…ewlines from source code. See See https://tracker.moodle.org/browse/MDL-78342.
  • Loading branch information
trampgeek committed Nov 6, 2023
1 parent 7392b6f commit 3172598
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions getallattempts.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,58 @@
* as URL parameter quizid. The 'format' (csv or excel) is a required parameter
* too.
* The user must have grade:viewall permissions to run the script.
*
*
* @package qtype_coderunner
* @copyright 2017 Richard Lobb, The University of Canterbury
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/** In Moodle 4.0 through to at least 4.3, the core tablelib code has screwed up
* the formatting of data by stripping out all the html tags and newlines.
* See https://tracker.moodle.org/browse/MDL-78342
* Pending a resolution (how many years to wait?) this override bypasses
* the flawed function that adds data to the table.
*/

require_once(__DIR__.'/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->libdir.'/tablelib.php');
require_once($CFG->libdir.'/../mod/quiz/accessmanager.php');

class table_dataformat_export_format_fixed extends table_dataformat_export_format {
/**
* Add a row of data. This is where the Moodle version was screwed.
*
* @param array $row One record of data
*/
public function add_data($row) {
//if (!$this->supports_html()) {
// $row = $this->format_data($row);
//}
$this->dataformat->write_record($row, $this->rownum++);
return true;
}
};

class UnscrewedSqlTable extends table_sql {
/**
* Get, and optionally set, the export class.
* @param $exportclass (optional) if passed, set the table to use this export class.
* @return table_default_export_format_parent the export class in use (after any set).
*/
function export_class_instance($exportclass = null) {
if (!is_null($exportclass)) {
$this->started_output = true;
$this->exportclass = $exportclass;
$this->exportclass->table = $this;
} else if (is_null($this->exportclass) && !empty($this->download)) {
$this->exportclass = new table_dataformat_export_format_fixed($this, $this->download);
if (!$this->exportclass->document_started()) {
$this->exportclass->start_document($this->filename, $this->sheettitle);
}
}
return $this->exportclass;
}
}

// Get the quiz-id and format parameters from the URL.
$quizid = required_param('quizid', PARAM_INT);
Expand All @@ -39,7 +80,12 @@
// Login and check permissions.
require_login();

$quiz = quiz_access_manager::load_quiz_and_settings($quizid);
if (class_exists('mod_quiz\access_manager')) {
$quiz = mod_quiz\access_manager::load_quiz_and_settings($quizid);
} else { // Older versions of Moodle.
require_once($CFG->libdir.'/../mod/quiz/accessmanager.php');
$quiz = quiz_access_manager::load_quiz_and_settings($quizid);
}
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);

Expand All @@ -51,7 +97,7 @@
if (!has_capability('moodle/grade:viewall', $coursecontext)) {
echo '<p>' . get_string('unauthoriseddbaccess', 'qtype_coderunner') . '</p>';
} else {
$table = new table_sql(uniqid());
$table = new UnscrewedSqlTable(uniqid());
$fields = "concat(quiza.uniqueid, qasd.attemptstepid, qasd.id) as uniquekey,
quiza.uniqueid as quizattemptid,
timestart,
Expand Down Expand Up @@ -81,8 +127,8 @@
JOIN {quiz_slots} slot ON qatt.slot = slot.slot AND slot.quizid = quiza.quiz";

$where = "quiza.preview = 0
AND (qasd.name NOT RLIKE '^-_' OR qasd.name = '-_rawfraction')
AND (qasd.name NOT RLIKE '^_' OR qasd.name = '_testoutcome')
AND (qasd.name NOT LIKE '-_%' OR qasd.name = '-_rawfraction')
AND (qasd.name NOT LIKE '^_%' OR qasd.name = '_testoutcome')
AND quest.length > 0
ORDER BY quiza.uniqueid, timestamp";

Expand Down

0 comments on commit 3172598

Please sign in to comment.