Skip to content

Commit

Permalink
MDL-48803 mod/lesson: add group support
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-or committed Feb 9, 2015
1 parent 88cd577 commit fdc790f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 39 deletions.
57 changes: 44 additions & 13 deletions mod/lesson/essay.php
Expand Up @@ -47,6 +47,8 @@
}
$PAGE->set_url($url);

$currentgroup = groups_get_activity_group($cm, true);

$attempt = new stdClass();
$user = new stdClass();
$attemptid = optional_param('attemptid', 0, PARAM_INT);
Expand Down Expand Up @@ -178,7 +180,12 @@
}
} else {
$queryadd = '';
$params = array ("lessonid" => $lesson->id);

// If group selected, only send to group members.
list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
list($sort, $sortparams) = users_order_by_sql('u');
$params['lessonid'] = $lesson->id;

// Need to use inner view to avoid distinct + text
if (!$users = $DB->get_records_sql("
SELECT u.*
Expand All @@ -187,7 +194,9 @@
SELECT DISTINCT userid
FROM {lesson_attempts}
WHERE lessonid = :lessonid
) ui ON u.id = ui.userid", $params)) {
) ui ON u.id = ui.userid
JOIN ($esql) ue ON ue.id = u.id
ORDER BY $sort", $params)) {
print_error('cannotfinduser', 'lesson');
}
}
Expand Down Expand Up @@ -284,27 +293,45 @@
}
}
if (count($pages) > 0) {
$params = array ("lessonid" => $lesson->id, "qtype" => LESSON_PAGE_ESSAY);
// Get only the attempts that are in response to essay questions
list($usql, $parameters) = $DB->get_in_or_equal(array_keys($pages));
if ($essayattempts = $DB->get_records_select('lesson_attempts', 'pageid '.$usql, $parameters)) {
// Get all the users who have taken this lesson, order by their last name
list($usql, $parameters) = $DB->get_in_or_equal(array_keys($pages), SQL_PARAMS_NAMED);
// If group selected, only get group members attempts.
list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
$parameters = array_merge($params, $parameters);

$sql = "SELECT a.*
FROM {lesson_attempts} a
JOIN ($esql) ue ON a.userid = ue.id
WHERE pageid $usql";
if ($essayattempts = $DB->get_records_sql($sql, $parameters)) {
$ufields = user_picture::fields('u');
// Get all the users who have taken this lesson.
list($sort, $sortparams) = users_order_by_sql('u');
$params = array_merge($params, $sortparams);

$params['lessonid'] = $lesson->id;
$sql = "SELECT DISTINCT $ufields
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
u.id = a.userid
FROM {user} u
JOIN {lesson_attempts} a ON u.id = a.userid
JOIN ($esql) ue ON ue.id = a.userid
WHERE a.lessonid = :lessonid
ORDER BY $sort";
if (!$users = $DB->get_records_sql($sql, $params)) {
$mode = 'none'; // not displaying anything
$lesson->add_message(get_string('noonehasanswered', 'lesson'));
if (!empty($currentgroup)) {
$groupname = groups_get_group_name($currentgroup);
$lesson->add_message(get_string('noonehasansweredgroup', 'lesson', $groupname));
} else {
$lesson->add_message(get_string('noonehasanswered', 'lesson'));
}
}
} else {
$mode = 'none'; // not displaying anything
$lesson->add_message(get_string('noonehasanswered', 'lesson'));
if (!empty($currentgroup)) {
$groupname = groups_get_group_name($currentgroup);
$lesson->add_message(get_string('noonehasansweredgroup', 'lesson', $groupname));
} else {
$lesson->add_message(get_string('noonehasanswered', 'lesson'));
}
}
} else {
$mode = 'none'; // not displaying anything
Expand All @@ -318,6 +345,7 @@

switch ($mode) {
case 'display':
groups_print_activity_menu($cm, $url);
// Expects $user, $essayattempts and $pages to be set already

// Group all the essays by userid
Expand Down Expand Up @@ -428,6 +456,9 @@

$mform->display();
break;
default:
groups_print_activity_menu($cm, $url);
break;
}

echo $OUTPUT->footer();
2 changes: 2 additions & 0 deletions mod/lesson/lang/en/lesson.php
Expand Up @@ -307,10 +307,12 @@
$string['noessayquestionsfound'] = 'No essay questions found in this lesson.';
$string['nohighscores'] = 'No high scores';
$string['nolessonattempts'] = 'No attempts have been made on this lesson.';
$string['nolessonattemptsgroup'] = 'No attempts have been made by {$a} group members on this lesson.';
$string['nooneansweredcorrectly'] = 'No one answered correctly.';
$string['nooneansweredthisquestion'] = 'No one answered this question.';
$string['nooneenteredthis'] = 'No one entered this.';
$string['noonehasanswered'] = 'No one has answered an essay question yet.';
$string['noonehasansweredgroup'] = 'No one in {$a} has answered an essay question yet.';
$string['noonecheckedthis'] = 'No one checked this.';
$string['noretake'] = 'You are not allowed to retake this lesson.';
$string['normal'] = 'Normal - follow lesson path';
Expand Down
4 changes: 2 additions & 2 deletions mod/lesson/lib.php
Expand Up @@ -740,9 +740,9 @@ function lesson_get_extra_capabilities() {
function lesson_supports($feature) {
switch($feature) {
case FEATURE_GROUPS:
return false;
return true;
case FEATURE_GROUPINGS:
return false;
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
Expand Down
51 changes: 27 additions & 24 deletions mod/lesson/report.php
Expand Up @@ -37,35 +37,30 @@

require_login($course, false, $cm);

$currentgroup = groups_get_activity_group($cm, true);

$context = context_module::instance($cm->id);
require_capability('mod/lesson:viewreports', $context);

$ufields = user_picture::fields('u'); // These fields are enough
$params = array("lessonid" => $lesson->id);
list($sort, $sortparams) = users_order_by_sql('u');
$params = array_merge($params, $sortparams);
// TODO: Improve this. Fetching all students always is crazy!
if (!empty($cm->groupingid)) {
$params["groupingid"] = $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 = gg.groupid
WHERE a.lessonid = :lessonid AND
gg.groupingid = :groupingid
ORDER BY $sort";
} else {
// Only load students if there attempts for this lesson.
if ($attempts = $DB->record_exists('lesson_attempts', array('lessonid' => $lesson->id))) {
list($esql, $params) = get_enrolled_sql($context, '', $currentgroup, true);
list($sort, $sortparams) = users_order_by_sql('u');

$params['lessonid'] = $lesson->id;
$ufields = user_picture::fields('u');
$sql = "SELECT DISTINCT $ufields
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
u.id = a.userid
FROM {user} u
JOIN {lesson_attempts} a ON u.id = a.userid
JOIN ($esql) ue ON ue.id = a.userid
WHERE a.lessonid = :lessonid
ORDER BY $sort";
}

$students = $DB->get_recordset_sql($sql, $params);
if (!$students->valid()) {
$students = $DB->get_recordset_sql($sql, $params);
if (!$students->valid()) {
$nothingtodisplay = true;
}
} else {
$nothingtodisplay = true;
}

Expand Down Expand Up @@ -97,7 +92,13 @@

if ($nothingtodisplay) {
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('nolessonattempts', 'lesson'));
echo $OUTPUT->notification(get_string('nolessonattempts', 'lesson'));
if (!empty($currentgroup)) {
$groupname = groups_get_group_name($currentgroup);
echo $OUTPUT->notification(get_string('nolessonattemptsgroup', 'lesson', $groupname));
} else {
echo $OUTPUT->notification(get_string('nolessonattempts', 'lesson'));
}
groups_print_activity_menu($cm, $url);
echo $OUTPUT->footer();
exit();
}
Expand Down Expand Up @@ -163,6 +164,7 @@
this action is for default view and overview view
**************************************************************************/
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('overview', 'lesson'));
groups_print_activity_menu($cm, $url);

$course_context = context_course::instance($course->id);
if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
Expand Down Expand Up @@ -392,6 +394,7 @@
**************************************************************************/
echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('detailedstats', 'lesson'));
groups_print_activity_menu($cm, $url);

$course_context = context_course::instance($course->id);
if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
Expand Down
32 changes: 32 additions & 0 deletions mod/lesson/tests/behat/teacher_grade_essays.feature
Expand Up @@ -10,6 +10,8 @@ Feature: In a lesson activity, a non editing teacher can grade essay questions
| teacher1 | Teacher | 1 | teacher1@asd.com |
| teacher2 | Teacher | 2 | teacher2@asd.com |
| student1 | Student | 1 | student1@asd.com |
| student2 | Student | 2 | student2@asd.com |
| student3 | Student | 3 | student3@asd.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
Expand All @@ -18,13 +20,28 @@ Feature: In a lesson activity, a non editing teacher can grade essay questions
| teacher1 | C1 | editingteacher |
| teacher2 | C1 | teacher |
| student1 | C1 | student |
| student2 | C1 | student |
| student3 | C1 | student |
And the following "groups" exist:
| name | course | idnumber |
| Group A | C1 | G1 |
| Group B | C1 | G2 |
| Group C | C1 | G3 |
And the following "group members" exist:
| user | group |
| teacher1 | G1 |
| teacher2 | G2 |
| student1 | G1 |
| student2 | G2 |
| student3 | G3 |
And I log in as "teacher1"
And I am on homepage
And I follow "Course 1"
And I turn editing mode on
And I add a "Lesson" to section "1" and I fill the form with:
| Name | Test lesson name |
| Description | Test lesson description |
| Group mode | Separate groups |
And I follow "Test lesson name"
And I follow "Add a question page"
And I set the field "Select a question type" to "Essay"
Expand All @@ -40,12 +57,19 @@ Feature: In a lesson activity, a non editing teacher can grade essay questions
And I set the field "Your answer" to "<p>Once upon a time there was a little green frog."
And I press "Submit"
And I log out
And I log in as "student2"
And I follow "Course 1"
And I follow "Test lesson name"
And I set the field "Your answer" to "<p>Once upon a time there were two little green frogs."
And I press "Submit"
And I log out
When I log in as "teacher2"
And I follow "Course 1"
And I follow "Test lesson name"
Then I should see "Grade essays"
And I follow "Grade essays"
And I should see "Student 1"
And I should see "Student 2"
And I should see "Essay question"
And I follow "Essay question"
And I should see "Student 1's response"
Expand All @@ -55,3 +79,11 @@ Feature: In a lesson activity, a non editing teacher can grade essay questions
| Essay score | 1 |
And I press "Save changes"
And I should see "Changes saved"
And I set the field "Separate groups" to "Group A"
And I should see "Student 1"
And I should not see "Student 2"
And I set the field "Separate groups" to "Group B"
And I should see "Student 2"
And I should not see "Student 1"
And I set the field "Separate groups" to "Group C"
And I should see "No one in Group C has answered an essay question yet."
2 changes: 2 additions & 0 deletions mod/lesson/upgrade.txt
Expand Up @@ -12,6 +12,8 @@ This files describes API changes in the lesson code.
located as part of the lesson_add_page_form_base class. If specified with a value of 'LESSON_ANSWER_HTML'
then a rich html editor is generated. Otherwise an editor is created with Moodle Auto Format
* removedoublecr() and importmodifiedaikenstyle() are now deprecated.
* Added support for groups and groupings. If enabled, this allows the Report and
Grade Essay view to be filtered by a group

=== Earlier changes ===

Expand Down

0 comments on commit fdc790f

Please sign in to comment.