Skip to content

Commit

Permalink
MOODLE_311_STABLE
Browse files Browse the repository at this point in the history
MOODLE_311_STABLE UPDATE
Add custom_completion.php
Ensure events always sent
  • Loading branch information
lushonline committed Jul 23, 2021
1 parent a1e5b3b commit f0a3152
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 26 deletions.
100 changes: 100 additions & 0 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* The mod_externalcontent module custom completion class.
*
* @package mod_externalcontent
* @copyright 2019-2021 LushOnline
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_externalcontent\completion;

defined('MOODLE_INTERNAL') || die();

if (!class_exists('\core_completion\activity_custom_completion')) {
// New API does not exist in this site, so do nothing.
return;
}

/**
* The mod_externalcontent module custom completion.
*
* @package mod_externalcontent
* @copyright 2019-2021 LushOnline
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_completion extends \core_completion\activity_custom_completion {
/**
* Fetches the completion state for a given completion rule.
*
* @param string $rule The completion rule.
* @return int The completion state.
*/
public function get_state(string $rule): int {
global $DB;

$this->validate_rule($rule);
// Get external content details.
$externalcontent = $DB->get_record('externalcontent', array('id' => $this->cm->instance), '*', MUST_EXIST);

switch ($rule) {
case 'completionexternally':
$params = array('userid' => $this->userid, 'externalcontentid' => $externalcontent->id, 'completed' => 1);
$completed = $DB->record_exists('externalcontent_track', $params);

$status = $completed ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
break;

default :
$status = COMPLETION_INCOMPLETE;
break;
}

return $status;
}

/**
* Fetch the list of custom completion rules that this module defines.
*
* @return array
*/
public static function get_defined_custom_rules(): array {
return ['completionexternally'];
}

/**
* Returns an associative array of the descriptions of custom completion rules.
*
* @return array
*/
public function get_custom_rule_descriptions(): array {
return ['completionexternally' => get_string('eventcompletedexternally', 'externalcontent')];
}

/**
* Returns an array of all completion rules, in the order they should be displayed to users.
*
* @return array
*/
public function get_sort_order(): array {
return [
'completionview',
'completionexternally',
];
}
}
3 changes: 2 additions & 1 deletion classes/event/course_module_completedexternally.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static function get_name() {
*/
public function get_description() {
return get_string('eventcompletedexternallydesc', 'externalcontent',
['userid' => $this->userid, 'contextinstanceid' => $this->contextinstanceid]);
['userid' => $this->relateduserid ?? $this->userid,
'contextinstanceid' => $this->contextinstanceid]);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion classes/event/course_module_scoredexternally.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public static function get_name() {
*/
public function get_description() {
return get_string('eventscoredexternallydesc', 'externalcontent',
['userid' => $this->userid, 'contextinstanceid' => $this->contextinstanceid, 'score' => $this->other]);
['userid' => $this->relateduserid ?? $this->userid,
'contextinstanceid' => $this->contextinstanceid,
'score' => $this->other]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
'capabilities' => 'mod/externalcontent:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);
);
4 changes: 2 additions & 2 deletions grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
$id = required_param('id', PARAM_INT);

if (!$cm = get_coursemodule_from_id('externalcontent', $id)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule', 'error');
}
$externalcontent = $DB->get_record('externalcontent', array('id' => $cm->instance), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);

require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);

redirect(new moodle_url('/mod/externalcontent/view.php', array('id' => $cm->id)));
redirect(new moodle_url('/mod/externalcontent/view.php', array('id' => $cm->id)));
29 changes: 15 additions & 14 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
}

$response = new \stdClass();
$response->userid = $userid;
$response->status = false;
$response->completionupdated = false;
$response->scoreupdated = false;
Expand Down Expand Up @@ -458,22 +459,22 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
}

if ($completed) {
$params = array(
'context' => $context,
'objectid' => $externalcontent->id,
'relateduserid' => $userid,
);

$event = \mod_externalcontent\event\course_module_completedexternally::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('externalcontent', $externalcontent);
$event->trigger();

if ($currentstate->completionstate == COMPLETION_COMPLETE) {
$response->message .= ' External content completion state already set to COMPLETION_COMPLETE.';
$response->completionupdated = false;
} else {
$params = array(
'context' => $context,
'objectid' => $externalcontent->id,
'userid' => $userid,
);

$event = \mod_externalcontent\event\course_module_completedexternally::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('externalcontent', $externalcontent);
$event->trigger();

$completion->update_state($cm, COMPLETION_COMPLETE, $userid);
$response->message .= ' External content completion status set to COMPLETION_COMPLETE.';
$response->completionupdated = true;
Expand All @@ -484,7 +485,7 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
$params = array(
'context' => $context,
'objectid' => $externalcontent->id,
'userid' => $userid,
'relateduserid' => $userid,
'other' => $score,
);

Expand Down Expand Up @@ -646,4 +647,4 @@ function externalcontent_update_grades($externalcontent, $userid = 0, $removegra
} else {
externalcontent_grade_item_update($externalcontent);
}
}
}
2 changes: 1 addition & 1 deletion lrs/aboutcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public function about(Request $request, Response $response, array $args) {

return $response->withJson($versions);
}
}
}
2 changes: 1 addition & 1 deletion lrs/statementcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ public function notimplemented(Request $request, Response $response, array $args
->withAddedHeader('Content-Type', 'text/plain')
->write($message);
}
}
}
2 changes: 1 addition & 1 deletion lrs/xapihelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,4 @@ public static function mark_completed($record) {
}
return $response;
}
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2021030400;
$plugin->version = 2021072100;
$plugin->requires = 2018051700; // Requires this Moodle version v3.5 see https://docs.moodle.org/dev/Releases.
$plugin->component = 'mod_externalcontent';
$plugin->maturity = MATURITY_STABLE;
$plugin->release = '1.6';
$plugin->release = '1.7';
5 changes: 3 additions & 2 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@

if ($p) {
if (!$externalcontent = $DB->get_record('externalcontent', array('id' => $p))) {
print_error('invalidaccessparameter');
throw new moodle_exception('invalidaccessparameter', 'error');
}
$cm = get_coursemodule_from_instance('externalcontent', $externalcontent->id,
$externalcontent->course, false, MUST_EXIST);

} else {
if (!$cm = get_coursemodule_from_id('externalcontent', $id)) {
print_error('invalidcoursemodule');
throw new moodle_exception('invalidcoursemodule', 'error');
}
$externalcontent = $DB->get_record('externalcontent', array('id' => $cm->instance), '*', MUST_EXIST);
}
Expand Down Expand Up @@ -91,3 +91,4 @@
}

echo $OUTPUT->footer();

0 comments on commit f0a3152

Please sign in to comment.