Skip to content

Commit

Permalink
Fixes #60 and #61 - Courses without teachers page bug when course has…
Browse files Browse the repository at this point in the history
… been deleted and when course has no weight report
  • Loading branch information
oliviervalentin committed May 31, 2024
1 parent f5dc77a commit 0a0aca6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 69 deletions.
154 changes: 87 additions & 67 deletions admin_dashboard/courses_without_teachers.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,26 @@

echo html_writer::div(get_string('admin_no_teacher_courses_info', 'report_coursemanager'));
echo html_writer::div(get_string('adminnoteachercoursesnote', 'report_coursemanager'));
// If course weight is enabled, add warning that some courses may not appear if weight hasn't been calculated.
if (get_config('report_coursemanager', 'enable_course_content_task') == 1) {
echo html_writer::div(get_string('adminnoteachercoursesweight', 'report_coursemanager'));
}

// Check for entries in coursemanager table for courses without teachers.
$sqlnoteacherincourse = 'SELECT r1.course AS course, r2.detail AS weight
FROM {report_coursemanager_reports} r1
JOIN {report_coursemanager_reports} r2 ON r1.course = r2.course
WHERE r1.report = "no_teacher_in_course" AND r2.report = "weight"
ORDER BY r2.detail DESC
';
// If course weight is enabled, retrieve concerned courses AND their weight.
if (get_config('report_coursemanager', 'enable_course_content_task') == 1) {
$sqlnoteacherincourse = 'SELECT r1.course AS course, r2.detail AS weight
FROM {report_coursemanager_reports} r1
JOIN {report_coursemanager_reports} r2 ON r1.course = r2.course
WHERE r1.report = "no_teacher_in_course" AND r2.report = "weight"
ORDER BY r2.detail DESC
';
} else {
$sqlnoteacherincourse = 'SELECT r1.course AS course
FROM {report_coursemanager_reports} r1
WHERE r1.report = "no_teacher_in_course"
';
}
$paramsnoteacherincourse = [];
$existsnoteacherincourse = $DB->get_records_sql($sqlnoteacherincourse, $paramsnoteacherincourse);

Expand All @@ -114,76 +126,84 @@
$table->head[] = get_string('tablecountenrolledstudents', 'report_coursemanager');
$table->head[] = get_string('tablelastaccess', 'report_coursemanager');
$table->head[] = get_string('tablecountmodules', 'report_coursemanager');
$table->head[] = get_string('tablecourseweight', 'report_coursemanager');
// If course weight is enabled, add weight column.
if (get_config('report_coursemanager', 'enable_course_content_task') == 1) {
$table->head[] = get_string('tablecourseweight', 'report_coursemanager');
}
$table->head[] = get_string('tablelastteacherlog', 'report_coursemanager');
$table->head[] = get_string('tablelastteacher', 'report_coursemanager');
$table->head[] = get_string('table_actions', 'report_coursemanager');

// For each course, retrieve informations for table.
$selectnoteacherincourse = array_slice($existsnoteacherincourse, $page * $perpage, $perpage);
foreach ($selectnoteacherincourse as $course) {
$coursecontext = \context_course::instance($course->course);
// Retrieve course general information.
$courseinfo = $DB->get_record('course', ['id' => $course->course]);
// Count enrolled students.
$allstudents = count(get_role_users(get_config('report_coursemanager', 'student_role_report'), $coursecontext));

// Retrieve last user access to course.
$sqllastaccess = 'SELECT MAX(timeaccess) AS lastaccess
FROM {user_lastaccess}
WHERE courseid = ?';
$paramslastaccess = [$course->course];
$dbresultlastaccess = $DB->get_record_sql($sqllastaccess, $paramslastaccess);

// Calculate number of activities.
$sqlemptycourse = 'SELECT COUNT(mcm.id) AS count_modules
FROM {course} mc
INNER JOIN {course_modules} mcm ON (mc.id = mcm.course)
INNER JOIN {modules} mm ON (mcm.module = mm.id)
WHERE mc.id = ?
AND mm.name <> \'forum\'
';
$paramsemptycourse = [$course->course];
$dbresultemptycourse = $DB->count_records_sql($sqlemptycourse, $paramsemptycourse);

// Calculate last teacher log and retrieve name of the probable last teacher.
// Information based on edulevel field of logstore table.
// Ignore if the user is a site admin.
$adminlist = array_keys(get_admins());
list($notinsql, $notinparams) = $DB->get_in_or_equal($adminlist, SQL_PARAMS_NAMED, 'param', false);
$sqllastteacherlog = 'SELECT id, userid AS teacher, timecreated AS lastlog
FROM {logstore_standard_log}
WHERE timecreated = (SELECT MAX(timecreated)
if ($DB->record_exists('course', ['id' => $course->course])) {
$coursecontext = \context_course::instance($course->course);
// Retrieve course general information.
$courseinfo = $DB->get_record('course', ['id' => $course->course]);
// Count enrolled students.
$allstudents = count(get_role_users(get_config('report_coursemanager', 'student_role_report'), $coursecontext));

// Retrieve last user access to course.
$sqllastaccess = 'SELECT MAX(timeaccess) AS lastaccess
FROM {user_lastaccess}
WHERE courseid = ?';
$paramslastaccess = [$course->course];
$dbresultlastaccess = $DB->get_record_sql($sqllastaccess, $paramslastaccess);

// Calculate number of activities.
$sqlemptycourse = 'SELECT COUNT(mcm.id) AS count_modules
FROM {course} mc
INNER JOIN {course_modules} mcm ON (mc.id = mcm.course)
INNER JOIN {modules} mm ON (mcm.module = mm.id)
WHERE mc.id = ?
AND mm.name <> \'forum\'
';
$paramsemptycourse = [$course->course];
$dbresultemptycourse = $DB->count_records_sql($sqlemptycourse, $paramsemptycourse);

// Calculate last teacher log and retrieve name of the probable last teacher.
// Information based on edulevel field of logstore table.
// Ignore if the user is a site admin.
$adminlist = array_keys(get_admins());
list($notinsql, $notinparams) = $DB->get_in_or_equal($adminlist, SQL_PARAMS_NAMED, 'param', false);
$sqllastteacherlog = 'SELECT id, userid AS teacher, timecreated AS lastlog
FROM {logstore_standard_log}
WHERE courseid = :courseid
AND edulevel = 1
AND userid '. $notinsql. ')';
$paramslastteacherlog = ['courseid' => $course->course] + $notinparams;
$dbresultlastteacherlog = $DB->get_record_sql($sqllastteacherlog, $paramslastteacherlog);

if ($dbresultlastteacherlog) {
$lastteacher = $DB->get_record('user', ['id' => $dbresultlastteacherlog->teacher]);
$namelastteacher = $lastteacher->lastname.' '.$lastteacher->firstname;
$lastlog = date('d M Y, H:i:s', $dbresultlastteacherlog->lastlog);
} else {
$namelastteacher = get_string('unknown', 'report_coursemanager');
$lastlog = get_string('unknown', 'report_coursemanager');
WHERE timecreated = (SELECT MAX(timecreated)
FROM {logstore_standard_log}
WHERE courseid = :courseid
AND edulevel = 1
AND userid '. $notinsql. ')';
$paramslastteacherlog = ['courseid' => $course->course] + $notinparams;
$dbresultlastteacherlog = $DB->get_record_sql($sqllastteacherlog, $paramslastteacherlog);

if ($dbresultlastteacherlog) {
$lastteacher = $DB->get_record('user', ['id' => $dbresultlastteacherlog->teacher]);
$namelastteacher = $lastteacher->lastname.' '.$lastteacher->firstname;
$lastlog = date('d M Y, H:i:s', $dbresultlastteacherlog->lastlog);
} else {
$namelastteacher = get_string('unknown', 'report_coursemanager');
$lastlog = get_string('unknown', 'report_coursemanager');
}

// Now start to build table rows.
$row = [];
$row[] = html_writer::link("/course/view.php?id=".$courseinfo->id, $courseinfo->fullname);
$row[] = html_writer::label($allstudents, null);
$row[] = html_writer::label(date('d M Y, H:i:s', $dbresultlastaccess->lastaccess), null);
$row[] = html_writer::label($dbresultemptycourse, null);
// If course weight is enabled, add course weight.
if (get_config('report_coursemanager', 'enable_course_content_task') == 1) {
$row[] = html_writer::label($course->weight.' Mo', null);
}
$row[] = html_writer::label($lastlog, null);
$row[] = html_writer::label($namelastteacher, null);

$deletelink = "<a href='/report/coursemanager/admin_dashboard/courses_without_teachers.php?delete=1
&instance=".$courseinfo->id."'>".get_string('text_link_delete', 'report_coursemanager')."</a>";
$row[] = html_writer::label($deletelink, null);
$table->data[] = $row;
}

// Now start to build table rows.
$row = [];
$row[] = html_writer::link("/course/view.php?id=".$courseinfo->id, $courseinfo->fullname);
$row[] = html_writer::label($allstudents, null);
$row[] = html_writer::label(date('d M Y, H:i:s', $dbresultlastaccess->lastaccess), null);
$row[] = html_writer::label($dbresultemptycourse, null);
$row[] = html_writer::label($course->weight.' Mo', null);
$row[] = html_writer::label($lastlog, null);
$row[] = html_writer::label($namelastteacher, null);

$deletelink = "<a href='/report/coursemanager/admin_dashboard/courses_without_teachers.php?delete=1
&instance=".$courseinfo->id."'>".get_string('text_link_delete', 'report_coursemanager')."</a>";
$row[] = html_writer::label($deletelink, null);
$table->data[] = $row;
}

// Print the whole table.
Expand Down
3 changes: 2 additions & 1 deletion lang/en/report_coursemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@
// Admin page for courses without teachers.
$string['title_admin_no_teacher_courses'] = '<b>Manage courses without teachers</b>';
$string['admin_no_teacher_courses_info'] = '<p>This tool lists all courses where no user is enrolled as teacher, and possibly move them in bin category if needed.</p>';
$string['adminnoteachercoursesnote'] = '<ul class="alert alert-info"><li>Informations concerning course weight and number of activities are not calculated in real time.</li>
$string['adminnoteachercoursesnote'] = '<ul><li>Informations concerning course weight and number of activities are not calculated in real time.</li>
<li>Informations concerning last teacher log are based on edulevel filed in logstore database. Modified permissions can distort this result.</li></ul>';
$string['adminnoteachercoursesweight'] = '<ul class="alert alert-warning">The task for weight calculation is activated. Courses for which weight has not been calculated yet will not appear in this list. If necessary, launch task manually or wait for next cron run.';
$string['tablecountenrolledstudents'] = 'Students';
$string['tablelastaccess'] = 'Last access in course';
$string['tablehascontents'] = 'Number of contents';
Expand Down
3 changes: 2 additions & 1 deletion lang/fr/report_coursemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@
$string['title_admin_no_teacher_courses'] = '<b>Gestion des cours sans enseignants</b>';
$string['admin_no_teacher_courses_info'] = '<p>Les cours peuvent se retrouver sans enseignant si la personne qui détenait ce rôle se désinscrit intentionnellement ou non du cours,
ou si son compte n\'existe plus. Cet outil permet de recenser les cours concernés, et éventuellement de les basculer dans la catégorie Cours sans enseignants.</p>';
$string['adminnoteachercoursesnote'] = '<ul class="alert alert-info"><li>Les informations concernant le poids du cours et le nombre de modules ne sont pas calculés en temps réel,
$string['adminnoteachercoursesnote'] = '<ul><li>Les informations concernant le poids du cours et le nombre de modules ne sont pas calculés en temps réel,
mais proviennent des rapports automatiques.</li><li>Les informations concernant le dernier log enseignant sont déduites à partir du champ edulevel de la table des
logs et sont à titre indicatif. Les permissions modifiées dans un cours peuvent fausser ce résultat.</li></ul>';
$string['adminnoteachercoursesweight'] = '<ul class="alert alert-warning">La tâche calculant le poids des cours est activée. Les cours pour lesquels le poids n\'a pas encore été calculé n\'apparaitront pas dans cette liste. Si besoin, lancez la tâche manuellement ou attendez le prochain passage du cron.';
$string['tablecountenrolledstudents'] = 'Etudiants';
$string['tablelastaccess'] = 'Dernier accès au cours';
$string['tablehascontents'] = 'Nombre de contenus du cours';
Expand Down

0 comments on commit 0a0aca6

Please sign in to comment.