[ Index ]

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

title

Body

[close]

/course/ -> edit.php (source)

   1  <?php // $Id: edit.php,v 1.108.2.2 2008/07/06 17:55:56 skodak Exp $
   2        // Edit course settings
   3  
   4      require_once ('../config.php');
   5      require_once($CFG->dirroot.'/enrol/enrol.class.php');
   6      require_once($CFG->libdir.'/blocklib.php');
   7      require_once ('lib.php');
   8      require_once ('edit_form.php');
   9  
  10      $id         = optional_param('id', 0, PARAM_INT);       // course id
  11      $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
  12  
  13  
  14  /// basic access control checks
  15      if ($id) { // editing course
  16  
  17          if($id == SITEID){
  18              // don't allow editing of  'site course' using this from
  19              error('You cannot edit the site course using this form');
  20          }
  21  
  22          if (!$course = get_record('course', 'id', $id)) {
  23              error('Course ID was incorrect');
  24          }
  25          require_login($course->id);
  26          $category = get_record('course_categories', 'id', $course->category);
  27          require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
  28  
  29      } else if ($categoryid) { // creating new course in this category
  30          $course = null;
  31          require_login();
  32          if (!$category = get_record('course_categories', 'id', $categoryid)) {
  33              error('Category ID was incorrect');
  34          }
  35          require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
  36      } else {
  37          require_login();
  38          error('Either course id or category must be specified');
  39      }
  40  
  41  /// prepare course
  42      if (!empty($course)) {
  43          $allowedmods = array();
  44          if (!empty($course)) {
  45              if ($am = get_records('course_allowed_modules','course',$course->id)) {
  46                  foreach ($am as $m) {
  47                      $allowedmods[] = $m->module;
  48                  }
  49              } else {
  50                  if (empty($course->restrictmodules)) {
  51                      $allowedmods = explode(',',$CFG->defaultallowedmodules);
  52                  } // it'll be greyed out but we want these by default anyway.
  53              }
  54              $course->allowedmods = $allowedmods;
  55  
  56              if ($course->enrolstartdate){
  57                  $course->enrolstartdisabled = 0;
  58              }
  59  
  60              if ($course->enrolenddate) {
  61                  $course->enrolenddisabled = 0;
  62              }
  63          }
  64      }
  65  
  66  
  67  /// first create the form
  68      $editform = new course_edit_form('edit.php', compact('course', 'category'));
  69      // now override defaults if course already exists
  70      if (!empty($course)) {
  71          $course->enrolpassword = $course->password; // we need some other name for password field MDL-9929
  72          $editform->set_data($course);
  73      }
  74      if ($editform->is_cancelled()){
  75          if (empty($course)) {
  76              redirect($CFG->wwwroot);
  77          } else {
  78              redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
  79          }
  80  
  81      } else if ($data = $editform->get_data()) {
  82  
  83          $data->password = $data->enrolpassword;  // we need some other name for password field MDL-9929
  84  /// process data if submitted
  85  
  86          //preprocess data
  87          if ($data->enrolstartdisabled){
  88              $data->enrolstartdate = 0;
  89          }
  90  
  91          if ($data->enrolenddisabled) {
  92              $data->enrolenddate = 0;
  93          }
  94  
  95          $data->timemodified = time();
  96  
  97          if (empty($course)) {
  98              if (!$course = create_course($data)) {
  99                  print_error('coursenotcreated');
 100              }
 101  
 102              $context = get_context_instance(CONTEXT_COURSE, $course->id);
 103  
 104              // assign default role to creator if not already having permission to manage course assignments
 105              if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
 106                  role_assign($CFG->creatornewroleid, $USER->id, 0, $context->id);
 107              }
 108  
 109              // ensure we can use the course right after creating it
 110              // this means trigger a reload of accessinfo...
 111              mark_context_dirty($context->path);
 112  
 113              if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) {
 114                  // Redirect users with metacourse capability to student import
 115                  redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
 116              } else {
 117                  // Redirect to roles assignment
 118                  redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
 119              }
 120  
 121          } else {
 122              if (!update_course($data)) {
 123                  print_error('coursenotupdated');
 124              }
 125              redirect($CFG->wwwroot."/course/view.php?id=$course->id");
 126          }
 127      }
 128  
 129  
 130  /// Print the form
 131  
 132      $site = get_site();
 133  
 134      $streditcoursesettings = get_string("editcoursesettings");
 135      $straddnewcourse = get_string("addnewcourse");
 136      $stradministration = get_string("administration");
 137      $strcategories = get_string("categories");
 138      $navlinks = array();
 139  
 140      if (!empty($course)) {
 141          $navlinks[] = array('name' => $streditcoursesettings,
 142                              'link' => null,
 143                              'type' => 'misc');
 144          $title = $streditcoursesettings;
 145          $fullname = $course->fullname;
 146      } else {
 147          $navlinks[] = array('name' => $stradministration,
 148                              'link' => "$CFG->wwwroot/$CFG->admin/index.php",
 149                              'type' => 'misc');
 150          $navlinks[] = array('name' => $strcategories,
 151                              'link' => 'index.php',
 152                              'type' => 'misc');
 153          $navlinks[] = array('name' => $straddnewcourse,
 154                              'link' => null,
 155                              'type' => 'misc');
 156          $title = "$site->shortname: $straddnewcourse";
 157          $fullname = $site->fullname;
 158      }
 159  
 160      $navigation = build_navigation($navlinks);
 161      print_header($title, $fullname, $navigation, $editform->focus());
 162      print_heading($streditcoursesettings);
 163  
 164      $editform->display();
 165  
 166      print_footer($course);
 167  
 168  ?>


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