2012-12-07 09:34:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of the customcert module for 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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit the customcert settings.
|
|
|
|
*
|
2013-04-26 07:30:33 +00:00
|
|
|
* @package mod_customcert
|
2012-12-07 09:34:46 +00:00
|
|
|
* @copyright Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('../../config.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/lib.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/edit_form.php');
|
2013-02-20 12:21:57 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$cmid = required_param('cmid', PARAM_INT);
|
|
|
|
$moveup = optional_param('moveup', 0, PARAM_INT);
|
|
|
|
$movedown = optional_param('movedown', 0, PARAM_INT);
|
2013-04-12 08:24:58 +00:00
|
|
|
$emoveup = optional_param('emoveup', 0, PARAM_INT);
|
|
|
|
$emovedown = optional_param('emovedown', 0, PARAM_INT);
|
2013-02-20 12:21:57 +00:00
|
|
|
$deleteelement = optional_param('deleteelement', 0, PARAM_INT);
|
|
|
|
$deletepage = optional_param('deletepage', 0, PARAM_INT);
|
2012-12-07 09:34:46 +00:00
|
|
|
$confirm = optional_param('confirm', 0, PARAM_INT);
|
|
|
|
|
|
|
|
$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST);
|
|
|
|
$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
|
|
|
|
$customcert = $DB->get_record('customcert', array('id'=>$cm->instance), '*', MUST_EXIST);
|
|
|
|
$context = context_module::instance($cm->id);
|
|
|
|
|
|
|
|
require_login($course, false, $cm);
|
|
|
|
|
|
|
|
require_capability('mod/customcert:manage', $context);
|
|
|
|
|
|
|
|
// Check if they are moving a custom certificate page.
|
|
|
|
if ((!empty($moveup)) || (!empty($movedown))) {
|
|
|
|
// Check if we are moving a page up.
|
|
|
|
if (!empty($moveup)) {
|
|
|
|
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $moveup))) {
|
|
|
|
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber - 1));
|
|
|
|
}
|
|
|
|
} else { // Must be moving a page down.
|
|
|
|
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $movedown))) {
|
|
|
|
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that there is a page to move, and a page to swap it with.
|
|
|
|
if ($swapcertpage && $movecertpage) {
|
|
|
|
$DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id));
|
|
|
|
$DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id));
|
|
|
|
}
|
2013-04-12 08:24:58 +00:00
|
|
|
} else if ((!empty($emoveup)) || (!empty($emovedown))) { // Check if we are moving a custom certificate element.
|
|
|
|
// Check if we are moving an element up.
|
|
|
|
if (!empty($emoveup)) {
|
|
|
|
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emoveup))) {
|
|
|
|
$swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence - 1));
|
|
|
|
}
|
|
|
|
} else { // Must be moving a element down.
|
|
|
|
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emovedown))) {
|
|
|
|
$swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that there is an element to move, and an element to swap it with.
|
|
|
|
if ($swapcertelement && $movecertelement) {
|
|
|
|
$DB->set_field('customcert_elements', 'sequence', $swapcertelement->sequence, array('id' => $movecertelement->id));
|
|
|
|
$DB->set_field('customcert_elements', 'sequence', $movecertelement->sequence, array('id' => $swapcertelement->id));
|
|
|
|
}
|
2013-02-20 12:21:57 +00:00
|
|
|
} else if ((!empty($deletepage)) && (!empty($confirm))) { // Check if we are deleting a page.
|
|
|
|
customcert_delete_page($deletepage);
|
|
|
|
} else if ((!empty($deleteelement)) && (!empty($confirm))) { // Check if we are deleting an element.
|
|
|
|
// Ensure element exists and delete it.
|
|
|
|
$element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST);
|
|
|
|
customcert_delete_element($element);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id,
|
|
|
|
'cmid' => $cm->id,
|
|
|
|
'course' => $course));
|
|
|
|
|
|
|
|
if ($data = $mform->get_data()) {
|
|
|
|
// Handle file uploads.
|
|
|
|
customcert_upload_imagefiles($data->customcertimage);
|
|
|
|
|
|
|
|
// Save any page data.
|
|
|
|
customcert_save_page_data($data);
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
// Flag to determine if we are deleting anything.
|
|
|
|
$deleting = false;
|
|
|
|
|
|
|
|
// Loop through the data.
|
2012-12-07 09:34:46 +00:00
|
|
|
foreach ($data as $key => $value) {
|
2013-02-20 12:21:57 +00:00
|
|
|
// Check if they requested to delete a page.
|
2012-12-07 09:34:46 +00:00
|
|
|
if (strpos($key, 'deletecertpage_') !== false) {
|
2013-02-20 12:21:57 +00:00
|
|
|
// Get the pageid.
|
2012-12-07 09:34:46 +00:00
|
|
|
$pageid = str_replace('deletecertpage_', '', $key);
|
2013-02-20 12:21:57 +00:00
|
|
|
// Set deletion flag to true.
|
|
|
|
$deleting = true;
|
|
|
|
// Create the message.
|
|
|
|
$message = get_string('deletepageconfirm', 'customcert');
|
|
|
|
// Create the link options.
|
|
|
|
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
|
|
|
|
'deletepage' => $pageid,
|
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
|
|
|
} else if (strpos($key, 'deleteelement_') !== false) { // Check if they requested to delete a page element.
|
|
|
|
// Get the element id.
|
|
|
|
$elementid = str_replace('deleteelement_', '', $key);
|
|
|
|
// Set deletion flag to true.
|
|
|
|
$deleting = true;
|
|
|
|
// Create the message.
|
|
|
|
$message = get_string('deleteelementconfirm', 'customcert');
|
2012-12-07 09:34:46 +00:00
|
|
|
// Create the link options.
|
|
|
|
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
|
2013-02-20 12:21:57 +00:00
|
|
|
'deleteelement' => $elementid,
|
2012-12-07 09:34:46 +00:00
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
2013-02-20 12:21:57 +00:00
|
|
|
} else if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
|
|
|
|
// Get the page id.
|
|
|
|
$pageid = str_replace('addelement_', '', $key);
|
|
|
|
// Get the element.
|
|
|
|
$element = "element_" . $pageid;
|
|
|
|
$element = $data->$element;
|
|
|
|
customcert_add_element($element, $pageid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are deleting either a page or an element.
|
|
|
|
if ($deleting) {
|
2012-12-07 09:34:46 +00:00
|
|
|
// Show a confirmation page.
|
2013-02-20 12:21:57 +00:00
|
|
|
$strheading = get_string('deleteconfirm', 'customcert');
|
2012-12-07 09:34:46 +00:00
|
|
|
$PAGE->navbar->add($strheading);
|
|
|
|
$PAGE->set_title($strheading);
|
|
|
|
$PAGE->set_heading($COURSE->fullname);
|
|
|
|
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading($strheading);
|
|
|
|
echo $OUTPUT->confirm($message, $yesurl, $nourl);
|
|
|
|
echo $OUTPUT->footer();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
// Check if they chose to add a page.
|
2012-12-07 09:34:46 +00:00
|
|
|
if (!empty($data->addcertpage)) {
|
|
|
|
customcert_add_page($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the editing page to show form with recent updates.
|
|
|
|
redirect($CFG->wwwroot . '/mod/customcert/edit.php?cmid=' . $cm->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name)));
|
|
|
|
$PAGE->set_heading($course->fullname);
|
|
|
|
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
|
|
|
|
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading(get_string('editcustomcert', 'mod_customcert'));
|
|
|
|
$mform->display();
|
|
|
|
echo $OUTPUT->footer();
|