MDL-69542 enrol_lti: add version based resource publication

Adds tabs and a new table allowing LTI Advantage published resources
to be viewed.
This commit is contained in:
Jake Dallimore 2022-01-24 17:12:47 +08:00
parent c6c7b1dadc
commit 2516938537
5 changed files with 391 additions and 19 deletions

View File

@ -0,0 +1,223 @@
<?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/>.
namespace enrol_lti\local\ltiadvantage\table;
use enrol_lti\helper;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/tablelib.php');
/**
* Class which displays a list of resources published over LTI Advantage.
*
* @package enrol_lti
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class published_resources_table extends \table_sql {
/**
* @var \enrol_plugin $ltiplugin
*/
protected $ltiplugin;
/**
* @var bool $ltienabled
*/
protected $ltienabled;
/**
* @var bool $canconfig
*/
protected $canconfig;
/**
* @var int $courseid The course id.
*/
protected $courseid;
/**
* Sets up the table.
*
* @param string $courseid The id of the course.
*/
public function __construct($courseid) {
parent::__construct('enrol_lti_manage_table');
$this->define_columns(array(
'name',
'launch',
'edit'
));
$this->define_headers(array(
get_string('name'),
get_string('lti13launchdetails', 'enrol_lti'),
get_string('edit')
));
$this->collapsible(false);
$this->sortable(false);
// Set the variables we need access to.
$this->ltiplugin = enrol_get_plugin('lti');
$this->ltienabled = enrol_is_enabled('lti');
$this->canconfig = has_capability('moodle/course:enrolconfig', \context_course::instance($courseid));
$this->courseid = $courseid;
// Set help icons.
$launchicon = new \help_icon('lti13launchdetails', 'enrol_lti');
$this->define_help_for_headers(['1' => $launchicon]);
}
/**
* Generate the name column.
*
* @param \stdClass $tool event data.
* @return string
*/
public function col_name($tool) {
$name = helper::get_name($tool);
return $this->get_display_text($tool, $name);
}
/**
* Generate the launch column.
*
* @param \stdClass $tool instance data.
* @return string
*/
public function col_launch($tool) {
global $OUTPUT;
$customparamslabel = get_string('customproperties', 'enrol_lti');
$customparams = "id={$tool->uuid}";
$launchurl = new \moodle_url('/enrol/lti/launch.php');
$launchurllabel = get_string('launchurl', 'enrol_lti');
$data = [
"rows" => [
[
"label" => $launchurllabel,
"text" => $launchurl->out(false),
"id" => "launchurl",
"hidelabel" => false
],
[
"label" => $customparamslabel,
"text" => $customparams,
"id" => "customparams",
"hidelabel" => false
]
]
];
$return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
return $return;
}
/**
* Generate the edit column.
*
* @param \stdClass $tool event data.
* @return string
*/
public function col_edit($tool) {
global $OUTPUT;
$buttons = array();
$instance = new \stdClass();
$instance->id = $tool->enrolid;
$instance->courseid = $tool->courseid;
$instance->enrol = 'lti';
$instance->status = $tool->status;
$strdelete = get_string('delete');
$strenable = get_string('enable');
$strdisable = get_string('disable');
$url = new \moodle_url('/enrol/lti/index.php', array('sesskey' => sesskey(), 'courseid' => $this->courseid));
if ($this->ltiplugin->can_delete_instance($instance)) {
$aurl = new \moodle_url($url, array('action' => 'delete', 'instanceid' => $instance->id));
$buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/delete', $strdelete, 'core',
array('class' => 'iconsmall')));
}
if ($this->ltienabled && $this->ltiplugin->can_hide_show_instance($instance)) {
if ($instance->status == ENROL_INSTANCE_ENABLED) {
$aurl = new \moodle_url($url, array('action' => 'disable', 'instanceid' => $instance->id));
$buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/hide', $strdisable, 'core',
array('class' => 'iconsmall')));
} else if ($instance->status == ENROL_INSTANCE_DISABLED) {
$aurl = new \moodle_url($url, array('action' => 'enable', 'instanceid' => $instance->id));
$buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/show', $strenable, 'core',
array('class' => 'iconsmall')));
}
}
if ($this->ltienabled && $this->canconfig) {
$linkparams = array(
'courseid' => $instance->courseid,
'id' => $instance->id, 'type' => $instance->enrol,
'returnurl' => new \moodle_url('/enrol/lti/index.php', array('courseid' => $this->courseid))
);
$editlink = new \moodle_url("/enrol/editinstance.php", $linkparams);
$buttons[] = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit'), 'core',
array('class' => 'iconsmall')));
}
return implode(' ', $buttons);
}
/**
* Query the reader. Store results in the object for use by build_table.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
$total = helper::count_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p3']);
$this->pagesize($pagesize, $total);
$tools = helper::get_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p3'],
$this->get_page_start(), $this->get_page_size());
$this->rawdata = $tools;
// Set initial bars.
if ($useinitialsbar) {
$this->initialbars($total > $pagesize);
}
}
/**
* Returns text to display in the columns.
*
* @param \stdClass $tool the tool
* @param string $text the text to alter
* @return string
*/
protected function get_display_text($tool, $text) {
if ($tool->status != ENROL_INSTANCE_ENABLED) {
return \html_writer::tag('span', $text, array('class' => 'dimmed_text'));
}
return $text;
}
}

View File

@ -182,7 +182,8 @@ class manage_table extends \table_sql {
$strenable = get_string('enable');
$strdisable = get_string('disable');
$url = new \moodle_url('/enrol/lti/index.php', array('sesskey' => sesskey(), 'courseid' => $this->courseid));
$url = new \moodle_url('/enrol/lti/index.php',
array('sesskey' => sesskey(), 'courseid' => $this->courseid, 'legacy' => 1));
if ($this->ltiplugin->can_delete_instance($instance)) {
$aurl = new \moodle_url($url, array('action' => 'delete', 'instanceid' => $instance->id));
@ -205,8 +206,11 @@ class manage_table extends \table_sql {
if ($this->ltienabled && $this->canconfig) {
$linkparams = array(
'courseid' => $instance->courseid,
'id' => $instance->id, 'type' => $instance->enrol,
'returnurl' => new \moodle_url('/enrol/lti/index.php', array('courseid' => $this->courseid))
'id' => $instance->id,
'type' => $instance->enrol,
'legacy' => 1,
'returnurl' => new \moodle_url('/enrol/lti/index.php',
array('courseid' => $this->courseid, 'legacy' => 1))
);
$editlink = new \moodle_url("/enrol/editinstance.php", $linkparams);
$buttons[] = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit'), 'core',
@ -223,10 +227,10 @@ class manage_table extends \table_sql {
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
$total = \enrol_lti\helper::count_lti_tools(array('courseid' => $this->courseid));
$total = \enrol_lti\helper::count_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p0/LTI-2p0']);
$this->pagesize($pagesize, $total);
$tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $this->courseid), $this->get_page_start(),
$this->get_page_size());
$tools = \enrol_lti\helper::get_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p0/LTI-2p0'],
$this->get_page_start(), $this->get_page_size());
$this->rawdata = $tools;
// Set initial bars.
if ($useinitialsbar) {

View File

@ -22,11 +22,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use enrol_lti\local\ltiadvantage\table\published_resources_table;
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot.'/enrol/lti/lib.php');
$courseid = required_param('courseid', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$legacy = optional_param('legacy', false, PARAM_BOOL);
if ($action) {
require_sesskey();
$instanceid = required_param('instanceid', PARAM_INT);
@ -43,7 +46,7 @@ require_capability('moodle/course:enrolreview', $context);
$ltiplugin = enrol_get_plugin('lti');
$canconfig = has_capability('moodle/course:enrolconfig', $context);
$pageurl = new moodle_url('/enrol/lti/index.php', array('courseid' => $courseid));
$pageurl = new moodle_url('/enrol/lti/index.php', array('courseid' => $courseid, 'legacy' => $legacy));
$PAGE->set_url($pageurl);
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
@ -98,27 +101,62 @@ if ($action) {
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('toolsprovided', 'enrol_lti'));
echo "<p>" .get_string('toolsprovided_help', 'enrol_lti') . "</p>";
echo "<p class=helplink>" . $OUTPUT->doc_link('enrol/lti/index',
get_string('morehelp')) ."</p>";
if (\enrol_lti\helper::count_lti_tools(array('courseid' => $courseid)) > 0) {
$table = new \enrol_lti\manage_table($courseid);
$table->define_baseurl($pageurl);
$table->out(50, false);
if ($legacy) {
echo $OUTPUT->heading(get_string('toolsprovided', 'enrol_lti'));
echo html_writer::tag('p', get_string('toolsprovided_help', 'enrol_lti'));
} else {
$notify = new \core\output\notification(get_string('notoolsprovided', 'enrol_lti'),
echo $OUTPUT->heading(get_string('publishedcontent', 'enrol_lti'));
echo html_writer::tag('p', get_string('publishedcontent_help', 'enrol_lti'));
}
echo html_writer::tag('p', $OUTPUT->doc_link('enrol/lti/index', get_string('morehelp')), ['class' => 'helplink']);
// Distinguish between legacy published tools and LTI-Advantage published resources.
$tabs = [
0 => [
new tabobject('0', new moodle_url('/enrol/lti/index.php', ['courseid' => $courseid]),
get_string('lti13', 'enrol_lti')),
new tabobject('1', new moodle_url('/enrol/lti/index.php', ['legacy' => 1, 'courseid' => $courseid]),
get_string('ltilegacy', 'enrol_lti')),
]
];
$selected = $legacy ? '1' : '0';
echo html_writer::div(print_tabs($tabs, $selected, null, null, true), 'lti-resource-publication');
if ($legacy) {
$notify = new \core\output\notification(get_string('ltilegacydeprecatednotice', 'enrol_lti'),
\core\output\notification::NOTIFY_WARNING);
echo $OUTPUT->render($notify);
if (\enrol_lti\helper::count_lti_tools(array('courseid' => $courseid, 'ltiversion' => 'LTI-1p0/LTI-2p0')) > 0) {
$table = new \enrol_lti\manage_table($courseid);
$table->define_baseurl($pageurl);
$table->out(50, false);
} else {
$notify = new \core\output\notification(get_string('notoolsprovided', 'enrol_lti'),
\core\output\notification::NOTIFY_INFO);
echo $OUTPUT->render($notify);
}
} else {
if (\enrol_lti\helper::count_lti_tools(array('courseid' => $courseid, 'ltiversion' => 'LTI-1p3')) > 0) {
$table = new published_resources_table($courseid);
$table->define_baseurl($pageurl);
$table->out(50, false);
} else {
$notify = new \core\output\notification(get_string('nopublishedcontent', 'enrol_lti'),
\core\output\notification::NOTIFY_INFO);
echo $OUTPUT->render($notify);
}
}
if ($ltiplugin->can_add_instance($course->id)) {
echo $OUTPUT->single_button(new moodle_url('/enrol/editinstance.php',
array(
'legacy' => $legacy,
'type' => 'lti',
'courseid' => $course->id,
'returnurl' => new moodle_url('/enrol/lti/index.php', array('courseid' => $course->id)))
'returnurl' => new moodle_url('/enrol/lti/index.php', ['courseid' => $course->id, 'legacy' => $legacy]))
),
get_string('add'));
}

View File

@ -30,9 +30,11 @@ Feature: Check that the page listing the shared external tools is functioning as
And I set the following fields to these values:
| Custom instance name | Assignment - LTI |
| Tool to be published | Test assignment name |
| LTI version | Legacy LTI (1.1/2.0) |
And I press "Add method"
And I am on "Course 1" course homepage
And I navigate to "Published as LTI tools" in current page administration
And I click on "Legacy LTI (1.1/2.0" "link"
And I should see "Assignment - LTI" in the ".generaltable" "css_element"
When I click on "Disable" "link" in the "Assignment - LTI" "table_row"
Then ".dimmed_text" "css_element" should exist in the "Assignment - LTI" "table_row"
@ -49,5 +51,5 @@ Feature: Check that the page listing the shared external tools is functioning as
And I should see "Course - LTI" in the ".generaltable" "css_element"
And I click on "Delete" "link" in the "Course - LTI" "table_row"
And I press "Continue"
And I should see "No tools provided"
And I should see "No resources or activities are published yet"
And I should not see "Course - LTI"

View File

@ -0,0 +1,105 @@
@enrol @enrol_lti
Feature: Publish activities and resources over LTI Advantage
In order to make content available to external platforms
As a teacher
I need to be able to publish and manage activities and resources using LTI Advantage
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber | section |
| assign | Test assignment name | Submit your online text | C1 | assign1 | 1 |
And I enable "lti" "enrol" plugin
Scenario: A teacher can publish an activity
Given I log in as "teacher1"
And I am on the "Course 1" "enrolment methods" page
When I select "Publish as LTI tool" from the "Add method" singleselect
And the following fields match these values:
| LTI version | LTI Advantage |
And I set the following fields to these values:
| Custom instance name | Published assignment |
| Tool to be published | Test assignment name |
And I press "Add method"
And I am on "Course 1" course homepage
And I navigate to "Published as LTI tools" in current page administration
Then I should see "Launch URL" in the "Published assignment" "table_row"
And I should see "Custom properties" in the "Published assignment" "table_row"
And "Edit" "link" should exist in the "Published assignment" "table_row"
And "Delete" "link" should exist in the "Published assignment" "table_row"
And "Disable" "link" should exist in the "Published assignment" "table_row"
Scenario: A teacher can edit a published resource/activity
Given the following "enrol_lti > published resources" exist:
| name | uuid | activity | course |
| Published assignment | my-uuid-123 | assign1 | C1 |
And I am on the "Course 1" "Course" page logged in as "teacher1"
And I navigate to "Published as LTI tools" in current page administration
And the "value" attribute of "Custom properties" "field" should contain "id=my-uuid-123"
When I click on "Edit" "link" in the "Published assignment" "table_row"
And I set the following fields to these values:
| Custom instance name | New instance name |
And I press "Cancel"
And I should see "Published assignment" in the "Published assignment" "table_row"
And the "value" attribute of "Custom properties" "field" should contain "id=my-uuid-123"
And I click on "Edit" "link" in the "Published assignment" "table_row"
And I set the following fields to these values:
| Custom instance name | New instance name |
And I press "Save changes"
Then I should see "New instance name"
And the "value" attribute of "Custom properties" "field" should contain "id=my-uuid-123"
Scenario: A teacher can disable and enable a published resource/activity
Given the following "enrol_lti > published resources" exist:
| name | activity | course |
| Published assignment | assign1 | C1 |
And I am on the "Course 1" "Course" page logged in as "teacher1"
And I navigate to "Published as LTI tools" in current page administration
When I click on "Disable" "link" in the "Published assignment" "table_row"
Then ".dimmed_text" "css_element" should exist in the "Published assignment" "table_row"
And I click on "Enable" "link" in the "Published assignment" "table_row"
And ".dimmed_text" "css_element" should not exist in the "Published assignment" "table_row"
Scenario: A teacher can delete a published tool
Given the following "enrol_lti > published resources" exist:
| name | activity | course |
| Published assignment | assign1 | C1 |
And I am on the "Course 1" "Course" page logged in as "teacher1"
And I navigate to "Published as LTI tools" in current page administration
When I click on "Delete" "link" in the "Published assignment" "table_row"
And I press "Cancel"
And I should see "Published assignment" in the "Published assignment" "table_row"
And I click on "Delete" "link" in the "Published assignment" "table_row"
And I press "Continue"
And I should see "No resources or activities are published yet"
And I should not see "Published assignment"
Scenario: A teacher can switch the version of a published resource from LTI 1.1 to LTI Advantage
Given the following "enrol_lti > published resources" exist:
| name | activity | course | ltiversion |
| Published assignment | assign1 | C1 | LTI-1p0/LTI-2p0 |
And I am on the "Course 1" "Course" page logged in as "teacher1"
And I navigate to "Published as LTI tools" in current page administration
And I should see "No resources or activities are published yet"
And I click on "Legacy LTI (1.1/2.0)" "link"
And I should see "Published assignment"
When I click on "Edit" "link" in the "Published assignment" "table_row"
And the following fields match these values:
| LTI version | Legacy LTI (1.1/2.0) |
And I set the following fields to these values:
| LTI version | LTI Advantage |
| Custom instance name | New instance name |
And I press "Save changes"
And I click on "LTI Advantage" "link"
Then I should see "New instance name"
And "LTI Advantage" "link" should not exist
And "Legacy LTI (1.1/2.0)" "link" should exist
And the "value" attribute of "Custom properties" "field" should contain "id="