$newreport->queryparams = serialize($queryparams);
    } else {
        $newreport->queryparams = '';
    }
    if ($id) {
        $newreport->id = $id;
        $ok = $DB->update_record('report_customsql_queries', $newreport);
        if (!$ok) {
            print_error('errorupdatingreport', 'report_customsql', report_customsql_url('edit.php?id=' . $id));
        }
    } else {
        $id = $DB->insert_record('report_customsql_queries', $newreport);
        if (!$id) {
            print_error('errorinsertingreport', 'report_customsql', report_customsql_url('edit.php'));
        }
    }
    report_customsql_log_edit($id);
    if ($newreport->runable == 'manual') {
        redirect(report_customsql_url('view.php?id=' . $id));
    } else {
        redirect(report_customsql_url('index.php'));
    }
}
admin_externalpage_setup('report_customsql');
echo $OUTPUT->header() . $OUTPUT->heading(get_string('editingareport', 'report_customsql'));
if ($report) {
    $report->description = array('text' => $report->description, 'format' => $report->descriptionformat);
    $mform->set_data($report);
}
$mform->display();
echo $OUTPUT->footer();
function report_customsql_print_reports($reports, $courseid = null)
{
    global $CFG, $OUTPUT;
    // DWE Edited By David Elliott on 27-4-2015
    // to view assigned reports to teachers
    // check admin or not - start
    if (is_siteadmin()) {
        $context = context_system::instance();
        $canedit = has_capability('report/customsql:definequeries', $context);
        $capabilities = report_customsql_capability_options();
    } else {
        // Set context for teachers
        $context = get_context_instance(CONTEXT_COURSE, $courseid);
        $canedit = '';
        $capabilities = '';
    }
    // End
    foreach ($reports as $report) {
        if (!empty($report->capability) && !has_capability($report->capability, $context)) {
            continue;
        }
        echo html_writer::start_tag('p');
        // DWE - David Elliott - passing course id with the url for checking context of the teacher - 27-4-2015
        echo html_writer::tag('a', format_string($report->displayname), array('href' => report_customsql_url('view.php?id=' . $report->id . '&cid=' . $courseid))) . ' ' . report_customsql_time_note($report, 'span');
        if ($canedit) {
            $imgedit = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/edit'), 'class' => 'iconsmall', 'alt' => get_string('edit')));
            $imgdelete = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/delete'), 'class' => 'iconsmall', 'alt' => get_string('delete')));
            echo ' ' . html_writer::tag('span', get_string('availableto', 'report_customsql', $capabilities[$report->capability]), array('class' => 'admin_note')) . ' ' . html_writer::tag('a', $imgedit, array('title' => get_string('editthisreport', 'report_customsql'), 'href' => report_customsql_url('edit.php?id=' . $report->id))) . ' ' . html_writer::tag('a', $imgdelete, array('title' => get_string('deletethisreport', 'report_customsql'), 'href' => report_customsql_url('delete.php?id=' . $report->id)));
        }
        echo html_writer::end_tag('p');
        echo "\n";
    }
}
require_once $CFG->libdir . '/adminlib.php';
$id = required_param('id', PARAM_INT);
$category = $DB->get_record('report_customsql_categories', array('id' => $id));
if (!$category) {
    print_error('invalidreportid', 'report_customsql', report_customsql_url('manage.php'), $id);
}
require_login();
$context = context_system::instance();
$PAGE->set_url(new moodle_url('/report/customsql/categorydelete.php'));
$PAGE->set_context($context);
require_capability('report/customsql:managecategories', $context);
if (optional_param('confirm', false, PARAM_BOOL)) {
    require_sesskey();
    if (!($queries = $DB->get_records('report_customsql_queries', array('categoryid' => $id)))) {
        $ok = $DB->delete_records('report_customsql_categories', array('id' => $id));
        if (!$ok) {
            print_error('errordeletingcategory', 'report_customsql', report_customsql_url('index.php'));
        }
        report_customsql_log_delete($id);
    } else {
        print_error('errordeletingcategory', 'report_customsql', report_customsql_url('index.php'));
    }
    redirect(report_customsql_url('manage.php'));
}
// Start the page.
admin_externalpage_setup('report_customsql');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deletecategoryareyousure', 'report_customsql'));
echo html_writer::tag('p', get_string('categorynamex', 'report_customsql', $category->name));
echo $OUTPUT->confirm(get_string('deletecategoryyesno', 'report_customsql'), new single_button(new moodle_url(report_customsql_url('categorydelete.php'), array('id' => $id, 'confirm' => 1, 'sesskey' => sesskey())), get_string('yes')), new single_button(new moodle_url(report_customsql_url('index.php')), get_string('no')));
echo $OUTPUT->footer();
    $category = $DB->get_record('report_customsql_categories', array('id' => $id), '*', MUST_EXIST);
} else {
    $queryparams['categoryid'] = null;
    $isadding = true;
}
$mform = new report_customsql_addcategory_form(report_customsql_url($relativeurl), $queryparams);
if ($mform->is_cancelled()) {
    redirect(report_customsql_url('manage.php'));
}
if ($data = $mform->get_data()) {
    if ($isadding) {
        $DB->insert_record('report_customsql_categories', $data, true);
    } else {
        $updrec = new stdClass();
        $updrec->id = $data->id;
        $updrec->name = $data->name;
        $DB->update_record('report_customsql_categories', $updrec);
    }
    redirect(report_customsql_url('manage.php'));
}
if ($id) {
    $headstr = get_string('editcategory', 'report_customsql');
} else {
    $headstr = get_string('addcategory', 'report_customsql');
}
echo $OUTPUT->header() . $OUTPUT->heading($headstr);
if ($id) {
    $mform->set_data($category);
}
$mform->display();
echo $OUTPUT->footer();
 */
require_once dirname(__FILE__) . '/../../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once $CFG->libdir . '/adminlib.php';
$id = required_param('id', PARAM_INT);
$report = get_record('report_customsql_queries', 'id', $id);
if (!$report) {
    print_error('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id);
}
require_login();
$context = get_context_instance(CONTEXT_SYSTEM);
require_capability('report/customsql:definequeries', $context);
if (optional_param('confirm', false, PARAM_BOOL)) {
    $ok = delete_records('report_customsql_queries', 'id', $id);
    if (!$ok) {
        print_error('errordeletingreport', 'report_customsql', report_customsql_url('index.php'));
    }
    report_customsql_log_delete($id);
    redirect(report_customsql_url('index.php'));
}
$runnableoptions = report_customsql_runable_options();
// Start the page.
admin_externalpage_setup('reportcustomsql');
admin_externalpage_print_header();
print_heading(get_string('deleteareyousure', 'report_customsql'));
echo '<p>' . get_string('displaynamex', 'report_customsql', '<b>' . format_string($report->displayname) . '</b>') . '</p>';
echo '<p>' . get_string('querysql', 'report_customsql') . '</p>';
echo '<pre>' . htmlspecialchars($report->querysql) . '</pre>';
echo '<p>' . get_string('runablex', 'report_customsql', $runnableoptions[$report->runable]) . '</p>';
notice_yesno(get_string('deleteareyousure', 'report_customsql'), report_customsql_url('delete.php'), report_customsql_url('index.php'), array('id' => $id, 'confirm' => 1), null, 'post', 'get');
admin_externalpage_print_footer();
function report_customsql_print_reports($reports)
{
    global $CFG;
    $context = get_context_instance(CONTEXT_SYSTEM);
    $canedit = has_capability('report/customsql:definequeries', $context);
    $capabilities = report_customsql_capability_options();
    foreach ($reports as $report) {
        if (!empty($report->capability) && !has_capability($report->capability, $context)) {
            continue;
        }
        echo '<p><span class="reporttitle" style="background-color:HoneyDew;font-size:1.3em;"><a href="' . report_customsql_url('view.php?id=' . $report->id) . '">' . format_string($report->displayname) . '</a></span> ';
        if ($canedit) {
            echo ' <a title="' . get_string('editthisreport', 'report_customsql') . '" href="' . report_customsql_url('edit.php?id=' . $report->id) . '"><img src="' . $CFG->pixpath . '/t/edit.gif" class="iconsmall" alt="' . get_string('edit') . '" /></a>';
            echo ' <a title="' . get_string('deletethisreport', 'report_customsql') . '" href="' . report_customsql_url('delete.php?id=' . $report->id) . '"><img src="' . $CFG->pixpath . '/t/delete.gif" class="iconsmall" alt="' . get_string('delete') . '" /></a>';
            echo ' <br/><span class="admin_note">' . get_string('availableto', 'report_customsql', $capabilities[$report->capability]) . '</span>';
        }
        echo '<br/>' . report_customsql_time_note($report, 'span');
        echo "</p>\n";
    }
}
/**
 * display the rports
 * @param object $reports, the result of DB query
 * @param string $type, type of report (manual, daily, weekly or monthly)
 */
function report_customsql_print_reports_for($reports, $type)
{
    global $OUTPUT;
    if (empty($reports)) {
        return;
    }
    if (!empty($type)) {
        $help = html_writer::tag('span', $OUTPUT->help_icon($type . 'header', 'report_customsql'));
        echo $OUTPUT->heading(get_string($type . 'header', 'report_customsql') . $help, 3);
    }
    $context = context_system::instance();
    $canedit = has_capability('report/customsql:definequeries', $context);
    $capabilities = report_customsql_capability_options();
    foreach ($reports as $report) {
        if (!empty($report->capability) && !has_capability($report->capability, $context)) {
            continue;
        }
        echo html_writer::start_tag('p');
        echo html_writer::tag('a', format_string($report->displayname), array('href' => report_customsql_url('view.php?id=' . $report->id))) . ' ' . report_customsql_time_note($report, 'span');
        if ($canedit) {
            $imgedit = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/edit'), 'class' => 'iconsmall', 'alt' => get_string('edit')));
            $imgdelete = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/delete'), 'class' => 'iconsmall', 'alt' => get_string('delete')));
            echo ' ' . html_writer::tag('span', get_string('availableto', 'report_customsql', $capabilities[$report->capability]), array('class' => 'admin_note')) . ' ' . html_writer::tag('a', $imgedit, array('title' => get_string('editthisreport', 'report_customsql'), 'href' => report_customsql_url('edit.php?id=' . $report->id))) . ' ' . html_writer::tag('a', $imgdelete, array('title' => get_string('deletethisreport', 'report_customsql'), 'href' => report_customsql_url('delete.php?id=' . $report->id)));
        }
        echo html_writer::end_tag('p');
        echo "\n";
    }
}
        }
        echo report_customsql_time_note($report, 'p');
        echo '<p><a href="' . report_customsql_url('download.php?id=' . $id . '&amp;timestamp=' . $cvstimestamp) . '">' . get_string('downloadthisreportascsv', 'report_customsql') . '</a></p>';
        $archivetimes = report_customsql_get_archive_times($report);
        if (count($archivetimes) > 1) {
            print_heading(get_string('archivedversions', 'report_customsql'), '', 3);
            echo '<ul>';
            foreach ($archivetimes as $time) {
                $formattedtime = userdate($time, get_string('strftimedate'));
                echo '<li>';
                if ($time == $cvstimestamp) {
                    echo '<b>' . $formattedtime . '</b>';
                } else {
                    echo '<a href="' . report_customsql_url('view.php?id=' . $id . '&amp;timestamp=' . $time) . '">' . $formattedtime . '</a>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }
}
if (has_capability('report/customsql:definequeries', $context)) {
    echo '<p><img src="' . $CFG->pixpath . '/t/edit.gif" class="iconsmall" alt="' . get_string('edit') . '" /> <a href="' . report_customsql_url('edit.php?id=' . $id) . '">' . get_string('editthisreport', 'report_customsql') . '</a></p>';
    echo '<p><img src="' . $CFG->pixpath . '/t/delete.gif" class="iconsmall" alt="' . get_string('delete') . '" /> <a href="' . report_customsql_url('delete.php?id=' . $id) . '">' . get_string('deletethisreport', 'report_customsql') . '</a></p>';
}
// chart api (nadavkav)
//echo '<p><img src="' . $CFG->pixpath . '/t/edit.gif" class="iconsmall" alt="' .
//        get_string('display') . '" /> <a href="' . report_customsql_url('displaychart.php?id=' . $id) .
//            '">' . get_string('displaychart', 'report_customsql') . '</a></p>';
echo '<p>' . $THEME->larrow . ' ' . '<a href="' . report_customsql_url('index.php') . '">' . get_string('backtoreportlist', 'report_customsql') . '</a></p>';
admin_externalpage_print_footer();
    // Category content.
    $cc = new stdClass();
    $cc->manual = count($manualreports);
    $cc->daily = count($dailyreports);
    $cc->weekly = count($weeklyreports);
    $cc->monthly = count($monthlyreports);
    $reportcounts = get_string('categorycontent', 'report_customsql', $cc);
    $reportcounts = html_writer::tag('span', $reportcounts, array('class' => 'reportcounts'));
    echo $OUTPUT->heading($link . ' ' . $reportcounts);
    echo html_writer::start_tag('div', array('class' => 'csql_category_reports'));
    if (empty($manualreports) && empty($dailyreports) && empty($weeklyreports) && empty($monthlyreports)) {
        echo $OUTPUT->heading(get_string('availablereports', 'report_customsql'), 3) . html_writer::tag('p', get_string('noreportsavailable', 'report_customsql'));
    } else {
        report_customsql_print_reports_for($manualreports, 'manual');
        report_customsql_print_reports_for($dailyreports, 'daily');
        report_customsql_print_reports_for($weeklyreports, 'weekly');
        report_customsql_print_reports_for($monthlyreports, 'monthly');
    }
    echo html_writer::end_tag('div');
    echo html_writer::end_tag('div');
}
if (has_capability('report/customsql:definequeries', $context)) {
    echo $OUTPUT->single_button(report_customsql_url('edit.php'), get_string('addreport', 'report_customsql'));
}
if (has_capability('report/customsql:managecategories', $context)) {
    echo html_writer::empty_tag('br');
    echo $OUTPUT->single_button(report_customsql_url('manage.php'), get_string('managecategories', 'report_customsql'));
}
// Add the reportcategories YUI script to the page.
$PAGE->requires->yui_module('moodle-report_customsql-reportcategories', 'M.report_customsql.init');
echo $OUTPUT->footer();
        echo report_customsql_time_note($report, 'p') . html_writer::start_tag('p') . html_writer::tag('a', get_string('downloadthisreportascsv', 'report_customsql'), array('href' => new moodle_url(report_customsql_url('download.php'), array('id' => $id, 'timestamp' => $csvtimestamp, 'cid' => $cid)))) . html_writer::end_tag('p');
        $archivetimes = report_customsql_get_archive_times($report);
        if (count($archivetimes) > 1) {
            echo $OUTPUT->heading(get_string('archivedversions', 'report_customsql'), 3) . html_writer::start_tag('ul');
            foreach ($archivetimes as $time) {
                $formattedtime = userdate($time, get_string('strftimedate'));
                echo html_writer::start_tag('li');
                if ($time == $csvtimestamp) {
                    echo html_writer::tag('b', $formattedtime);
                } else {
                    echo html_writer::tag('a', $formattedtime, array('href' => new moodle_url(report_customsql_url('view.php'), array('id' => $id, 'timestamp' => $time))));
                }
                echo '</li>';
            }
            echo html_writer::end_tag('ul');
        }
    }
}
if (!empty($queryparams)) {
    echo html_writer::tag('p', html_writer::link(new moodle_url(report_customsql_url('view.php'), array('id' => $id)), get_string('changetheparameters', 'report_customsql')));
}
if (has_capability('report/customsql:definequeries', $context)) {
    $imgedit = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/edit'), 'class' => 'iconsmall', 'alt' => get_string('edit')));
    $imgdelete = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/delete'), 'class' => 'iconsmall', 'alt' => get_string('delete')));
    echo html_writer::start_tag('p') . $OUTPUT->action_link(new moodle_url(report_customsql_url('edit.php'), array('id' => $id)), $imgedit . ' ' . get_string('editthisreport', 'report_customsql')) . html_writer::end_tag('p') . html_writer::start_tag('p') . $OUTPUT->action_link(new moodle_url(report_customsql_url('delete.php'), array('id' => $id)), $imgdelete . ' ' . get_string('deletethisreport', 'report_customsql')) . html_writer::end_tag('p');
}
$imglarrow = html_writer::tag('img', '', array('src' => $OUTPUT->pix_url('t/collapsed_rtl'), 'class' => 'iconsmall', 'alt' => ''));
// DWE Edited David Elliott on 28-4-2015
// passing course id as parameter to check teacher context.
echo html_writer::start_tag('p') . $OUTPUT->action_link(new moodle_url(report_customsql_url('index.php'), array('cid' => $cid)), $imglarrow . get_string('backtoreportlist', 'report_customsql')) . html_writer::end_tag('p') . $OUTPUT->footer();
/**
 * Script to delete a particular custom SQL report, with confirmation.
 *
 * @package report_customsql
 * @copyright 2009 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once $CFG->libdir . '/adminlib.php';
$id = required_param('id', PARAM_INT);
$report = $DB->get_record('report_customsql_queries', array('id' => $id));
if (!$report) {
    print_error('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id);
}
require_login();
$context = context_system::instance();
require_capability('report/customsql:definequeries', $context);
if (optional_param('confirm', false, PARAM_BOOL)) {
    $ok = $DB->delete_records('report_customsql_queries', array('id' => $id));
    if (!$ok) {
        print_error('errordeletingreport', 'report_customsql', report_customsql_url('index.php'));
    }
    report_customsql_log_delete($id);
    redirect(report_customsql_url('index.php'));
}
$runnableoptions = report_customsql_runable_options();
// Start the page.
admin_externalpage_setup('report_customsql');
echo $OUTPUT->header() . $OUTPUT->heading(get_string('deleteareyousure', 'report_customsql')) . html_writer::tag('p', get_string('displaynamex', 'report_customsql', html_writer::tag('b', format_string($report->displayname)))) . html_writer::tag('p', get_string('querysql', 'report_customsql')) . html_writer::tag('pre', htmlspecialchars($report->querysql)) . html_writer::tag('p', get_string('runablex', 'report_customsql', $runnableoptions[$report->runable])) . $OUTPUT->confirm(get_string('deleteareyousure', 'report_customsql'), new single_button(new moodle_url(report_customsql_url('delete.php'), array('id' => $id, 'confirm' => 1)), get_string('yes')), new single_button(new moodle_url(report_customsql_url('index.php')), get_string('no'))) . $OUTPUT->footer();
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once $CFG->libdir . '/adminlib.php';
require_login();
$manualreports = get_records('report_customsql_queries', 'runable', 'manual', 'displayname');
$scheduledreports = get_records_list('report_customsql_queries', 'runable', "'weekly','monthly'", 'displayname');
// Start the page.
admin_externalpage_setup('reportcustomsql');
admin_externalpage_print_header();
echo '<a target="_new" href="http://docs.moodle.org/en/ad-hoc_contributed_reports">Shared and Contributed Reports</a><br/>';
if (empty($manualreports) && empty($scheduledreports)) {
    print_heading(get_string('availablereports', 'report_customsql'));
    echo '<p>' . get_string('noreportsavailable', 'report_customsql') . '</p>';
} else {
    if (!empty($manualreports)) {
        print_heading(get_string('availablereports', 'report_customsql'));
        echo '<p>' . get_string('manualnote', 'report_customsql') . '</p>';
        report_customsql_print_reports($manualreports);
    }
    if (!empty($scheduledreports)) {
        print_heading(get_string('scheduledqueries', 'report_customsql'));
        echo '<p>' . get_string('schedulednote', 'report_customsql') . '</p>';
        report_customsql_print_reports($scheduledreports);
    }
}
if (has_capability('report/customsql:definequeries', get_context_instance(CONTEXT_SYSTEM))) {
    print_single_button(report_customsql_url('edit.php'), array(), get_string('addreport', 'report_customsql'));
}
admin_externalpage_print_footer();
// 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/>.
/**
 * Script to download the CSV version of a SQL report.
 *
 * @package report_customsql
 * @copyright 2009 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once $CFG->libdir . '/filelib.php';
$id = required_param('id', PARAM_INT);
$csvtimestamp = required_param('timestamp', PARAM_INT);
$report = $DB->get_record('report_customsql_queries', array('id' => $id));
if (!$report) {
    print_error('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id);
}
require_login();
$context = context_system::instance();
if (!empty($report->capability)) {
    require_capability($report->capability, $context);
}
list($csvfilename) = report_customsql_csv_filename($report, $csvtimestamp);
if (!is_readable($csvfilename)) {
    print_error('unknowndownloadfile', 'report_customsql', report_customsql_url('view.php?id=' . $id));
}
send_file($csvfilename, 'report.csv', 'default', 0, false, true, 'text/csv; charset=UTF-8');
 *
 * @package report_customsql
 * @copyright 2013 The Open University
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once $CFG->libdir . '/adminlib.php';
require_login();
$context = context_system::instance();
$PAGE->set_url(new moodle_url('/report/customsql/manage.php'));
$PAGE->set_context($context);
require_capability('report/customsql:managecategories', $context);
admin_externalpage_setup('report_customsql');
echo $OUTPUT->header() . $OUTPUT->heading(get_string('managecategories', 'report_customsql'));
$categories = $DB->get_records('report_customsql_categories', null, 'name ASC');
echo html_writer::tag('p', get_string('addcategorydesc', 'report_customsql'));
if (!empty($categories)) {
    foreach ($categories as $category) {
        echo html_writer::start_tag('div');
        $imgedit = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/edit'), 'class' => 'iconsmall', 'alt' => get_string('edit')));
        echo ' ' . html_writer::tag('span', $category->name . ' ', array('class' => 'report_customsql')) . html_writer::tag('a', $imgedit, array('title' => get_string('editthiscategory', 'report_customsql'), 'href' => report_customsql_url('addcategory.php?id=' . $category->id)));
        if ($category->id != 1 && !($queries = $DB->get_records('report_customsql_queries', array('categoryid' => $category->id)))) {
            $imgdelete = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'), 'class' => 'iconsmall', 'alt' => get_string('delete')));
            echo ' ' . html_writer::tag('a', $imgdelete, array('title' => get_string('deletethiscategory', 'report_customsql'), 'href' => report_customsql_url('categorydelete.php?id=' . $category->id)));
        }
        echo html_writer::end_tag('div');
    }
}
echo $OUTPUT->single_button(report_customsql_url('addcategory.php'), get_string('addcategory', 'report_customsql'));
echo $OUTPUT->footer();