Skip to content

Commit

Permalink
Fixes #57 and # 58 - Refactor code for orphaned submissions task and …
Browse files Browse the repository at this point in the history
…admin page. Improvments for admin pages with a paging bar
  • Loading branch information
oliviervalentin committed May 28, 2024
1 parent 9d224eb commit c553d7c
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 128 deletions.
7 changes: 3 additions & 4 deletions admin_dashboard/courses_without_teachers.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$instance = optional_param('instance', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 10, PARAM_INT);

$site = get_site();

$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 2, PARAM_INT); // how many per page

$PAGE = new moodle_page();
$PAGE->set_context(context_system::instance());
$PAGE->set_heading(get_string('title', 'report_coursemanager'));
Expand Down Expand Up @@ -95,7 +94,7 @@
echo html_writer::div(get_string('adminnoteachercoursesnote', 'report_coursemanager'));

// Checl for entries in coursemanager table for courses without teachers.
$existsnoteacherincourse = $DB->get_records('report_coursemanager_reports', ['report' => 'no_teacher_in_course']);
$existsnoteacherincourse = $DB->get_records('report_coursemanager_reports', ['report' => 'no_teacher_in_course'], ['weight DESC']);

if (count($existsnoteacherincourse) > 0) {
$table = new html_table();
Expand Down
98 changes: 26 additions & 72 deletions admin_dashboard/orphaned_submissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$instance = optional_param('instance', 0, PARAM_INT);
$course = optional_param('course', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 10, PARAM_INT);

$site = get_site();

Expand Down Expand Up @@ -92,6 +95,8 @@
foreach ($dbresultlistusersorphansubmissions as $userorphan) {
$delete = $targetassign->remove_submission($userorphan->id);
}
// Now that files are deleted, delete report entry.
$purgereport = $DB->delete_records('report_coursemanager_orphans', ['cmid' => $instance, 'course' => $course, ]);
$returnurl = "orphaned_submissions.php";
redirect($returnurl);
exit();
Expand All @@ -106,7 +111,7 @@
echo $OUTPUT->heading(get_string('title_admin_orphan_submissions', 'report_coursemanager'));

$urlconfirmdelete = new moodle_url('orphaned_submissions.php',
['confirm' => 1, 'delete' => 1, 'instance' => $instance, 'sesskey' => sesskey()]);
['confirm' => 1, 'delete' => 1, 'instance' => $instance, 'course' => $course, 'sesskey' => sesskey()]);

echo $OUTPUT->confirm(get_string('deleteorphansubmissionsconfirm', 'report_coursemanager'),
$urlconfirmdelete,
Expand All @@ -132,78 +137,27 @@
$table->head[] = get_string('table_files_weight', 'report_coursemanager');
$table->head[] = get_string('table_actions', 'report_coursemanager');

$listcourses = get_courses();
foreach ($listcourses as $course) {

$sql = 'SELECT cm.instance, a.name, cm.id
FROM {course_modules} cm
JOIN {course} c ON c.id = cm.course
JOIN {modules} m ON m.id = cm.module
JOIN {assign} a ON a.id = cm.instance
WHERE m.name =\'assign\'
AND c.id = ?';
$paramsdb = [$course->id];
$dbresult = $DB->get_records_sql($sql, $paramsdb);

if (count($dbresult) > 0) {
foreach ($dbresult as $assigninstance) {
$sqlassignsorphans = "SELECT DISTINCT(f.filesize) AS filesize
FROM
{files} AS f,
{assignsubmission_file} AS asf,
{assign} AS a,
{user} AS u,
{course} AS c,
{course_modules} AS cm
WHERE
component = 'assignsubmission_file'
AND asf.submission=f.itemid
AND a.id = asf.assignment
AND f.userid = u.id
AND filename != '.'
AND c.id = a.course
AND a.id = ?
AND a.id = cm.instance
AND u.id NOT IN
(SELECT us.id
FROM
{course} AS course,
{enrol} AS en,
{user_enrolments} AS ue,
{user} AS us
WHERE c.id=course.id
AND en.courseid = course.id
AND ue.enrolid = en.id
AND us.id = ue.userid
)
GROUP BY filesize
";
$paramsdbassignsorphans = [$assigninstance->instance];
$dbresultassignsorphans = $DB->get_records_sql($sqlassignsorphans, $paramsdbassignsorphans);

if ($dbresultassignsorphans) {
$row = [];
$totalsize = 0;
$totalfiles = 0;
foreach ($dbresultassignsorphans as $orphansubmission) {
$totalsize += $orphansubmission->filesize;
$totalfiles = $totalfiles + 1;
}
$row[] = html_writer::link("/course/view.php?id=".$course->id, $course->fullname);
$row[] = html_writer::link("/mod/assign/view.php?id=".$assigninstance->id, $assigninstance->name);
$row[] = html_writer::label($totalfiles, null);
$row[] = html_writer::label(number_format(ceil($totalsize / 1048576), 0, ',', '')." Mo", null);
$content = "<a href='/report/coursemanager/admin_dashboard/orphaned_submissions.php?delete=1
&instance=".$assigninstance->id."'>".get_string('deleteorphans', 'report_coursemanager')."</a>";
$row[] = html_writer::label($content, null);
$table->data[] = $row;

}
}
} else {
$row[] = html_writer::label(get_string('noassign', 'report_coursemanager'), null);
}
// Let's retrieve all records in orphans table.
$listassigns = $DB->get_records('report_coursemanager_orphans', [], 'weight DESC');
$selectedassigns = array_slice($listassigns, $page * $perpage, $perpage);
foreach ($selectedassigns as $assign) {
$cm = get_coursemodule_from_id('assign', $assign->cmid);
$course = $DB->get_record('course', array('id' => $assign->course));
// $context = context_module::instance($cm->id);
$row = [];
$row[] = html_writer::link("/course/view.php?id=".$assign->course, $course->fullname);
$row[] = html_writer::link("/mod/assign/view.php?id=".$assign->cmid, $cm->name);
$row[] = html_writer::label($assign->files, null);
$row[] = html_writer::label(number_format(ceil($assign->weight / 1048576), 0, ',', '')." Mo", null);
$content = "<a href='/report/coursemanager/admin_dashboard/orphaned_submissions.php?delete=1
&instance=".$assign->cmid."&course=".$assign->course."'>".get_string('deleteorphans', 'report_coursemanager')."</a>";
$row[] = html_writer::label($content, null);
$table->data[] = $row;
}

echo html_writer::table($table);

$baseurl = new moodle_url('/report/coursemanager/admin_dashboard/orphaned_submissions.php', array('perpage' => $perpage));
echo $OUTPUT->paging_bar(count($listassigns), $page, $perpage, $baseurl);

echo $OUTPUT->footer();
22 changes: 13 additions & 9 deletions admin_dashboard/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@
$countemptycourses = $DB->count_records('report_coursemanager_reports', ['report' => 'empty']);

// Count courses with orphan submissions in Course Manager table.
$countorphansubmissionscourses = $DB->count_records('report_coursemanager_reports', ['report' => 'orphan_submissions']);
$countorphansubmissionscourses = $DB->count_records('report_coursemanager_orphans');

// Sum filesize in Mo for orphan submissions.
if (!empty($countorphansubmissionscourses)) {
$sqltotalorphans = "SELECT ROUND(SUM(detail)/1024/1024)
FROM {report_coursemanager_reports}
WHERE report = 'orphan_submissions'
$sqltotalsizeorphans = "SELECT ROUND(SUM(weight)/1024/1024)
FROM {report_coursemanager_orphans}
";
$totalorphans = $DB->get_field_sql($sqltotalorphans);
$totalsizeorphans = $DB->get_field_sql($sqltotalsizeorphans);

$sqltotalfilesorphans = "SELECT SUM(files)
FROM {report_coursemanager_orphans}
";
$totalfilesorphans = $DB->get_field_sql($sqltotalfilesorphans);
}

// Count courses without teachers in Course Manager table.
Expand Down Expand Up @@ -187,10 +191,10 @@
</div>
<div class="card text-center m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">'.get_string('stats_courses_orphan_submissions', 'report_coursemanager').'</h5>
<h5 class="card-title">'.get_string('stats_files_orphan_submissions', 'report_coursemanager').'</h5>
<small class="card-subtitle mb-2 text-muted">'
.get_string('stats_courses_orphan_submissions_desc', 'report_coursemanager').'</small>
<p class="card-text display-4"><i class="fa fa fa-files-o"></i> '.$countorphansubmissionscourses.'</p>
.get_string('stats_files_orphan_submissions_desc', 'report_coursemanager').'</small>
<p class="card-text display-4"><i class="fa fa fa-files-o"></i> '.$totalfilesorphans.'</p>
</div>
</div>
';
Expand All @@ -201,7 +205,7 @@
<h5 class="card-title">'.get_string('stats_weight_courses_orphan_submissions', 'report_coursemanager').'</h5>
<small class="card-subtitle mb-2 text-muted">'
.get_string('stats_weight_courses_orphan_submissions_desc', 'report_coursemanager').'</small>
<p class="card-text display-4"><i class="fa fa-files-o"></i> '.$totalorphans.' Mo</p>
<p class="card-text display-4"><i class="fa fa-files-o"></i> '.$totalsizeorphans.' Mo</p>
</div>
</div>
';
Expand Down
10 changes: 8 additions & 2 deletions classes/task/mailing_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ public function execute() {
// For each report, we test each course for a teacher.
foreach ($dbresultlistcoursesforteacher as $listcourse) {
// If a report exists for a course, add course name to the list with direct link.
$checkreport = $DB->get_record('report_coursemanager_reports',
['course' => $listcourse->courseid, 'report' => $report['report']]);
if ($report['report'] == 'orphan_submissions') {
$checkreport = $DB->get_records('report_coursemanager_orphans',
['course' => $listcourse->courseid]);
} else {
$checkreport = $DB->get_record('report_coursemanager_reports',
['course' => $listcourse->courseid, 'report' => $report['report']]);
}

if (!empty($checkreport)) {
// Heavy report leads to the specific page about course files.
if ($report['report'] == 'heavy') {
Expand Down
5 changes: 2 additions & 3 deletions classes/task/run_orphan_submissions_report_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function execute() {
AND instance = ?
';
$dbresultcontextid = $DB->get_record_sql($sqlcontextid, [$assign->id]);

$cm = get_coursemodule_from_id('assign', $dbresultcontextid->id);
$context = \context_module::instance($cm->id);

Expand Down Expand Up @@ -90,13 +89,13 @@ public function execute() {
foreach ($dbresultassignsorphans as $orphan) {
// First check if this report exists.
$existsorphans = $DB->get_record('report_coursemanager_orphans',
['course' => $assign->course, 'contextid' => $context->id]);
['course' => $assign->course, 'cmid' => $cm->id]);

if ($orphan->total_files > 0) {
// Orphaned submissions detected for this assign, create or update entry.
$data = new \stdClass();
$data->course = $assign->course;
$data->contextid = $context->id;
$data->cmid = $cm->id;
$data->weight = $orphan->total_size;
$data->files = $orphan->total_files;
$data->timecreated = time();
Expand Down
62 changes: 31 additions & 31 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="report/coursemanager/db" VERSION="20230413" COMMENT="XMLDB file for Moodle report_coursemanager"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="report_coursemanager_reports" COMMENT="Stores observations for each course.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the course this activity is part of."/>
<FIELD NAME="report" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Comment to observ on this course"/>
<FIELD NAME="detail" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Details concerning this comment."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="report_coursemanager_orphans" COMMENT="Stores reports for assigns containing orphan submissions.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the course this activity is part of."/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Context id for this activity."/>
<FIELD NAME="weight" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Total orphaned files size."/>
<FIELD NAME="files" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Total count of orphaned files."/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Date of report."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="report/coursemanager/db" VERSION="20230413" COMMENT="XMLDB file for Moodle report_coursemanager"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="report_coursemanager_reports" COMMENT="Stores observations for each course.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the course this activity is part of."/>
<FIELD NAME="report" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Comment to observ on this course"/>
<FIELD NAME="detail" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Details concerning this comment."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="report_coursemanager_orphans" COMMENT="Stores reports for assigns containing orphan submissions.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the course this activity is part of."/>
<FIELD NAME="cmid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Course module id for this activity."/>
<FIELD NAME="weight" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Total orphaned files size."/>
<FIELD NAME="files" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Total count of orphaned files."/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" SEQUENCE="false" COMMENT="Date of report."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
13 changes: 13 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,18 @@ function xmldb_report_coursemanager_upgrade($oldversion) {
// Separating each report in a different task.
upgrade_plugin_savepoint(true, 2024050302, 'report', 'coursemanager');
}
if ($oldversion < 2024050304) {
// Redesign orphan submissions admin page needs to rename report_coursemanager_orphans field in database.
$table = new xmldb_table('report_coursemanager_orphans');
$field = new xmldb_field('contextid');
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'course');

if ($dbman->table_exists($table)) {
$dbman->rename_field($table, $field, 'cmid', $continue = true, $feedback = true);
}
// Must empty report_coursemanager_orphans table and rerun task for recording new values.
$DB->delete_records('report_coursemanager_orphans');
upgrade_plugin_savepoint(true, 2024050304, 'report', 'coursemanager');
}
return true;
}
4 changes: 2 additions & 2 deletions lang/en/report_coursemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@
$string['stats_heavy_courses_desc'] = 'Courses weighing more than {$a->totalfilesizethreshold} Mo (threshold defined in Course Manager settings).';
$string['stats_empty_courses'] = 'Empty courses';
$string['stats_empty_courses_desc'] = 'Number of courses having only native Forum for activity.';
$string['stats_courses_orphan_submissions'] = 'Courses with orphan submissions';
$string['stats_courses_orphan_submissions_desc'] = 'Number of courses having Assign activities containing submissions belonging to unenrolled users.';
$string['stats_files_orphan_submissions'] = 'Total of orphan submissions files';
$string['stats_files_orphan_submissions_desc'] = 'Number of files considered as submissions belonging to unenrolled users.';
$string['stats_weight_courses_orphan_submissions'] = 'Weight for orphan submissions';
$string['stats_weight_courses_orphan_submissions_desc'] = 'Total filesize for orphan submissions';
$string['stats_heaviest_course'] = 'Heaviest course';
Expand Down
5 changes: 2 additions & 3 deletions lang/fr/report_coursemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@
$string['stats_heavy_courses_desc'] = 'Cours pesant plus de {$a->totalfilesizethreshold} Mo (limite définie dans les paramètres de Course Manager).';
$string['stats_empty_courses'] = 'Cours vides';
$string['stats_empty_courses_desc'] = 'Nombre de cours contenant uniquement le forum des annonces.';
$string['stats_courses_orphan_submissions'] = 'Cours contenant des devoirs orphelins';
$string['stats_courses_orphan_submissions_desc'] = 'Nombre de cours avec des Devoirs contenant des fichiers déposés par des étudiants désinscrits.';
$string['stats_files_orphan_submissions'] = 'Total de devoirs orphelins';
$string['stats_files_orphan_submissions_desc'] = 'Nombre de fichiers déposés comme devoirs par des étudiants désinscrits.';
$string['stats_weight_courses_orphan_submissions'] = 'Poids des devoirs orphelins';
$string['stats_weight_courses_orphan_submissions_desc'] = 'Somme des fichiers considérés comme devoirs orphelins';
$string['stats_heaviest_course'] = 'Cours le plus lourd';
Expand All @@ -299,7 +299,6 @@
$string['stats_count_courses_without_visit_students'] = 'Cours sans visite d\'étudiants';
$string['stats_count_courses_without_visit_students_desc'] = 'Nombre de cours où aucun étudiant ne s\'est connecté depuis plus de {$a->lastaccessstudent} jours.';


// Confirmation alert on dashboard.
$string['confirm_cohort_unenrolled_title'] = 'Cohortes supprimées';
$string['confirm_cohort_unenrolled_message'] = 'Les cohortes ont bien été désinscrites du cours.';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024050302;
$plugin->release = '3.2.0';
$plugin->version = 2024050304;
$plugin->release = '3.2.1';
$plugin->requires = 2020061516;
$plugin->component = 'report_coursemanager';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit c553d7c

Please sign in to comment.