Skip to content

Commit

Permalink
exit/warn if completions not enabled in site/course
Browse files Browse the repository at this point in the history
  • Loading branch information
frumbert committed Aug 12, 2020
1 parent 77c2518 commit 8ad61d4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 92 deletions.
130 changes: 39 additions & 91 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,54 +112,6 @@ public static function find_activity_in_section($course,$section_name,$activity_
return null;
}

// public static function get_course_section($course, $name) {
// global $DB;

// // topic 0 might or might not have a name, so look it up by index
// if ($name === "0" && $record = $DB->get_records('course_sections', ['course' => $course->id, 'section' => 0], 'section', 'id')) {
// return array_pop($record);

// // otherwise match section by its name (case insensitive)
// } else if ($record = $DB->get_records('course_sections', ['course' => $course->id, 'name' => $name], 'section', 'id')) {
// return array_pop($record);

// } else {
// return null;
// }
// }

// // return the instance of the activity with the specified name in the course section
// public static function get_section_activity($course, $section, $find) {
// global $DB;

// $mi = get_fast_modinfo($course);
// echo "<pre>";
// var_dump($mi);
// echo "</pre>";
// exit;

// // all activities in this section that have completion enabled
// $sql = '
// SELECT cm.id, cm.instance, m.name
// FROM {course_modules} cm INNER JOIN {modules} m
// ON m.id = cm.module
// WHERE cm.course = ?
// AND cm.section = ?
// AND cm.completion > 0
// ';

// // select from table mdl_{$module} where name=$find to find cm
// if ($rows = $DB->get_records_sql($sql, [$course->id, $section->id])) {
// foreach ($rows as $row) {
// if ($DB->record_exists($row->name, ['name' => $find, 'course' => $course->id])) {
// $data = get_course_and_cm_from_instance($row->instance, $row->name, $course->id);
// return $data[1];
// }
// }
// }
// return false;
// }

/**
* Update page activity viewed
*
Expand Down Expand Up @@ -187,54 +139,50 @@ public static function mark_activity_as_completed($record, $studentrole) {
if ($course = self::get_course_by_field($record->coursefield, $record->coursevalue)) {
$response->course = $course;

// get the user record
if ($user =self::get_user_by_field($record->userfield, $record->uservalue)) {
$response->user = $user;

if ($cm = self::find_activity_in_section($course,$record->sectionname,$record->activityname)) {

// // get the section (topic) in the course
// if ($section = self::get_course_section($course, $record->sectionname)) {

// // get the cm id for this named activity
// if ($cm = self::get_section_activity($course, $section, $record->activityname)) {

// ensure the user is enrolled in this course
enrol_try_internal_enrol($course->id, $user->id, $studentrole->id);

// get the current completion state to avoid re-completion
$completion = new completion_info($course);
$currentstate = $completion->get_data($cm, false, $user->id, null);

// if the user can't override completion we need to bail
if (!$completion->user_can_override_completion($USER)) {
$response->message = 'Configured user unable to override completion in course ' . $course->fullname;
$response->skipped = 1;
$response->added = 0;
} else {
// override completion of this activity
$completion->update_state($cm, COMPLETION_COMPLETE, $user->id, true);

// $newstate = $completion->get_data($cm, false, $user->id, null);
// var_dump($currentstate, $newstate);
// exit;

$response->message = 'Activity "' . $record->activityname . '" in topic "' . $record->sectionname . '" was completed on behalf of user.';
$response->skipped = 0;
$response->added = 1;
}
// } else {
// $response->message = 'Unable to find activity "' . $record->activityname . '" in course "' . $course->fullname . '"';
// $response->skipped = 1;
// $response->added = 0;
// }
$completion = new completion_info($course);
if ($completion->is_enabled()) {

// get the user record
if ($user =self::get_user_by_field($record->userfield, $record->uservalue)) {
$response->user = $user;

if ($cm = self::find_activity_in_section($course,$record->sectionname,$record->activityname)) {

// ensure the user is enrolled in this course
enrol_try_internal_enrol($course->id, $user->id, $studentrole->id);

// test the current completion state to avoid re-completion
$currentstate = $completion->get_data($cm, false, $user->id, null);
if ($currentstate->completionstate == COMPLETION_COMPLETE) {
$response->message = 'Activity "' . $record->activityname . '" in topic "' . $record->sectionname . '" was already completed';
$response->skipped = 1;
$response->added = 0;
} else {
// if the user can't override completion we need to bail
if (!$completion->user_can_override_completion($USER)) {
$response->message = 'Configured user unable to override completion in course ' . $course->fullname;
$response->skipped = 1;
$response->added = 0;
} else {
// override completion of this activity
$completion->update_state($cm, COMPLETION_COMPLETE, $user->id, true);
$response->message = 'Activity "' . $record->activityname . '" in topic "' . $record->sectionname . '" was completed on behalf of user.';
$response->skipped = 0;
$response->added = 1;
}
}
} else {
$response->message = 'Unable to find activity "' . $record->activityname . '" in topic "' . $record->sectionname . '" in course "' . $course->fullname . '"';
$response->skipped = 1;
$response->added = 0;
}
} else {
$response->message = 'Unable to find activity "' . $record->activityname . '" in topic "' . $record->sectionname . '" in course "' . $course->fullname . '"';
$response->message = 'Unable to find user matching "' . $record->uservalue . '"';
$response->skipped = 1;
$response->added = 0;
}
} else {
$response->message = 'Unable to find user matching "' . $record->uservalue . '"';
$response->message = 'Course "' . $course->fullname . '" does not have completions enabled';
$response->skipped = 1;
$response->added = 0;
}
Expand Down
6 changes: 5 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@
// First time.
echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);
$mform2->display();
if (!completion_info::is_enabled_for_site()) {
echo get_string("completionnotenabledforsite");
} else {
$mform2->display();
}
echo $OUTPUT->footer();
die();
}
Expand Down

0 comments on commit 8ad61d4

Please sign in to comment.