MDL-36317 Execute course format callback when course or module is set on page

This commit is contained in:
Marina Glancy 2012-11-01 10:25:55 +08:00
parent 6109f2112c
commit 695705f5d4
2 changed files with 32 additions and 2 deletions

View File

@ -411,6 +411,24 @@ abstract class format_base {
);
return $blocknames;
}
/**
* Allows course format to execute code on moodle_page::set_course()
*
* @param moodle_page $page instance of page calling set_course
*/
public function page_set_course(moodle_page $page) {
}
/**
* Allows course format to execute code on moodle_page::set_cm()
*
* Current module can be accessed as $page->cm (returns instance of cm_info)
*
* @param moodle_page $page instance of page calling set_cm
*/
public function page_set_cm(moodle_page $page) {
}
}
/**

View File

@ -845,7 +845,7 @@ class moodle_page {
* @param stdClass $course the course to set as the global course.
*/
public function set_course($course) {
global $COURSE, $PAGE;
global $COURSE, $PAGE, $CFG, $SITE;
if (empty($course->id)) {
throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
@ -867,6 +867,12 @@ class moodle_page {
if (!$this->_context) {
$this->set_context(context_course::instance($this->_course->id));
}
// notify course format that this page is set for the course
if ($this->_course->id != $SITE->id) {
require_once($CFG->dirroot.'/course/lib.php');
course_get_format($this->_course)->page_set_course($this);
}
}
/**
@ -911,7 +917,7 @@ class moodle_page {
* @return void
*/
public function set_cm($cm, $course = null, $module = null) {
global $DB;
global $DB, $CFG, $SITE;
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.');
@ -943,6 +949,12 @@ class moodle_page {
if ($module) {
$this->set_activity_record($module);
}
// notify course format that this page is set for the course module
if ($this->_course->id != $SITE->id) {
require_once($CFG->dirroot.'/course/lib.php');
course_get_format($this->_course)->page_set_cm($this);
}
}
/**