MDL-65937 mod_lesson: Use new core/checkbox_toggleall

Plus some refactoring on the cell of the attempts column.
This commit is contained in:
Jun Pataleta 2017-12-21 14:23:41 +13:00
parent 414eca8923
commit ae612a5334
3 changed files with 97 additions and 51 deletions

View File

@ -151,6 +151,7 @@ $string['deletingpage'] = 'Deleting page: {$a}';
$string['dependencyon'] = 'Dependent on';
$string['dependencyon_help'] = 'This setting allows access to this lesson to be dependent upon a student\'s performance in another lesson in the same course. Any combination of time spent, completed or "grade better than" may be used.';
$string['description'] = 'Description';
$string['deselectallattempts'] = 'Deselect all attempts';
$string['detailedstats'] = 'Detailed statistics';
$string['didnotanswerquestion'] = 'Did not answer this question.';
$string['didnotreceivecredit'] = 'Did not receive credit';
@ -520,6 +521,7 @@ $string['scores'] = 'Scores';
$string['search:activity'] = 'Lesson - activity information';
$string['secondpluswrong'] = 'Not quite. Would you like to try again?';
$string['selectaqtype'] = 'Select a question type';
$string['selectallattempts'] = 'Select all attempts';
$string['sent'] = 'Sent';
$string['shortanswer'] = 'Short answer';
$string['showanunansweredpage'] = 'Show an unanswered page';
@ -578,6 +580,7 @@ $string['viewreports2'] = 'View {$a} completed attempts';
$string['warning'] = 'Warning';
$string['welldone'] = 'Well done!';
$string['whatdofirst'] = 'What would you like to do first?';
$string['withselectedattempts'] = 'With selected attempts...';
$string['wronganswerjump'] = 'Wrong answer jump';
$string['wronganswerscore'] = 'Wrong answer score';
$string['wrongresponse'] = 'Wrong response';

View File

@ -686,7 +686,7 @@ function lesson_process_group_deleted_in_course($courseid, $groupid = null) {
* @since Moodle 3.3
*/
function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup) {
global $DB, $CFG;
global $DB, $CFG, $OUTPUT;
require_once($CFG->dirroot . '/mod/lesson/pagetypes/branchtable.php');
$context = $lesson->context;
@ -902,7 +902,25 @@ function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup
$headers[] = get_user_field_name($field);
}
$headers [] = get_string('attempts', 'lesson');
$caneditlesson = has_capability('mod/lesson:edit', $context);
$attemptsheader = get_string('attempts', 'lesson');
if ($caneditlesson) {
$selectall = get_string('selectallattempts', 'lesson');
$deselectall = get_string('deselectallattempts', 'lesson');
// Build the select/deselect all control.
$selectallid = 'selectall-attempts';
$mastercheckbox = new \core\output\checkbox_toggleall('lesson-attempts', true, [
'id' => $selectallid,
'name' => $selectallid,
'value' => 1,
'label' => $selectall,
'selectall' => $selectall,
'deselectall' => $deselectall,
'labelclasses' => 'form-check-label'
]);
$attemptsheader = $OUTPUT->render($mastercheckbox);
}
$headers [] = $attemptsheader;
// Set up the table object.
if ($data->lessonscored) {
@ -924,7 +942,7 @@ function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup
$table->wrap = [];
$table->wrap = array_pad($table->wrap, $colcount, 'nowrap');
$table->attributes['class'] = 'standardtable generaltable';
$table->attributes['class'] = 'table table-striped';
// print out the $studentdata array
// going through each student that has attempted the lesson, so, each student should have something to be displayed
@ -937,7 +955,7 @@ function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup
$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);
@ -946,44 +964,57 @@ function lesson_get_overview_report_table_and_data(lesson $lesson, $currentgroup
$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 = '';
}
$attempturlparams = [
'id' => $cm->id,
'action' => 'reportdetail',
'userid' => $try['userid'],
'try' => $try['try'],
];
$temp .= "<a href=\"report.php?id=$cm->id&amp;action=reportdetail&amp;userid=".$try['userid']
.'&amp;try='.$try['try'].'" class="lesson-attempt-link">';
$attemptlinkcontents = '';
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;
$attemptlinkcontents .= $try["grade"]."%";
if ($try["grade"] > $bestgrade) {
$bestgrade = $try["grade"];
}
$temp .= "&nbsp;".userdate($try["timestart"]);
$temp .= ",&nbsp;(".format_time($timetotake).")</a>";
$attemptlinkcontents .= "&nbsp;".userdate($try["timestart"]);
$attemptlinkcontents .= ",&nbsp;(".format_time($timetotake).")";
} else {
if ($try["end"]) {
// User finished the lesson but has no grade. (Happens when there are only content pages).
$temp .= "&nbsp;".userdate($try["timestart"]);
$attemptlinkcontents .= "&nbsp;".userdate($try["timestart"]);
$timetotake = $try["timeend"] - $try["timestart"];
$temp .= ",&nbsp;(".format_time($timetotake).")</a>";
$attemptlinkcontents .= ",&nbsp;(".format_time($timetotake).")";
} else {
// This is what the link does/looks like when the user has not completed the attempt.
$temp .= get_string("notcompleted", "lesson");
$attemptlinkcontents .= get_string("notcompleted", "lesson");
if ($try['timestart'] !== 0) {
// Teacher previews do not track time spent.
$temp .= "&nbsp;".userdate($try["timestart"]);
$attemptlinkcontents .= "&nbsp;".userdate($try["timestart"]);
}
$temp .= "</a>";
$timetotake = null;
}
}
$attempturl = new moodle_url('/mod/lesson/report.php', $attempturlparams);
$attemptlink = html_writer::link($attempturl, $attemptlinkcontents, ['class' => 'lesson-attempt-link']);
if ($caneditlesson) {
$attemptid = 'attempt-' . $try['userid'] . '-' . $try['try'];
$attemptname = 'attempts[' . $try['userid'] . '][' . $try['try'] . ']';
$checkbox = new \core\output\checkbox_toggleall('lesson-attempts', false, [
'id' => $attemptid,
'name' => $attemptname,
'label' => $attemptlink
]);
$attemptlink = $OUTPUT->render($checkbox);
}
// build up the attempts array
$attempts[] = $temp;
$attempts[] = $attemptlink;
// Run these lines for the stats only if the user finnished the lesson.
if ($try["end"]) {

View File

@ -139,39 +139,51 @@ if ($action === 'delete') {
echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
}
// Print it all out!
// The attempts table.
$attemptstable = html_writer::table($table);
// The HTML that we will be displaying which includes the attempts table and bulk actions menu, if necessary.
$attemptshtml = $attemptstable;
// Show bulk actions when user has capability to edit the lesson.
if (has_capability('mod/lesson:edit', $context)) {
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=\"id\" value=\"$cm->id\" />\n";
$reporturl = new moodle_url('/mod/lesson/report.php');
$formid = 'mod-lesson-report-form';
// Sesskey hidden input.
$formcontents = html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
// CMID hidden input.
$formcontents .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'id', 'value' => $cm->id]);
// Attempts table.
$formcontents .= $attemptstable;
// Bulk actions menu.
$attemptsactions = [
'delete' => get_string('deleteselected')
];
$bulkactions = new single_select($reporturl, 'action', $attemptsactions, '', ['' => 'choosedots'], $formid);
$bulkactions->set_label(get_string('withselectedattempts', 'lesson'));
$bulkactions->disabled = true;
$bulkactions->attributes = [
'data-action' => 'toggle',
'data-togglegroup' => 'lesson-attempts',
'data-toggle' => 'action',
];
$bulkactionshtml = $OUTPUT->render($bulkactions);
$formcontents .= $OUTPUT->box($bulkactionshtml, 'center');
// Build the attempts form.
$formattributes = [
'id' => $formid,
'method' => 'post',
];
$attemptshtml = html_writer::tag('form', $formcontents, $formattributes);
}
echo html_writer::table($table);
if (has_capability('mod/lesson:edit', $context)) {
$checklinks = '<a id="checkall" href="#">'.get_string('selectall').'</a> / ';
$checklinks .= '<a id="checknone" href="#">'.get_string('deselectall').'</a>';
$checklinks .= html_writer::label('action', 'menuaction', false, array('class' => 'accesshide'));
$options = array('delete' => get_string('deleteselected'));
$attributes = array('id' => 'actionid', 'class' => 'custom-select ml-1');
$checklinks .= html_writer::select($options, 'action', 0, array('' => 'choosedots'), $attributes);
$PAGE->requires->js_amd_inline("
require(['jquery'], function($) {
$('#actionid').change(function() {
$('#mod-lesson-report-form').submit();
});
$('#checkall').click(function(e) {
$('#mod-lesson-report-form').find('input:checkbox').prop('checked', true);
e.preventDefault();
});
$('#checknone').click(function(e) {
$('#mod-lesson-report-form').find('input:checkbox').prop('checked', false);
e.preventDefault();
});
});");
echo $OUTPUT->box($checklinks, 'center');
echo '</form>';
}
// Show the attempts HTML.
echo $attemptshtml;
// Calculate the Statistics.
if ($data->avetime == null) {