Skip to content

Commit

Permalink
MDL-66934 mod_lti: support context history param
Browse files Browse the repository at this point in the history
  • Loading branch information
claudevervoort committed Sep 18, 2020
1 parent f852aa8 commit 5b8c646
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,11 @@ public function process_course($data) {
$data->idnumber = '';
}

// If we restore a course from this site, let's capture the original course id.
if ($isnewcourse && $this->get_task()->is_samesite()) {
$data->originalcourseid = $this->get_task()->get_old_courseid();
}

// Any empty value for course->hiddensections will lead to 0 (default, show collapsed).
// It has been reported that some old 1.9 courses may have it null leading to DB error. MDL-31532
if (empty($data->hiddensections)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20200804" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20200911" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -101,6 +101,7 @@
<FIELD NAME="enablecompletion" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="1 = allow use of 'completion' progress-tracking on this course. 0 = disable completion tracking on this course."/>
<FIELD NAME="completionnotify" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Notify users when they complete this course"/>
<FIELD NAME="cacherev" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Incrementing revision for validating the course content cache"/>
<FIELD NAME="originalcourseid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the id of the source course when a new course originates from a restore of another course on the same site."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
14 changes: 14 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2694,5 +2694,19 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021052500.13);
}

if ($oldversion < 2021052500.15) {
// Copy From id captures the id of the source course when a new course originates from a restore
// of another course on the same site.
$table = new xmldb_table('course');
$field = new xmldb_field('originalcourseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.15);
}

return true;
}
1 change: 1 addition & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.

=== 3.10 ===
* Retains the source course id when a course is copied from another course on the same site.
* Added function setScrollable in core/modal. This function can be used to set the modal's body to be scrollable or not
when the modal's height exceeds the browser's height. This is also supported in core/modal_factory through the
'scrollable' config parameter which can be set to either true or false. If not explicitly defined, the default value
Expand Down
21 changes: 21 additions & 0 deletions mod/lti/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2008,10 +2008,30 @@ function lti_calculate_custom_parameter($value) {
switch ($value) {
case 'Moodle.Person.userGroupIds':
return implode(",", groups_get_user_groups($COURSE->id, $USER->id)[0]);
case 'Context.id.history':
return implode(",", get_course_history($COURSE));
}
return null;
}

/**
* Build the history chain for this course using the course originalcourseid.
*
* @param object $course course for which the history is returned.
*
* @return array ids of the source course in ancestry order, immediate parent 1st.
*/
function get_course_history($course) {
global $DB;
$history = [];
$parentid = $course->originalcourseid;
while (!empty($parentid) && !in_array($parentid, $history)) {
$history[] = $parentid;
$parentid = $DB->get_field('course', 'originalcourseid', array('id' => $parentid));
}
return $history;
}

/**
* Used for building the names of the different custom parameters
*
Expand Down Expand Up @@ -3641,6 +3661,7 @@ function lti_get_capabilities() {
'Context.id' => 'context_id',
'Context.title' => 'context_title',
'Context.label' => 'context_label',
'Context.id.history' => null,
'Context.sourcedId' => 'lis_course_section_sourcedid',
'Context.longDescription' => '$COURSE->summary',
'Context.timeFrame.begin' => '$COURSE->startdate',
Expand Down
19 changes: 19 additions & 0 deletions mod/lti/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,25 @@ public function test_lti_get_launch_data_default_organizationid_orgid_override()
$this->assertEquals($launchdata[1]['tool_consumer_instance_guid'], 'overridden!');
}

public function test_get_course_history() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$parentparentcourse = $this->getDataGenerator()->create_course();
$parentcourse = $this->getDataGenerator()->create_course();
$parentcourse->originalcourseid = $parentparentcourse->id;
$DB->update_record('course', $parentcourse);
$course = $this->getDataGenerator()->create_course();
$course->originalcourseid = $parentcourse->id;
$DB->update_record('course', $course);
$this->assertEquals(get_course_history($parentparentcourse), []);
$this->assertEquals(get_course_history($parentcourse), [$parentparentcourse->id]);
$this->assertEquals(get_course_history($course), [$parentcourse->id, $parentparentcourse->id]);
$course->originalcourseid = 38903;
$DB->update_record('course', $course);
$this->assertEquals(get_course_history($course), [38903]);
}

/**
* Create an LTI Tool.
*
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

$version = 2021052500.14; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2021052500.15; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.0dev (Build: 20200918)'; // Human-friendly version name
Expand Down

0 comments on commit 5b8c646

Please sign in to comment.