Skip to content

Commit f0a3152

Browse files
committed
MOODLE_311_STABLE
MOODLE_311_STABLE UPDATE Add custom_completion.php Ensure events always sent
1 parent a1e5b3b commit f0a3152

11 files changed

+131
-26
lines changed
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* The mod_externalcontent module custom completion class.
19+
*
20+
* @package mod_externalcontent
21+
* @copyright 2019-2021 LushOnline
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace mod_externalcontent\completion;
26+
27+
defined('MOODLE_INTERNAL') || die();
28+
29+
if (!class_exists('\core_completion\activity_custom_completion')) {
30+
// New API does not exist in this site, so do nothing.
31+
return;
32+
}
33+
34+
/**
35+
* The mod_externalcontent module custom completion.
36+
*
37+
* @package mod_externalcontent
38+
* @copyright 2019-2021 LushOnline
39+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40+
*/
41+
class custom_completion extends \core_completion\activity_custom_completion {
42+
/**
43+
* Fetches the completion state for a given completion rule.
44+
*
45+
* @param string $rule The completion rule.
46+
* @return int The completion state.
47+
*/
48+
public function get_state(string $rule): int {
49+
global $DB;
50+
51+
$this->validate_rule($rule);
52+
// Get external content details.
53+
$externalcontent = $DB->get_record('externalcontent', array('id' => $this->cm->instance), '*', MUST_EXIST);
54+
55+
switch ($rule) {
56+
case 'completionexternally':
57+
$params = array('userid' => $this->userid, 'externalcontentid' => $externalcontent->id, 'completed' => 1);
58+
$completed = $DB->record_exists('externalcontent_track', $params);
59+
60+
$status = $completed ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
61+
break;
62+
63+
default :
64+
$status = COMPLETION_INCOMPLETE;
65+
break;
66+
}
67+
68+
return $status;
69+
}
70+
71+
/**
72+
* Fetch the list of custom completion rules that this module defines.
73+
*
74+
* @return array
75+
*/
76+
public static function get_defined_custom_rules(): array {
77+
return ['completionexternally'];
78+
}
79+
80+
/**
81+
* Returns an associative array of the descriptions of custom completion rules.
82+
*
83+
* @return array
84+
*/
85+
public function get_custom_rule_descriptions(): array {
86+
return ['completionexternally' => get_string('eventcompletedexternally', 'externalcontent')];
87+
}
88+
89+
/**
90+
* Returns an array of all completion rules, in the order they should be displayed to users.
91+
*
92+
* @return array
93+
*/
94+
public function get_sort_order(): array {
95+
return [
96+
'completionview',
97+
'completionexternally',
98+
];
99+
}
100+
}

classes/event/course_module_completedexternally.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public static function get_name() {
7070
*/
7171
public function get_description() {
7272
return get_string('eventcompletedexternallydesc', 'externalcontent',
73-
['userid' => $this->userid, 'contextinstanceid' => $this->contextinstanceid]);
73+
['userid' => $this->relateduserid ?? $this->userid,
74+
'contextinstanceid' => $this->contextinstanceid]);
7475
}
7576

7677
/**

classes/event/course_module_scoredexternally.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public static function get_name() {
7070
*/
7171
public function get_description() {
7272
return get_string('eventscoredexternallydesc', 'externalcontent',
73-
['userid' => $this->userid, 'contextinstanceid' => $this->contextinstanceid, 'score' => $this->other]);
73+
['userid' => $this->relateduserid ?? $this->userid,
74+
'contextinstanceid' => $this->contextinstanceid,
75+
'score' => $this->other]);
7476
}
7577

7678
/**

db/services.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
'capabilities' => 'mod/externalcontent:view',
4747
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
4848
),
49-
);
49+
);

grade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
$id = required_param('id', PARAM_INT);
2929

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

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

39-
redirect(new moodle_url('/mod/externalcontent/view.php', array('id' => $cm->id)));
39+
redirect(new moodle_url('/mod/externalcontent/view.php', array('id' => $cm->id)));

lib.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
426426
}
427427

428428
$response = new \stdClass();
429+
$response->userid = $userid;
429430
$response->status = false;
430431
$response->completionupdated = false;
431432
$response->scoreupdated = false;
@@ -458,22 +459,22 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
458459
}
459460

460461
if ($completed) {
462+
$params = array(
463+
'context' => $context,
464+
'objectid' => $externalcontent->id,
465+
'relateduserid' => $userid,
466+
);
467+
468+
$event = \mod_externalcontent\event\course_module_completedexternally::create($params);
469+
$event->add_record_snapshot('course_modules', $cm);
470+
$event->add_record_snapshot('course', $course);
471+
$event->add_record_snapshot('externalcontent', $externalcontent);
472+
$event->trigger();
473+
461474
if ($currentstate->completionstate == COMPLETION_COMPLETE) {
462475
$response->message .= ' External content completion state already set to COMPLETION_COMPLETE.';
463476
$response->completionupdated = false;
464477
} else {
465-
$params = array(
466-
'context' => $context,
467-
'objectid' => $externalcontent->id,
468-
'userid' => $userid,
469-
);
470-
471-
$event = \mod_externalcontent\event\course_module_completedexternally::create($params);
472-
$event->add_record_snapshot('course_modules', $cm);
473-
$event->add_record_snapshot('course', $course);
474-
$event->add_record_snapshot('externalcontent', $externalcontent);
475-
$event->trigger();
476-
477478
$completion->update_state($cm, COMPLETION_COMPLETE, $userid);
478479
$response->message .= ' External content completion status set to COMPLETION_COMPLETE.';
479480
$response->completionupdated = true;
@@ -484,7 +485,7 @@ function externalcontent_update_completion_state($course, $cm, $context = null,
484485
$params = array(
485486
'context' => $context,
486487
'objectid' => $externalcontent->id,
487-
'userid' => $userid,
488+
'relateduserid' => $userid,
488489
'other' => $score,
489490
);
490491

@@ -646,4 +647,4 @@ function externalcontent_update_grades($externalcontent, $userid = 0, $removegra
646647
} else {
647648
externalcontent_grade_item_update($externalcontent);
648649
}
649-
}
650+
}

lrs/aboutcontroller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ public function about(Request $request, Response $response, array $args) {
7575

7676
return $response->withJson($versions);
7777
}
78-
}
78+
}

lrs/statementcontroller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,4 @@ public function notimplemented(Request $request, Response $response, array $args
228228
->withAddedHeader('Content-Type', 'text/plain')
229229
->write($message);
230230
}
231-
}
231+
}

lrs/xapihelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,4 @@ public static function mark_completed($record) {
216216
}
217217
return $response;
218218
}
219-
}
219+
}

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

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

27-
$plugin->version = 2021030400;
27+
$plugin->version = 2021072100;
2828
$plugin->requires = 2018051700; // Requires this Moodle version v3.5 see https://docs.moodle.org/dev/Releases.
2929
$plugin->component = 'mod_externalcontent';
3030
$plugin->maturity = MATURITY_STABLE;
31-
$plugin->release = '1.6';
31+
$plugin->release = '1.7';

view.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535

3636
if ($p) {
3737
if (!$externalcontent = $DB->get_record('externalcontent', array('id' => $p))) {
38-
print_error('invalidaccessparameter');
38+
throw new moodle_exception('invalidaccessparameter', 'error');
3939
}
4040
$cm = get_coursemodule_from_instance('externalcontent', $externalcontent->id,
4141
$externalcontent->course, false, MUST_EXIST);
4242

4343
} else {
4444
if (!$cm = get_coursemodule_from_id('externalcontent', $id)) {
45-
print_error('invalidcoursemodule');
45+
throw new moodle_exception('invalidcoursemodule', 'error');
4646
}
4747
$externalcontent = $DB->get_record('externalcontent', array('id' => $cm->instance), '*', MUST_EXIST);
4848
}
@@ -91,3 +91,4 @@
9191
}
9292

9393
echo $OUTPUT->footer();
94+

0 commit comments

Comments
 (0)