MDL-42400 modlib: include module file when it's used

- changed the place where module file is included to make sure that add_moduleinfo() can be called independently from create_module()
- removed permission check from add_moduleinfo() because this function is not supposed to check permissions. Besides it's only half-check. See can_add_moduleinfo()
- added phpdocs to some functions to indicate that they can throw an exception
This commit is contained in:
Marina Glancy 2013-10-18 10:46:09 +11:00
parent 68291f2d57
commit b4b7587294
3 changed files with 13 additions and 15 deletions

View File

@ -3242,6 +3242,7 @@ function course_get_url($courseorid, $section = null, $options = array()) {
*
* @param object $module
* @return object the created module info
* @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
*/
function create_module($moduleinfo) {
global $DB, $CFG;
@ -3263,9 +3264,6 @@ function create_module($moduleinfo) {
$course = $DB->get_record('course', array('id'=>$moduleinfo->course), '*', MUST_EXIST);
list($module, $context, $cw) = can_add_moduleinfo($course, $moduleinfo->modulename, $moduleinfo->section);
// Load module library.
include_modulelib($module->name);
// Add the module.
$moduleinfo->module = $module->id;
$moduleinfo = add_moduleinfo($moduleinfo, $course, null);
@ -3282,6 +3280,7 @@ function create_module($moduleinfo) {
*
* @param object $module
* @return object the updated module info
* @throws moodle_exception if current user is not allowed to update the module
*/
function update_module($moduleinfo) {
global $DB, $CFG;
@ -3297,9 +3296,6 @@ function update_module($moduleinfo) {
// Some checks (capaibility / existing instances).
list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
// Load module library.
include_modulelib($module->name);
// Retrieve few information needed by update_moduleinfo.
$moduleinfo->modulename = $cm->modname;
if (!isset($moduleinfo->scale)) {

View File

@ -249,8 +249,6 @@ if (file_exists($modmoodleform)) {
print_error('noformdesc');
}
include_modulelib($module->name);
$mformclassname = 'mod_'.$module->name.'_mod_form';
$mform = new $mformclassname($data, $cw->section, $cm, $course);
$mform->set_data($data);

View File

@ -44,6 +44,9 @@ require_once($CFG->dirroot.'/course/lib.php');
function add_moduleinfo($moduleinfo, $course, $mform = null) {
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
@ -51,10 +54,6 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$moduleinfo->groupmode = 0; // Do not set groupmode.
}
if (!course_allowed_module($course, $moduleinfo->modulename)) {
print_error('moduledisable', '', '', $moduleinfo->modulename);
}
// First add course_module record because we need the context.
$newcm = new stdClass();
$newcm->course = $course->id;
@ -103,9 +102,9 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $cw->section));
print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
}
}
@ -308,7 +307,6 @@ function edit_module_post_actions($moduleinfo, $course) {
* @return object the completed module info
*/
function set_moduleinfo_defaults($moduleinfo) {
global $DB;
if (empty($moduleinfo->coursemodule)) {
// Add.
@ -361,6 +359,7 @@ function set_moduleinfo_defaults($moduleinfo) {
* @param object $modulename the module name
* @param object $section the section of the module
* @return array list containing module, context, course section.
* @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
*/
function can_add_moduleinfo($course, $modulename, $section) {
global $DB;
@ -385,6 +384,7 @@ function can_add_moduleinfo($course, $modulename, $section) {
*
* @param object $cm course module
* @return array - list of course module, context, module, moduleinfo, and course section.
* @throws moodle_exception if user is not allowed to perform the action
*/
function can_update_moduleinfo($cm) {
global $DB;
@ -420,6 +420,9 @@ function can_update_moduleinfo($cm) {
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
@ -515,6 +518,7 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
* Include once the module lib file.
*
* @param string $modulename module name of the lib to include
* @throws moodle_exception if lib.php file for the module does not exist
*/
function include_modulelib($modulename) {
global $CFG;