Skip to content

Commit

Permalink
Add ability to grab tagged resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dcai committed Jun 12, 2015
1 parent 07318d0 commit 7d662b9
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 13 deletions.
55 changes: 50 additions & 5 deletions block_equella_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once($CFG->dirroot.'/mod/equella/common/lib.php');
require_once($CFG->dirroot.'/mod/equella/common/soap.php');

class block_equella_links extends block_list {
function has_config() {
return true;
}

public function instance_allow_config() {
return true;
}

function init() {
$this->title = get_string('pluginname', 'block_equella_links');
Expand All @@ -26,6 +34,38 @@ function init() {
function instance_allow_multiple() {
return false;
}
function instance_config_save($data, $nolongerused = false) {
global $DB, $USER;

$course = $this->page->course;
$idnumber = $course->idnumber;
$context = context_course::instance($course->id);
$contextid = $context->id;
if (!empty($data->includetaggeditems) && !empty($idnumber)) {
$DB->delete_records('block_equella_links', array('contextid'=>$contextid, 'tagged'=>1));
$eq = new EQUELLA(equella_soap_endpoint());
$where = "where $data->xmlpath is '$idnumber'";
$sorttype = 0;
$q = null;
$r = $eq->searchItems($q, null, $where, 1, $sorttype, false, '', 20);
$xml = (string)$r;
foreach ($r->nodeList('/results/result/xml') as $el)
{
$record = new stdclass;
$record->url = $r->nodeValue('item/url', $el);
$record->title = $r->nodeValue('item/name', $el);
$record->userid = $USER->id;
$record->contextid = $contextid;
$record->tagged = 1;
$record->created = time();
$record->itemxml = $el->ownerDocument->saveXML($el);
$DB->insert_record('block_equella_links', $record);
}

}

parent::instance_config_save($data, $nolongerused);
}

function get_content() {
global $CFG, $COURSE, $OUTPUT, $DB;
Expand All @@ -43,10 +83,18 @@ function get_content() {
$this->instance->pageid = $COURSE->id;
}
}
$links = $DB->get_records('block_equella_links');

$context = context_course::instance($COURSE->id);
$contextid = $context->id;
$links = $DB->get_records('block_equella_links', array('contextid'=>$contextid));
$items = array();
foreach ($links as $link) {
$items[] = html_writer::link(new moodle_url(equella_appendtoken(equella_full_url(ltrim($link->url, '/')))), $link->title, array('target'=>'_blank'));
if (!filter_var($link->url, FILTER_VALIDATE_URL) === false) {
$url = new moodle_url(equella_appendtoken($link->url));
} else {
$url = new moodle_url(equella_appendtoken(equella_full_url(ltrim($link->url, '/'))));
}
$items[] = html_writer::link($url, $link->title, array('target'=>'_blank'));
}

$this->content = new stdClass;
Expand All @@ -61,7 +109,4 @@ function get_content() {
return $this->content;
}

function instance_allow_config() {
return true;
}
}
17 changes: 10 additions & 7 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/equella_links/db" VERSION="20131128" COMMENT="equella links block database table"
<XMLDB PATH="blocks/equella_links/db" VERSION="20150612" COMMENT="equella links block database table"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="block_equella_links" COMMENT="Default comment for block_equella_links, please edit me">
<TABLE NAME="block_equella_links" COMMENT="equella links table">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="url"/>
<FIELD NAME="url" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="userid" NEXT="contextid"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="url" NEXT="title"/>
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="contextid"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="url" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="created" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="tagged" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="itemxml" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
29 changes: 29 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,34 @@ function xmldb_block_equella_links_upgrade($oldversion, $block) {
upgrade_block_savepoint(true, 2013112806, 'equella_links');
}

if ($oldversion < 2015061101) {
// Define field created to be added to block_equella_links.
$table = new xmldb_table('block_equella_links');
$field = new xmldb_field('created', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'contextid');

// Conditionally launch add field created.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

$field = new xmldb_field('tagged', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'created');

// Conditionally launch add field tagged.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field itemxml to be added to block_equella_links.
$table = new xmldb_table('block_equella_links');
$field = new xmldb_field('itemxml', XMLDB_TYPE_TEXT, null, null, null, null, null, 'contextid');

// Conditionally launch add field itemxml.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Equella_links savepoint reached.
upgrade_block_savepoint(true, 2015061101, 'equella_links');
}
return true;
}
16 changes: 16 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class block_equella_links_edit_form extends block_edit_form {
protected function specific_definition($mform) {
global $CFG, $DB, $USER;
$mform->addElement('header', 'searchitems', 'Find tagged items');
$strrequired = get_string('required');

$mform->addElement('checkbox', 'config_includetaggeditems', get_string('configincludetaggeditems', 'block_equella_links'));
$mform->setType('config_includetaggeditems', PARAM_BOOL);

$mform->addElement('text', 'config_xmlpath', get_string('configxmlpath', 'block_equella_links'));
$mform->setType('config_xmlpath', PARAM_PATH);
$mform->setDefault('config_xmlpath', '/xml/integration/moodlecourseidnumber');
}
}
3 changes: 3 additions & 0 deletions lang/en/block_equella_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@
$string['addnewlink'] = 'Add a new EQUELLA link';
$string['equella_links:manageanylinks'] = 'Manage EQUELLA links';
$string['deletelinkconfirm'] = 'Are you sure you want to delete this EQUELLA link?';

$string['configincludetaggeditems'] = 'Content tagged to this course (inc. eReadings)';
$string['configxmlpath'] = 'XML Path (your EQ admin can provide)';
1 change: 1 addition & 0 deletions link_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function definition() {
}

$mform->addElement('hidden', 'linkid', '');
$mform->setType('linkid', PARAM_INT);

$mform->addElement('text', 'title', get_string('linktitle', 'block_equella_links'), array('size' => 60));
$mform->setType('title', PARAM_NOTAGS);
Expand Down
2 changes: 2 additions & 0 deletions managelinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
$addinglink = new stdClass;
$addinglink->title = $formdata->title;
$addinglink->url = $formdata->url;
$addinglink->created = time();
$addinglink->tagged = 0;
$DB->insert_record('block_equella_links', $addinglink);
}
redirect($baseurl);
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@

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

$plugin->version = 2013112806;
$plugin->version = 2015061101;
$plugin->component = 'block_equella_links'; // Full name of the plugin (used for diagnostics)

0 comments on commit 7d662b9

Please sign in to comment.