PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/MoodleWebRole/course/edit.php

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