Skip to content

Commit

Permalink
MDL-53443 mod_feedback: templates code clean up and behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 13, 2016
1 parent f40b444 commit df003a8
Show file tree
Hide file tree
Showing 15 changed files with 839 additions and 344 deletions.
2 changes: 1 addition & 1 deletion lib/tests/behat/behat_general.php
Expand Up @@ -1266,7 +1266,7 @@ public function following_should_not_exist_in_the_table($table, TableNode $data)
* @param string $link the text of the link.
* @return string the content of the downloaded file.
*/
protected function download_file_from_link($link) {
public function download_file_from_link($link) {
// Find the link.
$linknode = $this->find_link($link);
$this->ensure_node_is_visible($linknode);
Expand Down
87 changes: 87 additions & 0 deletions mod/feedback/classes/templates_table.php
@@ -0,0 +1,87 @@
<?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/>.

/**
* Contains class mod_feedback_templates_table
*
* @package mod_feedback
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

global $CFG;
require_once($CFG->libdir . '/tablelib.php');

/**
* Class mod_feedback_templates_table
*
* @package mod_feedback
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_feedback_templates_table extends flexible_table {

/**
* Constructor
* @param int $uniqueid all tables have to have a unique id, this is used
* as a key when storing table properties like sort order in the session.
* @param moodle_url $baseurl
*/
public function __construct($uniqueid, $baseurl) {
parent::__construct($uniqueid);

$tablecolumns = array('template', 'action');
$tableheaders = array(get_string('template', 'feedback'), '');

$this->set_attribute('class', 'templateslist');

$this->define_columns($tablecolumns);
$this->define_headers($tableheaders);
$this->define_baseurl($baseurl);
$this->column_class('template', 'template');
$this->column_class('action', 'action');

$this->sortable(false);
}

/**
* Displays the table with the given set of templates
* @param array $templates
*/
public function display($templates) {
global $OUTPUT;
if (empty($templates)) {
echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
'generalbox boxaligncenter');
return;
}

$this->setup();
$strdeletefeedback = get_string('delete_template', 'feedback');

foreach ($templates as $template) {
$data = array();
$data[] = format_string($template->name);
$url = new moodle_url($this->baseurl, array('deletetempl' => $template->id));

$data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
$this->add_data($data);
}
$this->finish_output();
}
}
169 changes: 27 additions & 142 deletions mod/feedback/delete_template.php
Expand Up @@ -24,89 +24,44 @@

require_once("../../config.php");
require_once("lib.php");
require_once('delete_template_form.php');
require_once($CFG->libdir.'/tablelib.php');

$current_tab = 'templates';

$id = required_param('id', PARAM_INT);
$canceldelete = optional_param('canceldelete', false, PARAM_INT);
$shoulddelete = optional_param('shoulddelete', false, PARAM_INT);
$deletetempl = optional_param('deletetempl', false, PARAM_INT);

$url = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
if ($canceldelete !== false) {
$url->param('canceldelete', $canceldelete);
}
if ($shoulddelete !== false) {
$url->param('shoulddelete', $shoulddelete);
}
if ($deletetempl !== false) {
$url->param('deletetempl', $deletetempl);
}
$PAGE->set_url($url);

if (($formdata = data_submitted()) AND !confirm_sesskey()) {
print_error('invalidsesskey');
}

if ($canceldelete == 1) {
$editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>'templates'));
redirect($editurl->out(false));
}

if (! $cm = get_coursemodule_from_id('feedback', $id)) {
print_error('invalidcoursemodule');
}

if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
print_error('coursemisconf');
}

if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
print_error('invalidcoursemodule');
}
$baseurl = new moodle_url('/mod/feedback/delete_template.php', array('id' => $id));
$PAGE->set_url($baseurl);

list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
$context = context_module::instance($cm->id);

require_login($course, true, $cm);

require_capability('mod/feedback:deletetemplate', $context);

$mform = new mod_feedback_delete_template_form();
$newformdata = array('id'=>$id,
'deletetempl'=>$deletetempl,
'confirmdelete'=>'1');
$feedback = $PAGE->activityrecord;
$systemcontext = context_system::instance();

$mform->set_data($newformdata);
$formdata = $mform->get_data();

$deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));

if ($mform->is_cancelled()) {
redirect($deleteurl->out(false));
}

if (isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1) {
if (!$template = $DB->get_record("feedback_template", array("id"=>$deletetempl))) {
print_error('error');
}
// Process template deletion.
if ($deletetempl && optional_param('confirm', 0, PARAM_BOOL) && confirm_sesskey()) {
$template = $DB->get_record('feedback_template', array('id' => $deletetempl), '*', MUST_EXIST);

if ($template->ispublic) {
$systemcontext = context_system::instance();
require_capability('mod/feedback:createpublictemplate', $systemcontext);
require_capability('mod/feedback:deletetemplate', $systemcontext);
}

feedback_delete_template($template);
redirect($deleteurl->out(false));
redirect($baseurl, get_string('template_deleted', 'feedback'));
}

/// Print the page header
$strfeedbacks = get_string("modulenameplural", "feedback");
$strfeedback = get_string("modulename", "feedback");
$strdeletefeedback = get_string('delete_template', 'feedback');

navigation_node::override_active_url(new moodle_url('/mod/feedback/edit.php',
array('id' => $id, 'do_show' => 'templates')));
$PAGE->set_heading($course->fullname);
$PAGE->set_title($feedback->name);
echo $OUTPUT->header();
Expand All @@ -119,100 +74,30 @@
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
echo $OUTPUT->heading($strdeletefeedback, 3);
if ($shoulddelete == 1) {

echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
echo html_writer::tag('p', get_string('confirmdeletetemplate', 'feedback'), array('class' => 'bold'));
$mform->display();
echo $OUTPUT->box_end();
if ($deletetempl) {
$continueurl = new moodle_url($baseurl, array('deletetempl' => $deletetempl, 'confirm' => 1, 'sesskey' => sesskey()));
echo $OUTPUT->confirm(get_string('confirmdeletetemplate', 'feedback'), $continueurl, $baseurl);
} else {
//first we get the own templates
// First we get the course templates.
$templates = feedback_get_template_list($course, 'own');
if (!is_array($templates)) {
echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
'generalbox boxaligncenter');
} else {
echo $OUTPUT->heading(get_string('course'), 4);
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
$tablecolumns = array('template', 'action');
$tableheaders = array(get_string('template', 'feedback'), '');
$tablecourse = new flexible_table('feedback_template_course_table');

$tablecourse->define_columns($tablecolumns);
$tablecourse->define_headers($tableheaders);
$tablecourse->define_baseurl($deleteurl);
$tablecourse->column_style('action', 'width', '10%');

$tablecourse->sortable(false);
$tablecourse->set_attribute('width', '100%');
$tablecourse->set_attribute('class', 'generaltable');
$tablecourse->setup();

foreach ($templates as $template) {
$data = array();
$data[] = $template->name;
$url = new moodle_url($deleteurl, array(
'id'=>$id,
'deletetempl'=>$template->id,
'shoulddelete'=>1,
));

$data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
$tablecourse->add_data($data);
}
$tablecourse->finish_output();
echo $OUTPUT->box_end();
}
//now we get the public templates if it is permitted
$systemcontext = context_system::instance();
echo $OUTPUT->box_start('coursetemplates');
echo $OUTPUT->heading(get_string('course'), 4);
$tablecourse = new mod_feedback_templates_table('feedback_template_course_table', $baseurl);
$tablecourse->display($templates);
echo $OUTPUT->box_end();
// Now we get the public templates if it is permitted.
if (has_capability('mod/feedback:createpublictemplate', $systemcontext) AND
has_capability('mod/feedback:deletetemplate', $systemcontext)) {
$templates = feedback_get_template_list($course, 'public');
if (!is_array($templates)) {
echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
'generalbox boxaligncenter');
} else {
echo $OUTPUT->heading(get_string('public', 'feedback'), 4);
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
$tablecolumns = array('template', 'action');
$tableheaders = array(get_string('template', 'feedback'), '');
$tablepublic = new flexible_table('feedback_template_public_table');

$tablepublic->define_columns($tablecolumns);
$tablepublic->define_headers($tableheaders);
$tablepublic->define_baseurl($deleteurl);
$tablepublic->column_style('action', 'width', '10%');

$tablepublic->sortable(false);
$tablepublic->set_attribute('width', '100%');
$tablepublic->set_attribute('class', 'generaltable');
$tablepublic->setup();

foreach ($templates as $template) {
$data = array();
$data[] = $template->name;
$url = new moodle_url($deleteurl, array(
'id'=>$id,
'deletetempl'=>$template->id,
'shoulddelete'=>1,
));

$data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
$tablepublic->add_data($data);
}
$tablepublic->finish_output();
echo $OUTPUT->box_end();
}
echo $OUTPUT->box_start('publictemplates');
echo $OUTPUT->heading(get_string('public', 'feedback'), 4);
$tablepublic = new mod_feedback_templates_table('feedback_template_public_table', $baseurl);
$tablepublic->display($templates);
echo $OUTPUT->box_end();
}

echo $OUTPUT->box_start('boxaligncenter boxwidthnormal');
$url = new moodle_url($deleteurl, array(
'id'=>$id,
'canceldelete'=>1,
));

$url = new moodle_url('/mod/feedback/edit.php', array('id' => $id, 'do_show' => 'templates'));
echo $OUTPUT->single_button($url, get_string('back'), 'post');
echo $OUTPUT->box_end();
}

echo $OUTPUT->footer();
Expand Down
49 changes: 0 additions & 49 deletions mod/feedback/delete_template_form.php

This file was deleted.

0 comments on commit df003a8

Please sign in to comment.