Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/course/ -> view.php (source)

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