[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/course/ -> rest.php (source)

   1  <?php  // $Id: rest.php,v 1.8.6.2 2008/07/18 14:00:56 nicolasconnault Exp $
   2         // Provide interface for topics AJAX course formats
   3  
   4  require_once ('../config.php');
   5  require_once($CFG->dirroot.'/course/lib.php');
   6  require_once($CFG->libdir .'/pagelib.php');
   7  require_once($CFG->libdir .'/blocklib.php');
   8  
   9  
  10  // Initialise ALL the incoming parameters here, up front.
  11  $courseid   = required_param('courseId', PARAM_INT);
  12  $class      = required_param('class', PARAM_ALPHA);
  13  $field      = optional_param('field', '', PARAM_ALPHA);
  14  $instanceid = optional_param('instanceId', 0, PARAM_INT);
  15  $sectionid  = optional_param('sectionId', 0, PARAM_INT);
  16  $beforeid   = optional_param('beforeId', 0, PARAM_INT);
  17  $value      = optional_param('value', 0, PARAM_INT);
  18  $column     = optional_param('column', 0, PARAM_ALPHA);
  19  $id         = optional_param('id', 0, PARAM_INT);
  20  $summary    = optional_param('summary', '', PARAM_RAW);
  21  $sequence   = optional_param('sequence', '', PARAM_SEQUENCE);
  22  $visible    = optional_param('visible', 0, PARAM_INT);
  23  
  24  
  25  // Authorise the user and verify some incoming data
  26  if (!$course = get_record('course', 'id', $courseid)) {
  27      error_log('AJAX commands.php: Course does not exist');
  28      die;
  29  }
  30  
  31  $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
  32  $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
  33  
  34  if (!empty($instanceid)) {
  35      $blockinstance = blocks_find_instance($instanceid, $pageblocks);
  36      if (!$blockinstance || $blockinstance->pageid != $course->id
  37                          || $blockinstance->pagetype != 'course-view') {
  38          error_log('AJAX commands.php: Bad block ID '.$instanceid);
  39          die;
  40      }
  41  }
  42  
  43  $context = get_context_instance(CONTEXT_COURSE, $course->id);
  44  require_login($course->id);
  45  require_capability('moodle/course:update', $context);
  46  
  47  
  48  
  49  // OK, now let's process the parameters and do stuff
  50  switch($_SERVER['REQUEST_METHOD']) {
  51      case 'POST':
  52  
  53          switch ($class) {
  54              case 'block':
  55  
  56                  switch ($field) {
  57                      case 'visible':
  58                          blocks_execute_action($PAGE, $pageblocks, 'toggle', $blockinstance);
  59                          break;
  60  
  61                      case 'position':  // Misleading case. Should probably call it 'move'.
  62                          // We want to move the block around. This means changing
  63                          // the column (position field) and/or block sort order
  64                          // (weight field).
  65                          blocks_move_block($PAGE, $blockinstance, $column, $value);
  66                          break;
  67                  }
  68                  break;
  69  
  70              case 'section':
  71  
  72                  if (!record_exists('course_sections','course',$course->id,'section',$id)) {
  73                      error_log('AJAX commands.php: Bad Section ID '.$id);
  74                      die;
  75                  }
  76  
  77                  switch ($field) {
  78                      case 'visible':
  79                          set_section_visible($course->id, $id, $value);
  80                          break;
  81  
  82                      case 'move':
  83                          move_section_to($course, $id, $value);
  84                          break;
  85                  }
  86                  rebuild_course_cache($course->id);
  87                  break;
  88  
  89              case 'resource':
  90                  if (!$mod = get_record('course_modules', 'id', $id, 'course', $course->id)) {
  91                      error_log('AJAX commands.php: Bad course module ID '.$id);
  92                      die;
  93                  }
  94                  switch ($field) {
  95                      case 'visible':
  96                          set_coursemodule_visible($mod->id, $value);
  97                          break;
  98  
  99                      case 'groupmode':
 100                          set_coursemodule_groupmode($mod->id, $value);
 101                          break;
 102  
 103                      case 'indentleft':
 104                          if ($mod->indent > 0) {
 105                              $mod->indent--;
 106                              update_record('course_modules', $mod);
 107                          }
 108                          break;
 109  
 110                      case 'indentright':
 111                          $mod->indent++;
 112                          update_record('course_modules', $mod);
 113                          break;
 114  
 115                      case 'move':
 116                          if (!$section = get_record('course_sections','course',$course->id,'section',$sectionid)) {
 117                              error_log('AJAX commands.php: Bad section ID '.$sectionid);
 118                              die;
 119                          }
 120  
 121                          if ($beforeid > 0){
 122                              $beforemod = get_record('course_modules', 'id', $beforeid);
 123                          } else {
 124                              $beforemod = NULL;
 125                          }
 126  
 127                          if (debugging('',DEBUG_DEVELOPER)) {
 128                              error_log(serialize($beforemod));
 129                          }
 130  
 131                          moveto_module($mod, $section, $beforemod);
 132                          break;
 133                  }
 134                  rebuild_course_cache($course->id);
 135                  break;
 136  
 137              case 'course':
 138                  switch($field) {
 139                      case 'marker':
 140                          $newcourse = new object;
 141                          $newcourse->id = $course->id;
 142                          $newcourse->marker = $value;
 143                          if (!update_record('course',$newcourse)) {
 144                              error_log('AJAX commands.php: Failed to update course marker for course '.$newcourse->id);
 145                              die;
 146                          }
 147                          break;
 148                  }
 149                  break;
 150          }
 151          break;
 152  
 153      case 'DELETE':
 154          switch ($class) {
 155              case 'block':
 156                  blocks_execute_action($PAGE, $pageblocks, 'delete', $blockinstance);
 157                  break;
 158  
 159              case 'resource':
 160                  if (!$cm = get_record('course_modules', 'id', $id, 'course', $course->id)) {
 161                      error_log('AJAX rest.php: Bad course module ID '.$id);
 162                      die;
 163                  }
 164                  if (!$mod = get_record('modules', 'id', $cm->module)) {
 165                      error_log('AJAX rest.php: Bad module ID '.$cm->module);
 166                      die;
 167                  }
 168                  $mod->name = clean_param($mod->name, PARAM_SAFEDIR);  // For safety
 169                  $modlib = "$CFG->dirroot/mod/$mod->name/lib.php";
 170  
 171                  if (file_exists($modlib)) {
 172                      include_once($modlib);
 173                  } else {
 174                      error_log("Ajax rest.php: This module is missing important code ($modlib)");
 175                      die;
 176                  }
 177                  $deleteinstancefunction = $mod->name."_delete_instance";
 178  
 179                  // Run the module's cleanup funtion.
 180                  if (!$deleteinstancefunction($cm->instance)) {
 181                      error_log("Ajax rest.php: Could not delete the $mod->name (instance)");
 182                      die;
 183                  }
 184                  // Remove the course_modules entry.
 185                  if (!delete_course_module($cm->id)) {
 186                      error_log("Ajax rest.php: Could not delete the $mod->modulename (coursemodule)");
 187                      die;
 188                  }
 189  
 190                  rebuild_course_cache($course->id);
 191  
 192                  add_to_log($courseid, "course", "delete mod",
 193                             "view.php?id=$courseid",
 194                             "$mod->name $cm->instance", $cm->id);
 195                  break;
 196          }
 197          break;
 198  }
 199  
 200  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7