case 'move':
require_capability('moodle/course:movesections', $coursecontext);
move_section_to($course, $id, $value);
// See if format wants to do something about it
$response = course_get_format($course)->ajax_section_move();
if ($response !== null) {
echo json_encode($response);
}
break;
}
break;
case 'resource':
switch ($field) {
case 'visible':
require_capability('moodle/course:activityvisibility', $modcontext);
set_coursemodule_visible($cm->id, $value);
\core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
break;
case 'duplicate':
require_capability('moodle/course:manageactivities', $modcontext);
require_capability('moodle/backup:backuptargetimport', $modcontext);
require_capability('moodle/restore:restoretargetimport', $modcontext);
if (!course_allowed_module($course, $cm->modname)) {
throw new moodle_exception('No permission to create that activity');
}
$sr = optional_param('sr', null, PARAM_INT);
$result = mod_duplicate_activity($course, $cm, $sr);
echo json_encode($result);
break;
case 'groupmode':
require_capability('moodle/course:manageactivities', $modcontext);
/**
* Move the module object $mod to the specified $section
* If $beforemod exists then that is the module
* before which $modid should be inserted
* All parameters are objects
*/
function moveto_module($mod, $section, $beforemod = NULL)
{
global $DB, $OUTPUT;
/// Remove original module from original section
if (!delete_mod_from_section($mod->id, $mod->section)) {
echo $OUTPUT->notification("Could not delete module from existing section");
}
/// Update module itself if necessary
if ($mod->section != $section->id) {
$mod->section = $section->id;
$DB->update_record("course_modules", $mod);
// if moving to a hidden section then hide module
if (!$section->visible) {
set_coursemodule_visible($mod->id, 0);
}
}
/// Add the module into the new section
$mod->course = $section->course;
$mod->section = $section->section;
// need relative reference
$mod->coursemodule = $mod->id;
if (!add_mod_to_section($mod, $beforemod)) {
return false;
}
return true;
}
开发者ID:numbas,项目名称:moodle,代码行数:32,代码来源:lib.php
示例5: test_blank_hidden_total_and_adjust_bounds
/**
* Tests grade_report::blank_hidden_total_and_adjust_bounds()
*/
public function test_blank_hidden_total_and_adjust_bounds()
{
global $DB;
$this->resetAfterTest(true);
$student = $this->getDataGenerator()->create_user();
$this->setUser($student);
// Create a course and two activities.
// One activity will be hidden.
$course = $this->getDataGenerator()->create_course();
$coursegradeitem = grade_item::fetch_course_item($course->id);
$coursecontext = context_course::instance($course->id);
$data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$datacm = get_coursemodule_from_id('data', $data->cmid);
$forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$forumcm = get_coursemodule_from_id('forum', $forum->cmid);
// Insert student grades for the two activities.
$gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
$datagrade = 50;
$grade_grade = new grade_grade();
$grade_grade->itemid = $gi->id;
$grade_grade->userid = $student->id;
$grade_grade->rawgrade = $datagrade;
$grade_grade->finalgrade = $datagrade;
$grade_grade->rawgrademax = 100;
$grade_grade->rawgrademin = 0;
$grade_grade->timecreated = time();
$grade_grade->timemodified = time();
$grade_grade->insert();
$gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
$forumgrade = 70;
$grade_grade = new grade_grade();
$grade_grade->itemid = $gi->id;
$grade_grade->userid = $student->id;
$grade_grade->rawgrade = $forumgrade;
$grade_grade->finalgrade = $forumgrade;
$grade_grade->rawgrademax = 100;
$grade_grade->rawgrademin = 0;
$grade_grade->timecreated = time();
$grade_grade->timemodified = time();
$grade_grade->insert();
// Hide the database activity.
set_coursemodule_visible($datacm->id, 0);
$gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
$report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
// Should return the supplied student total grade regardless of hiding.
$report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN);
$result = $report->blank_hidden_total_and_adjust_bounds($course->id, $coursegradeitem, $datagrade + $forumgrade);
$this->assertEquals(array('grade' => $datagrade + $forumgrade, 'grademax' => $coursegradeitem->grademax, 'grademin' => $coursegradeitem->grademin, 'aggregationstatus' => 'unknown', 'aggregationweight' => null), $result);
// Should blank the student total as course grade depends on a hidden item.
$report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN);
$result = $report->blank_hidden_total_and_adjust_bounds($course->id, $coursegradeitem, $datagrade + $forumgrade);
$this->assertEquals(array('grade' => null, 'grademax' => $coursegradeitem->grademax, 'grademin' => $coursegradeitem->grademin, 'aggregationstatus' => 'unknown', 'aggregationweight' => null), $result);
// Should return the course total minus the hidden database activity grade.
$report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN);
$result = $report->blank_hidden_total_and_adjust_bounds($course->id, $coursegradeitem, $datagrade + $forumgrade);
$this->assertEquals(array('grade' => floatval($forumgrade), 'grademax' => $coursegradeitem->grademax, 'grademin' => $coursegradeitem->grademin, 'aggregationstatus' => 'unknown', 'aggregationweight' => null), $result);
// Note: we cannot simply hide modules and call $report->blank_hidden_total() again.
// It stores grades in a static variable so $report->blank_hidden_total() will return incorrect totals
// In practice this isn't a problem. Grade visibility isn't altered mid-request outside of the unit tests.
// Add a second course to test:
// 1) How a course with no visible activities behaves.
// 2) That $report->blank_hidden_total() correctly moves on to the new course.
$course = $this->getDataGenerator()->create_course();
$coursegradeitem = grade_item::fetch_course_item($course->id);
$coursecontext = context_course::instance($course->id);
$data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$datacm = get_coursemodule_from_id('data', $data->cmid);
$forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$forumcm = get_coursemodule_from_id('forum', $forum->cmid);
$gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
$datagrade = 50;
$grade_grade = new grade_grade();
$grade_grade->itemid = $gi->id;
$grade_grade->userid = $student->id;
$grade_grade->rawgrade = $datagrade;
$grade_grade->finalgrade = $datagrade;
$grade_grade->rawgrademax = 100;
$grade_grade->rawgrademin = 0;
$grade_grade->timecreated = time();
$grade_grade->timemodified = time();
$grade_grade->insert();
$gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
$forumgrade = 70;
$grade_grade = new grade_grade();
$grade_grade->itemid = $gi->id;
$grade_grade->userid = $student->id;
$grade_grade->rawgrade = $forumgrade;
$grade_grade->finalgrade = $forumgrade;
$grade_grade->rawgrademax = 100;
$grade_grade->rawgrademin = 0;
$grade_grade->timecreated = time();
$grade_grade->timemodified = time();
$grade_grade->insert();
// Hide both activities.
set_coursemodule_visible($datacm->id, 0);
set_coursemodule_visible($forumcm->id, 0);
$gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
//.........这里部分代码省略.........
/**
* Move the module object $mod to the specified $section
* If $beforemod exists then that is the module
* before which $modid should be inserted
*
* @param stdClass|cm_info $mod
* @param stdClass|section_info $section
* @param int|stdClass $beforemod id or object with field id corresponding to the module
* before which the module needs to be included. Null for inserting in the
* end of the section
* @return int new value for module visibility (0 or 1)
*/
function moveto_module($mod, $section, $beforemod = NULL)
{
global $OUTPUT, $DB;
// Current module visibility state - return value of this function.
$modvisible = $mod->visible;
// Remove original module from original section.
if (!delete_mod_from_section($mod->id, $mod->section)) {
echo $OUTPUT->notification("Could not delete module from existing section");
}
// If moving to a hidden section then hide module.
if ($mod->section != $section->id) {
if (!$section->visible && $mod->visible) {
// Module was visible but must become hidden after moving to hidden section.
$modvisible = 0;
set_coursemodule_visible($mod->id, 0);
// Set visibleold to 1 so module will be visible when section is made visible.
$DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
}
if ($section->visible && !$mod->visible) {
// Hidden module was moved to the visible section, restore the module visibility from visibleold.
set_coursemodule_visible($mod->id, $mod->visibleold);
$modvisible = $mod->visibleold;
}
}
// Add the module into the new section.
course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
return $modvisible;
}
function moveto_module($mod, $section, $beforemod = NULL)
{
/// All parameters are objects
/// Move the module object $mod to the specified $section
/// If $beforemod exists then that is the module
/// before which $modid should be inserted
/// Remove original module from original section
if (!delete_mod_from_section($mod->id, $mod->section)) {
notify("Could not delete module from existing section");
}
/// Update module itself if necessary
if ($mod->section != $section->id) {
$mod->section = $section->id;
if (!update_record("course_modules", $mod)) {
return false;
}
// if moving to a hidden section then hide module
if (!$section->visible) {
set_coursemodule_visible($mod->id, 0);
}
}
/// Add the module into the new section
$mod->course = $section->course;
$mod->section = $section->section;
// need relative reference
$mod->coursemodule = $mod->id;
if (!add_mod_to_section($mod, $beforemod)) {
return false;
}
return true;
}
/**
* 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);
}
/**
* 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);
//.........这里部分代码省略.........
请发表评论