function link_to_gdoc($name, $link, $type = null, $modtype = 'url')
{
    global $COURSE, $DB, $CFG, $USER;
    require_once "{$CFG->dirroot}/mod/{$modtype}/lib.php";
    //add
    $fromform = new stdClass();
    $newform = new stdClass();
    $mform = new MoodleQuickForm(null, 'POST', 'nothing');
    $module = $DB->get_record("modules", array('name' => $modtype));
    $course = $COURSE;
    $cw = get_course_section(0, $course->id);
    $cm = null;
    // fields for mdl_url
    $fromform->course = $course->id;
    $fromform->name = $name;
    $fromform->introformat = 0;
    $fromform->introeditor = 0;
    $fromform->externalurl = $link;
    /*    if ($type !== 'dir') {
            $fromform->display          = 6;
            $fromform->displayoptions = 'a:2:{s:10:"popupwidth";i:1024;s:11:"popupheight";i:768;}';
        } else {
    */
    $fromform->display = 0;
    $fromform->popupwidth = 1024;
    $fromform->popupheight = 768;
    $fromform->popupwidth = null;
    $fromform->popupheight = null;
    $fromform->displayoptions = 'a:1:{s:10:"printintro";i:0;}';
    //    }
    // fields for mdl_course_module
    $fromform->module = $module->id;
    $fromform->instance = '';
    $fromform->section = 0;
    // The section number itself - relative!!! (section column in course_sections)
    $fromform->idnumber = null;
    $fromform->score = 0;
    $fromform->indent = 0;
    $fromform->visible = 1;
    $fromform->visibleold = 1;
    $fromform->groupmode = $course->groupmode;
    $fromform->groupingid = 0;
    $fromform->groupmembersonly = 0;
    $fromform->completion = 0;
    $fromform->completionview = 0;
    $fromform->completionexpected = 0;
    $fromform->availablefrom = 0;
    $fromform->availableuntil = 0;
    $fromform->showavailability = 0;
    $fromform->showdescription = 0;
    $fromform->conditiongradegroup = array();
    $fromform->conditionfieldgroup = array();
    // fields for mdl_course_sections
    $fromform->summaryformat = 0;
    $fromform->modulename = clean_param($module->name, PARAM_SAFEDIR);
    // For safety
    //	$fromform->add              = 'resource';
    //	$fromform->type             = $type == 'dir' ? 'collection' : 'file';
    //	$fromform->return           = 0; //must be false if this is an add, go back to course view on cancel
    //    $fromform->coursemodule 	= '';
    //	$fromform->popup			= 'resizable=1,scrollbars=1,directories=1,location=1,menubar=1,toolbar=1,status=1,width=1024,height=768';
    //	require_login($course->id); // needed to setup proper $COURSE
    $context = get_context_instance(CONTEXT_COURSE, $course->id);
    require_capability('moodle/course:manageactivities', $context);
    if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
        $fromform->groupmode = 0;
        // do not set groupmode
    }
    if (!course_allowed_module($course, $fromform->modulename)) {
        print_error('moduledisable', '', '', $fromform->modulename);
    }
    // first add course_module record because we need the context
    $newcm = new stdClass();
    $newcm->course = $course->id;
    $newcm->module = $fromform->module;
    $newcm->instance = 0;
    // not known yet, will be updated later (this is similar to restore code)
    $newcm->visible = $fromform->visible;
    $newcm->groupmode = $fromform->groupmode;
    $newcm->groupingid = $fromform->groupingid;
    $newcm->groupmembersonly = $fromform->groupmembersonly;
    $completion = new completion_info($course);
    if ($completion->is_enabled()) {
        $newcm->completion = $fromform->completion;
        $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
        $newcm->completionview = $fromform->completionview;
        $newcm->completionexpected = $fromform->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        $newcm->availablefrom = $fromform->availablefrom;
        $newcm->availableuntil = $fromform->availableuntil;
        $newcm->showavailability = $fromform->showavailability;
    }
    if (isset($fromform->showdescription)) {
        $newcm->showdescription = $fromform->showdescription;
    } else {
        $newcm->showdescription = 0;
    }
    if (!($fromform->coursemodule = add_course_module($newcm))) {
        print_error('cannotaddcoursemodule');
    }
    if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
        $draftid_editor = file_get_submitted_draft_itemid('introeditor');
        file_prepare_draft_area($draftid_editor, null, null, null, null);
        $fromform->introeditor = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
        // TODO: add better default
    }
    if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
        $introeditor = $fromform->introeditor;
        unset($fromform->introeditor);
        $fromform->intro = $introeditor['text'];
        $fromform->introformat = $introeditor['format'];
    }
    $addinstancefunction = $fromform->modulename . "_add_instance";
    $updateinstancefunction = $fromform->modulename . "_update_instance";
    $returnfromfunc = $addinstancefunction($fromform, $mform);
    //	$returnfromfunc = url_add_instance($fromform, $mform);
    if (!$returnfromfunc or !is_number($returnfromfunc)) {
        // undo everything we can
        $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
        delete_context(CONTEXT_MODULE, $fromform->coursemodule);
        $DB->delete_records('course_modules', array('id' => $fromform->coursemodule));
        if (!is_number($returnfromfunc)) {
            print_error('invalidfunction', '', course_get_url($course, $cw->section));
        } else {
            print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $fromform->modulename);
        }
    }
    $fromform->instance = $returnfromfunc;
    $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $fromform->coursemodule));
    // update embedded links and save files
    $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
    if (!empty($introeditor)) {
        $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
        $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance));
    }
    // course_modules and course_sections each contain a reference
    // to each other, so we have to update one of them twice.
    $sectionid = add_mod_to_section($fromform);
    $DB->set_field('course_modules', 'section', $sectionid, array('id' => $fromform->coursemodule));
    // make sure visibility is set correctly (in particular in calendar)
    set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
    if (isset($fromform->cmidnumber)) {
        //label
        // set cm idnumber
        set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
    }
    // Set up conditions
    if ($CFG->enableavailability) {
        condition_info::update_cm_from_form((object) array('id' => $fromform->coursemodule), $fromform, false);
    }
    $eventname = 'mod_created';
    add_to_log($course->id, "course", "add mod", "../mod/{$fromform->modulename}/view.php?id={$fromform->coursemodule}", "{$fromform->modulename} {$fromform->instance}");
    add_to_log($course->id, $fromform->modulename, "add", "view.php?id={$fromform->coursemodule}", "{$fromform->instance}", $fromform->coursemodule);
    // Trigger mod_created/mod_updated event with information about this module.
    $eventdata = new stdClass();
    $eventdata->modulename = $fromform->modulename;
    $eventdata->name = $fromform->name;
    $eventdata->cmid = $fromform->coursemodule;
    $eventdata->courseid = $course->id;
    $eventdata->userid = $USER->id;
    events_trigger($eventname, $eventdata);
    rebuild_course_cache($course->id);
    return 1;
}
 /**
  * Setup the DB fixture data
  */
 public function setup()
 {
     parent::setUp();
     $tables = array('block_instance', 'cache_flags', 'capabilities', 'context', 'context_temp', 'course', 'course_modules', 'course_categories', 'course_sections', 'files', 'files_cleanup', 'grade_items', 'grade_categories', 'groups', 'groups_members', 'modules', 'role', 'role_names', 'role_context_levels', 'role_assignments', 'role_capabilities', 'user');
     $this->create_test_tables($tables, 'lib');
     $this->create_test_table('forum', 'mod/forum');
     $this->switch_to_test_db();
     global $DB, $CFG;
     // Insert needed capabilities
     $DB->insert_record('capabilities', array('id' => 45, 'name' => 'moodle/course:update', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 4));
     $DB->insert_record('capabilities', array('id' => 14, 'name' => 'moodle/site:backup', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 28));
     $DB->insert_record('capabilities', array('id' => 17, 'name' => 'moodle/site:restore', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 28));
     $DB->insert_record('capabilities', array('id' => 52, 'name' => 'moodle/course:managefiles', 'cattype' => 'write', 'contextlevel' => 50, 'component' => 'moodle', 'riskbitmask' => 4));
     $DB->insert_record('capabilities', array('id' => 73, 'name' => 'moodle/user:editownprofile', 'cattype' => 'write', 'contextlevel' => 10, 'component' => 'moodle', 'riskbitmask' => 16));
     // Insert system context
     $DB->insert_record('context', array('id' => 1, 'contextlevel' => 10, 'instanceid' => 0, 'path' => '/1', 'depth' => 1));
     $DB->insert_record('context', array('id' => 2, 'contextlevel' => 50, 'instanceid' => 1, 'path' => '/1/2', 'depth' => 2));
     // Insert site course
     $DB->insert_record('course', array('category' => 0, 'sortorder' => 1, 'fullname' => 'Test site', 'shortname' => 'test', 'format' => 'site', 'modinfo' => 'a:0:{}'));
     // User and capability stuff (stolen from testaccesslib.php)
     $syscontext = get_system_context(false);
     $adminrole = create_role(get_string('administrator'), 'admin', get_string('administratordescription'), 'moodle/legacy:admin');
     /// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
     assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $syscontext->id);
     update_capabilities('moodle');
     update_capabilities('mod/forum');
     $contexts = $this->load_test_data('context', array('contextlevel', 'instanceid', 'path', 'depth'), array(1 => array(40, 666, '', 2)));
     $contexts[0] = $syscontext;
     $contexts[1]->path = $contexts[0]->path . '/' . $contexts[1]->id;
     $this->testdb->set_field('context', 'path', $contexts[1]->path, array('id' => $contexts[1]->id));
     $users = $this->load_test_data('user', array('username', 'confirmed', 'deleted'), array('a' => array('a', 1, 0)));
     $admin = $this->testdb->get_record('role', array('shortname' => 'admin'));
     $ras = $this->load_test_data('role_assignments', array('userid', 'roleid', 'contextid'), array('a' => array($users['a']->id, $admin->id, $contexts[0]->id)));
     $this->switch_global_user_id(1);
     accesslib_clear_all_caches_for_unit_testing();
     // Create a coursecat
     $newcategory = new stdClass();
     $newcategory->name = 'test category';
     $newcategory->sortorder = 999;
     if (!($newcategory->id = $DB->insert_record('course_categories', $newcategory))) {
         print_error('cannotcreatecategory', '', '', format_string($newcategory->name));
     }
     $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
     mark_context_dirty($newcategory->context->path);
     fix_course_sortorder();
     // Required to build course_categories.depth and .path.
     $this->coursecat = $DB->get_record('course_categories', array('id' => $newcategory->id));
     // Create a course
     $coursedata = new stdClass();
     $coursedata->category = $newcategory->id;
     $coursedata->shortname = 'testcourse';
     $coursedata->fullname = 'Test Course';
     try {
         $this->course = create_course($coursedata);
     } catch (moodle_exception $e) {
         // Most likely the result of an aborted unit test: the test course was not correctly deleted
         $this->course = $DB->get_record('course', array('shortname' => $coursedata->shortname));
     }
     // Create a user
     $this->user = new stdClass();
     $this->user->username = '******';
     $this->user->password = '******';
     $this->user->firstname = 'TestUser';
     $this->user->lastname = 'TestUser';
     $this->user->email = '*****@*****.**';
     try {
         $this->user->id = create_user($this->user);
     } catch (moodle_exception $e) {
         // Most likely the result of an aborted unit test: the test user was not correctly deleted
         $this->user->id = $DB->get_field('user', 'id', array('username' => $this->user->username));
     }
     // Assign user to course
     // role_assign(5, $this->user->id, 0, get_context_instance(CONTEXT_COURSE, $this->course->id)->id);
     // Create a module
     $module = new stdClass();
     $module->intro = 'Forum used for testing filelib API';
     $module->type = 'general';
     $module->forcesubscribe = 1;
     $module->format = 1;
     $module->name = 'Test Forum';
     $module->module = $DB->get_field('modules', 'id', array('name' => 'forum'));
     $module->modulename = 'forum';
     $module->add = 'forum';
     $module->cmidnumber = '';
     $module->course = $this->course->id;
     $module->instance = forum_add_instance($module, '');
     $this->section = get_course_section(1, $this->course->id);
     $module->section = $this->section->id;
     $module->coursemodule = add_course_module($module);
     add_mod_to_section($module);
     $module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, '');
     rebuild_course_cache($this->course->id);
     $this->module = $DB->get_record('forum', array('id' => $module->instance));
     $this->module->instance = $module->instance;
     // Update local copy of course
     $this->course = $DB->get_record('course', array('id' => $this->course->id));
 }
Beispiel #3
0
/**
 * Update the module info.
 * This function doesn't check the user capabilities. It updates the course module and the module instance.
 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
 *
 * @param object $cm course module
 * @param object $moduleinfo module info
 * @param object $course course of the module
 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
 * @return array list of course module and module info.
 */
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);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = $cm->groupmode;
        // Keep original.
    }
    // Update course module first.
    $cm->groupmode = $moduleinfo->groupmode;
    if (isset($moduleinfo->groupingid)) {
        $cm->groupingid = $moduleinfo->groupingid;
    }
    $completion = new completion_info($course);
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        // Update completion settings.
        $cm->completion = $moduleinfo->completion;
        $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
        $cm->completionview = $moduleinfo->completionview;
        $cm->completionexpected = $moduleinfo->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        // This code is used both when submitting the form, which uses a long
        // name to avoid clashes, and by unit test code which uses the real
        // name in the table.
        if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
            if ($moduleinfo->availabilityconditionsjson !== '') {
                $cm->availability = $moduleinfo->availabilityconditionsjson;
            } else {
                $cm->availability = null;
            }
        } else {
            if (property_exists($moduleinfo, 'availability')) {
                $cm->availability = $moduleinfo->availability;
            }
        }
    }
    if (isset($moduleinfo->showdescription)) {
        $cm->showdescription = $moduleinfo->showdescription;
    } else {
        $cm->showdescription = 0;
    }
    $DB->update_record('course_modules', $cm);
    $modcontext = context_module::instance($moduleinfo->coursemodule);
    // Update embedded links and save files.
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
        $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']);
        $moduleinfo->introformat = $moduleinfo->introeditor['format'];
        unset($moduleinfo->introeditor);
    }
    $updateinstancefunction = $moduleinfo->modulename . "_update_instance";
    if (!$updateinstancefunction($moduleinfo, $mform)) {
        print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
    }
    // Make sure visibility is set correctly (in particular in calendar).
    if (has_capability('moodle/course:activityvisibility', $modcontext)) {
        set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
    }
    if (isset($moduleinfo->cmidnumber)) {
        // Label.
        // Set cm idnumber - uniqueness is already verified by form validation.
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
    }
    // Now that module is fully updated, also update completion data if required.
    // (this will wipe all user completion data and recalculate it)
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        $completion->reset_all_state($cm);
    }
    $cm->name = $moduleinfo->name;
    \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    return array($cm, $moduleinfo);
}
Beispiel #4
0
 public function generate_modules($courses)
 {
     global $DB, $CFG;
     // Parse the modules-list variable
     $this->verbose("Generating " . $this->get('number_of_sections') . " sections with " . $this->get('number_of_modules') . " modules in each section, for each course...");
     list($modules_list_sql, $modules_params) = $DB->get_in_or_equal($this->get('modules_list'), SQL_PARAMS_NAMED, 'param0000', true);
     list($modules_ignored_sql, $ignore_params) = $DB->get_in_or_equal($this->modules_to_ignore, SQL_PARAMS_NAMED, 'param2000', false);
     $wheresql = "name {$modules_list_sql} AND name {$modules_ignored_sql}";
     $modules = $DB->get_records_select('modules', $wheresql, array_merge($modules_params, $ignore_params));
     foreach ($modules as $key => $module) {
         $module->count = 0;
         // Scorm, lams and hotpot are too complex to set up, remove them
         if (in_array($module->name, $this->modules_to_ignore) || !in_array($module->name, $this->modules_list)) {
             unset($modules[$key]);
         }
     }
     // Dirty hack for renumbering the modules array's keys
     $first_module = reset($modules);
     array_shift($modules);
     array_unshift($modules, $first_module);
     $modules_array = array();
     if (count($courses) > 0) {
         $libraries = array();
         foreach ($courses as $courseid) {
             // Text resources
             for ($i = 1; $i <= $this->get('number_of_sections'); $i++) {
                 for ($j = 0; $j < $this->get('number_of_modules'); $j++) {
                     $module = new stdClass();
                     // If only one module is created, and we also need to add a question to a quiz, create only a quiz
                     if ($this->get('number_of_modules') == 1 && $this->get('questions_per_quiz') > 0 && !empty($modules[8])) {
                         $moduledata = $modules[8];
                     } else {
                         $moduledata = $modules[array_rand($modules)];
                     }
                     $libfile = "{$CFG->dirroot}/mod/{$moduledata->name}/lib.php";
                     if (file_exists($libfile)) {
                         if (!in_array($libfile, $libraries)) {
                             $this->verbose("Including library for {$moduledata->name}...");
                             $libraries[] = $libfile;
                             require_once $libfile;
                         }
                     } else {
                         $this->verbose("Could not load lib file for module {$moduledata->name}!");
                         if (!$this->get('ignore_errors')) {
                             die;
                         }
                     }
                     // Basically 2 types of text fields: description and content
                     $description = "This {$moduledata->name} has been randomly generated by a very useful script, " . "for the purpose of testing " . "the boundaries of Moodle in various contexts. Moodle should be able to scale to " . "any size without " . "its speed and ease of use being affected dramatically.";
                     $content = 'Very useful content, I am sure you would agree';
                     $module_type_index = 0;
                     $module->introformat = FORMAT_MOODLE;
                     $module->messageformat = FORMAT_MOODLE;
                     // Special module-specific config
                     switch ($moduledata->name) {
                         case 'assignment':
                             $module->intro = $description;
                             $module->assignmenttype = $this->get_module_type('assignment');
                             $module->timedue = mktime() + 89487321;
                             $module->grade = rand(50, 100);
                             break;
                         case 'chat':
                             $module->intro = $description;
                             $module->schedule = 1;
                             $module->chattime = 60 * 60 * 4;
                             break;
                         case 'data':
                             $module->intro = $description;
                             $module->name = 'test';
                             break;
                         case 'choice':
                             $module->intro = $description;
                             $module->text = $content;
                             $module->option = array('Good choice', 'Bad choice', 'No choice');
                             $module->limit = array(1, 5, 0);
                             break;
                         case 'comments':
                             $module->intro = $description;
                             $module->comments = $content;
                             break;
                         case 'feedback':
                             $module->intro = $description;
                             $module->page_after_submit = $description;
                             $module->comments = $content;
                             break;
                         case 'forum':
                             $module->intro = $description;
                             $module->type = $this->get_module_type('forum');
                             $module->forcesubscribe = rand(0, 1);
                             $module->format = 1;
                             break;
                         case 'glossary':
                             $module->intro = $description;
                             $module->displayformat = $this->glossary_formats[rand(0, count($this->glossary_formats) - 1)];
                             $module->cmidnumber = rand(0, 999999);
                             break;
                         case 'label':
                             $module->content = $content;
                             $module->intro = $description;
                             break;
                         case 'lesson':
                             $module->lessondefault = 1;
                             $module->available = mktime();
                             $module->deadline = mktime() + 719891987;
                             $module->grade = 100;
                             break;
                         case 'quiz':
                             $module->intro = $description;
                             $module->feedbacktext = 'blah';
                             $module->feedback = 1;
                             $module->feedbackboundaries = array(2, 1);
                             $module->grade = 10;
                             $module->timeopen = time();
                             $module->timeclose = time() + 68854;
                             $module->shufflequestions = true;
                             $module->shuffleanswers = true;
                             $module->quizpassword = '';
                             break;
                         case 'resource':
                             $module->type = $this->get_module_type('resource');
                             $module->alltext = $content;
                             $module->summary = $description;
                             $module->windowpopup = rand(0, 1);
                             $module->display = rand(0, 1);
                             $module->resizable = rand(0, 1);
                             $module->scrollbars = rand(0, 1);
                             $module->directories = rand(0, 1);
                             $module->location = 'file.txt';
                             $module->menubar = rand(0, 1);
                             $module->toolbar = rand(0, 1);
                             $module->status = rand(0, 1);
                             $module->width = rand(200, 600);
                             $module->height = rand(200, 600);
                             $module->directories = rand(0, 1);
                             $module->files = false;
                             $module->param_navigationmenu = rand(0, 1);
                             $module->param_navigationbuttons = rand(0, 1);
                             $module->reference = 1;
                             $module->forcedownload = 1;
                             break;
                         case 'survey':
                             $module->template = rand(1, 5);
                             $module->intro = $description;
                             break;
                         case 'wiki':
                             $module->intro = $description;
                             $module->summary = $description;
                             break;
                     }
                     $module->name = ucfirst($moduledata->name) . ' ' . $moduledata->count++;
                     $module->course = $courseid;
                     $module->section = $i;
                     $module->module = $moduledata->id;
                     $module->modulename = $moduledata->name;
                     $module->add = $moduledata->name;
                     $module->cmidnumber = '';
                     $module->coursemodule = '';
                     $add_instance_function = $moduledata->name . '_add_instance';
                     $section = get_course_section($i, $courseid);
                     $module->section = $section->id;
                     $module->coursemodule = add_course_module($module);
                     $module->section = $i;
                     if (function_exists($add_instance_function)) {
                         $this->verbose("Calling module function {$add_instance_function}");
                         $module->instance = $add_instance_function($module, '');
                         $DB->set_field('course_modules', 'instance', $module->instance, array('id' => $module->coursemodule));
                     } else {
                         $this->verbose("Function {$add_instance_function} does not exist!");
                         if (!$this->get('ignore_errors')) {
                             die;
                         }
                     }
                     add_mod_to_section($module);
                     $module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, '');
                     $this->verbose("A {$moduledata->name} module was added to section {$i} (id {$module->section}) " . "of course {$courseid}.");
                     rebuild_course_cache($courseid);
                     $module_instance = $DB->get_field('course_modules', 'instance', array('id' => $module->coursemodule));
                     $module_record = $DB->get_record($moduledata->name, array('id' => $module_instance));
                     $module_record->instance = $module_instance;
                     if (empty($modules_array[$moduledata->name])) {
                         $modules_array[$moduledata->name] = array();
                     }
                     // TODO Find out why some $module_record end up empty here... (particularly quizzes)
                     if (!empty($module_record->instance)) {
                         $modules_array[$moduledata->name][] = $module_record;
                     }
                 }
             }
         }
         if (!$this->get('quiet')) {
             echo "Successfully generated " . $this->get('number_of_modules') * $this->get('number_of_sections') . " modules in each course!{$this->eolchar}";
         }
         return $modules_array;
     }
     return null;
 }
/**
 * Add course module.
 *
 * The function does not check user capabilities.
 * The function creates course module, module instance, add the module to the correct section.
 * It also trigger common action that need to be done after adding/updating a module.
 *
 * @param object $moduleinfo the moudle data
 * @param object $course the course of the module
 * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
 * @return object the updated module info
 */
function local_ltiprovider_add_moduleinfo($moduleinfo, $course, $mform = null)
{
    global $DB, $CFG;
    $moduleinfo->course = $course->id;
    $moduleinfo = local_ltiprovider_set_moduleinfo_defaults($moduleinfo);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $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;
    $newcm->module = $moduleinfo->module;
    $newcm->instance = 0;
    // Not known yet, will be updated later (this is similar to restore code).
    $newcm->visible = $moduleinfo->visible;
    $newcm->visibleold = $moduleinfo->visible;
    $newcm->groupmode = $moduleinfo->groupmode;
    $newcm->groupingid = $moduleinfo->groupingid;
    $newcm->groupmembersonly = $moduleinfo->groupmembersonly;
    $completion = new completion_info($course);
    if ($completion->is_enabled()) {
        $newcm->completion = $moduleinfo->completion;
        $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
        $newcm->completionview = $moduleinfo->completionview;
        $newcm->completionexpected = $moduleinfo->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        $newcm->availablefrom = $moduleinfo->availablefrom;
        $newcm->availableuntil = $moduleinfo->availableuntil;
        $newcm->showavailability = $moduleinfo->showavailability;
    }
    if (isset($moduleinfo->showdescription)) {
        $newcm->showdescription = $moduleinfo->showdescription;
    } else {
        $newcm->showdescription = 0;
    }
    if (!($moduleinfo->coursemodule = add_course_module($newcm))) {
        print_error('cannotaddcoursemodule');
    }
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
        $introeditor = $moduleinfo->introeditor;
        unset($moduleinfo->introeditor);
        $moduleinfo->intro = $introeditor['text'];
        $moduleinfo->introformat = $introeditor['format'];
    }
    $addinstancefunction = $moduleinfo->modulename . "_add_instance";
    $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
    if (!$returnfromfunc or !is_number($returnfromfunc)) {
        // Undo everything we can.
        $modcontext = context_module::instance($moduleinfo->coursemodule);
        delete_context(CONTEXT_MODULE, $moduleinfo->coursemodule);
        $DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
        if (!is_number($returnfromfunc)) {
            print_error('invalidfunction', '', course_get_url($course, $cw->section));
        } else {
            print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
        }
    }
    $moduleinfo->instance = $returnfromfunc;
    $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $moduleinfo->coursemodule));
    // Update embedded links and save files.
    $modcontext = context_module::instance($moduleinfo->coursemodule);
    if (!empty($introeditor)) {
        $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
        $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id' => $moduleinfo->instance));
    }
    // Course_modules and course_sections each contain a reference to each other.
    // So we have to update one of them twice.
    $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
    // Make sure visibility is set correctly (in particular in calendar).
    // Note: allow them to set it even without moodle/course:activityvisibility.
    set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
    if (isset($moduleinfo->cmidnumber)) {
        // Label.
        // Set cm idnumber - uniqueness is already verified by form validation.
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
    }
    // Set up conditions.
    if ($CFG->enableavailability) {
        condition_info::update_cm_from_form((object) array('id' => $moduleinfo->coursemodule), $moduleinfo, false);
    }
    $eventname = 'mod_created';
    add_to_log($course->id, "course", "add mod", "../mod/{$moduleinfo->modulename}/view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->modulename} {$moduleinfo->instance}");
    add_to_log($course->id, $moduleinfo->modulename, "add", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
    $moduleinfo = local_ltiprovider_edit_module_post_actions($moduleinfo, $course, 'mod_created');
    return $moduleinfo;
}
Beispiel #6
0
/**
 * Update the module info.
 * This function doesn't check the user capabilities. It updates the course module and the module instance.
 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
 *
 * @param object $cm course module
 * @param object $moduleinfo module info
 * @param object $course course of the module
 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
 * @return array list of course module and module info.
 */
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);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = $cm->groupmode;
        // Keep original.
    }
    // Update course module first.
    $cm->groupmode = $moduleinfo->groupmode;
    if (isset($moduleinfo->groupingid)) {
        $cm->groupingid = $moduleinfo->groupingid;
    }
    if (isset($moduleinfo->groupmembersonly)) {
        $cm->groupmembersonly = $moduleinfo->groupmembersonly;
    }
    $completion = new completion_info($course);
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        // Update completion settings.
        $cm->completion = $moduleinfo->completion;
        $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
        $cm->completionview = $moduleinfo->completionview;
        $cm->completionexpected = $moduleinfo->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        $cm->availablefrom = $moduleinfo->availablefrom;
        $cm->availableuntil = $moduleinfo->availableuntil;
        $cm->showavailability = $moduleinfo->showavailability;
        condition_info::update_cm_from_form($cm, $moduleinfo, true);
    }
    if (isset($moduleinfo->showdescription)) {
        $cm->showdescription = $moduleinfo->showdescription;
    } else {
        $cm->showdescription = 0;
    }
    $DB->update_record('course_modules', $cm);
    $modcontext = context_module::instance($moduleinfo->coursemodule);
    // Update embedded links and save files.
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
        $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']);
        $moduleinfo->introformat = $moduleinfo->introeditor['format'];
        unset($moduleinfo->introeditor);
    }
    $updateinstancefunction = $moduleinfo->modulename . "_update_instance";
    if (!$updateinstancefunction($moduleinfo, $mform)) {
        print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
    }
    // Make sure visibility is set correctly (in particular in calendar).
    if (has_capability('moodle/course:activityvisibility', $modcontext)) {
        set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
    }
    if (isset($moduleinfo->cmidnumber)) {
        // Label.
        // Set cm idnumber - uniqueness is already verified by form validation.
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
    }
    // Now that module is fully updated, also update completion data if required.
    // (this will wipe all user completion data and recalculate it)
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        $completion->reset_all_state($cm);
    }
    // Trigger event based on the action we did.
    $event = \core\event\course_module_updated::create(array('courseid' => $course->id, 'context' => $modcontext, 'objectid' => $moduleinfo->coursemodule, 'other' => array('modulename' => $moduleinfo->modulename, 'name' => $moduleinfo->name, 'instanceid' => $moduleinfo->instance)));
    $event->trigger();
    add_to_log($course->id, $moduleinfo->modulename, "update", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    return array($cm, $moduleinfo);
}
Beispiel #7
0
/**
 * Update the module info.
 * This function doesn't check the user capabilities. It updates the course module and the module instance.
 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
 *
 * @param object $cm course module
 * @param object $moduleinfo module info
 * @param object $course course of the module
 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
 * @return array list of course module and module info.
 */
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null)
{
    global $DB, $CFG;
    $data = new stdClass();
    if ($mform) {
        $data = $mform->get_data();
    }
    // 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);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = $cm->groupmode;
        // Keep original.
    }
    // Update course module first.
    $cm->groupmode = $moduleinfo->groupmode;
    if (isset($moduleinfo->groupingid)) {
        $cm->groupingid = $moduleinfo->groupingid;
    }
    $completion = new completion_info($course);
    if ($completion->is_enabled()) {
        // Completion settings that would affect users who have already completed
        // the activity may be locked; if so, these should not be updated.
        if (!empty($moduleinfo->completionunlocked)) {
            $cm->completion = $moduleinfo->completion;
            $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
            $cm->completionview = $moduleinfo->completionview;
        }
        // The expected date does not affect users who have completed the activity,
        // so it is safe to update it regardless of the lock status.
        $cm->completionexpected = $moduleinfo->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        // This code is used both when submitting the form, which uses a long
        // name to avoid clashes, and by unit test code which uses the real
        // name in the table.
        if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
            if ($moduleinfo->availabilityconditionsjson !== '') {
                $cm->availability = $moduleinfo->availabilityconditionsjson;
            } else {
                $cm->availability = null;
            }
        } else {
            if (property_exists($moduleinfo, 'availability')) {
                $cm->availability = $moduleinfo->availability;
            }
        }
        // If there is any availability data, verify it.
        if ($cm->availability) {
            $tree = new \core_availability\tree(json_decode($cm->availability));
            // Save time and database space by setting null if the only data
            // is an empty tree.
            if ($tree->is_empty()) {
                $cm->availability = null;
            }
        }
    }
    if (isset($moduleinfo->showdescription)) {
        $cm->showdescription = $moduleinfo->showdescription;
    } else {
        $cm->showdescription = 0;
    }
    $DB->update_record('course_modules', $cm);
    $modcontext = context_module::instance($moduleinfo->coursemodule);
    // Update embedded links and save files.
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
        $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']);
        $moduleinfo->introformat = $moduleinfo->introeditor['format'];
        unset($moduleinfo->introeditor);
    }
    // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
    $oldgradeitem = null;
    $newgradeitem = null;
    if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
        // Fetch the grade item before it is updated.
        $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'itemnumber' => 0, 'courseid' => $moduleinfo->course));
    }
    $updateinstancefunction = $moduleinfo->modulename . "_update_instance";
    if (!$updateinstancefunction($moduleinfo, $mform)) {
        print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
    }
    // This needs to happen AFTER the grademin/grademax have already been updated.
    if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
        // Get the grade_item after the update call the activity to scale the grades.
        $newgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => $moduleinfo->modulename, 'iteminstance' => $moduleinfo->instance, 'itemnumber' => 0, 'courseid' => $moduleinfo->course));
        if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) {
            $params = array($course, $cm, $oldgradeitem->grademin, $oldgradeitem->grademax, $newgradeitem->grademin, $newgradeitem->grademax);
            if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) {
                print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
            }
        }
    }
    // Make sure visibility is set correctly (in particular in calendar).
    if (has_capability('moodle/course:activityvisibility', $modcontext)) {
        set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
    }
    if (isset($moduleinfo->cmidnumber)) {
        // Label.
        // Set cm idnumber - uniqueness is already verified by form validation.
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
    }
    // Update module tags.
    if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
        core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
    }
    // Now that module is fully updated, also update completion data if required.
    // (this will wipe all user completion data and recalculate it)
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        $completion->reset_all_state($cm);
    }
    $cm->name = $moduleinfo->name;
    \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    return array($cm, $moduleinfo);
}
function RWSAUQuiz()
{
    global $CFG;
    global $DB;
    global $RWSLB;
    global $RWSUID;
    RWSCMAuth();
    RWSCRAuth();
    RWSCMUSvc();
    RWSCMMaint();
    $r_pm = RWSGSOpt("quizid", PARAM_ALPHANUM);
    if ($r_pm === false || strlen($r_pm) == 0) {
        RWSSErr("2067");
    }
    $r_qzmi = intval($r_pm);
    $r_cmod = RWSCMUQuiz($r_qzmi);
    $r_sfl = RWSGSOpt("sfile", RWSPRF);
    if ($r_sfl === false) {
        $r_sn = RWSGSOpt("sname", PARAM_FILE);
        $r_sd = RWSGSOpt("sdata", PARAM_NOTAGS);
        $r_ecd = true;
    } else {
        $r_sn = $r_sfl->filename;
        $r_sd = $r_sfl->filedata;
        $r_ecd = false;
    }
    $r_imp = false;
    if ($r_sd !== false && strlen($r_sd) > 0) {
        if ($r_sn === false || strlen($r_sn) == 0) {
            RWSSErr("2075");
        }
        $r_sn = clean_filename($r_sn);
        $r_imp = true;
    }
    $r_cid = $r_cmod->course;
    $r_crs = RWSCMUCourse($r_cid, true);
    $r_mr = $DB->get_record("modules", array("id" => $r_cmod->module));
    if ($r_mr === false) {
        RWSSErr("2043");
    }
    $r_qiz = $DB->get_record($r_mr->name, array("id" => $r_cmod->instance));
    if ($r_qiz === false) {
        RWSSErr("2044");
    }
    $r_ren = false;
    $r_pm = RWSGSOpt("rename", PARAM_TEXT);
    if ($r_pm !== false && strlen($r_pm) > 0) {
        $r_ren = trim(clean_text(strip_tags($r_pm, "<lang><span>")));
        $r_qiz->name = $r_ren;
    }
    if ($r_ren === false) {
        if ($r_sd === false || strlen($r_sd) == 0) {
            RWSSErr("2080");
        }
    }
    $r_sec = $DB->get_record("course_sections", array("id" => $r_cmod->section));
    if ($r_sec === false) {
        RWSSErr("2079");
    }
    $r_qiz->coursemodule = $r_cmod->id;
    $r_qiz->section = $r_sec->section;
    $r_qiz->visible = $r_cmod->visible;
    $r_qiz->cmidnumber = $r_cmod->idnumber;
    $r_qiz->groupmode = groups_get_activity_groupmode($r_cmod);
    $r_qiz->groupingid = $r_cmod->groupingid;
    $r_qiz->groupmembersonly = $r_cmod->groupmembersonly;
    $r_qiz->course = $r_cid;
    $r_qiz->module = $r_mr->id;
    $r_qiz->modulename = $r_mr->name;
    $r_qiz->instance = $r_cmod->instance;
    if (respondusws_floatcompare($CFG->version, 2011120500.0, 2) >= 0) {
        $r_qiz->showdescription = 0;
    }
    $r_cpl = new completion_info($r_crs);
    if ($r_cpl->is_enabled()) {
        $r_qiz->completion = $r_cmod->completion;
        $r_qiz->completionview = $r_cmod->completionview;
        $r_qiz->completionexpected = $r_cmod->completionexpected;
        $r_qiz->completionusegrade = is_null($r_cmod->completiongradeitemnumber) ? 0 : 1;
    }
    if ($CFG->enableavailability) {
        $r_qiz->availablefrom = $r_cmod->availablefrom;
        $r_qiz->availableuntil = $r_cmod->availableuntil;
        if ($r_qiz->availableuntil) {
            $r_qiz->availableuntil = strtotime("23:59:59", $r_qiz->availableuntil);
        }
        $r_qiz->showavailability = $r_cmod->showavailability;
    }
    $r_its = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $r_qiz->modulename, 'iteminstance' => $r_qiz->instance, 'courseid' => $r_cid));
    if ($r_its) {
        foreach ($r_its as $r_it) {
            if (!empty($r_it->outcomeid)) {
                $r_qiz->{'outcome_' . $r_it->outcomeid} = 1;
            }
        }
        $r_gc = false;
        foreach ($r_its as $r_it) {
            if ($r_gc === false) {
                $r_gc = $r_it->categoryid;
                continue;
            }
            if ($r_gc != $r_it->categoryid) {
                $r_gc = false;
                break;
            }
        }
        if ($r_gc !== false) {
            $r_qiz->gradecat = $r_gc;
        }
    }
    if ($r_imp) {
        RWSIQSet($r_qiz, $r_sn, $r_sd, $r_ecd);
    }
    $DB->update_record("course_modules", $r_qiz);
    if (is_null($r_qiz->quizpassword) && !is_null($r_qiz->password)) {
        $r_qiz->quizpassword = $r_qiz->password;
    }
    $r_res = quiz_update_instance($r_qiz);
    if (!$r_res || is_string($r_res)) {
        RWSSErr("2081");
    }
    RWSSLBSet($r_qiz);
    set_coursemodule_visible($r_qzmi, $r_qiz->visible);
    if (isset($r_qiz->cmidnumber)) {
        set_coursemodule_idnumber($r_qzmi, $r_qiz->cmidnumber);
    }
    RWSUQGrades($r_qiz);
    if ($r_cpl->is_enabled() && !empty($r_qiz->completionunlocked)) {
        $r_cpl->reset_all_state($r_qiz);
    }
    if (respondusws_floatcompare($CFG->version, 2014051200, 2) >= 0) {
        $r_qiz->modname = $r_qiz->modulename;
        $r_qiz->id = $r_qiz->coursemodule;
        \core\event\course_module_updated::create_from_cm($r_qiz)->trigger();
    } else {
        $r_evt = new stdClass();
        $r_evt->modulename = $r_qiz->modulename;
        $r_evt->name = $r_qiz->name;
        $r_evt->cmid = $r_qiz->coursemodule;
        $r_evt->courseid = $r_qiz->course;
        $r_evt->userid = $RWSUID;
        events_trigger("mod_updated", $r_evt);
    }
    rebuild_course_cache($r_cid);
    grade_regrade_final_grades($r_cid);
    if ($RWSLB->mex || $RWSLB->bex) {
        if ($RWSLB->mok) {
            if ($RWSLB->perr) {
                RWSSWarn("3003");
            }
        } else {
            if ($RWSLB->bok) {
                if ($RWSLB->perr) {
                    RWSSWarn("3003");
                }
            } else {
                RWSSWarn("3001");
            }
        }
    } else {
        RWSSWarn("3000");
    }
    RWSSStat("1004");
}
Beispiel #9
0
/**
* basic encapsulation of creating a glossary instance - as would be done in modedit.php
* with glossary_add_instance();
*
* @param int $courseid the id of the course to add forum to
* @param string $glossary_name  name of the glossary
* @param string $description descrition of the glossary
* @return $glossary object
* */
function local_create_glossary($courseid, $glossary_name, $description)
{
    global $CFG;
    require_once $CFG->dirroot . "/mod/glossary/lib.php";
    require_once $CFG->dirroot . "/lib/grouplib.php";
    if (!($course = get_record("course", "id", $courseid))) {
        error("This course doesn't exist");
    }
    $glossary = new object();
    $glossary->course = $courseid;
    $glossary->cmidnumber = 1;
    $glossary->name = $glossary_name;
    $glossary->intro = $description;
    $glossary->entbypage = 10;
    $glossary->mainglossary = 1;
    $glossary->globalglossary = false;
    $glossary->userrating = false;
    $glossary->ratingtime = false;
    if ($CFG->enablerssfeeds && isset($CFG->glossary_enablerssfeeds) && $CFG->glossary_enablerssfeeds) {
        $glossary->rsstype = 2;
        $glossary->rssarticles = 10;
    }
    $glossary->allowduplicatedentries = 0;
    $glossary->allowcomments = 1;
    $glossary->allowprintview = 1;
    $glossary->usedynalink = 1;
    $glossary->defaultapproval = 1;
    $glossary->displayformat = 'faq';
    $glossary->showspecial = 1;
    $glossary->showalphabet = 1;
    $glossary->showall = 1;
    $glossary->editalways = 0;
    $glossary->assesstimestart = time();
    $glossary->assesstimefinish = time();
    $glossary->showall = 1;
    $glossary->showall = 1;
    $glossary->showall = 1;
    $glossary->showall = 1;
    $glossary->groupingid = $course->defaultgroupingid;
    $glossary->groupmembersonly = 0;
    //    gradecat    8   - uncategorised
    $glossary->groupmode = 0;
    $glossary->visible = '1';
    $glossary->module = get_field('modules', 'id', 'name', 'glossary');
    $glossary->id = glossary_add_instance($glossary);
    $glossary->modulename = 'glossary';
    $glossary->instance = $glossary->id;
    $glossary->section = 0;
    // default to first level section
    $glossary->coursemodule = add_course_module($glossary);
    $sectionid = add_mod_to_section($glossary);
    set_field("course_modules", "section", $sectionid, "id", $glossary->coursemodule);
    set_coursemodule_visible($glossary->coursemodule, $glossary->visible);
    set_coursemodule_idnumber($glossary->coursemodule, $glossary->cmidnumber);
    rebuild_course_cache($glossary->course);
    return $glossary;
}
 // update embedded links and save files
 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
 if (!empty($introeditor)) {
     $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
     $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance));
 }
 // course_modules and course_sections each contain a reference
 // to each other, so we have to update one of them twice.
 $sectionid = add_mod_to_section($fromform);
 $DB->set_field('course_modules', 'section', $sectionid, array('id' => $fromform->coursemodule));
 // make sure visibility is set correctly (in particular in calendar)
 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
 if (isset($fromform->cmidnumber)) {
     //label
     // set cm idnumber - uniqueness is already verified by form validation
     set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
 }
 // Set up conditions
 if ($CFG->enableavailability) {
     condition_info::update_cm_from_form((object) array('id' => $fromform->coursemodule), $fromform, false);
 }
 // Trigger mod_created event with information about this module.
 $eventdata = new stdClass();
 $eventdata->modulename = $fromform->modulename;
 $eventdata->name = $fromform->name;
 $eventdata->cmid = $fromform->coursemodule;
 $eventdata->courseid = $course->id;
 $eventdata->userid = $USER->id;
 events_trigger('mod_created', $eventdata);
 add_to_log($course->id, "course", "add mod", "../mod/{$fromform->modulename}/view.php?id={$fromform->coursemodule}", "{$fromform->modulename} {$fromform->instance}");
 add_to_log($course->id, $fromform->modulename, "add", "view.php?id={$fromform->coursemodule}", "{$fromform->instance}", $fromform->coursemodule);
 $module->cmidnumber = '';
 $add_instance_function = $moduledata->name . '_add_instance';
 if (function_exists($add_instance_function)) {
     $module->instance = $add_instance_function($module, '');
 } else {
     verbose("Function {$add_instance_function} does not exist!");
     if (!$settings['ignore-errors']) {
         die;
     }
 }
 $section = get_course_section($i, $courseid);
 $module->section = $section->id;
 $module->coursemodule = add_course_module($module);
 $module->section = $i;
 add_mod_to_section($module);
 $module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, '');
 verbose("A {$moduledata->name} module was added to section {$i} (id {$module->section}) of course {$courseid}.");
 rebuild_course_cache($courseid);
 if ($moduledata->name == 'quiz') {
     $quiz_instance = $DB->get_field('course_modules', 'instance', array('id' => $module->coursemodule));
     $quiz = $DB->get_record('quiz', array('id' => $quiz_instance));
     $quiz->instance = $quiz_instance;
     $quizzes[] = $quiz;
 } elseif ($moduledata->name == 'assignment') {
     $assignment_instance = $DB->get_field('course_modules', 'instance', array('id' => $module->coursemodule));
     $assignment = $DB->get_record('assignment', array('id' => $assignment_instance));
     $assignment->instance = $assignment_instance;
     $assignments[] = $assignment;
 } elseif ($moduledata->name == 'glossary') {
     $glossary_instance = $DB->get_field('course_modules', 'instance', array('id' => $module->coursemodule));
     $glossary = $DB->get_record('glossary', array('id' => $glossary_instance));
 private function get_item($isbn, $mybooksconfig)
 {
     global $DB, $CFG;
     // Deleted book
     if (!($book = $DB->get_record('rcommon_books', array('isbn' => $isbn)))) {
         return false;
     }
     // Old book formats
     $bookformat = core_text::strtolower($book->format);
     if (!in_array($bookformat, rcommon_book::$allowedformats)) {
         return false;
     }
     // Check rcontent to know if isset entry for that book
     if ($rcontent = $DB->get_record('rcontent', array('course' => SITEID, 'bookid' => $book->id, 'unitid' => 0))) {
         // Search course_module
         if (!($cm = get_coursemodule_from_instance('rcontent', $rcontent->id, 1))) {
             return get_string('error_loading_data', 'block_my_books', $book->name);
         }
         $cmid = $cm->id;
     } else {
         // Add new rcontent
         $add = new stdClass();
         $add->course = SITEID;
         $add->name = addslashes($book->name);
         $add->levelid = $book->levelid;
         $add->isbn = $book->id;
         $add->unit = 0;
         $add->activity = 0;
         $add->intro = 'Auto added from my_books block';
         $add->introformat = FORMAT_MOODLE;
         $add->whatgrade = 0;
         $add->frame = 0;
         $add->coursemodule = 0;
         $add->cmidnumber = 0;
         $add->popup = 0;
         $add->scrollbars = 1;
         $add->menubar = 0;
         $add->toolbar = 1;
         $add->status = 1;
         $add->width = 800;
         $add->height = 600;
         $add->section = 2;
         $add->visible = 1;
         $add->module = $DB->get_field('modules', 'id', array('name' => 'rcontent'));
         // Add entry to rscorm table
         if (!($add->instance = rcontent_add_instance($add))) {
             return get_string('error_loading_data', 'block_my_books', $book->name);
         }
         if (!($add->coursemodule = add_course_module($add))) {
             return get_string('error_loading_data', 'block_my_books', $book->name);
         }
         $sectionid = course_add_cm_to_section($add->course, $add->module, $add->section);
         if (!$sectionid) {
             return get_string('error_loading_data', 'block_my_books', $book->name);
         }
         if (!$DB->set_field('course_modules', 'section', $sectionid, array("id" => $add->coursemodule))) {
             return get_string('error_loading_data', 'block_my_books', $book->name);
         }
         // Make sure visibility is set correctly
         set_coursemodule_visible($add->coursemodule, $add->visible);
         if (isset($add->cmidnumber)) {
             // Label
             // Set cm idnumber
             set_coursemodule_idnumber($add->coursemodule, $add->cmidnumber);
         }
         $cmid = $add->coursemodule;
     }
     $target = $mybooksconfig->viewer_opening == 1 ? ' target="_blank"' : "";
     return '<a href="' . $CFG->wwwroot . '/mod/rcontent/view.php?id=' . $cmid . '" title="' . $book->name . '" ' . $target . '>' . $book->name . '</a>';
 }
function respondusws_install()
{
    global $DB;
    global $CFG;
    $dbman = $DB->get_manager();
    if (!isset($CFG->respondusws_initialdisable)) {
        if (!$dbman->table_exists("respondusws") || $DB->count_records("respondusws") == 0) {
            $DB->set_field("modules", "visible", 0, array("name" => "respondusws"));
            set_config("respondusws_initialdisable", 1);
        }
    }
    $module = $DB->get_record("modules", array("name" => "respondusws"));
    if ($module === false) {
        throw new moodle_exception("installmodulerecord", "respondusws");
    }
    $instance = new stdClass();
    $instance->course = SITEID;
    $instance->name = get_string("sharedname", "respondusws");
    $instance->intro = get_string("sharedintro", "respondusws");
    $instance->introformat = FORMAT_HTML;
    $instance->modulename = $module->name;
    $instance->module = $module->id;
    $instance->section = 0;
    $instance->coursemodule = "";
    $instance->instance = "";
    $instance->cmidnumber = "";
    $instance->groupmode = 0;
    $instance->groupingid = 0;
    $instance->groupmembersonly = 0;
    $instance->visible = false;
    $instance_id = respondusws_add_instance($instance);
    if (is_string($instance_id)) {
        $a = (object) array("detail" => $instance_id);
        throw new moodle_exception("installaddinstancedetail", "respondusws", "", $a);
    } else {
        if ($instance_id === false) {
            throw new moodle_exception("installaddinstance", "respondusws");
        }
    }
    $instance->instance = $instance_id;
    $cmid = add_course_module($instance);
    if (!$cmid) {
        throw new moodle_exception("installcoursemodule", "respondusws");
    }
    $instance->coursemodule = $cmid;
    if (respondusws_floatcompare($CFG->version, 2012120300, 2) >= 0) {
        $section = $DB->get_record("course_sections", array("course" => $instance->course, "section" => $instance->section));
        if ($section === false) {
            $section = new stdClass();
            $section->course = $instance->course;
            $section->section = $instance->section;
            $section->summary = "";
            $section->summaryformat = FORMAT_HTML;
            $section->sequence = "";
            $section->id = $DB->insert_record("course_sections", $section);
        }
        if (empty($section->sequence)) {
            $newsequence = "{$instance->coursemodule}";
        } else {
            $newsequence = "{$section->sequence},{$instance->coursemodule}";
        }
        $DB->set_field("course_sections", "sequence", $newsequence, array("id" => $section->id));
        $section_id = $section->id;
    } else {
        $section_id = add_mod_to_section($instance);
    }
    if (!$section_id) {
        throw new moodle_exception("installmodsection", "respondusws");
    }
    $DB->set_field("course_modules", "section", $section_id, array("id" => $cmid));
    set_coursemodule_visible($cmid, $instance->visible);
    set_coursemodule_idnumber($cmid, $instance->cmidnumber);
    rebuild_course_cache(SITEID, true);
}