2009-11-01 12:51:40 +00:00
|
|
|
<?php
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is used to manage repositories
|
|
|
|
*
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.0
|
2010-09-06 11:17:43 +00:00
|
|
|
* @package core
|
2009-12-02 10:04:04 +00:00
|
|
|
* @subpackage repository
|
2010-09-06 11:17:43 +00:00
|
|
|
* @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2009-12-02 10:04:04 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-26 17:47:58 +11:00
|
|
|
require_once(__DIR__ . '/../config.php');
|
2009-12-02 10:04:04 +00:00
|
|
|
require_once($CFG->dirroot . '/repository/lib.php');
|
|
|
|
|
|
|
|
$edit = optional_param('edit', 0, PARAM_INT);
|
2012-05-11 13:42:27 +08:00
|
|
|
$new = optional_param('new', '', PARAM_ALPHANUMEXT);
|
2009-12-02 10:04:04 +00:00
|
|
|
$delete = optional_param('delete', 0, PARAM_INT);
|
|
|
|
$sure = optional_param('sure', '', PARAM_ALPHA);
|
|
|
|
$contextid = optional_param('contextid', 0, PARAM_INT);
|
|
|
|
$usercourseid = optional_param('usercourseid', SITEID, PARAM_INT); // Extra: used for user context only
|
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/repository/manage_instances.php');
|
2010-03-02 07:38:04 +00:00
|
|
|
$baseurl = new moodle_url('/repository/manage_instances.php');
|
2009-12-02 10:04:04 +00:00
|
|
|
|
|
|
|
if ($edit){
|
|
|
|
$url->param('edit', $edit);
|
|
|
|
$pagename = 'repositoryinstanceedit';
|
|
|
|
} else if ($delete) {
|
|
|
|
$url->param('delete', $delete);
|
|
|
|
$pagename = 'repositorydelete';
|
|
|
|
} else if ($new) {
|
|
|
|
$url->param('new', $new);
|
|
|
|
$pagename = 'repositoryinstancenew';
|
|
|
|
} else {
|
|
|
|
$pagename = 'repositorylist';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sure !== '') {
|
|
|
|
$url->param('sure', $sure);
|
|
|
|
}
|
|
|
|
if ($contextid !== 0) {
|
|
|
|
$url->param('contextid', $contextid);
|
2010-03-02 07:38:04 +00:00
|
|
|
$baseurl->param('contextid', $contextid);
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
2010-08-18 22:18:53 +00:00
|
|
|
if ($usercourseid != SITEID) {
|
2009-12-02 10:04:04 +00:00
|
|
|
$url->param('usercourseid', $usercourseid);
|
|
|
|
}
|
|
|
|
|
2012-08-21 14:20:30 +08:00
|
|
|
$context = context::instance_by_id($contextid);
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2010-06-22 07:40:30 +00:00
|
|
|
$PAGE->set_url($url);
|
|
|
|
$PAGE->set_context($context);
|
2014-12-12 13:36:45 +13:00
|
|
|
$PAGE->set_pagelayout('standard');
|
2010-06-22 07:40:30 +00:00
|
|
|
|
2008-09-15 07:37:21 +00:00
|
|
|
/// Security: make sure we're allowed to do this operation
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($context->contextlevel == CONTEXT_COURSE) {
|
|
|
|
$pagename = get_string("repositorycourse",'repository');
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcourseid');
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
2010-06-22 07:40:30 +00:00
|
|
|
require_login($course, false);
|
|
|
|
// If the user is allowed to edit this course, he's allowed to edit list of repository instances
|
|
|
|
require_capability('moodle/course:update', $context);
|
|
|
|
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
} else if ($context->contextlevel == CONTEXT_USER) {
|
2010-06-22 07:40:30 +00:00
|
|
|
require_login();
|
2015-02-25 16:24:10 +01:00
|
|
|
$pagename = get_string('manageinstances', 'repository');
|
2009-12-02 10:04:04 +00:00
|
|
|
//is the user looking at its own repository instances
|
|
|
|
if ($USER->id != $context->instanceid){
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('notyourinstances', 'repository');
|
2008-09-05 08:51:25 +00:00
|
|
|
}
|
2009-12-02 10:04:04 +00:00
|
|
|
$user = $USER;
|
|
|
|
} else {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcontext');
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
|
2013-03-15 13:45:31 +08:00
|
|
|
if (!empty($new) && empty($edit)){
|
2009-12-02 10:04:04 +00:00
|
|
|
$type = repository::get_type_by_typename($new);
|
|
|
|
} else if (!empty($edit)){
|
2017-12-21 11:33:11 +01:00
|
|
|
$instance = repository::get_repository_by_id($edit, $context->id);
|
2009-12-02 10:04:04 +00:00
|
|
|
$type = repository::get_type_by_id($instance->options['typeid']);
|
|
|
|
} else if (!empty($delete)){
|
2017-12-21 11:33:11 +01:00
|
|
|
$instance = repository::get_repository_by_id($delete, $context->id);
|
2009-12-02 10:04:04 +00:00
|
|
|
$type = repository::get_type_by_id($instance->options['typeid']);
|
|
|
|
}
|
|
|
|
|
2012-08-21 17:40:03 +08:00
|
|
|
if (isset($type)) {
|
|
|
|
if (!$type->get_visible()) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('typenotvisible', 'repository', $baseurl);
|
2012-08-21 17:40:03 +08:00
|
|
|
}
|
|
|
|
// Prevents the user from creating/editing an instance if the repository is not visible in
|
|
|
|
// this context OR if the user does not have the capability to view this repository in this context.
|
|
|
|
$canviewrepository = has_capability('repository/'.$type->get_typename().':view', $context);
|
|
|
|
if (!$type->get_contextvisibility($context) || !$canviewrepository) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('usercontextrepositorydisabled', 'repository', $baseurl);
|
2012-08-21 17:40:03 +08:00
|
|
|
}
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2013-03-07 13:20:29 +08:00
|
|
|
// We have an instance when we are going to edit, or delete. Several checks need to be done!
|
|
|
|
if (!empty($instance)) {
|
|
|
|
// The context passed MUST match the context of the repository. And as both have to be
|
|
|
|
// similar, this also ensures that the context is either a user one, or a course one.
|
|
|
|
if ($instance->instance->contextid != $context->id) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcontext');
|
2013-03-07 13:20:29 +08:00
|
|
|
}
|
|
|
|
if ($instance->readonly) {
|
|
|
|
// Cannot edit, or delete a readonly instance.
|
|
|
|
throw new repository_exception('readonlyinstance', 'repository');
|
|
|
|
} else if (!$instance->can_be_edited_by_user()) {
|
|
|
|
// The user has to have the right to edit the instance.
|
|
|
|
throw new repository_exception('nopermissiontoaccess', 'repository');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 16:24:10 +01:00
|
|
|
// Create navigation links.
|
2009-12-02 10:04:04 +00:00
|
|
|
if (!empty($course)) {
|
2015-02-25 16:24:10 +01:00
|
|
|
$pageheading = $course->fullname;
|
2009-12-02 10:04:04 +00:00
|
|
|
} else {
|
2015-02-25 16:24:10 +01:00
|
|
|
$pageheading = $pagename;
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 13:20:29 +08:00
|
|
|
// Display page header.
|
|
|
|
$PAGE->set_title($pagename);
|
2015-02-25 16:24:10 +01:00
|
|
|
$PAGE->set_heading($pageheading);
|
2008-09-23 03:27:46 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
$return = true;
|
|
|
|
if (!empty($edit) || !empty($new)) {
|
|
|
|
if (!empty($edit)) {
|
|
|
|
$instancetype = repository::get_type_by_id($instance->options['typeid']);
|
|
|
|
$classname = 'repository_' . $instancetype->get_typename();
|
|
|
|
$configs = $instance->get_instance_option_names();
|
|
|
|
$plugin = $instancetype->get_typename();
|
|
|
|
$typeid = $instance->options['typeid'];
|
|
|
|
} else {
|
|
|
|
$plugin = $new;
|
|
|
|
$typeid = $new;
|
|
|
|
$instance = null;
|
|
|
|
}
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
/// Create edit form for this instance
|
|
|
|
$mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
/// Process the form data if any, or display
|
|
|
|
if ($mform->is_cancelled()){
|
|
|
|
redirect($baseurl);
|
|
|
|
exit;
|
2008-09-15 07:37:21 +00:00
|
|
|
|
2009-12-02 10:04:04 +00:00
|
|
|
} else if ($fromform = $mform->get_data()){
|
|
|
|
if ($edit) {
|
|
|
|
$settings = array();
|
|
|
|
$settings['name'] = $fromform->name;
|
|
|
|
foreach($configs as $config) {
|
2013-05-13 17:27:42 +08:00
|
|
|
$settings[$config] = isset($fromform->$config) ? $fromform->$config : null;
|
2008-09-15 07:37:21 +00:00
|
|
|
}
|
2009-12-02 10:04:04 +00:00
|
|
|
$success = $instance->set_option($settings);
|
|
|
|
} else {
|
2012-08-21 14:20:30 +08:00
|
|
|
$success = repository::static_function($plugin, 'create', $plugin, 0, context::instance_by_id($contextid), $fromform);
|
2009-12-02 10:04:04 +00:00
|
|
|
$data = data_submitted();
|
2008-09-05 06:30:18 +00:00
|
|
|
}
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($success) {
|
|
|
|
$savedstr = get_string('configsaved', 'repository');
|
2010-06-22 07:40:30 +00:00
|
|
|
redirect($baseurl);
|
2009-12-02 10:04:04 +00:00
|
|
|
} else {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('instancenotsaved', 'repository', $baseurl);
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
|
|
|
exit;
|
|
|
|
} else { // Display the form
|
2013-05-02 12:02:17 +08:00
|
|
|
echo $OUTPUT->header();
|
2009-12-02 10:04:04 +00:00
|
|
|
echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
|
|
|
|
$OUTPUT->box_start();
|
|
|
|
$mform->display();
|
|
|
|
$OUTPUT->box_end();
|
2008-09-05 06:30:18 +00:00
|
|
|
$return = false;
|
|
|
|
}
|
2009-12-02 10:04:04 +00:00
|
|
|
} else if (!empty($delete)) {
|
|
|
|
if ($sure) {
|
2022-04-11 12:04:50 +01:00
|
|
|
require_sesskey();
|
2009-12-02 10:04:04 +00:00
|
|
|
if ($instance->delete()) {
|
|
|
|
$deletedstr = get_string('instancedeleted', 'repository');
|
|
|
|
redirect($baseurl, $deletedstr, 3);
|
|
|
|
} else {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('instancenotdeleted', 'repository', $baseurl);
|
2009-12-02 10:04:04 +00:00
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
2013-05-02 12:02:17 +08:00
|
|
|
echo $OUTPUT->header();
|
2010-01-03 20:47:13 +00:00
|
|
|
$formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
|
|
|
|
$formcancel = new single_button($baseurl, get_string('no'));
|
2009-12-02 10:04:04 +00:00
|
|
|
echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
|
|
|
|
$return = false;
|
|
|
|
} else {
|
2013-05-02 12:02:17 +08:00
|
|
|
echo $OUTPUT->header();
|
2009-12-02 10:04:04 +00:00
|
|
|
repository::display_instances_list($context);
|
|
|
|
$return = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($return)) {
|
|
|
|
redirect($baseurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $OUTPUT->footer();
|