From c553d7c3f6b2d2b731bb812489908fa142241fd1 Mon Sep 17 00:00:00 2001 From: oliviervalentin Date: Tue, 28 May 2024 10:15:27 +0200 Subject: [PATCH] Fixes #57 and # 58 - Refactor code for orphaned submissions task and admin page. Improvments for admin pages with a paging bar --- admin_dashboard/courses_without_teachers.php | 7 +- admin_dashboard/orphaned_submissions.php | 98 +++++-------------- admin_dashboard/stats.php | 22 +++-- classes/task/mailing_task.php | 10 +- .../run_orphan_submissions_report_task.php | 5 +- db/install.xml | 62 ++++++------ db/upgrade.php | 13 +++ lang/en/report_coursemanager.php | 4 +- lang/fr/report_coursemanager.php | 5 +- version.php | 4 +- 10 files changed, 102 insertions(+), 128 deletions(-) diff --git a/admin_dashboard/courses_without_teachers.php b/admin_dashboard/courses_without_teachers.php index 6bdc9c8..69046ec 100644 --- a/admin_dashboard/courses_without_teachers.php +++ b/admin_dashboard/courses_without_teachers.php @@ -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')); @@ -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(); diff --git a/admin_dashboard/orphaned_submissions.php b/admin_dashboard/orphaned_submissions.php index 81b47ba..9fa6de6 100644 --- a/admin_dashboard/orphaned_submissions.php +++ b/admin_dashboard/orphaned_submissions.php @@ -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(); @@ -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(); @@ -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, @@ -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 = "".get_string('deleteorphans', 'report_coursemanager').""; - $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 = "".get_string('deleteorphans', 'report_coursemanager').""; + $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(); diff --git a/admin_dashboard/stats.php b/admin_dashboard/stats.php index 2464fdc..e1bf451 100644 --- a/admin_dashboard/stats.php +++ b/admin_dashboard/stats.php @@ -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. @@ -187,10 +191,10 @@
-
'.get_string('stats_courses_orphan_submissions', 'report_coursemanager').'
+
'.get_string('stats_files_orphan_submissions', 'report_coursemanager').'
' - .get_string('stats_courses_orphan_submissions_desc', 'report_coursemanager').' -

'.$countorphansubmissionscourses.'

+ .get_string('stats_files_orphan_submissions_desc', 'report_coursemanager').' +

'.$totalfilesorphans.'

'; @@ -201,7 +205,7 @@
'.get_string('stats_weight_courses_orphan_submissions', 'report_coursemanager').'
' .get_string('stats_weight_courses_orphan_submissions_desc', 'report_coursemanager').' -

'.$totalorphans.' Mo

+

'.$totalsizeorphans.' Mo

'; diff --git a/classes/task/mailing_task.php b/classes/task/mailing_task.php index c7a5ce0..5dbf8a9 100644 --- a/classes/task/mailing_task.php +++ b/classes/task/mailing_task.php @@ -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') { diff --git a/classes/task/run_orphan_submissions_report_task.php b/classes/task/run_orphan_submissions_report_task.php index 63283b0..d5fd339 100644 --- a/classes/task/run_orphan_submissions_report_task.php +++ b/classes/task/run_orphan_submissions_report_task.php @@ -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); @@ -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(); diff --git a/db/install.xml b/db/install.xml index 703cc4b..c1150d6 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,32 +1,32 @@ - - - - - - - - - - - - - -
- - - - - - - - - - - - -
-
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+
\ No newline at end of file diff --git a/db/upgrade.php b/db/upgrade.php index afa6a9a..bf471cb 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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; } diff --git a/lang/en/report_coursemanager.php b/lang/en/report_coursemanager.php index 0e893ff..cb5c308 100644 --- a/lang/en/report_coursemanager.php +++ b/lang/en/report_coursemanager.php @@ -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'; diff --git a/lang/fr/report_coursemanager.php b/lang/fr/report_coursemanager.php index 0659c66..8a5e484 100644 --- a/lang/fr/report_coursemanager.php +++ b/lang/fr/report_coursemanager.php @@ -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'; @@ -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.'; diff --git a/version.php b/version.php index 60126c6..286a298 100644 --- a/version.php +++ b/version.php @@ -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;