Skip to content

Commit

Permalink
Merge pull request #96 from catalyst/issue#28-MOODLE_39
Browse files Browse the repository at this point in the history
Issue#28 moodle 39
  • Loading branch information
Matt P authored Mar 18, 2021
2 parents d44d748 + 73596df commit cc50aad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
34 changes: 31 additions & 3 deletions classes/frequency.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,8 @@ public function get_frequency_array(int $year, string $metric, array $modules) :
* @return array $data The data for the download file.
*/
public function get_download_data(int $year, string $metric, array $modules) : array {
global $DB;

$data = array();
$events = array();
$from = mktime(0, 0, 0, 1, 1, $year);
Expand Down Expand Up @@ -1296,12 +1298,38 @@ public function get_download_data(int $year, string $metric, array $modules) : a
// Format the data ready for download.
foreach ($events as $event) {
$row = array();

// Catch exception when context does not exist because assessfreq tables are out of sync.
try {
$context = \context::instance_by_id($event->contextid);
} catch (\dml_missing_record_exception $ex) {
continue;
}

$activity = get_string('modulename', $event->module);
$date = userdate($event->timeend, get_string('strftimedatetimeshort', 'langconfig'));
$context = \context::instance_by_id($event->contextid);
$startdate = userdate($event->timestart, get_string('strftimedatetimeshort', 'langconfig'));
$duedate = userdate($event->timeend, get_string('strftimedatetimeshort', 'langconfig'));
$name = $context->get_context_name();
$url = $context->get_url()->out(false);

$row = array($date, $activity, $url);
if ($metric == 'assess') {
$usercount = count($this->get_event_users($event->contextid, $event->module));
$row = array($startdate, $duedate, $activity, $name, $url, $usercount);
} else if ($metric == 'students') {
$selects = get_extra_user_fields_sql($context);

$sql = "SELECT CONCAT(firstname, ' ', lastname) $selects
FROM {user}
WHERE id = ?";
$params = [$event->userid];
$userfields = $DB->get_record_sql($sql, $params);

$row = array($startdate, $duedate, $activity, $name, $url);

foreach ($userfields as $userfield) {
$row[] = $userfield;
}
}
$data[] = $row;
}

Expand Down
13 changes: 12 additions & 1 deletion download.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,20 @@

$dataformat = 'csv';
$fields = array(
get_string('quiztimeopen', 'local_assessfreq'),
get_string('duedate', 'local_assessfreq'),
get_string('activity', 'local_assessfreq'),
get_string('url', 'local_assessfreq'));
get_string('title', 'local_assessfreq'),
get_string('url', 'local_assessfreq')
);

if ($metric == 'students') {
$extrafields = get_extra_user_fields($context);
$fields[] = get_string('fullname');
$fields = array_merge($fields, $extrafields);
} else {
$fields[] = get_string('students', 'local_assessfreq');
}

$frequency = new \local_assessfreq\frequency();
$data = $frequency->get_download_data($year, $metric, $modules);
Expand Down
8 changes: 6 additions & 2 deletions tests/frequency_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,12 @@ public function test_get_download_data() {

$result = $frequency->get_download_data($year, $metric, $modules);

$this->assertRegexp('/mod\/assign\/view/', $result[0][2]);
$this->assertRegexp('/mod\/assign\/view/', $result[1][2]);
$this->assertRegexp('/mod\/assign\/view/', $result[0][4]);
$this->assertRegexp('/mod\/assign\/view/', $result[1][4]);

// Test number of students for each assessment.
$this->assertEquals(2, $result[0][5]);
$this->assertEquals(2, $result[1][5]);
}

/**
Expand Down

0 comments on commit cc50aad

Please sign in to comment.