Skip to content

Commit

Permalink
EQUELLA link block is configurable now
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai committed Dec 3, 2013
1 parent 4ede569 commit f8936cf
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 10 deletions.
File renamed without changes.
26 changes: 19 additions & 7 deletions equella_links/block_equella_links.php → block_equella_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ function init() {
$this->title = get_string('pluginname', 'block_equella_links');
}

function instance_allow_multiple() {
return false;
}

function get_content() {
global $CFG, $COURSE, $SESSION;
global $CFG, $COURSE, $OUTPUT, $DB;

if( $this->content !== NULL ) {
return $this->content;
Expand All @@ -39,17 +43,25 @@ function get_content() {
$this->instance->pageid = $COURSE->id;
}
}

$links = $DB->get_records('block_equella_links');
$items = array();
$items[]= '<a target="_blank" href="'.equella_appendtoken(equella_full_url('/access/search.do')).'">Search</a>';
$items[]= '<a target="_blank" href="'.equella_appendtoken(equella_full_url('/access/contribute.do')).'">Contribute</a>';
$items[]= '<a target="_blank" href="'.equella_appendtoken(equella_full_url('/access/myresources.do')).'">My Resources</a>';
foreach ($links as $link) {
$items[] = html_writer::link(new moodle_url(equella_appendtoken(equella_full_url(ltrim($link->url, '/')))), $link->title, array('target'=>'_blank'));
}

$this->content = new stdClass;
$this->content->items = $items;
$this->content->icons = array();
$this->content->footer = '';

if (has_any_capability(array('block/equella_links:manageanylinks'), $this->context)) {
$url = new moodle_url('/blocks/equella_links/managelinks.php', array('courseid'=>$this->page->course->id));
$this->content->footer = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edit')));
}

return $this->content;
}

function instance_allow_config() {
return true;
}
}
13 changes: 12 additions & 1 deletion equella_links/db/access.php → db/access.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// This file is part of the EQUELLA Moodle Integration - https://github.com/equella/moodle-block-tasks
// This file is part of the EQUELLA Moodle Integration - https://github.com/equella/moodle-block-links
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,4 +30,15 @@

'clonepermissionsfrom' => 'moodle/site:manageblocks'
),

'block/equella_links:manageanylinks' => array(

'riskbitmask' => RISK_SPAM,

'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'manager' => CAP_ALLOW
)
),
);
20 changes: 20 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/equella_links/db" VERSION="20131128" 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">
<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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
66 changes: 66 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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/>.

function xmldb_block_equella_links_upgrade($oldversion, $block) {
global $DB, $USER;

$dbman = $DB->get_manager();

if ($oldversion < 2013112805) {

// Define table block_equella_links to be created
$table = new xmldb_table('block_equella_links');

// Adding fields to table block_equella_links
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('url', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);

// Adding keys to table block_equella_links
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for block_equella_links
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// equella_links savepoint reached
upgrade_block_savepoint(true, 2013112805, 'equella_links');
}
if ($oldversion < 2013112806) {
$syscontext = get_system_context();
$contextid = $syscontext->id;
$link = new stdClass;
$link->contextid = $contextid;
$link->userid = $USER->id;
$link->url = '/access/search.do';
$link->title = 'Search';
$DB->insert_record('block_equella_links', $link);
$link->url = '/access/contribute.do';
$link->title = 'Contribute';
$DB->insert_record('block_equella_links', $link);
$link->url = '/access/myresources.do';
$link->title = 'My Resources';
$DB->insert_record('block_equella_links', $link);
// equella_links savepoint reached
upgrade_block_savepoint(true, 2013112806, 'equella_links');
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,20 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

$string['pluginname'] = 'EQUELLA Links';
$string['searchlink'] = 'Search';
$string['myresourceslink'] = 'My Resources';
$string['contributelink'] = 'Contribute';
$string['links'] = 'Links';

?>
$string['config.title'] = 'EQUELLA links settings';

$string['linksaddedit'] = 'Manage links';

$string['addinglinkheader'] = 'Adding link';
$string['editinglinkheader'] = 'Editing link';
$string['linktitle'] = 'Title';
$string['equellaurl'] = 'EQUELLA URL';

$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?';
47 changes: 47 additions & 0 deletions link_edit_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/formslib.php');

class equella_links_edit_form extends moodleform {
protected $isadding;
protected $title = '';
protected $description = '';

function __construct($actionurl, $isadding = false) {
$this->isadding = $isadding;
parent::moodleform($actionurl);
}

function definition() {
$mform =& $this->_form;

// Then show the fields about where this block appears.
if ($this->isadding) {
$mform->addElement('header', 'addinglinkheader', get_string('addinglinkheader', 'block_equella_links'));
} else {
$mform->addElement('header', 'editinglinkheader', get_string('editinglinkheader', 'block_equella_links'));
}

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

$mform->addElement('text', 'title', get_string('linktitle', 'block_equella_links'), array('size' => 60));
$mform->setType('title', PARAM_NOTAGS);
$mform->addRule('title', null, 'required');

$mform->addElement('text', 'url', get_string('equellaurl', 'block_equella_links'), array('size' => 60));
$mform->setType('url', PARAM_URL);
$mform->addRule('url', null, 'required');

$submitlabal = null; // Default
if ($this->isadding) {
$submitlabal = get_string('addnewlink', 'block_equella_links');
}
$this->add_action_buttons(true, $submitlabal);
}


function set_data($params) {
parent::set_data($params);
}
}
86 changes: 86 additions & 0 deletions managelinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/link_edit_form.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/tablelib.php');

$courseid = required_param('courseid', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$linkid = optional_param('linkid', '', PARAM_INT);

$baseurl = new moodle_url('/blocks/equella_links/managelinks.php', array('courseid'=>$courseid));
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = context_course::instance($courseid);

$PAGE->set_course($course);
$PAGE->set_url($baseurl);
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('linksaddedit', 'block_equella_links'));

require_capability('block/equella_links:manageanylinks', $context);



if (!empty($action)) {
$link = $DB->get_record('block_equella_links', array('id' => $linkid), '*', MUST_EXIST);
if ($action == 'edit') {
$mform = new equella_links_edit_form($PAGE->url, false);
$mform->set_data(array('title'=>$link->title, 'url'=>$link->url, 'linkid'=>$link->id));
} else if ($action == 'delete') {
$DB->delete_records('block_equella_links', array('id' => $linkid));
// we are done here
redirect($baseurl);
}
} else {
$mform = new equella_links_edit_form($PAGE->url, true);
}

if ($mform->is_cancelled()) {
redirect($baseurl);
}

if ($formdata = $mform->get_data()) {
if (!empty($formdata->linkid)) {
$editinglink = new stdClass;
$editinglink->id = $formdata->linkid;
$editinglink->title = $formdata->title;
$editinglink->url = $formdata->url;
$DB->update_record('block_equella_links', $editinglink);
} else {
$addinglink = new stdClass;
$addinglink->title = $formdata->title;
$addinglink->url = $formdata->url;
$DB->insert_record('block_equella_links', $addinglink);
}
redirect($baseurl);
}


echo $OUTPUT->header();

$table = new flexible_table('equella-links-display');
$table->define_columns(array('link', 'title', 'actions'));
$table->define_headers(array(get_string('equellaurl', 'block_equella_links'), get_string('linktitle', 'block_equella_links'), get_string('actions', 'moodle')));
$table->define_baseurl($baseurl);
$table->set_attribute('cellspacing', '0');
$table->set_attribute('class', 'generaltable generalbox');

$table->setup();
$links = $DB->get_records('block_equella_links');
foreach ($links as $link) {
$editurl = new moodle_url('/blocks/equella_links/managelinks.php', array('linkid'=>$link->id, 'action'=>'edit', 'courseid'=>$courseid));
$editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')));

$deleteurl = new moodle_url('/blocks/equella_links/managelinks.php', array('linkid'=>$link->id, 'action'=>'delete', 'courseid'=>$courseid));
$deleteicon = new pix_icon('t/delete', get_string('delete'));
$deleteaction = $OUTPUT->action_icon($deleteurl, $deleteicon, new confirm_action(get_string('deletelinkconfirm', 'block_equella_links')));
$action = $editaction . ' ' . $deleteaction;
$table->add_data(array($link->url, $link->title, $action));
}

$table->print_html();

$mform->display();

echo $OUTPUT->footer();
2 changes: 1 addition & 1 deletion equella_links/version.php → version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@

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

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

0 comments on commit f8936cf

Please sign in to comment.