本文整理汇总了PHP中completion_info::get_data方法的典型用法代码示例。如果您正苦于以下问题:PHP completion_info::get_data方法的具体用法?PHP completion_info::get_data怎么用?PHP completion_info::get_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类completion_info
的用法示例。
在下文中一共展示了completion_info::get_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_update_activity_completion_status_manually
/**
* Test update_activity_completion_status_manually
*/
public function test_update_activity_completion_status_manually()
{
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id), array('completion' => 1));
$cm = get_coursemodule_from_id('data', $data->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->setUser($user);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, true);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_completion_external::update_activity_completion_status_manually_returns(), $result);
// Check in DB.
$this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate', array('coursemoduleid' => $data->cmid)));
// Check using the API.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
$this->assertTrue($result['status']);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, false);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_completion_external::update_activity_completion_status_manually_returns(), $result);
$this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate', array('coursemoduleid' => $data->cmid)));
$completiondata = $completion->get_data($cm);
$this->assertEquals(0, $completiondata->completionstate);
$this->assertTrue($result['status']);
}
示例2: section_activity_summary
/**
* Taken from /format/renderer.php
* Generate a summary of the activites in a section
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course the course record from DB
* @param array $mods (argument not used)
* @return string HTML to output.
*/
public static function section_activity_summary($section, $course, $mods)
{
global $CFG;
require_once $CFG->libdir . '/completionlib.php';
$modinfo = get_fast_modinfo($course);
if (empty($modinfo->sections[$section->section])) {
return '';
}
// Generate array with count of activities in this section.
$sectionmods = array();
$total = 0;
$complete = 0;
$cancomplete = isloggedin() && !isguestuser();
$completioninfo = new completion_info($course);
foreach ($modinfo->sections[$section->section] as $cmid) {
$thismod = $modinfo->cms[$cmid];
if ($thismod->uservisible) {
if (isset($sectionmods[$thismod->modname])) {
$sectionmods[$thismod->modname]['name'] = $thismod->modplural;
$sectionmods[$thismod->modname]['count']++;
} else {
$sectionmods[$thismod->modname]['name'] = $thismod->modfullname;
$sectionmods[$thismod->modname]['count'] = 1;
}
if ($cancomplete && $completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
$total++;
$completiondata = $completioninfo->get_data($thismod, true);
if ($completiondata->completionstate == COMPLETION_COMPLETE || $completiondata->completionstate == COMPLETION_COMPLETE_PASS) {
$complete++;
}
}
}
}
if (empty($sectionmods)) {
// No sections.
return '';
}
// Output section activities summary.
$o = '';
$o .= "<div class='section-summary-activities mdl-right'>";
foreach ($sectionmods as $mod) {
$o .= "<span class='activity-count'>";
$o .= $mod['name'] . ': ' . $mod['count'];
$o .= "</span>";
}
$o .= "</div>";
$a = false;
// Output section completion data.
if ($total > 0) {
$a = new stdClass();
$a->complete = $complete;
$a->total = $total;
$a->percentage = $complete / $total * 100;
$o .= "<div class='section-summary-activities mdl-right'>";
$o .= "<span class='activity-count'>" . get_string('progresstotal', 'completion', $a) . "</span>";
$o .= "</div>";
}
$retobj = (object) array('output' => $o, 'progress' => $a, 'complete' => $complete, 'total' => $total);
return $retobj;
}
示例3: is_available
public function is_available($not, \core_availability\info $info, $grabthelot, $userid)
{
$modinfo = $info->get_modinfo();
$completion = new \completion_info($modinfo->get_course());
if (!array_key_exists($this->cmid, $modinfo->cms)) {
// If the cmid cannot be found, always return false regardless
// of the condition or $not state. (Will be displayed in the
// information message.)
$allow = false;
} else {
// The completion system caches its own data so no caching needed here.
$completiondata = $completion->get_data((object) array('id' => $this->cmid), $grabthelot, $userid, $modinfo);
$allow = true;
if ($this->expectedcompletion == COMPLETION_COMPLETE) {
// Complete also allows the pass, fail states.
switch ($completiondata->completionstate) {
case COMPLETION_COMPLETE:
case COMPLETION_COMPLETE_FAIL:
case COMPLETION_COMPLETE_PASS:
break;
default:
$allow = false;
}
} else {
// Other values require exact match.
if ($completiondata->completionstate != $this->expectedcompletion) {
$allow = false;
}
}
if ($not) {
$allow = !$allow;
}
}
return $allow;
}
示例4: navbuttons_activity_showbuttons
/**
* Check if an activity is configured to only show navbuttons when
* complete and then check if the activity is complete
* @param cm_info $cm the course module for the activity
* @return boolean true if the navbuttons should be shown
*/
function navbuttons_activity_showbuttons($cm)
{
$modname = $cm->modname;
$show = get_config('block_navbuttons', 'activity' . $modname);
if ($show === false || $show == NAVBUTTONS_ACTIVITY_ALWAYS) {
return true;
// No config or 'always show'
}
if ($show == NAVBUTTONS_ACTIVITY_NEVER) {
return false;
}
if ($show == NAVBUTTONS_ACTIVITY_COMPLETE) {
$completion = new completion_info($cm->get_course());
if (!$completion->is_enabled($cm)) {
return true;
// No completion tracking - show the buttons
}
$cmcompletion = $completion->get_data($cm);
if ($cmcompletion->completionstate == COMPLETION_INCOMPLETE) {
return false;
}
return true;
}
if (!isloggedin() || isguestuser()) {
return true;
// Always show the buttons if not logged in
}
// NAVBUTTONS_ACTIVITY_CUSTOM
$funcname = 'navbuttons_mod_' . $modname . '_showbuttons';
if (!function_exists($funcname)) {
return true;
// Shouldn't have got to here, but allow the buttons anyway
}
return $funcname($cm);
}
示例5: test_survey_view
/**
* Test survey_view
* @return void
*/
public function test_survey_view()
{
global $CFG;
$CFG->enablecompletion = 1;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id), array('completion' => 2, 'completionview' => 1));
$context = context_module::instance($survey->cmid);
$cm = get_coursemodule_from_instance('survey', $survey->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
survey_view($survey, $course, $cm, $context, 'form');
$events = $sink->get_events();
// 2 additional events thanks to completion.
$this->assertCount(3, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_survey\\event\\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/survey/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEquals('form', $event->other['viewed']);
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Check completion status.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
}
示例6: test_glossary_view
public function test_glossary_view()
{
global $CFG;
$origcompletion = $CFG->enablecompletion;
$CFG->enablecompletion = true;
$this->resetAfterTest(true);
// Generate all the things.
$c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1));
$g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => 1));
$u1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
$modinfo = course_modinfo::instance($c1->id);
$cm1 = $modinfo->get_cm($g1->cmid);
$cm2 = $modinfo->get_cm($g2->cmid);
$ctx1 = $cm1->context;
$completion = new completion_info($c1);
$this->setUser($u1);
// Confirm what we've set up.
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
// Simulate the view call.
$sink = $this->redirectEvents();
glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
$events = $sink->get_events();
// Assertions.
$this->assertCount(3, $events);
$this->assertEquals('\\core\\event\\course_module_completion_updated', $events[0]->eventname);
$this->assertEquals('\\core\\event\\course_module_completion_updated', $events[1]->eventname);
$this->assertEquals('\\mod_glossary\\event\\course_module_viewed', $events[2]->eventname);
$this->assertEquals($g1->id, $events[2]->objectid);
$this->assertEquals('letter', $events[2]->other['mode']);
$this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
// Tear down.
$sink->close();
$CFG->enablecompletion = $origcompletion;
}
示例7: get_activity_tree
/**
* get_activity_tree :: course_modinfo -> integer -> context -> array
* @param \course_modinfo $modinfo
* @param integer $section_number
* @param \context $context
* @return array
*/
function get_activity_tree(\course_modinfo $modinfo, $section_number, \context $context)
{
$completion_info = new \completion_info($modinfo->get_course());
return F\map(F\filter($modinfo->get_section_info_all(), function (\section_info $section_info) {
return $section_info->visible;
}), function (\section_info $section_info) use($completion_info, $section_number, $context) {
$mods = F\map(F\filter($section_info->modinfo->cms, function (\cm_info $cm_info) use($section_info) {
global $CFG;
return ($cm_info->uservisible || $CFG->enableavailability && !empty($cm_info->availableinfo)) && (int) $cm_info->sectionnum === (int) $section_info->section;
}), function (\cm_info $cm_info) use($completion_info, $context) {
global $CFG;
$canComplete = $CFG->enablecompletion && isloggedin() && !isguestuser() && (int) $completion_info->is_enabled($cm_info) === COMPLETION_TRACKING_MANUAL;
$hasCompleted = false;
if ($canComplete) {
$completion_data = $completion_info->get_data($cm_info, true);
$hasCompleted = F\contains([COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS], (int) $completion_data->completionstate);
}
return (object) ['id' => (int) $cm_info->id, 'name' => $cm_info->name, 'modname' => $cm_info->modname, 'current' => is_current_mod($cm_info, $context), 'available' => $cm_info->available, 'canComplete' => $canComplete, 'hasCompleted' => $hasCompleted];
});
return (object) ['id' => (int) $section_info->id, 'section' => (int) $section_info->section, 'name' => \get_section_name($section_info->modinfo->courseid, $section_info->section), 'current' => is_current_section($section_info, $section_number, $context), 'activities' => array_values($mods)];
});
}
示例8: course_section_cm_completion
/**
* Renders html for completion box on course page
*
* If completion is disabled, returns empty string
* If completion is automatic, returns an icon of the current completion state
* If completion is manual, returns a form (with an icon inside) that allows user to
* toggle completion
*
* @param stdClass $course course object
* @param completion_info $completioninfo completion info for the course, it is recommended
* to fetch once for all modules in course/section for performance
* @param cm_info $mod module to show completion for
* @param array $displayoptions display options, not used in core
* @return string
*/
public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array())
{
global $CFG;
$output = '';
if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
return $output;
}
if ($completioninfo === null) {
$completioninfo = new completion_info($course);
}
$completion = $completioninfo->is_enabled($mod);
if ($completion == COMPLETION_TRACKING_NONE) {
if ($this->page->user_is_editing()) {
$output .= html_writer::span(' ', 'filler');
}
return $output;
}
$completiondata = $completioninfo->get_data($mod, true);
$completionicon = '';
if ($this->page->user_is_editing()) {
switch ($completion) {
case COMPLETION_TRACKING_MANUAL:
$completionicon = 'manual-enabled';
break;
case COMPLETION_TRACKING_AUTOMATIC:
$completionicon = 'auto-enabled';
break;
}
} else {
if ($completion == COMPLETION_TRACKING_MANUAL) {
switch ($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'manual-n';
break;
case COMPLETION_COMPLETE:
$completionicon = 'manual-y';
break;
}
} else {
// Automatic
switch ($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'auto-n';
break;
case COMPLETION_COMPLETE:
$completionicon = 'auto-y';
break;
case COMPLETION_COMPLETE_PASS:
$completionicon = 'auto-pass';
break;
case COMPLETION_COMPLETE_FAIL:
$completionicon = 'auto-fail';
break;
}
}
}
if ($completionicon) {
$formattedname = $mod->get_formatted_name();
$imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
if ($this->page->user_is_editing()) {
// When editing, the icon is just an image.
$completionpixicon = new pix_icon('i/completion-' . $completionicon, $imgalt, '', array('title' => $imgalt, 'class' => 'iconsmall'));
$output .= html_writer::tag('span', $this->output->render($completionpixicon), array('class' => 'autocompletion'));
} else {
if ($completion == COMPLETION_TRACKING_MANUAL) {
$imgtitle = get_string('completion-title-' . $completionicon, 'completion', $formattedname);
$newstate = $completiondata->completionstate == COMPLETION_COMPLETE ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
// In manual mode the icon is a toggle form...
// If this completion state is used by the
// conditional activities system, we need to turn
// off the JS.
$extraclass = '';
if (!empty($CFG->enableavailability) && core_availability\info::completion_value_used($course, $mod->id)) {
$extraclass = ' preventjs';
}
$output .= html_writer::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/course/togglecompletion.php'), 'class' => 'togglecompletion' . $extraclass));
$output .= html_writer::start_tag('div');
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'modulename', 'value' => $mod->name));
$output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
$output .= html_writer::empty_tag('input', array('type' => 'image', 'src' => $this->output->pix_url('i/completion-' . $completionicon), 'alt' => $imgalt, 'title' => $imgtitle, 'aria-live' => 'polite'));
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
} else {
//.........这里部分代码省略.........
示例9: execute
/**
* Performs the synchronisation of grades.
*
* @return bool|void
*/
public function execute()
{
global $DB, $CFG;
require_once $CFG->dirroot . '/enrol/lti/ims-blti/OAuth.php';
require_once $CFG->dirroot . '/enrol/lti/ims-blti/OAuthBody.php';
require_once $CFG->dirroot . '/lib/completionlib.php';
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/grade/querylib.php';
// Check if the authentication plugin is disabled.
if (!is_enabled_auth('lti')) {
mtrace('Skipping task - ' . get_string('pluginnotenabled', 'auth', get_string('pluginname', 'auth_lti')));
return true;
}
// Check if the enrolment plugin is disabled - isn't really necessary as the task should not run if
// the plugin is disabled, but there is no harm in making sure core hasn't done something wrong.
if (!enrol_is_enabled('lti')) {
mtrace('Skipping task - ' . get_string('enrolisdisabled', 'enrol_lti'));
return true;
}
// Get all the enabled tools.
if ($tools = \enrol_lti\helper::get_lti_tools(array('status' => ENROL_INSTANCE_ENABLED, 'gradesync' => 1))) {
foreach ($tools as $tool) {
mtrace("Starting - Grade sync for shared tool '{$tool->id}' for the course '{$tool->courseid}'.");
// Variables to keep track of information to display later.
$usercount = 0;
$sendcount = 0;
// We check for all the users - users can access the same tool from different consumers.
if ($ltiusers = $DB->get_records('enrol_lti_users', array('toolid' => $tool->id), 'lastaccess DESC')) {
$completion = new \completion_info(get_course($tool->courseid));
foreach ($ltiusers as $ltiuser) {
$mtracecontent = "for the user '{$ltiuser->userid}' in the tool '{$tool->id}' for the course " . "'{$tool->courseid}'";
$usercount = $usercount + 1;
// Check if we do not have a serviceurl - this can happen if the sync process has an unexpected error.
if (empty($ltiuser->serviceurl)) {
mtrace("Skipping - Empty serviceurl {$mtracecontent}.");
continue;
}
// Check if we do not have a sourceid - this can happen if the sync process has an unexpected error.
if (empty($ltiuser->sourceid)) {
mtrace("Skipping - Empty sourceid {$mtracecontent}.");
continue;
}
// Need a valid context to continue.
if (!($context = \context::instance_by_id($tool->contextid))) {
mtrace("Failed - Invalid contextid '{$tool->contextid}' for the tool '{$tool->id}'.");
continue;
}
// Ok, let's get the grade.
$grade = false;
if ($context->contextlevel == CONTEXT_COURSE) {
// Check if the user did not completed the course when it was required.
if ($tool->gradesynccompletion && !$completion->is_course_complete($ltiuser->userid)) {
mtrace("Skipping - Course not completed {$mtracecontent}.");
continue;
}
// Get the grade.
if ($grade = grade_get_course_grade($ltiuser->userid, $tool->courseid)) {
$grademax = floatval($grade->item->grademax);
$grade = $grade->grade;
}
} else {
if ($context->contextlevel == CONTEXT_MODULE) {
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
if ($tool->gradesynccompletion) {
$data = $completion->get_data($cm, false, $ltiuser->userid);
if ($data->completionstate != COMPLETION_COMPLETE_PASS && $data->completionstate != COMPLETION_COMPLETE) {
mtrace("Skipping - Activity not completed {$mtracecontent}.");
continue;
}
}
$grades = grade_get_grades($cm->course, 'mod', $cm->modname, $cm->instance, $ltiuser->userid);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
if (!empty($grade->item)) {
$grademax = floatval($grade->item->grademax);
} else {
$grademax = floatval($grades->items[0]->grademax);
}
$grade = $grade->grade;
}
}
}
if ($grade === false || $grade === null || strlen($grade) < 1) {
mtrace("Skipping - Invalid grade {$mtracecontent}.");
continue;
}
// No need to be dividing by zero.
if (empty($grademax)) {
mtrace("Skipping - Invalid grade {$mtracecontent}.");
continue;
}
// This can happen if the sync process has an unexpected error.
if ($grade == $ltiuser->lastgrade) {
mtrace("Not sent - The grade {$mtracecontent} was not sent as the grades are the same.");
continue;
//.........这里部分代码省略.........
示例10: course_completion_progress
/**
* Get course completion progress for specific course.
* NOTE: It is by design that even teachers get course completion progress, this is so that they see exactly the
* same as a student would in the personal menu.
*
* @param $course
* @return stdClass | null
*/
public static function course_completion_progress($course)
{
if (!isloggedin() || isguestuser()) {
return null;
// Can't get completion progress for users who aren't logged in.
}
// Security check - are they enrolled on course.
$context = \context_course::instance($course->id);
if (!is_enrolled($context, null, '', true)) {
return null;
}
$completioninfo = new \completion_info($course);
$trackcount = 0;
$compcount = 0;
if ($completioninfo->is_enabled()) {
$modinfo = get_fast_modinfo($course);
foreach ($modinfo->cms as $thismod) {
if (!$thismod->uservisible) {
// Skip when mod is not user visible.
continue;
}
$completioninfo->get_data($thismod, true);
if ($completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
$trackcount++;
$completiondata = $completioninfo->get_data($thismod, true);
if ($completiondata->completionstate == COMPLETION_COMPLETE || $completiondata->completionstate == COMPLETION_COMPLETE_PASS) {
$compcount++;
}
}
}
}
$compobj = (object) array('complete' => $compcount, 'total' => $trackcount, 'progresshtml' => '');
if ($trackcount > 0) {
$progress = get_string('progresstotal', 'completion', $compobj);
// TODO - we should be putting our HTML in a renderer.
$progressinfo = '<div class="completionstatus outoftotal">' . $progress . '</div>';
$compobj->progresshtml = $progressinfo;
}
return $compobj;
}
示例11: array
function test_get_data() {
global $DB, $SESSION;
$c = new completion_info((object)array('id'=>42));
$cm = (object)array('id'=>13, 'course'=>42);
// 1. Not current user, record exists
$sillyrecord = (object)array('frog'=>'kermit');
/** @var $DB PHPUnit_Framework_MockObject_MockObject */
$DB->expects($this->at(0))
->method('get_record')
->with('course_modules_completion', array('coursemoduleid'=>13,'userid'=>123))
->will($this->returnValue($sillyrecord));
$result = $c->get_data($cm,false,123);
$this->assertEquals($sillyrecord, $result);
$this->assertTrue(empty($SESSION->completioncache));
// 2. Not current user, default record, wholecourse (ignored)
$DB->expects($this->at(0))
->method('get_record')
->with('course_modules_completion', array('coursemoduleid'=>13,'userid'=>123))
->will($this->returnValue(false));
$result=$c->get_data($cm,true,123);
$this->assertEquals((object)array(
'id'=>'0','coursemoduleid'=>13,'userid'=>123,'completionstate'=>0,
'viewed'=>0,'timemodified'=>0),$result);
$this->assertTrue(empty($SESSION->completioncache));
// 3. Current user, single record, not from cache
$DB->expects($this->at(0))
->method('get_record')
->with('course_modules_completion', array('coursemoduleid'=>13,'userid'=>314159))
->will($this->returnValue($sillyrecord));
$result = $c->get_data($cm);
$this->assertEquals($sillyrecord, $result);
$this->assertEquals($sillyrecord, $SESSION->completioncache[42][13]);
// When checking time(), allow for second overlaps
$this->assertTrue(time()-$SESSION->completioncache[42]['updated']<2);
// 4. Current user, 'whole course', but from cache
$result = $c->get_data($cm, true);
$this->assertEquals($sillyrecord, $result);
// 5. Current user, single record, cache expired
$SESSION->completioncache[42]['updated']=37; // Quite a long time ago
$now = time();
$SESSION->completioncache[17]['updated']=$now;
$SESSION->completioncache[39]['updated']=72; // Also a long time ago
$DB->expects($this->at(0))
->method('get_record')
->with('course_modules_completion', array('coursemoduleid'=>13,'userid'=>314159))
->will($this->returnValue($sillyrecord));
$result = $c->get_data($cm, false);
$this->assertEquals($sillyrecord, $result);
// Check that updated value is right, then fudge it to make next compare
// work
$this->assertTrue(time()-$SESSION->completioncache[42]['updated']<2);
$SESSION->completioncache[42]['updated']=$now;
// Check things got expired from cache
$this->assertEquals(array(42=>array(13=>$sillyrecord, 'updated'=>$now), 17=>array('updated'=>$now)), $SESSION->completioncache);
// 6. Current user, 'whole course' and record not in cache
unset($SESSION->completioncache);
// Scenario: Completion data exists for one CMid
$basicrecord = (object)array('coursemoduleid'=>13);
$DB->expects($this->at(0))
->method('get_records_sql')
->will($this->returnValue(array('1'=>$basicrecord)));
/*
$DB->expectAt(0,'get_records_sql',array(new IgnoreWhitespaceExpectation("
SELECT
cmc.*
FROM
{course_modules} cm
INNER JOIN {course_modules_completion} cmc ON cmc.coursemoduleid=cm.id
WHERE
cm.course=? AND cmc.userid=?"),array(42,314159)));
*/
// There are two CMids in total, the one we had data for and another one
$modinfo = new stdClass();
$modinfo->cms = array((object)array('id'=>13), (object)array('id'=>14));
$result = $c->get_data($cm, true, 0, $modinfo);
// Check result
$this->assertEquals($basicrecord, $result);
// Check the cache contents
$this->assertTrue(time()-$SESSION->completioncache[42]['updated']<2);
$SESSION->completioncache[42]['updated'] = $now;
$this->assertEquals(array(42=>array(13=>$basicrecord, 14=>(object)array(
'id'=>'0', 'coursemoduleid'=>14, 'userid'=>314159, 'completionstate'=>0,
'viewed'=>0, 'timemodified'=>0), 'updated'=>$now)), $SESSION->completioncache);
}
示例12: review
/**
* Review this criteria and decide if the user has completed
*
* @param completion_completion $completion The user's completion record
* @param bool $mark Optionally set false to not save changes to database
* @return bool
*/
public function review($completion, $mark = true)
{
global $DB;
$course = $DB->get_record('course', array('id' => $completion->course));
$cm = $DB->get_record('course_modules', array('id' => $this->moduleinstance));
$info = new completion_info($course);
$data = $info->get_data($cm, false, $completion->userid);
// If the activity is complete
if (in_array($data->completionstate, array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL))) {
if ($mark) {
$completion->mark_complete();
}
return true;
}
return false;
}
示例13: test_wiki_page_view
/**
* Test wiki_page_view.
*
* @return void
*/
public function test_wiki_page_view()
{
global $CFG;
$CFG->enablecompletion = COMPLETION_ENABLED;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED));
$options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED);
$wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options);
$context = context_module::instance($wiki->cmid);
$cm = get_coursemodule_from_instance('wiki', $wiki->id);
$firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($wiki);
// Trigger and capture the event.
$sink = $this->redirectEvents();
wiki_page_view($wiki, $firstpage, $course, $cm, $context);
$events = $sink->get_events();
// 2 additional events thanks to completion.
$this->assertCount(3, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_wiki\\event\\page_viewed', $event);
$this->assertEquals($context, $event->get_context());
$pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $firstpage->id));
$this->assertEquals($pageurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Check completion status.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
}
示例14: test_course_completion
public function test_course_completion()
{
global $DB;
$this->resetAfterTest();
// Enable avaibility.
// If not enabled all conditional fields will be ignored.
set_config('enableavailability', 1);
// Enable course completion.
// If not enabled all completion settings will be ignored.
set_config('enablecompletion', COMPLETION_ENABLED);
$generator = $this->getDataGenerator();
// Create course with completion tracking enabled.
$course = $generator->create_course(['enablecompletion' => 1, 'numsections' => 3], ['createsections' => true]);
// Enrol user to completion tracking course.
$sturole = $DB->get_record('role', array('shortname' => 'student'));
$generator->enrol_user($this->user1->id, $course->id, $sturole->id);
// Create page with completion marked on view.
$page1 = $generator->create_module('page', array('course' => $course->id, 'name' => 'page1 complete on view'), array('completion' => 2, 'completionview' => 1));
$modinfo = get_fast_modinfo($course);
$page1cm = $modinfo->get_cm($page1->cmid);
// Create page restricted to only show when first page is viewed.
$moduleinfo = (object) [];
$moduleinfo->course = $course->id;
$moduleinfo->name = 'page2 available after page1 viewed';
$moduleinfo->availability = json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&'));
$page2 = $generator->create_module('page', $moduleinfo);
// Make section 2 restricted to only show when first page is viewed.
$section = $modinfo->get_section_info(2);
$sectionupdate = ['id' => $section->id, 'availability' => json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&'))];
$DB->update_record('course_sections', $sectionupdate);
// Check user1 has expected unavailable section and mod.
$this->setUser($this->user1);
// Dump cache and reget modinfo.
get_fast_modinfo($course, 0, true);
$modinfo = get_fast_modinfo($course);
$page2cm = $modinfo->get_cm($page2->cmid);
list($previouslyunavailablesections, $previouslyunavailablemods) = local::conditionally_unavailable_elements($course);
$this->assertContains(2, $previouslyunavailablesections);
$this->assertContains($page2cm->id, $previouslyunavailablemods);
// View page1 to trigger completion
$context = context_module::instance($page1->cmid);
page_view($page1, $course, $page1cm, $context);
$completion = new completion_info($course);
$completiondata = $completion->get_data($page1cm);
$this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
get_fast_modinfo($course, 0, true);
// Reset modinfo.
// Make sure that unavailable sections and mods no longer contain the ones requiring availabililty criteria
// satisfying.
list($unavailablesections, $unavailablemods) = local::conditionally_unavailable_elements($course);
$this->assertNotContains($page2cm->id, $unavailablemods);
$this->assertNotContains(2, $unavailablesections);
$result = $this->courseservice->course_completion($course->shortname, $previouslyunavailablesections, $previouslyunavailablemods);
// Make sure that the second page module (which is now newly available) appears in the list of newly available
// module html.
$this->assertTrue(isset($result['newlyavailablemodhtml'][$page2->cmid]));
// Make sure that the second section (which is now wnely available) appears in the list of newly available
// section html.
$this->assertTrue(isset($result['newlyavailablesectionhtml'][2]));
}
示例15: slides_make_outline
function slides_make_outline($course,$topics_info,$sections,$editing){
// TOPICS OUTLINE list
echo '<li id="section--1" class="section outline clearfix" >';
echo "<ul id='section-outline'>\n";
// check activity completion and add to array where section id is key
$completionbysection = array();
$completioninfo = new completion_info($course);
$activitieswithcompletion = $completioninfo->get_activities();
// unknown > 0 = incomplete < complete
foreach($activitieswithcompletion as $activity){
$completionbysection[$activity->section] = "complete";
$activity_status_data = $completioninfo->get_data($activity->id, $USER->id);
if($activity_status_data->completionstate < 0) {
$completionbysection[$activity->section] = "unknown";
} else if ($activity_status_data->completionstate === 0 && $completionbysection[$activity->section] != "unknown") {
$completionbysection[$activity->section] = "incomplete";
}
}
//sort by id
for($i=0; $i<=$course->numsections; $i++) {
$section = $sections[$i];
$section_complete = isset($completionbysection[$section->id]) ? $completionbysection[$section->id] : "complete";
$sectionname = !empty($section->name) ? $section->name : "Topic " . $section->section;
$style = "left:" . $topics_info[$section->id]->x_offset . "; top:" .$topics_info[$section->id]->y_offset;
$css = $editing ? "$section_complete editing" : "$section_complete";
$css .= $section->visible ? "" : " hidden";
$css .= $course->marker == $i ? " highlight" : "";
echo '<li class="'. $css . '" id="topiclink' . $section->id . '" style="' .$style . ';">';
echo '<a href="view.php?id='.$course->id.'&topic='.$section->section.'" title="'.$sectionname.'" class="outline-link">' .$sectionname;
// echo "</a> $complete</li>\n";
// TODO: Fix complete
echo "</a></li>\n";
}
echo "</ul>\n";
echo "</li>\n";
}