Permalink
Browse files

MDL-26265 use cm_info in PAGE->cm because it has more useful informat…

…ion; add context property to cm_info
  • Loading branch information...
1 parent f2f4527 commit 4478743ca4baca9ca6f6da7ab31c03f4fc04bb22 @skodak skodak committed Feb 7, 2011
Showing with 28 additions and 13 deletions.
  1. +10 −1 lib/modinfolib.php
  2. +18 −12 lib/pagelib.php
View
@@ -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 @@ public function set_name($name) {
*/
public function set_no_view_link() {
$this->check_not_view_only();
- $url = null;
+ $this->url = null;
}
/**
@@ -791,6 +799,7 @@ public function __construct(course_modinfo $modinfo, $course, $mod, $info) {
$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
View
@@ -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 @@ protected function magic_get_course() {
* 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,31 +775,37 @@ public function set_context($context) {
/**
* 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
*/
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);
}

0 comments on commit 4478743

Please sign in to comment.