.
/**
* Displays the lesson statistics.
*
* @package mod
* @subpackage lesson
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
**/
require_once('../../config.php');
require_once($CFG->dirroot.'/mod/lesson/locallib.php');
$id = required_param('id', PARAM_INT); // Course Module ID
$pageid = optional_param('pageid', NULL, PARAM_INT); // Lesson Page ID
$action = optional_param('action', 'reportoverview', PARAM_ALPHA); // action to take
$nothingtodisplay = false;
$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
require_login($course, false, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/lesson:manage', $context);
$ufields = user_picture::fields('u'); // These fields are enough
$params = array("lessonid" => $lesson->id);
// TODO: Improve this. Fetching all students always is crazy!
if (!empty($cm->groupingid)) {
$params["groupid"] = $cm->groupingid;
$sql = "SELECT DISTINCT $ufields
FROM {lesson_attempts} a
INNER JOIN {user} u ON u.id = a.userid
INNER JOIN {groups_members} gm ON gm.userid = u.id
INNER JOIN {groupings_groups} gg ON gm.groupid = :groupid
WHERE a.lessonid = :lessonid
ORDER BY u.lastname";
} else {
$sql = "SELECT DISTINCT $ufields
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
u.id = a.userid
ORDER BY u.lastname";
}
if (! $students = $DB->get_records_sql($sql, $params)) {
$nothingtodisplay = true;
}
$url = new moodle_url('/mod/lesson/report.php', array('id'=>$id));
if ($action !== 'reportoverview') {
$url->param('action', $action);
}
if ($pageid !== NULL) {
$url->param('pageid', $pageid);
}
$PAGE->set_url($url);
if ($action == 'reportoverview') {
$PAGE->navbar->add(get_string('reports', 'lesson'));
$PAGE->navbar->add(get_string('overview', 'lesson'));
}
$lessonoutput = $PAGE->get_renderer('mod_lesson');
if (! $attempts = $DB->get_records('lesson_attempts', array('lessonid' => $lesson->id), 'timeseen')) {
$nothingtodisplay = true;
}
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();
}
if ($nothingtodisplay) {
echo $lessonoutput->header($lesson, $cm, $action);
echo $OUTPUT->notification(get_string('nolessonattempts', 'lesson'));
echo $OUTPUT->footer();
exit();
}
if ($action === 'delete') {
/// Process any form data before fetching attempts, grades and times
if (has_capability('mod/lesson:edit', $context) and $form = data_submitted() and confirm_sesskey()) {
/// Cycle through array of userids with nested arrays of tries
if (!empty($form->attempts)) {
foreach ($form->attempts as $userid => $tries) {
// Modifier IS VERY IMPORTANT! What does it do?
// Well, it is for when you delete multiple attempts for the same user.
// If you delete try 1 and 3 for a user, then after deleting try 1, try 3 then
// becomes try 2 (because try 1 is gone and all tries after try 1 get decremented).
// So, the modifier makes sure that the submitted try refers to the current try in the
// database - hope this all makes sense :)
$modifier = 0;
foreach ($tries as $try => $junk) {
$try -= $modifier;
/// Clean up the timer table by removing using the order - this is silly, it should be linked to specific attempt (skodak)
$params = array ("userid" => $userid, "lessonid" => $lesson->id);
$timers = $DB->get_records_sql("SELECT id FROM {lesson_timer}
WHERE userid = :userid AND lessonid = :lessonid
ORDER BY starttime", $params, $try, 1);
if ($timers) {
$timer = reset($timers);
$DB->delete_records('lesson_timer', array('id' => $timer->id));
}
/// Remove the grade from the grades and high_scores tables - this is silly, it should be linked to specific attempt (skodak)
$grades = $DB->get_records_sql("SELECT id FROM {lesson_grades}
WHERE userid = :userid AND lessonid = :lessonid
ORDER BY completed", $params, $try, 1);
if ($grades) {
$grade = reset($grades);
$DB->delete_records('lesson_grades', array('id' => $grade->id));
$DB->delete_records('lesson_high_scores', array('gradeid' => $grade->id, 'lessonid' => $lesson->id, 'userid' => $userid));
}
/// Remove attempts and update the retry number
$DB->delete_records('lesson_attempts', array('userid' => $userid, 'lessonid' => $lesson->id, 'retry' => $try));
$DB->execute("UPDATE {lesson_attempts} SET retry = retry - 1 WHERE userid = ? AND lessonid = ? AND retry > ?", array($userid, $lesson->id, $try));
/// Remove seen branches and update the retry number
$DB->delete_records('lesson_branch', array('userid' => $userid, 'lessonid' => $lesson->id, 'retry' => $try));
$DB->execute("UPDATE {lesson_branch} SET retry = retry - 1 WHERE userid = ? AND lessonid = ? AND retry > ?", array($userid, $lesson->id, $try));
/// update central gradebook
lesson_update_grades($lesson, $userid);
$modifier++;
}
}
}
}
redirect(new moodle_url($PAGE->url, array('action'=>'reportoverview')));
} else if ($action === 'reportoverview') {
/**************************************************************************
this action is for default view and overview view
**************************************************************************/
echo $lessonoutput->header($lesson, $cm, $action);
$course_context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
$seeallgradeslink = new moodle_url('/grade/report/grader/index.php', array('id'=>$course->id));
$seeallgradeslink = html_writer::link($seeallgradeslink, get_string('seeallcoursegrades', 'grades'));
echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
}
$studentdata = array();
// build an array for output
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 (!array_key_exists($attempt->userid, $studentdata) || !array_key_exists($attempt->retry, $studentdata[$attempt->userid])) {
// restore/setup defaults
$n = 0;
$timestart = 0;
$timeend = 0;
$usergrade = NULL;
// 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;
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,
"try" => $attempt->retry,
"userid" => $attempt->userid);
}
}
// 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
$table->head = array(get_string('name'), get_string('attempts', 'lesson'), get_string('highscore', '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 = "{$student->lastname}, $student->firstname";
foreach ($tries as $try) {
// start to build up the checkbox and link
if (has_capability('mod/lesson:edit', $context)) {
$temp = ' ';
} else {
$temp = '';
}
$temp .= "id&action=reportdetail&userid=".$try['userid'].'&try='.$try['try'].'">';
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).")";
} else {
// this is what the link does/looks like when the user has not completed the try
$temp .= get_string("notcompleted", "lesson");
$temp .= " ".userdate($try["timestart"])."";
$timetotake = NULL;
}
// build up the attempts array
$attempts[] = $temp;
// run these lines for the stats only if the user finnished the lesson
if ($try["grade"] !== NULL) {
$numofattempts++;
$avescore += $try["grade"];
$avetime += $timetotake;
if ($try["grade"] > $highscore || $highscore == NULL) {
$highscore = $try["grade"];
}
if ($try["grade"] < $lowscore || $lowscore == NULL) {
$lowscore = $try["grade"];
}
if ($timetotake > $hightime || $hightime == NULL) {
$hightime = $timetotake;
}
if ($timetotake < $lowtime || $lowtime == NULL) {
$lowtime = $timetotake;
}
}
}
// get line breaks in after each attempt
$attempts = implode("
\n", $attempts);
// add it to the table data[] object
$table->data[] = array($studentname, $attempts, $bestgrade."%");
}
}
// print it all out !
if (has_capability('mod/lesson:edit', $context)) {
echo "