fix: Extract user selection components to classes
Extracts and refactors user selection logic by moving user selector and form functionality into separate class files. This enhances code modularity and reusability across different parts of the application. Adds a detailed user grades report page that displays quiz attempts and grades for selected users, improving user grade reporting. Updates admin settings to include detailed report page link for navigation ease.
This commit is contained in:
parent
d3eed13d51
commit
e3fd691910
5 changed files with 147 additions and 48 deletions
29
classes/user_selector.php
Normal file
29
classes/user_selector.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class user_selector extends user_selector_base
|
||||||
|
{
|
||||||
|
|
||||||
|
public function find_users($search)
|
||||||
|
{
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
// Define the fields you want to select.
|
||||||
|
$fields = 'id, firstname, lastname, email';
|
||||||
|
|
||||||
|
// Create an SQL query to find users based on the search criteria.
|
||||||
|
$sql = "SELECT $fields
|
||||||
|
FROM {user}
|
||||||
|
WHERE CONCAT(firstname, ' ', lastname) LIKE ? OR email LIKE ?";
|
||||||
|
|
||||||
|
// Execute the SQL query and retrieve the users.
|
||||||
|
$users = $DB->get_records_sql($sql, array('%' . $search . '%', '%' . $search . '%'));
|
||||||
|
|
||||||
|
// Format the users into a format expected by the user selector.
|
||||||
|
$options = [];
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$options[$user->id] = $user->firstname . ' ' . $user->lastname . ' (' . $user->email . ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(get_string('users') => $options);
|
||||||
|
}
|
||||||
|
}
|
21
classes/usergrades_form.php
Normal file
21
classes/usergrades_form.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Form class for user selection
|
||||||
|
*/
|
||||||
|
class usergrades_form extends moodleform
|
||||||
|
{
|
||||||
|
public function definition()
|
||||||
|
{
|
||||||
|
$mform = $this->_form;
|
||||||
|
|
||||||
|
// User selector
|
||||||
|
$user_selector = new user_selector('userid', array('multiselect' => false));
|
||||||
|
$users = $user_selector->find_users('');
|
||||||
|
|
||||||
|
$mform->addElement('select', 'userid', get_string('selectuser', 'report_usergrades'), $users[get_string('users')]);
|
||||||
|
|
||||||
|
// Add form action buttons
|
||||||
|
$this->add_action_buttons(false, get_string('showgrades', 'report_usergrades'));
|
||||||
|
}
|
||||||
|
}
|
88
details.php
Normal file
88
details.php
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
require_once('../../config.php');
|
||||||
|
require_once($CFG->libdir . '/adminlib.php');
|
||||||
|
require_once($CFG->libdir . '/gradelib.php');
|
||||||
|
require_once($CFG->libdir . '/tablelib.php');
|
||||||
|
require_once($CFG->libdir . '/formslib.php');
|
||||||
|
require_once($CFG->dirroot . '/user/selector/lib.php');
|
||||||
|
require_once('classes/user_selector.php');
|
||||||
|
require_once('classes/usergrades_form.php');
|
||||||
|
|
||||||
|
// Set up the page context and other configurations
|
||||||
|
admin_externalpage_setup('report_usergrades_details', '', null, '', array('capability' => 'report/usergrades:view'));
|
||||||
|
|
||||||
|
// Instantiate the form
|
||||||
|
$mform = new usergrades_form();
|
||||||
|
echo $OUTPUT->header();
|
||||||
|
echo $OUTPUT->heading(get_string('usergradesreport', 'report_usergrades'));
|
||||||
|
|
||||||
|
// Form processing and displaying is done here
|
||||||
|
if ($mform->is_cancelled()) {
|
||||||
|
// Handle form cancellation, if necessary
|
||||||
|
redirect(new moodle_url('/admin/report.php'));
|
||||||
|
} else if ($data = $mform->get_data()) {
|
||||||
|
$userid = $data->userid;
|
||||||
|
|
||||||
|
// Fetch and display user grades if a user is selected
|
||||||
|
if (!empty($userid)) {
|
||||||
|
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
|
||||||
|
$courses = enrol_get_users_courses($user->id, true);
|
||||||
|
|
||||||
|
foreach ($courses as $course) {
|
||||||
|
// Basic information about the course
|
||||||
|
echo $OUTPUT->heading($course->fullname, 3);
|
||||||
|
|
||||||
|
// Fetch the questions in the course
|
||||||
|
$questions = $DB->get_records('question', array('course' => $course->id));
|
||||||
|
|
||||||
|
// Fetch all quizzes in the course
|
||||||
|
$quizzes = $DB->get_records('quiz', array('course' => $course->id));
|
||||||
|
|
||||||
|
if ($quizzes) {
|
||||||
|
echo $OUTPUT->heading(get_string('quizzes', 'report_usergrades'), 4);
|
||||||
|
|
||||||
|
foreach ($quizzes as $quiz) {
|
||||||
|
// Fetch the quiz questions
|
||||||
|
$quiz_questions = $DB->get_records('quiz_question_instances', array('quiz' => $quiz->id));
|
||||||
|
|
||||||
|
// Fetch the quiz attempts
|
||||||
|
$quiz_attempts = $DB->get_records('quiz_attempts', array('quiz' => $quiz->id, 'userid' => $user->id));
|
||||||
|
|
||||||
|
if ($quiz_attempts) {
|
||||||
|
echo $OUTPUT->heading($quiz->name, 5);
|
||||||
|
|
||||||
|
foreach ($quiz_attempts as $attempt) {
|
||||||
|
echo $OUTPUT->heading(get_string('attempt', 'report_usergrades') . ' ' . $attempt->attempt, 6);
|
||||||
|
|
||||||
|
$attempt_table = new html_table();
|
||||||
|
$attempt_table->head = array(get_string('question', 'report_usergrades'), get_string('response', 'report_usergrades'), get_string('grade', 'report_usergrades'));
|
||||||
|
|
||||||
|
foreach ($quiz_questions as $quiz_question) {
|
||||||
|
$question = $questions[$quiz_question->question];
|
||||||
|
|
||||||
|
$response = $DB->get_record('question_states', array('attempt' => $attempt->id, 'question' => $question->id));
|
||||||
|
$grade = $DB->get_record('question_attempt_steps', array('questionattemptid' => $response->id));
|
||||||
|
|
||||||
|
$attempt_table->data[] = array($question->name, $response->answer, $grade->fraction);
|
||||||
|
}
|
||||||
|
|
||||||
|
echo html_writer::table($attempt_table);
|
||||||
|
|
||||||
|
$total_grade = $DB->get_record('quiz_grades', array('quiz' => $quiz->id, 'userid' => $user->id));
|
||||||
|
|
||||||
|
echo $OUTPUT->heading(get_string('totalgrade', 'report_usergrades') . ': ' . $total_grade->grade, 6);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo $OUTPUT->notification(get_string('nouserselected', 'report_usergrades'), 'notifyproblem');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Display form if no data is submitted
|
||||||
|
$mform->display();
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $OUTPUT->footer();
|
50
index.php
50
index.php
|
@ -5,58 +5,12 @@ require_once($CFG->libdir . '/gradelib.php');
|
||||||
require_once($CFG->libdir . '/tablelib.php');
|
require_once($CFG->libdir . '/tablelib.php');
|
||||||
require_once($CFG->libdir . '/formslib.php');
|
require_once($CFG->libdir . '/formslib.php');
|
||||||
require_once($CFG->dirroot . '/user/selector/lib.php');
|
require_once($CFG->dirroot . '/user/selector/lib.php');
|
||||||
|
require_once('classes/user_selector.php');
|
||||||
|
require_once('classes/usergrades_form.php');
|
||||||
|
|
||||||
// Set up the page context and other configurations
|
// Set up the page context and other configurations
|
||||||
admin_externalpage_setup('report_usergrades', '', null, '', array('capability' => 'report/usergrades:view'));
|
admin_externalpage_setup('report_usergrades', '', null, '', array('capability' => 'report/usergrades:view'));
|
||||||
|
|
||||||
class user_selector extends user_selector_base
|
|
||||||
{
|
|
||||||
|
|
||||||
public function find_users($search)
|
|
||||||
{
|
|
||||||
global $DB;
|
|
||||||
|
|
||||||
// Define the fields you want to select.
|
|
||||||
$fields = 'id, firstname, lastname, email';
|
|
||||||
|
|
||||||
// Create an SQL query to find users based on the search criteria.
|
|
||||||
$sql = "SELECT $fields
|
|
||||||
FROM {user}
|
|
||||||
WHERE CONCAT(firstname, ' ', lastname) LIKE ? OR email LIKE ?";
|
|
||||||
|
|
||||||
// Execute the SQL query and retrieve the users.
|
|
||||||
$users = $DB->get_records_sql($sql, array('%' . $search . '%', '%' . $search . '%'));
|
|
||||||
|
|
||||||
// Format the users into a format expected by the user selector.
|
|
||||||
$options = [];
|
|
||||||
foreach ($users as $user) {
|
|
||||||
$options[$user->id] = $user->firstname . ' ' . $user->lastname . ' (' . $user->email . ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
return array(get_string('users') => $options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Form class for user selection
|
|
||||||
*/
|
|
||||||
class usergrades_form extends moodleform
|
|
||||||
{
|
|
||||||
public function definition()
|
|
||||||
{
|
|
||||||
$mform = $this->_form;
|
|
||||||
|
|
||||||
// User selector
|
|
||||||
$user_selector = new user_selector('userid', array('multiselect' => false));
|
|
||||||
$users = $user_selector->find_users('');
|
|
||||||
|
|
||||||
$mform->addElement('select', 'userid', get_string('selectuser', 'report_usergrades'), $users[get_string('users')]);
|
|
||||||
|
|
||||||
// Add form action buttons
|
|
||||||
$this->add_action_buttons(false, get_string('showgrades', 'report_usergrades'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantiate the form
|
// Instantiate the form
|
||||||
$mform = new usergrades_form();
|
$mform = new usergrades_form();
|
||||||
echo $OUTPUT->header();
|
echo $OUTPUT->header();
|
||||||
|
|
|
@ -7,3 +7,10 @@ $ADMIN->add('reports', new admin_externalpage(
|
||||||
$CFG->wwwroot . "/report/usergrades/index.php",
|
$CFG->wwwroot . "/report/usergrades/index.php",
|
||||||
'report/usergrades:view'
|
'report/usergrades:view'
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$ADMIN->add('reports', new admin_externalpage(
|
||||||
|
'report_usergrades_details',
|
||||||
|
get_string('pluginname', 'report_usergrades'),
|
||||||
|
$CFG->wwwroot . "/report/usergrades/index.php",
|
||||||
|
'report/usergrades:view'
|
||||||
|
));
|
||||||
|
|
Loading…
Reference in a new issue