diff --git a/lib/modinfolib.php b/lib/modinfolib.php index 0d5bf93a381..8785e50045a 100644 --- a/lib/modinfolib.php +++ b/lib/modinfolib.php @@ -489,6 +489,14 @@ class cm_info extends stdClass { */ public $uservisible; + /** + * Module context - hacky shortcut + * @deprecated + * @var stdClass + */ + public $context; + + // New data available only via functions //////////////////////////////////////// @@ -706,7 +714,7 @@ class cm_info extends stdClass { */ public function set_no_view_link() { $this->check_not_view_only(); - $url = null; + $this->url = null; } /** @@ -791,6 +799,7 @@ class cm_info extends stdClass { $this->icon = isset($mod->icon) ? $mod->icon : ''; $this->iconcomponent = isset($mod->iconcomponent) ? $mod->iconcomponent : ''; $this->customdata = isset($mod->customdata) ? $mod->customdata : ''; + $this->context = get_context_instance(CONTEXT_MODULE, $mod->cm); $this->state = self::STATE_BASIC; // This special case handles old label data. Labels used to use the 'name' field for diff --git a/lib/pagelib.php b/lib/pagelib.php index 5e5de36493c..7764aba2b55 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -102,9 +102,8 @@ class moodle_page { protected $_course = null; /** - * If this page belongs to a module, this is the row from the course_modules - * table, as fetched by get_coursemodule_from_id or get_coursemodule_from_instance, - * so the extra modname and name fields are present. + * If this page belongs to a module, this is the cm_info module description object. + * @var cm_info */ protected $_cm = null; @@ -272,6 +271,7 @@ class moodle_page { * if this page is not within a module. This is a full cm object, as loaded * by get_coursemodule_from_id or get_coursemodule_from_instance, * so the extra modname and name fields are present. + * @return cm_info */ protected function magic_get_cm() { return $this->_cm; @@ -775,7 +775,7 @@ class moodle_page { /** * The course module that this page belongs to (if it does belong to one). * - * @param stdClass $cm a full cm object obtained from get_coursemodule_from_id or get_coursemodule_from_instance. + * @param stdClass|cm_info $cm a record from course_modules table or cm_info from get_fast_modinfo(). * @param stdClass $course * @param stdClass $module * @return void @@ -783,23 +783,29 @@ class moodle_page { public function set_cm($cm, $course = null, $module = null) { global $DB; - if (!isset($cm->name) || !isset($cm->modname) || !isset($cm->id)) { - throw new coding_exception('The $cm you set on $PAGE must have been obtained with get_coursemodule_from_id or get_coursemodule_from_instance. That is, the ->name and -> modname fields must be present and correct.'); - } - $this->_cm = $cm; - $this->_cm->context = get_context_instance(CONTEXT_MODULE, $cm->id); // hacky shortcut - if (!$this->_context) { - $this->set_context($this->_cm->context); + if (!isset($cm->id) || !isset($cm->course)) { + throw new coding_exception('Invalid $cm parameter for $PAGE object, it has to be instance of cm_info or record from the course_modules table.'); } + if (!$this->_course || $this->_course->id != $cm->course) { if (!$course) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); } if ($course->id != $cm->course) { - throw new coding_exception('The course you passed to $PAGE->set_cm does not seem to correspond to the $cm.'); + throw new coding_exception('The course you passed to $PAGE->set_cm does not correspond to the $cm.'); } $this->set_course($course); } + + // make sure we have a $cm from get_fast_modinfo as this contains activity access details + if (!($cm instanceof cm_info)) { + $modinfo = get_fast_modinfo($this->_course); + $cm = $modinfo->get_cm($cm->id); + } + $this->_cm = $cm; + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $this->set_context($context); // the content of page MUST match the cm, this prints warning if there is any problem + if ($module) { $this->set_activity_record($module); }