/**
  * Observer for \core\event\course_module_created event.
  *
  * @param \core\event\course_module_created $event
  * @return void
  */
 public static function course_module_created(\core\event\course_module_created $event)
 {
     global $CFG;
     if ($event->other['modulename'] === 'facetoface') {
         // Include the Face-to-Face library to make use of the facetoface_instance_created function.
         require_once $CFG->dirroot . '/mod/facetoface/lib.php';
         $facetoface = $event->get_record_snapshot('facetoface', $event->other['instanceid']);
         facetoface_instance_created($event->get_context(), $forum);
     }
 }
 public static function process_course_module_created(\core\event\course_module_created $event)
 {
     global $CFG;
     require_once $CFG->dirroot . "/local/morph/classes/logger/Logger.php";
     require_once $CFG->dirroot . "/mod/project/projectadminlib.php";
     $log = new moodle\local\morph\Logger(array("prefix" => 'project_'));
     $log->debug("OBSERVED COURSE MODULE CREATED EVENT: data:" . json_encode($event->get_data()));
     $eventdata = $event->get_data();
     if ($eventdata['other']['modulename'] === 'project') {
         $log->debug("CREATED NEW PROJECT");
         handle_new_project_created_event($event);
     }
 }
Exemple #3
0
 /**
  * Observer for \core\event\course_module_created event.
  *
  * @param \core\event\course_module_created $event
  * @return void
  */
 public static function course_module_created(\core\event\course_module_created $event)
 {
     global $CFG;
     if ($event->other['modulename'] === 'quora') {
         // Include the quora library to make use of the quora_instance_created function.
         require_once $CFG->dirroot . '/mod/quora/lib.php';
         $quora = $event->get_record_snapshot('quora', $event->other['instanceid']);
         quora_instance_created($event->get_context(), $quora);
     }
 }
 /**
  * Observer for \core\event\course_module_created event.
  *
  * @param \core\event\course_module_created $event
  * @return void
  */
 public static function course_module_created(\core\event\course_module_created $event)
 {
     global $CFG;
     if ($event->other['modulename'] === 'hsuforum') {
         // Include the forum library to make use of the hsuforum_instance_created function.
         require_once $CFG->dirroot . '/mod/hsuforum/lib.php';
         $forum = $event->get_record_snapshot('hsuforum', $event->other['instanceid']);
         hsuforum_instance_created($event->get_context(), $forum);
     }
 }
Exemple #5
0
/**
 * Duplicate a module on the course.
 *
 * @param object $course The course
 * @param object $cm The course module to duplicate
 * @throws moodle_exception if the plugin doesn't support duplication
 * @return Object containing:
 * - fullcontent: The HTML markup for the created CM
 * - cmid: The CMID of the newly created CM
 * - redirect: Whether to trigger a redirect following this change
 */
function mod_duplicate_activity($course, $cm, $sr = null)
{
    global $CFG, $USER, $PAGE, $DB;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    $a = new stdClass();
    $a->modtype = get_string('modulename', $cm->modname);
    $a->modname = format_string($cm->name);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        throw new moodle_exception('duplicatenosupport', 'error');
    }
    // backup the activity
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // restore the backup immediately
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
    $cmcontext = context_module::instance($cm->id);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
        }
    }
    $rc->execute_plan();
    // now a bit hacky part follows - we try to get the cmid of the newly
    // restored copy of the module
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        error_log("Looking at a task");
        if (is_subclass_of($task, 'restore_activity_task')) {
            error_log("Looking at a restore_activity_task task");
            if ($task->get_old_contextid() == $cmcontext->id) {
                error_log("Contexts match");
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    // if we know the cmid of the new course module, let us move it
    // right below the original one. otherwise it will stay at the
    // end of the section
    if ($newcmid) {
        $info = get_fast_modinfo($course);
        $newcm = $info->get_cm($newcmid);
        $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
        moveto_module($newcm, $section, $cm);
        moveto_module($cm, $section, $newcm);
        // Trigger course module created event. We can trigger the event only if we know the newcmid.
        $event = \core\event\course_module_created::create_from_cm($newcm);
        $event->trigger();
    }
    rebuild_course_cache($cm->course);
    $rc->destroy();
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    $resp = new stdClass();
    if ($newcm) {
        $courserenderer = $PAGE->get_renderer('core', 'course');
        $completioninfo = new completion_info($course);
        $modulehtml = $courserenderer->course_section_cm($course, $completioninfo, $newcm, null, array());
        $resp->fullcontent = $courserenderer->course_section_cm_list_item($course, $completioninfo, $newcm, $sr);
        $resp->cmid = $newcm->id;
    } else {
        // Trigger a redirect
        $resp->redirect = true;
    }
    return $resp;
}
Exemple #6
0
 /**
  * Tests for event validations related to course module creation.
  */
 public function test_course_module_created_event_exceptions()
 {
     $this->resetAfterTest();
     // Generate data.
     $modinfo = $this->create_specific_module_test('assign');
     $context = context_module::instance($modinfo->coursemodule);
     // Test not setting instanceid.
     try {
         $event = \core\event\course_module_created::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('modulename' => 'assign', 'name' => 'My assignment')));
         $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without\n                    other['instanceid']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
     }
     // Test not setting modulename.
     try {
         $event = \core\event\course_module_created::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('instanceid' => $modinfo->instance, 'name' => 'My assignment')));
         $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without\n                    other['modulename']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
     }
     // Test not setting name.
     try {
         $event = \core\event\course_module_created::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('modulename' => 'assign', 'instanceid' => $modinfo->instance)));
         $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without\n                    other['name']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
     }
 }
Exemple #7
0
/**
 * Purpose: add a Moodle page resource to a course
 *
 * @author Andrew Zoltay
 * date    2011-04-27
 * @global $CFG - configuration settings for CACE
 * @global object $DB Moodle database object
 * @global $CACE_CFG - configuration settings for CACE
 * @param int $courseid
 * @param object $pagedata
 */
function cace_add_page_resource($course, $pagedata)
{
    global $CFG, $DB, $CACE_CFG, $USER;
    require_once "{$CFG->dirroot}/mod/page/lib.php";
    try {
        // Create a new course module for the page.
        $newcm = new stdClass();
        $newcm->course = $course->id;
        $newcm->module = $pagedata->module;
        $newcm->instance = 0;
        // Don't have this value yet, but will update later.
        $newcm->visible = $pagedata->visible;
        $newcm->groupmode = $pagedata->groupmode;
        $newcm->groupingid = $pagedata->groupingid;
        $newcm->groupmembersonly = $pagedata->groupmembersonly;
        $pagedata->coursemodule = add_course_module($newcm);
        // This is lame, but need to create a dummy object for page_add_instance()
        // useless $mform parameter that isn't used in the function.
        $dummyform = new stdClass();
        // Create the page instance.
        $pageid = page_add_instance($pagedata, $dummyform);
        $pagedata->instance = $pageid;
        // Update course_modules table with new instance.
        $DB->set_field('course_modules', 'instance', $pageid, array('id' => $pagedata->coursemodule));
        // Add the module to the correct section of the course
        // 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($pagedata->course, $pagedata->coursemodule, $pagedata->section);
        $DB->set_field('course_modules', 'section', $sectionid, array('id' => $pagedata->coursemodule));
        set_coursemodule_visible($pagedata->coursemodule, $pagedata->visible);
        // Trigger mod_created event with information about the new module.
        // Use the new events system to log the creation of a resource.
        $eventdata = clone $pagedata;
        $eventdata->modname = $pagedata->modulename;
        $eventdata->name = $pagedata->name;
        $eventdata->id = $pagedata->coursemodule;
        $eventdata->courseid = $course->id;
        $eventdata->userid = $CACE_CFG->admin_user;
        // Use Admin account for event data.
        $event = \core\event\course_module_created::create_from_cm($eventdata);
        $event->trigger();
        return true;
    } catch (Exception $e) {
        return false;
    }
}
 /**
  *
  * @param unknown $user
  * @param unknown $courseid
  * @param unknown $sectionid
  * @param unknown $itemUuid
  * @param unknown $itemVersion
  * @param unknown $url
  * @param unknown $title
  * @param unknown $description
  * @param unknown $attachmentUuid
  * @return array
  */
 public static function add_item_to_course($username, $courseid, $sectionnum, $itemUuid, $itemVersion, $url, $title, $description, $attachmentUuid)
 {
     global $DB, $USER;
     $params = self::validate_parameters(self::add_item_to_course_parameters(), array('user' => $username, 'courseid' => $courseid, 'sectionid' => $sectionnum, 'itemUuid' => $itemUuid, 'itemVersion' => $itemVersion, 'url' => $url, 'title' => $title, 'description' => $description, 'attachmentUuid' => $attachmentUuid));
     self::check_modify_permissions($username, $courseid);
     $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
     $modname = 'equella';
     $module = $DB->get_record('modules', array('name' => $modname));
     $eq = new stdClass();
     $eq->course = $courseid;
     $eq->module = $module->id;
     $eq->name = $title;
     $eq->intro = $description;
     $eq->introformat = FORMAT_HTML;
     $eq->url = $url;
     $eq->uuid = $itemUuid;
     $eq->version = $itemVersion;
     $eq->mimetype = mimeinfo('type', $title);
     if (!empty($attachmentUuid)) {
         $eq->filename = $title;
         $eq->attachmentuuid = $attachmentUuid;
     }
     $eqid = equella_add_instance($eq);
     $cmid = null;
     $mod = new stdClass();
     $mod->course = $courseid;
     $mod->module = $module->id;
     $mod->instance = $eqid;
     $mod->modulename = $modname;
     $mod->section = 0;
     if (!($cmid = add_course_module($mod))) {
         throw new moodle_exception('cannotaddcoursemodule');
     }
     if (!($addedsectionid = course_add_cm_to_section($courseid, $cmid, $sectionnum))) {
         throw new moodle_exception('cannotaddcoursemoduletosection');
     }
     set_coursemodule_visible($cmid, true);
     if (class_exists('core\\event\\course_module_created')) {
         $cm = get_coursemodule_from_id('equella', $cmid, 0, false, MUST_EXIST);
         $event = \core\event\course_module_created::create_from_cm($cm);
         $event->trigger();
     } else {
         $eventdata = new stdClass();
         $eventdata->modulename = $modname;
         $eventdata->name = $eq->name;
         $eventdata->cmid = $cmid;
         $eventdata->courseid = $eq->course;
         $eventdata->userid = $USER->id;
         events_trigger('mod_created', $eventdata);
         add_to_log($eq->course, "course", "add mod", "../mod/{$modname}/view.php?id={$cmid}", "{$modname} {$eqid}");
         add_to_log($eq->course, $modname, "add equella resource", "view.php?id={$cmid}", "{$eqid}", $cmid);
     }
     $result = array('courseid' => $courseid, 'coursename' => $course->fullname, 'sectionid' => $params['sectionid'], 'sectionname' => get_section_name($courseid, $sectionnum));
     //flog("DB queries: " . $DB->perf_get_queries());
     return $result;
 }
Exemple #9
0
/**
 * 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 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);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = 0;
        // Do not set groupmode.
    }
    // 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;
    if (isset($moduleinfo->cmidnumber)) {
        $newcm->idnumber = $moduleinfo->cmidnumber;
    }
    $newcm->groupmode = $moduleinfo->groupmode;
    $newcm->groupingid = $moduleinfo->groupingid;
    $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)) {
        // 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.
        $newcm->availability = null;
        if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
            if ($moduleinfo->availabilityconditionsjson !== '') {
                $newcm->availability = $moduleinfo->availabilityconditionsjson;
            }
        } else {
            if (property_exists($moduleinfo, 'availability')) {
                $newcm->availability = $moduleinfo->availability;
            }
        }
    }
    if (isset($moduleinfo->showdescription)) {
        $newcm->showdescription = $moduleinfo->showdescription;
    } else {
        $newcm->showdescription = 0;
    }
    // From this point we make database changes, so start transaction.
    $transaction = $DB->start_delegated_transaction();
    if (!($moduleinfo->coursemodule = add_course_module($newcm))) {
        print_error('cannotaddcoursemodule');
    }
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) && isset($moduleinfo->introeditor)) {
        $introeditor = $moduleinfo->introeditor;
        unset($moduleinfo->introeditor);
        $moduleinfo->intro = $introeditor['text'];
        $moduleinfo->introformat = $introeditor['format'];
    }
    $addinstancefunction = $moduleinfo->modulename . "_add_instance";
    try {
        $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
    } catch (moodle_exception $e) {
        $returnfromfunc = $e;
    }
    if (!$returnfromfunc or !is_number($returnfromfunc)) {
        // Undo everything we can. This is not necessary for databases which
        // support transactions, but improves consistency for other databases.
        $modcontext = context_module::instance($moduleinfo->coursemodule);
        context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
        $DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
        if ($e instanceof moodle_exception) {
            throw $e;
        } else {
            if (!is_number($returnfromfunc)) {
                print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
            } else {
                print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->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);
    // Trigger event based on the action we did.
    // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
    $eventdata = clone $moduleinfo;
    $eventdata->modname = $eventdata->modulename;
    $eventdata->id = $eventdata->coursemodule;
    $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);
    $event->trigger();
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    $transaction->allow_commit();
    return $moduleinfo;
}
Exemple #10
0
/**
 * Api to duplicate a module.
 *
 * @param object $course course object.
 * @param object $cm course module object to be duplicated.
 * @since Moodle 2.8
 *
 * @throws Exception
 * @throws coding_exception
 * @throws moodle_exception
 * @throws restore_controller_exception
 *
 * @return cm_info|null cminfo object if we sucessfully duplicated the mod and found the new cm.
 */
function duplicate_module($course, $cm)
{
    global $CFG, $DB, $USER;
    require_once $CFG->dirroot . '/backup/util/includes/backup_includes.php';
    require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
    require_once $CFG->libdir . '/filelib.php';
    $a = new stdClass();
    $a->modtype = get_string('modulename', $cm->modname);
    $a->modname = format_string($cm->name);
    if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
        throw new moodle_exception('duplicatenosupport', 'error', '', $a);
    }
    // Backup the activity.
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
    $backupid = $bc->get_backupid();
    $backupbasepath = $bc->get_plan()->get_basepath();
    $bc->execute_plan();
    $bc->destroy();
    // Restore the backup immediately.
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
    $cmcontext = context_module::instance($cm->id);
    if (!$rc->execute_precheck()) {
        $precheckresults = $rc->get_precheck_results();
        if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
            if (empty($CFG->keeptempdirectoriesonbackup)) {
                fulldelete($backupbasepath);
            }
        }
    }
    $rc->execute_plan();
    // Now a bit hacky part follows - we try to get the cmid of the newly
    // restored copy of the module.
    $newcmid = null;
    $tasks = $rc->get_plan()->get_tasks();
    foreach ($tasks as $task) {
        if (is_subclass_of($task, 'restore_activity_task')) {
            if ($task->get_old_contextid() == $cmcontext->id) {
                $newcmid = $task->get_moduleid();
                break;
            }
        }
    }
    // If we know the cmid of the new course module, let us move it
    // right below the original one. otherwise it will stay at the
    // end of the section.
    if ($newcmid) {
        $info = get_fast_modinfo($course);
        $newcm = $info->get_cm($newcmid);
        $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
        moveto_module($newcm, $section, $cm);
        moveto_module($cm, $section, $newcm);
        // Update calendar events with the duplicated module.
        $refresheventsfunction = $newcm->modname . '_refresh_events';
        if (function_exists($refresheventsfunction)) {
            call_user_func($refresheventsfunction, $newcm->course);
        }
        // Trigger course module created event. We can trigger the event only if we know the newcmid.
        $event = \core\event\course_module_created::create_from_cm($newcm);
        $event->trigger();
    }
    rebuild_course_cache($cm->course);
    $rc->destroy();
    if (empty($CFG->keeptempdirectoriesonbackup)) {
        fulldelete($backupbasepath);
    }
    return isset($newcm) ? $newcm : null;
}
Exemple #11
0
/**
 * 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 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);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = 0;
        // Do not set groupmode.
    }
    // 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;
    if (isset($moduleinfo->cmidnumber)) {
        $newcm->idnumber = $moduleinfo->cmidnumber;
    }
    $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) && isset($moduleinfo->introeditor)) {
        $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);
        context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
        $DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
        if (!is_number($returnfromfunc)) {
            print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
        } else {
            print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->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);
    // Set up conditions.
    if ($CFG->enableavailability) {
        condition_info::update_cm_from_form((object) array('id' => $moduleinfo->coursemodule), $moduleinfo, false);
    }
    // Trigger event based on the action we did.
    $event = \core\event\course_module_created::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, "add", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    return $moduleinfo;
}
function RWSAAQuiz()
{
    global $CFG;
    global $DB;
    global $RWSLB;
    global $RWSUID;
    RWSCMAuth();
    RWSCRAuth();
    RWSCMUSvc();
    RWSCMMaint();
    $r_pm = RWSGSOpt("courseid", PARAM_ALPHANUM);
    if ($r_pm === false || strlen($r_pm) == 0) {
        RWSSErr("2057");
    }
    $r_cid = intval($r_pm);
    $r_crs = RWSCMUCourse($r_cid, true);
    $r_si = false;
    $r_pm = RWSGSOpt("sectionid", PARAM_ALPHANUM);
    if ($r_pm !== false && strlen($r_pm) > 0) {
        $r_si = intval($r_pm);
    }
    if ($r_si === false) {
        $r_sr = 0;
    } else {
        $r_sec = $DB->get_record("course_sections", array("id" => $r_si));
        if ($r_sec === false) {
            RWSSErr("2071");
        }
        if ($r_sec->course != $r_cid) {
            RWSSErr("2072");
        }
        $r_sr = $r_sec->section;
    }
    $r_pm = RWSGSOpt("name", PARAM_TEXT);
    if ($r_pm === false || strlen($r_pm) == 0) {
        RWSSErr("2073");
    }
    $r_qzn = trim(clean_text(strip_tags($r_pm, "<lang><span>")));
    $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_mr = $DB->get_record("modules", array("name" => "quiz"));
    if ($r_mr === false) {
        RWSSErr("2074");
    }
    $r_qiz = new stdClass();
    $r_qiz->name = $r_qzn;
    $r_qiz->section = $r_sr;
    $r_qiz->course = $r_cid;
    $r_qiz->coursemodule = 0;
    $r_qiz->instance = 0;
    $r_qiz->id = 0;
    $r_qiz->modulename = $r_mr->name;
    $r_qiz->module = $r_mr->id;
    $r_qiz->groupmembersonly = 0;
    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 = COMPLETION_TRACKING_NONE;
        $r_qiz->completionview = COMPLETION_VIEW_NOT_REQUIRED;
        $r_qiz->completiongradeitemnumber = null;
        $r_qiz->completionexpected = 0;
    }
    if ($CFG->enableavailability) {
        $r_qiz->availablefrom = 0;
        $r_qiz->availableuntil = 0;
        if ($r_qiz->availableuntil) {
            $r_qiz->availableuntil = strtotime("23:59:59", $r_qiz->availableuntil);
        }
        $r_qiz->showavailability = CONDITION_STUDENTVIEW_HIDE;
    }
    RWSSQDefs($r_qiz);
    if ($r_imp) {
        RWSIQSet($r_qiz, $r_sn, $r_sd, $r_ecd);
    }
    if (is_null($r_qiz->quizpassword) && !is_null($r_qiz->password)) {
        $r_qiz->quizpassword = $r_qiz->password;
    }
    $r_qzmi = add_course_module($r_qiz);
    if (!$r_qzmi) {
        RWSSErr("2077");
    }
    $r_qiz->coursemodule = $r_qzmi;
    $r_insi = quiz_add_instance($r_qiz);
    if (!$r_insi || is_string($r_insi)) {
        RWSSErr("2076");
    }
    $r_qiz->instance = $r_insi;
    if (respondusws_floatcompare($CFG->version, 2012120300, 2) >= 0) {
        $r_siu = course_add_cm_to_section($r_qiz->course, $r_qiz->coursemodule, $r_qiz->section);
    } else {
        $r_siu = add_mod_to_section($r_qiz);
    }
    if (!$r_siu) {
        RWSSErr("2078");
    }
    if (respondusws_floatcompare($CFG->version, 2012120300, 2) < 0) {
        $DB->set_field("course_modules", "section", $r_siu, array("id" => $r_qzmi));
    }
    if ($r_si !== false && $r_siu != $r_si) {
        RWSSErr("2078");
    }
    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 (respondusws_floatcompare($CFG->version, 2014051200, 2) >= 0) {
        $r_qiz->modname = $r_qiz->modulename;
        $r_qiz->id = $r_qiz->coursemodule;
        $r_evt = \core\event\course_module_created::create_from_cm($r_qiz);
        $r_evt->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_created", $r_evt);
    }
    rebuild_course_cache($r_cid);
    grade_regrade_final_grades($r_cid);
    RWSRHXml();
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
    echo "<addquiz>\r\n";
    echo "\t<name>";
    echo utf8_encode(htmlspecialchars(trim($r_qiz->name)));
    echo "</name>\r\n";
    echo "\t<id>";
    echo utf8_encode(htmlspecialchars(trim($r_qzmi)));
    echo "</id>\r\n";
    echo "\t<section_id>";
    echo utf8_encode(htmlspecialchars(trim($r_siu)));
    echo "</section_id>\r\n";
    echo "\t<writable>yes</writable>\r\n";
    if ($RWSLB->mex || $RWSLB->bex) {
        if ($RWSLB->mok) {
            if ($RWSLB->perr) {
                echo "\t<service_warning>3003</service_warning>\r\n";
            }
        } else {
            if ($RWSLB->bok) {
                if ($RWSLB->perr) {
                    echo "\t<service_warning>3003</service_warning>\r\n";
                }
            } else {
                echo "\t<service_warning>3001</service_warning>\r\n";
            }
        }
    } else {
        echo "\t<service_warning>3000</service_warning>\r\n";
    }
    echo "</addquiz>\r\n";
    exit;
}
Exemple #13
0
 /**
  * Called after the mod has set itself up, to finish off any course module settings
  * (set instance id, add to correct section, set visibility, etc.) and send the response
  *
  * @param int $instanceid id returned by the mod when it was created
  */
 protected function finish_setup_course_module($instanceid)
 {
     global $DB, $USER;
     if (!$instanceid) {
         // Something has gone wrong - undo everything we can.
         course_delete_module($this->cm->id);
         throw new moodle_exception('errorcreatingactivity', 'moodle', '', $this->module->name);
     }
     // Note the section visibility
     $visible = get_fast_modinfo($this->course)->get_section_info($this->section)->visible;
     $DB->set_field('course_modules', 'instance', $instanceid, array('id' => $this->cm->id));
     // Rebuild the course cache after update action
     rebuild_course_cache($this->course->id, true);
     $sectionid = course_add_cm_to_section($this->course, $this->cm->id, $this->section);
     set_coursemodule_visible($this->cm->id, $visible);
     if (!$visible) {
         $DB->set_field('course_modules', 'visibleold', 1, array('id' => $this->cm->id));
     }
     // retrieve the final info about this module.
     $info = get_fast_modinfo($this->course);
     if (!isset($info->cms[$this->cm->id])) {
         // The course module has not been properly created in the course - undo everything.
         course_delete_module($this->cm->id);
         throw new moodle_exception('errorcreatingactivity', 'moodle', '', $this->module->name);
     }
     $mod = $info->get_cm($this->cm->id);
     // Trigger course module created event.
     $event = \core\event\course_module_created::create(array('courseid' => $this->course->id, 'context' => context_module::instance($mod->id), 'objectid' => $mod->id, 'other' => array('modulename' => $mod->modname, 'name' => $mod->name, 'instanceid' => $instanceid)));
     $event->trigger();
     add_to_log($this->course->id, $mod->modname, "add", "view.php?id={$mod->id}", "{$instanceid}", $mod->id);
     $this->send_response($mod);
 }
foreach ($tasks as $task) {
    if (is_subclass_of($task, 'restore_activity_task')) {
        if ($task->get_old_contextid() == $cmcontext->id) {
            $newcmid = $task->get_moduleid();
            break;
        }
    }
}
// if we know the cmid of the new course module, let us move it
// right below the original one. otherwise it will stay at the
// end of the section
if ($newcmid) {
    $newcm = get_coursemodule_from_id('', $newcmid, $course->id, true, MUST_EXIST);
    moveto_module($newcm, $section, $cm);
    moveto_module($cm, $section, $newcm);
    // Trigger course module created event. We can trigger the event only if we know the newcmid.
    $event = \core\event\course_module_created::create_from_cm($newcm);
    $event->trigger();
}
$rc->destroy();
if (empty($CFG->keeptempdirectoriesonbackup)) {
    fulldelete($backupbasepath);
}
echo $output->header();
if ($newcmid) {
    echo $output->confirm(get_string('duplicatesuccess', 'core', $a), new single_button(new moodle_url('/course/modedit.php', array('update' => $newcmid, 'sr' => $sectionreturn)), get_string('duplicatecontedit'), 'get'), new single_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)), get_string('duplicatecontcourse'), 'get'));
} else {
    echo $output->notification(get_string('duplicatesuccess', 'core', $a), 'notifysuccess');
    echo $output->continue_button(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
}
echo $output->footer();