Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/rest.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 
00026 require_once('../config.php');
00027 require_once($CFG->dirroot.'/course/lib.php');
00028 
00029 // Initialise ALL the incoming parameters here, up front.
00030 $courseid   = required_param('courseId', PARAM_INT);
00031 $class      = required_param('class', PARAM_ALPHA);
00032 $field      = optional_param('field', '', PARAM_ALPHA);
00033 $instanceid = optional_param('instanceId', 0, PARAM_INT);
00034 $sectionid  = optional_param('sectionId', 0, PARAM_INT);
00035 $beforeid   = optional_param('beforeId', 0, PARAM_INT);
00036 $value      = optional_param('value', 0, PARAM_INT);
00037 $column     = optional_param('column', 0, PARAM_ALPHA);
00038 $id         = optional_param('id', 0, PARAM_INT);
00039 $summary    = optional_param('summary', '', PARAM_RAW);
00040 $sequence   = optional_param('sequence', '', PARAM_SEQUENCE);
00041 $visible    = optional_param('visible', 0, PARAM_INT);
00042 $pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command
00043 
00044 $PAGE->set_url('/course/rest.php', array('courseId'=>$courseid,'class'=>$class));
00045 
00046 //NOTE: when making any changes here please make sure it is using the same access control as course/mod.php !!
00047 
00048 require_login();
00049 
00050 // Authorise the user and verify some incoming data
00051 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
00052     error_log('AJAX commands.php: Course does not exist');
00053     die;
00054 }
00055 
00056 if (empty($CFG->enablecourseajax)) {
00057     error_log('Course AJAX not allowed');
00058     die;
00059 }
00060 
00061 require_sesskey();
00062 
00063 // OK, now let's process the parameters and do stuff
00064 // MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
00065 $requestmethod = $_SERVER['REQUEST_METHOD'];
00066 if ($pageaction == 'DELETE') {
00067     $requestmethod = 'DELETE';
00068 }
00069 
00070 switch($requestmethod) {
00071     case 'POST':
00072 
00073         switch ($class) {
00074             case 'block':
00075                 // not used any more
00076                 break;
00077 
00078             case 'section':
00079                 require_login($course);
00080                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00081                 require_capability('moodle/course:update', $coursecontext);
00082 
00083                 if (!$DB->record_exists('course_sections', array('course'=>$course->id, 'section'=>$id))) {
00084                     error_log('AJAX commands.php: Bad Section ID '.$id);
00085                     die;
00086                 }
00087 
00088                 switch ($field) {
00089                     case 'visible':
00090                         set_section_visible($course->id, $id, $value);
00091                         break;
00092 
00093                     case 'move':
00094                         move_section_to($course, $id, $value);
00095                         break;
00096                 }
00097                 rebuild_course_cache($course->id);
00098                 break;
00099 
00100             case 'resource':
00101                 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
00102                     error_log('AJAX commands.php: Bad course module ID '.$id);
00103                     die;
00104                 }
00105                 require_login($course, false, $cm);
00106                 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00107                 switch ($field) {
00108                     case 'visible':
00109                         require_capability('moodle/course:activityvisibility', $modcontext);
00110                         set_coursemodule_visible($cm->id, $value);
00111                         break;
00112 
00113                     case 'groupmode':
00114                         require_capability('moodle/course:manageactivities', $modcontext);
00115                         set_coursemodule_groupmode($cm->id, $value);
00116                         break;
00117 
00118                     case 'indentleft':
00119                         require_capability('moodle/course:manageactivities', $modcontext);
00120                         if ($cm->indent > 0) {
00121                             $cm->indent--;
00122                             $DB->update_record('course_modules', $cm);
00123                         }
00124                         break;
00125 
00126                     case 'indentright':
00127                         require_capability('moodle/course:manageactivities', $modcontext);
00128                         $cm->indent++;
00129                         $DB->update_record('course_modules', $cm);
00130                         break;
00131 
00132                     case 'move':
00133                         require_capability('moodle/course:manageactivities', $modcontext);
00134                         if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid))) {
00135                             error_log('AJAX commands.php: Bad section ID '.$sectionid);
00136                             die;
00137                         }
00138 
00139                         if ($beforeid > 0){
00140                             $beforemod = get_coursemodule_from_id('', $beforeid, $course->id);
00141                             $beforemod = $DB->get_record('course_modules', array('id'=>$beforeid));
00142                         } else {
00143                             $beforemod = NULL;
00144                         }
00145 
00146                         if (debugging('',DEBUG_DEVELOPER)) {
00147                             error_log(serialize($beforemod));
00148                         }
00149 
00150                         moveto_module($cm, $section, $beforemod);
00151                         break;
00152                 }
00153                 rebuild_course_cache($course->id);
00154                 break;
00155 
00156             case 'course':
00157                 switch($field) {
00158                     case 'marker':
00159                         require_login($course);
00160                         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00161                         require_capability('moodle/course:update', $coursecontext);
00162                         $newcourse = new stdClass();
00163                         $newcourse->id = $course->id;
00164                         $newcourse->marker = $value;
00165                         $DB->update_record('course', $newcourse);
00166                         break;
00167                 }
00168                 break;
00169         }
00170         break;
00171 
00172     case 'DELETE':
00173         switch ($class) {
00174             case 'block':
00175                 // not used any more
00176                 break;
00177 
00178             case 'resource':
00179                 if (!$cm = get_coursemodule_from_id('', $id, $course->id)) {
00180                     error_log('AJAX rest.php: Bad course module ID '.$id);
00181                     die;
00182                 }
00183                 require_login($course, false, $cm);
00184                 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00185                 require_capability('moodle/course:manageactivities', $modcontext);
00186                 $modlib = "$CFG->dirroot/mod/$cm->modname/lib.php";
00187 
00188                 if (file_exists($modlib)) {
00189                     include_once($modlib);
00190                 } else {
00191                     error_log("Ajax rest.php: This module is missing mod/$cm->modname/lib.php");
00192                     die;
00193                 }
00194                 $deleteinstancefunction = $cm->modname."_delete_instance";
00195 
00196                 // Run the module's cleanup funtion.
00197                 if (!$deleteinstancefunction($cm->instance)) {
00198                     error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (instance)");
00199                     die;
00200                 }
00201 
00202                 // remove all module files in case modules forget to do that
00203                 $fs = get_file_storage();
00204                 $fs->delete_area_files($modcontext->id);
00205 
00206                 if (!delete_course_module($cm->id)) {
00207                     error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name (coursemodule)");
00208                 }
00209                 // Remove the course_modules entry.
00210                 if (!delete_mod_from_section($cm->id, $cm->section)) {
00211                     error_log("Ajax rest.php: Could not delete the $cm->modname $cm->name from section");
00212                 }
00213 
00214                 // Trigger a mod_deleted event with information about this module.
00215                 $eventdata = new stdClass();
00216                 $eventdata->modulename = $cm->modname;
00217                 $eventdata->cmid       = $cm->id;
00218                 $eventdata->courseid   = $course->id;
00219                 $eventdata->userid     = $USER->id;
00220                 events_trigger('mod_deleted', $eventdata);
00221 
00222                 rebuild_course_cache($course->id);
00223 
00224                 add_to_log($courseid, "course", "delete mod",
00225                            "view.php?id=$courseid",
00226                            "$cm->modname $cm->instance", $cm->id);
00227                 break;
00228         }
00229         break;
00230 }
00231 
00232 
 All Data Structures Namespaces Files Functions Variables Enumerations