Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/manage_instances.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00029 require_once(dirname(dirname(__FILE__)) . '/config.php');
00030 require_once($CFG->dirroot . '/repository/lib.php');
00031 
00032 $edit    = optional_param('edit', 0, PARAM_INT);
00033 $new     = optional_param('new', '', PARAM_FORMAT);
00034 $delete  = optional_param('delete', 0, PARAM_INT);
00035 $sure    = optional_param('sure', '', PARAM_ALPHA);
00036 $contextid = optional_param('contextid', 0, PARAM_INT);
00037 $usercourseid = optional_param('usercourseid', SITEID, PARAM_INT);  // Extra: used for user context only
00038 
00039 $url = new moodle_url('/repository/manage_instances.php');
00040 
00041 $baseurl = new moodle_url('/repository/manage_instances.php');
00042 $baseurl->param('sesskey', sesskey());
00043 
00044 if ($edit){
00045     $url->param('edit', $edit);
00046     $pagename = 'repositoryinstanceedit';
00047 } else if ($delete) {
00048     $url->param('delete', $delete);
00049     $pagename = 'repositorydelete';
00050 } else if ($new) {
00051     $url->param('new', $new);
00052     $pagename = 'repositoryinstancenew';
00053 } else {
00054     $pagename = 'repositorylist';
00055 }
00056 
00057 if ($sure !== '') {
00058     $url->param('sure', $sure);
00059 }
00060 if ($contextid !== 0) {
00061     $url->param('contextid', $contextid);
00062     $baseurl->param('contextid', $contextid);
00063 }
00064 if ($usercourseid != SITEID) {
00065     $url->param('usercourseid', $usercourseid);
00066 }
00067 
00068 $context = get_context_instance_by_id($contextid);
00069 
00070 $PAGE->set_url($url);
00071 $PAGE->set_context($context);
00072 
00074 if ($context->contextlevel == CONTEXT_COURSE) {
00075     $pagename = get_string("repositorycourse",'repository');
00076 
00077     if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
00078         print_error('invalidcourseid');
00079     }
00080     require_login($course, false);
00081     // If the user is allowed to edit this course, he's allowed to edit list of repository instances
00082     require_capability('moodle/course:update',  $context);
00083 
00084 
00085 } else if ($context->contextlevel == CONTEXT_USER) {
00086     require_login();
00087     $pagename = get_string("personalrepositories",'repository');
00088     //is the user looking at its own repository instances
00089     if ($USER->id != $context->instanceid){
00090         print_error('notyourinstances', 'repository');
00091     }
00092     $user = $USER;
00093     $PAGE->set_pagelayout('mydashboard');
00094 } else {
00095     print_error('invalidcontext');
00096 }
00097 
00099 if (!empty($new)){
00100     $type = repository::get_type_by_typename($new);
00101 } else if (!empty($edit)){
00102     $instance = repository::get_instance($edit);
00103     $type = repository::get_type_by_id($instance->options['typeid']);
00104 } else if (!empty($delete)){
00105     $instance = repository::get_instance($delete);
00106     $type = repository::get_type_by_id($instance->options['typeid']);
00107 }
00108 
00109 if (isset($type) && !$type->get_visible()) {
00110     print_error('typenotvisible', 'repository', $baseurl);
00111 }
00112 
00113 if (isset($type) && !$type->get_contextvisibility($context)) {
00114     print_error('usercontextrepositorydisabled', 'repository', $baseurl);
00115 }
00116 
00118 if (!empty($course)) {
00119     $PAGE->navbar->add($pagename);
00120     $fullname = $course->fullname;
00121 } else {
00122     $fullname = fullname($user);
00123     $strrepos = get_string('repositories', 'repository');
00124     $PAGE->navbar->add($fullname, new moodle_url('/user/view.php', array('id'=>$user->id)));
00125     $PAGE->navbar->add($strrepos);
00126 }
00127 
00128 $title = $pagename;
00129 
00131 $PAGE->set_title($title);
00132 $PAGE->set_heading($fullname);
00133 echo $OUTPUT->header();
00134 
00135 if ($context->contextlevel == CONTEXT_USER) {
00136     if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
00137         print_error('invalidcourseid');
00138     }
00139 }
00140 
00141 $return = true;
00142 if (!empty($edit) || !empty($new)) {
00143     if (!empty($edit)) {
00144         $instance = repository::get_instance($edit);
00145         //if you try to edit an instance set as readonly, display an error message
00146         if ($instance->readonly) {
00147             throw new repository_exception('readonlyinstance', 'repository');
00148         }
00149         $instancetype = repository::get_type_by_id($instance->options['typeid']);
00150         $classname = 'repository_' . $instancetype->get_typename();
00151         $configs  = $instance->get_instance_option_names();
00152         $plugin = $instancetype->get_typename();
00153         $typeid = $instance->options['typeid'];
00154     } else {
00155         $plugin = $new;
00156         $typeid = $new;
00157         $instance = null;
00158     }
00159 
00161     $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
00162 
00164     if ($mform->is_cancelled()){
00165         redirect($baseurl);
00166         exit;
00167 
00168     } else if ($fromform = $mform->get_data()){
00169         if (!confirm_sesskey()) {
00170             print_error('confirmsesskeybad', '', $baseurl);
00171         }
00172         if ($edit) {
00173             $settings = array();
00174             $settings['name'] = $fromform->name;
00175             foreach($configs as $config) {
00176                 $settings[$config] = $fromform->$config;
00177             }
00178             $success = $instance->set_option($settings);
00179         } else {
00180             $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
00181             $data = data_submitted();
00182         }
00183         if ($success) {
00184             $savedstr = get_string('configsaved', 'repository');
00185             echo $OUTPUT->heading($savedstr);
00186             redirect($baseurl);
00187         } else {
00188             print_error('instancenotsaved', 'repository', $baseurl);
00189         }
00190         exit;
00191     } else {     // Display the form
00192         echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
00193         $OUTPUT->box_start();
00194         $mform->display();
00195         $OUTPUT->box_end();
00196         $return = false;
00197     }
00198 } else if (!empty($delete)) {
00199     // echo $OUTPUT->header();
00200     $instance = repository::get_instance($delete);
00201      //if you try to delete an instance set as readonly, display an error message
00202     if ($instance->readonly) {
00203         throw new repository_exception('readonlyinstance', 'repository');
00204     }
00205     if ($sure) {
00206         if (!confirm_sesskey()) {
00207             print_error('confirmsesskeybad', '', $baseurl);
00208         }
00209         if ($instance->delete()) {
00210             $deletedstr = get_string('instancedeleted', 'repository');
00211             echo $OUTPUT->heading($deletedstr);
00212             redirect($baseurl, $deletedstr, 3);
00213         } else {
00214             print_error('instancenotdeleted', 'repository', $baseurl);
00215         }
00216         exit;
00217     }
00218     $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
00219     $formcancel = new single_button($baseurl, get_string('no'));
00220     echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
00221     $return = false;
00222 } else {
00223     repository::display_instances_list($context);
00224     $return = false;
00225 }
00226 
00227 if (!empty($return)) {
00228     redirect($baseurl);
00229 }
00230 
00231 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations