Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/course/ -> view.php (source)

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

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