Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/course/ -> view.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  //  Display the course home page.
   4  
   5      require_once('../config.php');
   6      require_once ('lib.php');
   7      require_once($CFG->libdir.'/completionlib.php');
   8  
   9      redirect_if_major_upgrade_required();
  10  
  11      $id          = optional_param('id', 0, PARAM_INT);
  12      $name        = optional_param('name', '', PARAM_TEXT);
  13      $edit        = optional_param('edit', -1, PARAM_BOOL);
  14      $hide        = optional_param('hide', 0, PARAM_INT);
  15      $show        = optional_param('show', 0, PARAM_INT);
  16      $idnumber    = optional_param('idnumber', '', PARAM_RAW);
  17      $sectionid   = optional_param('sectionid', 0, PARAM_INT);
  18      $section     = optional_param('section', 0, PARAM_INT);
  19      $move        = optional_param('move', 0, PARAM_INT);
  20      $marker      = optional_param('marker',-1 , PARAM_INT);
  21      $switchrole  = optional_param('switchrole',-1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
  22      $return      = optional_param('return', 0, PARAM_LOCALURL);
  23  
  24      $params = array();
  25      if (!empty($name)) {
  26          $params = array('shortname' => $name);
  27      } else if (!empty($idnumber)) {
  28          $params = array('idnumber' => $idnumber);
  29      } else if (!empty($id)) {
  30          $params = array('id' => $id);
  31      }else {
  32          throw new \moodle_exception('unspecifycourseid', 'error');
  33      }
  34  
  35      $course = $DB->get_record('course', $params, '*', MUST_EXIST);
  36  
  37      $urlparams = array('id' => $course->id);
  38  
  39      // Sectionid should get priority over section number
  40      if ($sectionid) {
  41          $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
  42      }
  43      if ($section) {
  44          $urlparams['section'] = $section;
  45      }
  46  
  47      $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
  48  
  49      // Prevent caching of this page to stop confusion when changing page after making AJAX changes
  50      $PAGE->set_cacheable(false);
  51  
  52      context_helper::preload_course($course->id);
  53      $context = context_course::instance($course->id, MUST_EXIST);
  54  
  55      // Remove any switched roles before checking login
  56      if ($switchrole == 0 && confirm_sesskey()) {
  57          role_switch($switchrole, $context);
  58      }
  59  
  60      require_login($course);
  61  
  62      // Switchrole - sanity check in cost-order...
  63      $reset_user_allowed_editing = false;
  64      if ($switchrole > 0 && confirm_sesskey() &&
  65          has_capability('moodle/role:switchroles', $context)) {
  66          // is this role assignable in this context?
  67          // inquiring minds want to know...
  68          $aroles = get_switchable_roles($context);
  69          if (is_array($aroles) && isset($aroles[$switchrole])) {
  70              role_switch($switchrole, $context);
  71              // Double check that this role is allowed here
  72              require_login($course);
  73          }
  74          // reset course page state - this prevents some weird problems ;-)
  75          $USER->activitycopy = false;
  76          $USER->activitycopycourse = NULL;
  77          unset($USER->activitycopyname);
  78          unset($SESSION->modform);
  79          $USER->editing = 0;
  80          $reset_user_allowed_editing = true;
  81      }
  82  
  83      //If course is hosted on an external server, redirect to corresponding
  84      //url with appropriate authentication attached as parameter
  85      if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
  86          include $CFG->dirroot .'/course/externservercourse.php';
  87          if (function_exists('extern_server_course')) {
  88              if ($extern_url = extern_server_course($course)) {
  89                  redirect($extern_url);
  90              }
  91          }
  92      }
  93  
  94  
  95      require_once($CFG->dirroot.'/calendar/lib.php');    /// This is after login because it needs $USER
  96  
  97      // Must set layout before gettting section info. See MDL-47555.
  98      $PAGE->set_pagelayout('course');
  99      $PAGE->add_body_class('limitedwidth');
 100  
 101      if ($section and $section > 0) {
 102  
 103          // Get section details and check it exists.
 104          $modinfo = get_fast_modinfo($course);
 105          $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
 106  
 107          // Check user is allowed to see it.
 108          if (!$coursesections->uservisible) {
 109              // Check if coursesection has conditions affecting availability and if
 110              // so, output availability info.
 111              if ($coursesections->visible && $coursesections->availableinfo) {
 112                  $sectionname     = get_section_name($course, $coursesections);
 113                  $message = get_string('notavailablecourse', '', $sectionname);
 114                  redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
 115              } else {
 116                  // Note: We actually already know they don't have this capability
 117                  // or uservisible would have been true; this is just to get the
 118                  // correct error message shown.
 119                  require_capability('moodle/course:viewhiddensections', $context);
 120              }
 121          }
 122      }
 123  
 124      // Fix course format if it is no longer installed
 125      $format = course_get_format($course);
 126      $course->format = $format->get_format();
 127  
 128      $PAGE->set_pagetype('course-view-' . $course->format);
 129      $PAGE->set_other_editing_capability('moodle/course:update');
 130      $PAGE->set_other_editing_capability('moodle/course:manageactivities');
 131      $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
 132      if (course_format_uses_sections($course->format)) {
 133          $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
 134          $PAGE->set_other_editing_capability('moodle/course:movesections');
 135      }
 136  
 137      // Preload course format renderer before output starts.
 138      // This is a little hacky but necessary since
 139      // format.php is not included until after output starts
 140      $format->get_renderer($PAGE);
 141  
 142      if ($reset_user_allowed_editing) {
 143          // ugly hack
 144          unset($PAGE->_user_allowed_editing);
 145      }
 146  
 147      if (!isset($USER->editing)) {
 148          $USER->editing = 0;
 149      }
 150      if ($PAGE->user_allowed_editing()) {
 151          if (($edit == 1) and confirm_sesskey()) {
 152              $USER->editing = 1;
 153              // Redirect to site root if Editing is toggled on frontpage
 154              if ($course->id == SITEID) {
 155                  redirect($CFG->wwwroot .'/?redirect=0');
 156              } else if (!empty($return)) {
 157                  redirect($CFG->wwwroot . $return);
 158              } else {
 159                  $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
 160                  redirect($url);
 161              }
 162          } else if (($edit == 0) and confirm_sesskey()) {
 163              $USER->editing = 0;
 164              if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
 165                  $USER->activitycopy       = false;
 166                  $USER->activitycopycourse = NULL;
 167              }
 168              // Redirect to site root if Editing is toggled on frontpage
 169              if ($course->id == SITEID) {
 170                  redirect($CFG->wwwroot .'/?redirect=0');
 171              } else if (!empty($return)) {
 172                  redirect($CFG->wwwroot . $return);
 173              } else {
 174                  redirect($PAGE->url);
 175              }
 176          }
 177  
 178          if (has_capability('moodle/course:sectionvisibility', $context)) {
 179              if ($hide && confirm_sesskey()) {
 180                  set_section_visible($course->id, $hide, '0');
 181                  redirect($PAGE->url);
 182              }
 183  
 184              if ($show && confirm_sesskey()) {
 185                  set_section_visible($course->id, $show, '1');
 186                  redirect($PAGE->url);
 187              }
 188          }
 189  
 190          if (!empty($section) && !empty($move) &&
 191                  has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
 192              $destsection = $section + $move;
 193              if (move_section_to($course, $section, $destsection)) {
 194                  if ($course->id == SITEID) {
 195                      redirect($CFG->wwwroot . '/?redirect=0');
 196                  } else {
 197                      if ($format->get_course_display() == COURSE_DISPLAY_MULTIPAGE) {
 198                          redirect(course_get_url($course));
 199                      } else {
 200                          redirect(course_get_url($course, $destsection));
 201                      }
 202                  }
 203              } else {
 204                  echo $OUTPUT->notification('An error occurred while moving a section');
 205              }
 206          }
 207      } else {
 208          $USER->editing = 0;
 209      }
 210  
 211      $SESSION->fromdiscussion = $PAGE->url->out(false);
 212  
 213  
 214      if ($course->id == SITEID) {
 215          // This course is not a real course.
 216          redirect($CFG->wwwroot .'/?redirect=0');
 217      }
 218  
 219      // Determine whether the user has permission to download course content.
 220      $candownloadcourse = \core\content::can_export_context($context, $USER);
 221  
 222      // We are currently keeping the button here from 1.x to help new teachers figure out
 223      // what to do, even though the link also appears in the course admin block.  It also
 224      // means you can back out of a situation where you removed the admin block. :)
 225      if ($PAGE->user_allowed_editing()) {
 226          $buttons = $OUTPUT->edit_button($PAGE->url);
 227          $PAGE->set_button($buttons);
 228      }
 229  
 230      $editingtitle = '';
 231      if ($PAGE->user_is_editing()) {
 232          // Append this to the page title's lang string to get its equivalent when editing mode is turned on.
 233          $editingtitle = 'editing';
 234      }
 235  
 236      // If viewing a section, make the title more specific
 237      if ($section and $section > 0 and course_format_uses_sections($course->format)) {
 238          $sectionname = get_string('sectionname', "format_$course->format");
 239          $sectiontitle = get_section_name($course, $section);
 240          $PAGE->set_title(get_string('coursesectiontitle' . $editingtitle, 'moodle', array(
 241              'course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)
 242          ));
 243      } else {
 244          $PAGE->set_title(get_string('coursetitle' . $editingtitle, 'moodle', array('course' => $course->fullname)));
 245      }
 246  
 247      $PAGE->set_heading($course->fullname);
 248      echo $OUTPUT->header();
 249  
 250      if ($USER->editing == 1) {
 251  
 252          // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
 253          require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
 254  
 255          if (async_helper::is_async_pending($id, 'course', 'backup')) {
 256              echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
 257          }
 258      }
 259  
 260      // Course wrapper start.
 261      echo html_writer::start_tag('div', array('class'=>'course-content'));
 262  
 263      // make sure that section 0 exists (this function will create one if it is missing)
 264      course_create_sections_if_missing($course, 0);
 265  
 266      // get information about course modules and existing module types
 267      // format.php in course formats may rely on presence of these variables
 268      $modinfo = get_fast_modinfo($course);
 269      $modnames = get_module_types_names();
 270      $modnamesplural = get_module_types_names(true);
 271      $modnamesused = $modinfo->get_used_module_names();
 272      $mods = $modinfo->get_cms();
 273      $sections = $modinfo->get_section_info_all();
 274  
 275      // CAUTION, hacky fundamental variable defintion to follow!
 276      // Note that because of the way course fromats are constructed though
 277      // inclusion we pass parameters around this way..
 278      $displaysection = $section;
 279  
 280      // Include course AJAX
 281      include_course_ajax($course, $modnamesused);
 282  
 283      // Include the actual course format.
 284      require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
 285      // Content wrapper end.
 286  
 287      echo html_writer::end_tag('div');
 288  
 289      // Trigger course viewed event.
 290      // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
 291      // anything after that point.
 292      course_view(context_course::instance($course->id), $section);
 293  
 294      // If available, include the JS to prepare the download course content modal.
 295      if ($candownloadcourse) {
 296          $PAGE->requires->js_call_amd('core_course/downloadcontent', 'init');
 297      }
 298  
 299      // Load the view JS module if completion tracking is enabled for this course.
 300      $completion = new completion_info($course);
 301      if ($completion->is_enabled()) {
 302          $PAGE->requires->js_call_amd('core_course/view', 'init');
 303      }
 304  
 305      echo $OUTPUT->footer();