/** * Test get_course_contents */ public function test_get_course_contents() { $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forumcm = get_coursemodule_from_id('forum', $forum->cmid); $forumcontext = context_module::instance($forum->cmid); $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id)); $datacontext = context_module::instance($data->cmid); $datacm = get_coursemodule_from_instance('page', $data->id); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); $pagecontext = context_module::instance($page->cmid); $pagecm = get_coursemodule_from_instance('page', $page->id); // Set the required capabilities by the external function. $context = context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:view', $context->id); $this->assignUserCapability('moodle/course:update', $context->id, $roleid); $courses = core_course_external::get_course_contents($course->id, array()); // We need to execute the return values cleaning process to simulate the web service server. $courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses); // Check that the course has the 3 created modules $this->assertEquals(3, count($courses[0]['modules'])); }
/** * Test get_course_contents */ public function test_get_course_contents() { $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); $forumdescription = 'This is the forum description'; $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'intro' => $forumdescription), array('showdescription' => true)); $forumcm = get_coursemodule_from_id('forum', $forum->cmid); $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id)); $datacm = get_coursemodule_from_instance('page', $data->id); $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); $pagecm = get_coursemodule_from_instance('page', $page->id); $labeldescription = 'This is a very long label to test if more than 50 characters are returned. So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.'; $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id, 'intro' => $labeldescription)); $labelcm = get_coursemodule_from_instance('label', $label->id); $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id, 'name' => 'URL: % & $ ../')); // Set the required capabilities by the external function. $context = context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:view', $context->id); $this->assignUserCapability('moodle/course:update', $context->id, $roleid); $sections = core_course_external::get_course_contents($course->id, array()); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); // Check that forum and label descriptions are correctly returned. $firstsection = array_pop($sections); $modinfo = get_fast_modinfo($course); $testexecuted = 0; foreach ($firstsection['modules'] as $module) { if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') { $cm = $modinfo->cms[$forumcm->id]; $formattedtext = format_text($cm->content, FORMAT_HTML, array('noclean' => true, 'para' => false, 'filter' => false)); $this->assertEquals($formattedtext, $module['description']); $testexecuted = $testexecuted + 1; } else { if ($module['id'] == $labelcm->id and $module['modname'] == 'label') { $cm = $modinfo->cms[$labelcm->id]; $formattedtext = format_text($cm->content, FORMAT_HTML, array('noclean' => true, 'para' => false, 'filter' => false)); $this->assertEquals($formattedtext, $module['description']); $testexecuted = $testexecuted + 1; } } } $this->assertEquals(2, $testexecuted); // Check that the only return section has the 5 created modules $this->assertEquals(5, count($firstsection['modules'])); }
/** * Test get_course_contents filtering by modname */ public function test_get_course_contents_modid() { $this->resetAfterTest(true); list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test(); // Test exclude modules. $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "page"), array("name" => "modid", "value" => $pagecm->instance))); // We need to execute the return values cleaning process to simulate the web service server. $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections); $this->assertCount(2, $sections); $this->assertCount(1, $sections[0]['modules']); $this->assertEquals("page", $sections[0]['modules'][0]["modname"]); $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]); }
private static function fetchActiveCourseIds($userId, $categoryId, $categoryType, $from) { $courses = core_enrol_external::get_users_courses_subcat_offline($userId, $categoryId, $categoryType); $acids = array(); foreach ($courses as $i => $course) { $moduleIds = array(); //@TODO performance - courses could have skipped when fetching from db itself rather than skipping it when iterating. if (!empty($from) && $course['timemodified'] < $from) { unset($courses[$i]); } try { $topicsWithModules = core_course_external::get_course_contents($course['id'], array(), $from); $topics = array_merge($topics, self::extractTopics($course['id'], $topicsWithModules, $from)); foreach ($topicsWithModules as $topicWithModule) { if (isset($topicWithModule['modules']) && is_array($topicWithModule['modules'])) { $moduleIds = array_merge($moduleIds, self::extractModuleIds($course['id'], $topicWithModule['id'], $topicWithModule['modules'], $userid, $fromtimestamp)); } } $cID = $course['id']; $activecourse_mod = $moduleIds; $acids[] = array("id" => $cID, "modules" => $activecourse_mod); } catch (Exception $ex) { } } return $acids; }