mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-57754 mod_lesson: Move overview report code to function
Move the table and statistics generation to a new function
This commit is contained in:
parent
216ea39be7
commit
7d5564d959
2 changed files with 362 additions and 323 deletions
|
@ -674,6 +674,341 @@ function lesson_process_group_deleted_in_course($courseid, $groupid = null) {
|
||||||
$DB->delete_records_list('lesson_overrides', 'id', array_keys($records));
|
$DB->delete_records_list('lesson_overrides', 'id', array_keys($records));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the overview report table and data.
|
||||||
|
*
|
||||||
|
* @param lesson $lesson lesson instance
|
||||||
|
* @param mixed $currentgroup false if not group used, 0 for all groups, group id (int) to filter by that groups
|
||||||
|
* @return mixed false if there is no information otherwise html_table and stdClass with the table and data
|
||||||
|
* @since Moodle 3.3
|
||||||
|
*/
|
||||||
|
function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup) {
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
$context = $lesson->context;
|
||||||
|
$cm = $lesson->cm;
|
||||||
|
// Count the number of branch and question pages in this lesson.
|
||||||
|
$branchcount = $DB->count_records('lesson_pages', array('lessonid' => $lesson->id, 'qtype' => LESSON_PAGE_BRANCHTABLE));
|
||||||
|
$questioncount = ($DB->count_records('lesson_pages', array('lessonid' => $lesson->id)) - $branchcount);
|
||||||
|
|
||||||
|
// Only load students if there attempts for this lesson.
|
||||||
|
$attempts = $DB->record_exists('lesson_attempts', array('lessonid' => $lesson->id));
|
||||||
|
$branches = $DB->record_exists('lesson_branch', array('lessonid' => $lesson->id));
|
||||||
|
$timer = $DB->record_exists('lesson_timer', array('lessonid' => $lesson->id));
|
||||||
|
if ($attempts or $branches or $timer) {
|
||||||
|
list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
|
||||||
|
list($sort, $sortparams) = users_order_by_sql('u');
|
||||||
|
|
||||||
|
$params['a1lessonid'] = $lesson->id;
|
||||||
|
$params['b1lessonid'] = $lesson->id;
|
||||||
|
$params['c1lessonid'] = $lesson->id;
|
||||||
|
$ufields = user_picture::fields('u');
|
||||||
|
$sql = "SELECT DISTINCT $ufields
|
||||||
|
FROM {user} u
|
||||||
|
JOIN (
|
||||||
|
SELECT userid, lessonid FROM {lesson_attempts} a1
|
||||||
|
WHERE a1.lessonid = :a1lessonid
|
||||||
|
UNION
|
||||||
|
SELECT userid, lessonid FROM {lesson_branch} b1
|
||||||
|
WHERE b1.lessonid = :b1lessonid
|
||||||
|
UNION
|
||||||
|
SELECT userid, lessonid FROM {lesson_timer} c1
|
||||||
|
WHERE c1.lessonid = :c1lessonid
|
||||||
|
) a ON u.id = a.userid
|
||||||
|
JOIN ($esql) ue ON ue.id = a.userid
|
||||||
|
ORDER BY $sort";
|
||||||
|
|
||||||
|
$students = $DB->get_recordset_sql($sql, $params);
|
||||||
|
if (!$students->valid()) {
|
||||||
|
$students->close();
|
||||||
|
return array(false, false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return array(false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $grades = $DB->get_records('lesson_grades', array('lessonid' => $lesson->id), 'completed')) {
|
||||||
|
$grades = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $times = $DB->get_records('lesson_timer', array('lessonid' => $lesson->id), 'starttime')) {
|
||||||
|
$times = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build an array for output.
|
||||||
|
$studentdata = array();
|
||||||
|
|
||||||
|
$attempts = $DB->get_recordset('lesson_attempts', array('lessonid' => $lesson->id), 'timeseen');
|
||||||
|
foreach ($attempts as $attempt) {
|
||||||
|
// if the user is not in the array or if the retry number is not in the sub array, add the data for that try.
|
||||||
|
if (empty($studentdata[$attempt->userid]) || empty($studentdata[$attempt->userid][$attempt->retry])) {
|
||||||
|
// restore/setup defaults
|
||||||
|
$n = 0;
|
||||||
|
$timestart = 0;
|
||||||
|
$timeend = 0;
|
||||||
|
$usergrade = null;
|
||||||
|
$eol = false;
|
||||||
|
|
||||||
|
// search for the grade record for this try. if not there, the nulls defined above will be used.
|
||||||
|
foreach($grades as $grade) {
|
||||||
|
// check to see if the grade matches the correct user
|
||||||
|
if ($grade->userid == $attempt->userid) {
|
||||||
|
// see if n is = to the retry
|
||||||
|
if ($n == $attempt->retry) {
|
||||||
|
// get grade info
|
||||||
|
$usergrade = round($grade->grade, 2); // round it here so we only have to do it once
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$n++; // if not equal, then increment n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$n = 0;
|
||||||
|
// search for the time record for this try. if not there, the nulls defined above will be used.
|
||||||
|
foreach($times as $time) {
|
||||||
|
// check to see if the grade matches the correct user
|
||||||
|
if ($time->userid == $attempt->userid) {
|
||||||
|
// see if n is = to the retry
|
||||||
|
if ($n == $attempt->retry) {
|
||||||
|
// get grade info
|
||||||
|
$timeend = $time->lessontime;
|
||||||
|
$timestart = $time->starttime;
|
||||||
|
$eol = $time->completed;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$n++; // if not equal, then increment n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// build up the array.
|
||||||
|
// this array represents each student and all of their tries at the lesson
|
||||||
|
$studentdata[$attempt->userid][$attempt->retry] = array( "timestart" => $timestart,
|
||||||
|
"timeend" => $timeend,
|
||||||
|
"grade" => $usergrade,
|
||||||
|
"end" => $eol,
|
||||||
|
"try" => $attempt->retry,
|
||||||
|
"userid" => $attempt->userid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$attempts->close();
|
||||||
|
|
||||||
|
$branches = $DB->get_recordset('lesson_branch', array('lessonid' => $lesson->id), 'timeseen');
|
||||||
|
foreach ($branches as $branch) {
|
||||||
|
// If the user is not in the array or if the retry number is not in the sub array, add the data for that try.
|
||||||
|
if (empty($studentdata[$branch->userid]) || empty($studentdata[$branch->userid][$branch->retry])) {
|
||||||
|
// Restore/setup defaults.
|
||||||
|
$n = 0;
|
||||||
|
$timestart = 0;
|
||||||
|
$timeend = 0;
|
||||||
|
$usergrade = null;
|
||||||
|
$eol = false;
|
||||||
|
// Search for the time record for this try. if not there, the nulls defined above will be used.
|
||||||
|
foreach ($times as $time) {
|
||||||
|
// Check to see if the grade matches the correct user.
|
||||||
|
if ($time->userid == $branch->userid) {
|
||||||
|
// See if n is = to the retry.
|
||||||
|
if ($n == $branch->retry) {
|
||||||
|
// Get grade info.
|
||||||
|
$timeend = $time->lessontime;
|
||||||
|
$timestart = $time->starttime;
|
||||||
|
$eol = $time->completed;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$n++; // If not equal, then increment n.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build up the array.
|
||||||
|
// This array represents each student and all of their tries at the lesson.
|
||||||
|
$studentdata[$branch->userid][$branch->retry] = array( "timestart" => $timestart,
|
||||||
|
"timeend" => $timeend,
|
||||||
|
"grade" => $usergrade,
|
||||||
|
"end" => $eol,
|
||||||
|
"try" => $branch->retry,
|
||||||
|
"userid" => $branch->userid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$branches->close();
|
||||||
|
|
||||||
|
// Need the same thing for timed entries that were not completed.
|
||||||
|
foreach ($times as $time) {
|
||||||
|
$endoflesson = $time->completed;
|
||||||
|
// If the time start is the same with another record then we shouldn't be adding another item to this array.
|
||||||
|
if (isset($studentdata[$time->userid])) {
|
||||||
|
$foundmatch = false;
|
||||||
|
$n = 0;
|
||||||
|
foreach ($studentdata[$time->userid] as $key => $value) {
|
||||||
|
if ($value['timestart'] == $time->starttime) {
|
||||||
|
// Don't add this to the array.
|
||||||
|
$foundmatch = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$n = count($studentdata[$time->userid]) + 1;
|
||||||
|
if (!$foundmatch) {
|
||||||
|
// Add a record.
|
||||||
|
$studentdata[$time->userid][] = array(
|
||||||
|
"timestart" => $time->starttime,
|
||||||
|
"timeend" => $time->lessontime,
|
||||||
|
"grade" => null,
|
||||||
|
"end" => $endoflesson,
|
||||||
|
"try" => $n,
|
||||||
|
"userid" => $time->userid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$studentdata[$time->userid][] = array(
|
||||||
|
"timestart" => $time->starttime,
|
||||||
|
"timeend" => $time->lessontime,
|
||||||
|
"grade" => null,
|
||||||
|
"end" => $endoflesson,
|
||||||
|
"try" => 0,
|
||||||
|
"userid" => $time->userid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// To store all the data to be returned by the function.
|
||||||
|
$data = new stdClass();
|
||||||
|
|
||||||
|
// Determine if lesson should have a score.
|
||||||
|
if ($branchcount > 0 AND $questioncount == 0) {
|
||||||
|
// This lesson only contains content pages and is not graded.
|
||||||
|
$data->lessonscored = false;
|
||||||
|
} else {
|
||||||
|
// This lesson is graded.
|
||||||
|
$data->lessonscored = true;
|
||||||
|
}
|
||||||
|
// set all the stats variables
|
||||||
|
$data->numofattempts = 0;
|
||||||
|
$data->avescore = 0;
|
||||||
|
$data->avetime = 0;
|
||||||
|
$data->highscore = null;
|
||||||
|
$data->lowscore = null;
|
||||||
|
$data->hightime = null;
|
||||||
|
$data->lowtime = null;
|
||||||
|
$data->students = array();
|
||||||
|
|
||||||
|
$table = new html_table();
|
||||||
|
|
||||||
|
// Set up the table object.
|
||||||
|
if ($data->lessonscored) {
|
||||||
|
$table->head = array(get_string('name'), get_string('attempts', 'lesson'), get_string('highscore', 'lesson'));
|
||||||
|
} else {
|
||||||
|
$table->head = array(get_string('name'), get_string('attempts', 'lesson'));
|
||||||
|
}
|
||||||
|
$table->align = array('center', 'left', 'left');
|
||||||
|
$table->wrap = array('nowrap', 'nowrap', 'nowrap');
|
||||||
|
$table->attributes['class'] = 'standardtable generaltable';
|
||||||
|
$table->size = array(null, '70%', null);
|
||||||
|
|
||||||
|
// print out the $studentdata array
|
||||||
|
// going through each student that has attempted the lesson, so, each student should have something to be displayed
|
||||||
|
foreach ($students as $student) {
|
||||||
|
// check to see if the student has attempts to print out
|
||||||
|
if (array_key_exists($student->id, $studentdata)) {
|
||||||
|
// set/reset some variables
|
||||||
|
$attempts = array();
|
||||||
|
$dataforstudent = new stdClass;
|
||||||
|
$dataforstudent->attempts = array();
|
||||||
|
// gather the data for each user attempt
|
||||||
|
$bestgrade = 0;
|
||||||
|
$bestgradefound = false;
|
||||||
|
// $tries holds all the tries/retries a student has done
|
||||||
|
$tries = $studentdata[$student->id];
|
||||||
|
$studentname = fullname($student, true);
|
||||||
|
|
||||||
|
foreach ($tries as $try) {
|
||||||
|
$dataforstudent->attempts[] = $try;
|
||||||
|
|
||||||
|
// Start to build up the checkbox and link.
|
||||||
|
if (has_capability('mod/lesson:edit', $context)) {
|
||||||
|
$temp = '<input type="checkbox" id="attempts" name="attempts['.$try['userid'].']['.$try['try'].']" /> ';
|
||||||
|
} else {
|
||||||
|
$temp = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$temp .= "<a href=\"report.php?id=$cm->id&action=reportdetail&userid=".$try['userid']
|
||||||
|
.'&try='.$try['try'].'" class="lesson-attempt-link">';
|
||||||
|
if ($try["grade"] !== null) { // if null then not done yet
|
||||||
|
// this is what the link does when the user has completed the try
|
||||||
|
$timetotake = $try["timeend"] - $try["timestart"];
|
||||||
|
|
||||||
|
$temp .= $try["grade"]."%";
|
||||||
|
$bestgradefound = true;
|
||||||
|
if ($try["grade"] > $bestgrade) {
|
||||||
|
$bestgrade = $try["grade"];
|
||||||
|
}
|
||||||
|
$temp .= " ".userdate($try["timestart"]);
|
||||||
|
$temp .= ", (".format_time($timetotake).")</a>";
|
||||||
|
} else {
|
||||||
|
if ($try["end"]) {
|
||||||
|
// User finished the lesson but has no grade. (Happens when there are only content pages).
|
||||||
|
$temp .= " ".userdate($try["timestart"]);
|
||||||
|
$timetotake = $try["timeend"] - $try["timestart"];
|
||||||
|
$temp .= ", (".format_time($timetotake).")</a>";
|
||||||
|
} else {
|
||||||
|
// This is what the link does/looks like when the user has not completed the attempt.
|
||||||
|
$temp .= get_string("notcompleted", "lesson");
|
||||||
|
if ($try['timestart'] !== 0) {
|
||||||
|
// Teacher previews do not track time spent.
|
||||||
|
$temp .= " ".userdate($try["timestart"]);
|
||||||
|
}
|
||||||
|
$temp .= "</a>";
|
||||||
|
$timetotake = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// build up the attempts array
|
||||||
|
$attempts[] = $temp;
|
||||||
|
|
||||||
|
// Run these lines for the stats only if the user finnished the lesson.
|
||||||
|
if ($try["end"]) {
|
||||||
|
// User has completed the lesson.
|
||||||
|
$data->numofattempts++;
|
||||||
|
$data->avetime += $timetotake;
|
||||||
|
if ($timetotake > $data->hightime || $data->hightime == null) {
|
||||||
|
$data->hightime = $timetotake;
|
||||||
|
}
|
||||||
|
if ($timetotake < $data->lowtime || $data->lowtime == null) {
|
||||||
|
$data->lowtime = $timetotake;
|
||||||
|
}
|
||||||
|
if ($try["grade"] !== null) {
|
||||||
|
// The lesson was scored.
|
||||||
|
$data->avescore += $try["grade"];
|
||||||
|
if ($try["grade"] > $data->highscore || $data->highscore === null) {
|
||||||
|
$data->highscore = $try["grade"];
|
||||||
|
}
|
||||||
|
if ($try["grade"] < $data->lowscore || $data->lowscore === null) {
|
||||||
|
$data->lowscore = $try["grade"];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// get line breaks in after each attempt
|
||||||
|
$attempts = implode("<br />\n", $attempts);
|
||||||
|
|
||||||
|
if ($data->lessonscored) {
|
||||||
|
// Add the grade if the lesson is graded.
|
||||||
|
$table->data[] = array($studentname, $attempts, $bestgrade . "%");
|
||||||
|
} else {
|
||||||
|
// This lesson does not have a grade.
|
||||||
|
$table->data[] = array($studentname, $attempts);
|
||||||
|
}
|
||||||
|
// Add the student data.
|
||||||
|
$dataforstudent->id = $student->id;
|
||||||
|
$dataforstudent->fullname = $studentname;
|
||||||
|
$dataforstudent->bestgrade = $bestgrade;
|
||||||
|
$data->students[] = $dataforstudent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$students->close();
|
||||||
|
if ($data->numofattempts > 0) {
|
||||||
|
$data->avescore = $data->avescore / $data->numofattempts;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array($table, $data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class that page type's MUST inherit from.
|
* Abstract class that page type's MUST inherit from.
|
||||||
*
|
*
|
||||||
|
|
|
@ -114,47 +114,10 @@ if ($action === 'delete') {
|
||||||
this action is for default view and overview view
|
this action is for default view and overview view
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
// Count the number of branch and question pages in this lesson.
|
// Get the table and data for build statistics.
|
||||||
$branchcount = $DB->count_records('lesson_pages', array('lessonid' => $lesson->id, 'qtype' => LESSON_PAGE_BRANCHTABLE));
|
list($table, $data) = lesson_get_overview_report_table_and_data($lesson, $currentgroup);
|
||||||
$questioncount = ($DB->count_records('lesson_pages', array('lessonid' => $lesson->id)) - $branchcount);
|
|
||||||
|
|
||||||
// Only load students if there attempts for this lesson.
|
if ($table === false) {
|
||||||
$attempts = $DB->record_exists('lesson_attempts', array('lessonid' => $lesson->id));
|
|
||||||
$branches = $DB->record_exists('lesson_branch', array('lessonid' => $lesson->id));
|
|
||||||
$timer = $DB->record_exists('lesson_timer', array('lessonid' => $lesson->id));
|
|
||||||
if ($attempts or $branches or $timer) {
|
|
||||||
list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
|
|
||||||
list($sort, $sortparams) = users_order_by_sql('u');
|
|
||||||
|
|
||||||
$params['a1lessonid'] = $lesson->id;
|
|
||||||
$params['b1lessonid'] = $lesson->id;
|
|
||||||
$params['c1lessonid'] = $lesson->id;
|
|
||||||
$ufields = user_picture::fields('u');
|
|
||||||
$sql = "SELECT DISTINCT $ufields
|
|
||||||
FROM {user} u
|
|
||||||
JOIN (
|
|
||||||
SELECT userid, lessonid FROM {lesson_attempts} a1
|
|
||||||
WHERE a1.lessonid = :a1lessonid
|
|
||||||
UNION
|
|
||||||
SELECT userid, lessonid FROM {lesson_branch} b1
|
|
||||||
WHERE b1.lessonid = :b1lessonid
|
|
||||||
UNION
|
|
||||||
SELECT userid, lessonid FROM {lesson_timer} c1
|
|
||||||
WHERE c1.lessonid = :c1lessonid
|
|
||||||
) a ON u.id = a.userid
|
|
||||||
JOIN ($esql) ue ON ue.id = a.userid
|
|
||||||
ORDER BY $sort";
|
|
||||||
|
|
||||||
$students = $DB->get_recordset_sql($sql, $params);
|
|
||||||
if (!$students->valid()) {
|
|
||||||
$students->close();
|
|
||||||
$nothingtodisplay = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$nothingtodisplay = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($nothingtodisplay) {
|
|
||||||
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('nolessonattempts', 'lesson'));
|
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('nolessonattempts', 'lesson'));
|
||||||
if (!empty($currentgroup)) {
|
if (!empty($currentgroup)) {
|
||||||
$groupname = groups_get_group_name($currentgroup);
|
$groupname = groups_get_group_name($currentgroup);
|
||||||
|
@ -167,14 +130,6 @@ if ($action === 'delete') {
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $grades = $DB->get_records('lesson_grades', array('lessonid' => $lesson->id), 'completed')) {
|
|
||||||
$grades = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! $times = $DB->get_records('lesson_timer', array('lessonid' => $lesson->id), 'starttime')) {
|
|
||||||
$times = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('overview', 'lesson'));
|
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('overview', 'lesson'));
|
||||||
groups_print_activity_menu($cm, $url);
|
groups_print_activity_menu($cm, $url);
|
||||||
|
|
||||||
|
@ -185,266 +140,15 @@ if ($action === 'delete') {
|
||||||
echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
|
echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build an array for output.
|
|
||||||
$studentdata = array();
|
|
||||||
|
|
||||||
$attempts = $DB->get_recordset('lesson_attempts', array('lessonid' => $lesson->id), 'timeseen');
|
|
||||||
foreach ($attempts as $attempt) {
|
|
||||||
// if the user is not in the array or if the retry number is not in the sub array, add the data for that try.
|
|
||||||
if (empty($studentdata[$attempt->userid]) || empty($studentdata[$attempt->userid][$attempt->retry])) {
|
|
||||||
// restore/setup defaults
|
|
||||||
$n = 0;
|
|
||||||
$timestart = 0;
|
|
||||||
$timeend = 0;
|
|
||||||
$usergrade = null;
|
|
||||||
$eol = false;
|
|
||||||
|
|
||||||
// search for the grade record for this try. if not there, the nulls defined above will be used.
|
|
||||||
foreach($grades as $grade) {
|
|
||||||
// check to see if the grade matches the correct user
|
|
||||||
if ($grade->userid == $attempt->userid) {
|
|
||||||
// see if n is = to the retry
|
|
||||||
if ($n == $attempt->retry) {
|
|
||||||
// get grade info
|
|
||||||
$usergrade = round($grade->grade, 2); // round it here so we only have to do it once
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$n++; // if not equal, then increment n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$n = 0;
|
|
||||||
// search for the time record for this try. if not there, the nulls defined above will be used.
|
|
||||||
foreach($times as $time) {
|
|
||||||
// check to see if the grade matches the correct user
|
|
||||||
if ($time->userid == $attempt->userid) {
|
|
||||||
// see if n is = to the retry
|
|
||||||
if ($n == $attempt->retry) {
|
|
||||||
// get grade info
|
|
||||||
$timeend = $time->lessontime;
|
|
||||||
$timestart = $time->starttime;
|
|
||||||
$eol = $time->completed;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$n++; // if not equal, then increment n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// build up the array.
|
|
||||||
// this array represents each student and all of their tries at the lesson
|
|
||||||
$studentdata[$attempt->userid][$attempt->retry] = array( "timestart" => $timestart,
|
|
||||||
"timeend" => $timeend,
|
|
||||||
"grade" => $usergrade,
|
|
||||||
"end" => $eol,
|
|
||||||
"try" => $attempt->retry,
|
|
||||||
"userid" => $attempt->userid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$attempts->close();
|
|
||||||
|
|
||||||
$branches = $DB->get_recordset('lesson_branch', array('lessonid' => $lesson->id), 'timeseen');
|
|
||||||
foreach ($branches as $branch) {
|
|
||||||
// If the user is not in the array or if the retry number is not in the sub array, add the data for that try.
|
|
||||||
if (empty($studentdata[$branch->userid]) || empty($studentdata[$branch->userid][$branch->retry])) {
|
|
||||||
// Restore/setup defaults.
|
|
||||||
$n = 0;
|
|
||||||
$timestart = 0;
|
|
||||||
$timeend = 0;
|
|
||||||
$usergrade = null;
|
|
||||||
$eol = false;
|
|
||||||
// Search for the time record for this try. if not there, the nulls defined above will be used.
|
|
||||||
foreach ($times as $time) {
|
|
||||||
// Check to see if the grade matches the correct user.
|
|
||||||
if ($time->userid == $branch->userid) {
|
|
||||||
// See if n is = to the retry.
|
|
||||||
if ($n == $branch->retry) {
|
|
||||||
// Get grade info.
|
|
||||||
$timeend = $time->lessontime;
|
|
||||||
$timestart = $time->starttime;
|
|
||||||
$eol = $time->completed;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$n++; // If not equal, then increment n.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build up the array.
|
|
||||||
// This array represents each student and all of their tries at the lesson.
|
|
||||||
$studentdata[$branch->userid][$branch->retry] = array( "timestart" => $timestart,
|
|
||||||
"timeend" => $timeend,
|
|
||||||
"grade" => $usergrade,
|
|
||||||
"end" => $eol,
|
|
||||||
"try" => $branch->retry,
|
|
||||||
"userid" => $branch->userid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$branches->close();
|
|
||||||
|
|
||||||
// Need the same thing for timed entries that were not completed.
|
|
||||||
foreach ($times as $time) {
|
|
||||||
$endoflesson = $time->completed;
|
|
||||||
// If the time start is the same with another record then we shouldn't be adding another item to this array.
|
|
||||||
if (isset($studentdata[$time->userid])) {
|
|
||||||
$foundmatch = false;
|
|
||||||
$n = 0;
|
|
||||||
foreach ($studentdata[$time->userid] as $key => $value) {
|
|
||||||
if ($value['timestart'] == $time->starttime) {
|
|
||||||
// Don't add this to the array.
|
|
||||||
$foundmatch = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$n = count($studentdata[$time->userid]) + 1;
|
|
||||||
if (!$foundmatch) {
|
|
||||||
// Add a record.
|
|
||||||
$studentdata[$time->userid][] = array(
|
|
||||||
"timestart" => $time->starttime,
|
|
||||||
"timeend" => $time->lessontime,
|
|
||||||
"grade" => null,
|
|
||||||
"end" => $endoflesson,
|
|
||||||
"try" => $n,
|
|
||||||
"userid" => $time->userid
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$studentdata[$time->userid][] = array(
|
|
||||||
"timestart" => $time->starttime,
|
|
||||||
"timeend" => $time->lessontime,
|
|
||||||
"grade" => null,
|
|
||||||
"end" => $endoflesson,
|
|
||||||
"try" => 0,
|
|
||||||
"userid" => $time->userid
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Determine if lesson should have a score.
|
|
||||||
if ($branchcount > 0 AND $questioncount == 0) {
|
|
||||||
// This lesson only contains content pages and is not graded.
|
|
||||||
$lessonscored = false;
|
|
||||||
} else {
|
|
||||||
// This lesson is graded.
|
|
||||||
$lessonscored = true;
|
|
||||||
}
|
|
||||||
// set all the stats variables
|
|
||||||
$numofattempts = 0;
|
|
||||||
$avescore = 0;
|
|
||||||
$avetime = 0;
|
|
||||||
$highscore = null;
|
|
||||||
$lowscore = null;
|
|
||||||
$hightime = null;
|
|
||||||
$lowtime = null;
|
|
||||||
|
|
||||||
$table = new html_table();
|
|
||||||
|
|
||||||
// Set up the table object.
|
|
||||||
if ($lessonscored) {
|
|
||||||
$table->head = array(get_string('name'), get_string('attempts', 'lesson'), get_string('highscore', 'lesson'));
|
|
||||||
} else {
|
|
||||||
$table->head = array(get_string('name'), get_string('attempts', 'lesson'));
|
|
||||||
}
|
|
||||||
$table->align = array('center', 'left', 'left');
|
|
||||||
$table->wrap = array('nowrap', 'nowrap', 'nowrap');
|
|
||||||
$table->attributes['class'] = 'standardtable generaltable';
|
|
||||||
$table->size = array(null, '70%', null);
|
|
||||||
|
|
||||||
// print out the $studentdata array
|
|
||||||
// going through each student that has attempted the lesson, so, each student should have something to be displayed
|
|
||||||
foreach ($students as $student) {
|
|
||||||
// check to see if the student has attempts to print out
|
|
||||||
if (array_key_exists($student->id, $studentdata)) {
|
|
||||||
// set/reset some variables
|
|
||||||
$attempts = array();
|
|
||||||
// gather the data for each user attempt
|
|
||||||
$bestgrade = 0;
|
|
||||||
$bestgradefound = false;
|
|
||||||
// $tries holds all the tries/retries a student has done
|
|
||||||
$tries = $studentdata[$student->id];
|
|
||||||
$studentname = fullname($student, true);
|
|
||||||
foreach ($tries as $try) {
|
|
||||||
// start to build up the checkbox and link
|
|
||||||
if (has_capability('mod/lesson:edit', $context)) {
|
|
||||||
$temp = '<input type="checkbox" id="attempts" name="attempts['.$try['userid'].']['.$try['try'].']" /> ';
|
|
||||||
} else {
|
|
||||||
$temp = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$temp .= "<a href=\"report.php?id=$cm->id&action=reportdetail&userid=".$try['userid']
|
|
||||||
.'&try='.$try['try'].'" class="lesson-attempt-link">';
|
|
||||||
if ($try["grade"] !== null) { // if null then not done yet
|
|
||||||
// this is what the link does when the user has completed the try
|
|
||||||
$timetotake = $try["timeend"] - $try["timestart"];
|
|
||||||
|
|
||||||
$temp .= $try["grade"]."%";
|
|
||||||
$bestgradefound = true;
|
|
||||||
if ($try["grade"] > $bestgrade) {
|
|
||||||
$bestgrade = $try["grade"];
|
|
||||||
}
|
|
||||||
$temp .= " ".userdate($try["timestart"]);
|
|
||||||
$temp .= ", (".format_time($timetotake).")</a>";
|
|
||||||
} else {
|
|
||||||
if ($try["end"]) {
|
|
||||||
// User finished the lesson but has no grade. (Happens when there are only content pages).
|
|
||||||
$temp .= " ".userdate($try["timestart"]);
|
|
||||||
$timetotake = $try["timeend"] - $try["timestart"];
|
|
||||||
$temp .= ", (".format_time($timetotake).")</a>";
|
|
||||||
} else {
|
|
||||||
// This is what the link does/looks like when the user has not completed the attempt.
|
|
||||||
$temp .= get_string("notcompleted", "lesson");
|
|
||||||
if ($try['timestart'] !== 0) {
|
|
||||||
// Teacher previews do not track time spent.
|
|
||||||
$temp .= " ".userdate($try["timestart"]);
|
|
||||||
}
|
|
||||||
$temp .= "</a>";
|
|
||||||
$timetotake = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// build up the attempts array
|
|
||||||
$attempts[] = $temp;
|
|
||||||
|
|
||||||
// Run these lines for the stats only if the user finnished the lesson.
|
|
||||||
if ($try["end"]) {
|
|
||||||
// User has completed the lesson.
|
|
||||||
$numofattempts++;
|
|
||||||
$avetime += $timetotake;
|
|
||||||
if ($timetotake > $hightime || $hightime == null) {
|
|
||||||
$hightime = $timetotake;
|
|
||||||
}
|
|
||||||
if ($timetotake < $lowtime || $lowtime == null) {
|
|
||||||
$lowtime = $timetotake;
|
|
||||||
}
|
|
||||||
if ($try["grade"] !== null) {
|
|
||||||
// The lesson was scored.
|
|
||||||
$avescore += $try["grade"];
|
|
||||||
if ($try["grade"] > $highscore || $highscore === null) {
|
|
||||||
$highscore = $try["grade"];
|
|
||||||
}
|
|
||||||
if ($try["grade"] < $lowscore || $lowscore === null) {
|
|
||||||
$lowscore = $try["grade"];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// get line breaks in after each attempt
|
|
||||||
$attempts = implode("<br />\n", $attempts);
|
|
||||||
|
|
||||||
if ($lessonscored) {
|
|
||||||
// Add the grade if the lesson is graded.
|
|
||||||
$bestgrade = $bestgrade."%";
|
|
||||||
$table->data[] = array($studentname, $attempts, $bestgrade);
|
|
||||||
} else {
|
|
||||||
// This lesson does not have a grade.
|
|
||||||
$table->data[] = array($studentname, $attempts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$students->close();
|
|
||||||
// Print it all out!
|
// Print it all out!
|
||||||
if (has_capability('mod/lesson:edit', $context)) {
|
if (has_capability('mod/lesson:edit', $context)) {
|
||||||
echo "<form id=\"mod-lesson-report-form\" method=\"post\" action=\"report.php\">\n
|
echo "<form id=\"mod-lesson-report-form\" method=\"post\" action=\"report.php\">\n
|
||||||
<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n
|
<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n
|
||||||
<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
|
<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
echo html_writer::table($table);
|
echo html_writer::table($table);
|
||||||
|
|
||||||
if (has_capability('mod/lesson:edit', $context)) {
|
if (has_capability('mod/lesson:edit', $context)) {
|
||||||
$checklinks = '<a id="checkall" href="#">'.get_string('selectall').'</a> / ';
|
$checklinks = '<a id="checkall" href="#">'.get_string('selectall').'</a> / ';
|
||||||
$checklinks .= '<a id="checknone" href="#">'.get_string('deselectall').'</a>';
|
$checklinks .= '<a id="checknone" href="#">'.get_string('deselectall').'</a>';
|
||||||
|
@ -471,38 +175,38 @@ if ($action === 'delete') {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the Statistics.
|
// Calculate the Statistics.
|
||||||
if ($avetime == null) {
|
if ($data->avetime == null) {
|
||||||
$avetime = get_string("notcompleted", "lesson");
|
$data->avetime = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$avetime = format_float($avetime/$numofattempts, 0);
|
$data->avetime = format_float($data->avetime / $data->numofattempts, 0);
|
||||||
$avetime = format_time($avetime);
|
$data->avetime = format_time($data->avetime);
|
||||||
}
|
}
|
||||||
if ($hightime == null) {
|
if ($data->hightime == null) {
|
||||||
$hightime = get_string("notcompleted", "lesson");
|
$data->hightime = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$hightime = format_time($hightime);
|
$data->hightime = format_time($data->hightime);
|
||||||
}
|
}
|
||||||
if ($lowtime == null) {
|
if ($data->lowtime == null) {
|
||||||
$lowtime = get_string("notcompleted", "lesson");
|
$data->lowtime = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$lowtime = format_time($lowtime);
|
$data->lowtime = format_time($data->lowtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($lessonscored) {
|
if ($data->lessonscored) {
|
||||||
if ($numofattempts == 0) {
|
if ($data->numofattempts == 0) {
|
||||||
$avescore = get_string("notcompleted", "lesson");
|
$data->avescore = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$avescore = format_float($avescore / $numofattempts, 2) . '%';
|
$data->avescore = format_float($data->avescore, 2) . '%';
|
||||||
}
|
}
|
||||||
if ($highscore === null) {
|
if ($data->highscore === null) {
|
||||||
$highscore = get_string("notcompleted", "lesson");
|
$data->highscore = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$highscore .= '%';
|
$data->highscore .= '%';
|
||||||
}
|
}
|
||||||
if ($lowscore === null) {
|
if ($data->lowscore === null) {
|
||||||
$lowscore = get_string("notcompleted", "lesson");
|
$data->lowscore = get_string("notcompleted", "lesson");
|
||||||
} else {
|
} else {
|
||||||
$lowscore .= '%';
|
$data->lowscore .= '%';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the full stats for the lesson.
|
// Display the full stats for the lesson.
|
||||||
|
@ -514,7 +218,7 @@ if ($action === 'delete') {
|
||||||
$stattable->align = array('center', 'center', 'center', 'center', 'center', 'center');
|
$stattable->align = array('center', 'center', 'center', 'center', 'center', 'center');
|
||||||
$stattable->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
|
$stattable->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
|
||||||
$stattable->attributes['class'] = 'standardtable generaltable';
|
$stattable->attributes['class'] = 'standardtable generaltable';
|
||||||
$stattable->data[] = array($avescore, $avetime, $highscore, $lowscore, $hightime, $lowtime);
|
$stattable->data[] = array($data->avescore, $data->avetime, $data->highscore, $data->lowscore, $data->hightime, $data->lowtime);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Display simple stats for the lesson.
|
// Display simple stats for the lesson.
|
||||||
|
@ -525,7 +229,7 @@ if ($action === 'delete') {
|
||||||
$stattable->align = array('center', 'center', 'center');
|
$stattable->align = array('center', 'center', 'center');
|
||||||
$stattable->wrap = array('nowrap', 'nowrap', 'nowrap');
|
$stattable->wrap = array('nowrap', 'nowrap', 'nowrap');
|
||||||
$stattable->attributes['class'] = 'standardtable generaltable';
|
$stattable->attributes['class'] = 'standardtable generaltable';
|
||||||
$stattable->data[] = array($avetime, $hightime, $lowtime);
|
$stattable->data[] = array($data->avetime, $data->hightime, $data->lowtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo html_writer::table($stattable);
|
echo html_writer::table($stattable);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue