本文整理汇总了PHP中moodle_url::make_webservice_pluginfile_url方法的典型用法代码示例。如果您正苦于以下问题:PHP moodle_url::make_webservice_pluginfile_url方法的具体用法?PHP moodle_url::make_webservice_pluginfile_url怎么用?PHP moodle_url::make_webservice_pluginfile_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类moodle_url
的用法示例。
在下文中一共展示了moodle_url::make_webservice_pluginfile_url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_forum_discussion_posts
//.........这里部分代码省略.........
$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
// Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
$modcontext = context_module::instance($cm->id);
self::validate_context($modcontext);
// This require must be here, see mod/forum/discuss.php.
require_once($CFG->dirroot . "/mod/forum/lib.php");
// Check they have the view forum capability.
require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');
if (! $post = forum_get_post_full($discussion->firstpost)) {
throw new moodle_exception('notexists', 'forum');
}
// This function check groups, qanda, timed discussions, etc.
if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
throw new moodle_exception('noviewdiscussionspermission', 'forum');
}
$canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
// We will add this field in the response.
$canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
$forumtracked = forum_tp_is_tracked($forum);
$sort = 'p.' . $sortby . ' ' . $sortdirection;
$allposts = forum_get_all_discussion_posts($discussion->id, $sort, $forumtracked);
foreach ($allposts as $post) {
if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
$warning = array();
$warning['item'] = 'post';
$warning['itemid'] = $post->id;
$warning['warningcode'] = '1';
$warning['message'] = 'You can\'t see this post';
$warnings[] = $warning;
continue;
}
// Function forum_get_all_discussion_posts adds postread field.
// Note that the value returned can be a boolean or an integer. The WS expects a boolean.
if (empty($post->postread)) {
$post->postread = false;
} else {
$post->postread = true;
}
$post->canreply = $canreply;
if (!empty($post->children)) {
$post->children = array_keys($post->children);
} else {
$post->children = array();
}
$userpicture = new user_picture($post);
$userpicture->size = 1; // Size f1.
$post->userpictureurl = $userpicture->get_url($PAGE)->out(false);
$user = new stdclass();
$user->id = $post->userid;
$user = username_load_fields_from_object($user, $post);
$post->userfullname = fullname($user, $canviewfullname);
// Rewrite embedded images URLs.
list($post->message, $post->messageformat) =
external_format_text($post->message, $post->messageformat, $modcontext->id, 'mod_forum', 'post', $post->id);
// List attachments.
if (!empty($post->attachment)) {
$post->attachments = array();
$fs = get_file_storage();
if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment', $post->id, "filename", false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$fileurl = moodle_url::make_webservice_pluginfile_url(
$modcontext->id, 'mod_forum', 'attachment', $post->id, '/', $filename);
$post->attachments[] = array(
'filename' => $filename,
'mimetype' => $file->get_mimetype(),
'fileurl' => $fileurl->out(false)
);
}
}
}
$posts[] = $post;
}
$result = array();
$result['posts'] = $posts;
$result['warnings'] = $warnings;
return $result;
}
示例2: search_courses
/**
* Search courses following the specified criteria.
*
* @param string $criterianame Criteria name (search, modulelist (only admins), blocklist (only admins), tagid)
* @param string $criteriavalue Criteria value
* @param int $page Page number (for pagination)
* @param int $perpage Items per page
* @param array $requiredcapabilities Optional list of required capabilities (used to filter the list).
* @param int $limittoenrolled Limit to only enrolled courses
* @return array of course objects and warnings
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function search_courses($criterianame, $criteriavalue, $page = 0, $perpage = 0, $requiredcapabilities = array(), $limittoenrolled = 0)
{
global $CFG;
require_once $CFG->libdir . '/coursecatlib.php';
$warnings = array();
$parameters = array('criterianame' => $criterianame, 'criteriavalue' => $criteriavalue, 'page' => $page, 'perpage' => $perpage, 'requiredcapabilities' => $requiredcapabilities);
$params = self::validate_parameters(self::search_courses_parameters(), $parameters);
self::validate_context(context_system::instance());
$allowedcriterianames = array('search', 'modulelist', 'blocklist', 'tagid');
if (!in_array($params['criterianame'], $allowedcriterianames)) {
throw new invalid_parameter_exception('Invalid value for criterianame parameter (value: ' . $params['criterianame'] . '),' . 'allowed values are: ' . implode(',', $allowedcriterianames));
}
if ($params['criterianame'] == 'modulelist' or $params['criterianame'] == 'blocklist') {
require_capability('moodle/site:config', context_system::instance());
}
$paramtype = array('search' => PARAM_RAW, 'modulelist' => PARAM_PLUGIN, 'blocklist' => PARAM_INT, 'tagid' => PARAM_INT);
$params['criteriavalue'] = clean_param($params['criteriavalue'], $paramtype[$params['criterianame']]);
// Prepare the search API options.
$searchcriteria = array();
$searchcriteria[$params['criterianame']] = $params['criteriavalue'];
$options = array();
if ($params['perpage'] != 0) {
$offset = $params['page'] * $params['perpage'];
$options = array('offset' => $offset, 'limit' => $params['perpage']);
}
// Search the courses.
$courses = coursecat::search_courses($searchcriteria, $options, $params['requiredcapabilities']);
$totalcount = coursecat::search_courses_count($searchcriteria, $options, $params['requiredcapabilities']);
if (!empty($limittoenrolled)) {
// Get the courses where the current user has access.
$enrolled = enrol_get_my_courses(array('id', 'cacherev'));
}
$finalcourses = array();
$categoriescache = array();
foreach ($courses as $course) {
if (!empty($limittoenrolled)) {
// Filter out not enrolled courses.
if (!isset($enrolled[$course->id])) {
$totalcount--;
continue;
}
}
$coursecontext = context_course::instance($course->id);
// Category information.
if (!isset($categoriescache[$course->category])) {
$categoriescache[$course->category] = coursecat::get($course->category);
}
$category = $categoriescache[$course->category];
// Retrieve course overfiew used files.
$files = array();
foreach ($course->get_course_overviewfiles() as $file) {
$fileurl = moodle_url::make_webservice_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename())->out(false);
$files[] = array('filename' => $file->get_filename(), 'fileurl' => $fileurl, 'filesize' => $file->get_filesize());
}
// Retrieve the course contacts,
// we need here the users fullname since if we are not enrolled can be difficult to obtain them via other Web Services.
$coursecontacts = array();
foreach ($course->get_course_contacts() as $contact) {
$coursecontacts[] = array('id' => $contact['user']->id, 'fullname' => $contact['username']);
}
// Allowed enrolment methods (maybe we can self-enrol).
$enroltypes = array();
$instances = enrol_get_instances($course->id, true);
foreach ($instances as $instance) {
$enroltypes[] = $instance->enrol;
}
// Format summary.
list($summary, $summaryformat) = external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null);
$displayname = get_course_display_name_for_list($course);
$coursereturns = array();
$coursereturns['id'] = $course->id;
$coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id);
$coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id);
$coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id);
$coursereturns['categoryid'] = $course->category;
$coursereturns['categoryname'] = $category->name;
$coursereturns['summary'] = $summary;
$coursereturns['summaryformat'] = $summaryformat;
$coursereturns['overviewfiles'] = $files;
$coursereturns['contacts'] = $coursecontacts;
$coursereturns['enrollmentmethods'] = $enroltypes;
$finalcourses[] = $coursereturns;
}
return array('total' => $totalcount, 'courses' => $finalcourses, 'warnings' => $warnings);
}
示例3: get_chat_users
/**
* Get the list of users in the given chat session.
*
* @param int $chatsid the chat session id
* @return array of warnings and the user lists
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function get_chat_users($chatsid) {
global $DB;
$params = self::validate_parameters(self::get_chat_users_parameters(),
array(
'chatsid' => $chatsid
));
$warnings = array();
// Request and permission validation.
if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) {
throw new moodle_exception('notlogged', 'chat');
}
$chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat');
$context = context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/chat:chat', $context);
// First, delete old users from the chats.
chat_delete_old_users();
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
$returnedusers = array();
foreach ($users as $user) {
$usercontext = context_user::instance($user->id, IGNORE_MISSING);
$profileimageurl = '';
if ($usercontext) {
$profileimageurl = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
}
$returnedusers[] = array(
'id' => $user->id,
'fullname' => fullname($user),
'profileimageurl' => $profileimageurl
);
}
$result = array();
$result['users'] = $returnedusers;
$result['warnings'] = $warnings;
return $result;
}
示例4: test_mod_forum_get_forum_discussions_paginated
/**
* Test get forum discussions paginated
*/
public function test_mod_forum_get_forum_discussions_paginated()
{
global $USER, $CFG, $DB;
$this->resetAfterTest(true);
// Set the CFG variable to allow track forums.
$CFG->forum_trackreadposts = true;
// Create a user who can track forums.
$record = new stdClass();
$record->trackforums = true;
$user1 = self::getDataGenerator()->create_user($record);
// Create a bunch of other users to post.
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// Set the first created user to the test user.
self::setUser($user1);
// Create courses to add the modules.
$course1 = self::getDataGenerator()->create_course();
// First forum with tracking off.
$record = new stdClass();
$record->course = $course1->id;
$record->trackingtype = FORUM_TRACKING_OFF;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
// Add discussions to the forums.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Add three replies to the discussion 1 from different users.
$record = new stdClass();
$record->discussion = $discussion1->id;
$record->parent = $discussion1->firstpost;
$record->userid = $user2->id;
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
$record->parent = $discussion1reply1->id;
$record->userid = $user3->id;
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
$record->userid = $user4->id;
$discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
// Enrol the user in the first course.
$enrol = enrol_get_plugin('manual');
// We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
$enrolinstances = enrol_get_instances($course1->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance1 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance1, $user1->id);
// Delete one user.
delete_user($user4);
// Assign capabilities to view discussions for forum 1.
$cm = get_coursemodule_from_id('forum', $forum1->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
$this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
// Create what we expect to be returned when querying the forums.
$post1 = $DB->get_record('forum_posts', array('id' => $discussion1->firstpost), '*', MUST_EXIST);
$userpictureurl = moodle_url::make_webservice_pluginfile_url(context_user::instance($user1->id)->id, 'user', 'icon', null, '/', 'f1');
// We expect an empty URL since we deleted the user4.
$usermodifiedpictureurl = '';
$expecteddiscussions = array('id' => $discussion1->firstpost, 'name' => $discussion1->name, 'groupid' => $discussion1->groupid, 'timemodified' => $discussion1reply3->created, 'usermodified' => $discussion1reply3->userid, 'timestart' => $discussion1->timestart, 'timeend' => $discussion1->timeend, 'discussion' => $discussion1->id, 'parent' => 0, 'userid' => $discussion1->userid, 'created' => $post1->created, 'modified' => $post1->modified, 'mailed' => $post1->mailed, 'subject' => $post1->subject, 'message' => $post1->message, 'messageformat' => $post1->messageformat, 'messagetrust' => $post1->messagetrust, 'attachment' => $post1->attachment, 'totalscore' => $post1->totalscore, 'mailnow' => $post1->mailnow, 'userfullname' => fullname($user1), 'usermodifiedfullname' => fullname($user4), 'userpictureurl' => $userpictureurl, 'usermodifiedpictureurl' => $usermodifiedpictureurl, 'numreplies' => 3, 'numunread' => 0);
// Call the external function passing forum id.
$discussions = mod_forum_external::get_forum_discussions_paginated($forum1->id);
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
$expectedreturn = array('discussions' => array($expecteddiscussions), 'warnings' => array());
$this->assertEquals($expectedreturn, $discussions);
// Call without required view discussion capability.
$this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
try {
mod_forum_external::get_forum_discussions_paginated($forum1->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('noviewdiscussionspermission', $e->errorcode);
}
// Unenrol user from second course.
$enrol->unenrol_user($instance1, $user1->id);
// Call for the second course we unenrolled the user from, make sure exception thrown.
try {
mod_forum_external::get_forum_discussions_paginated($forum1->id);
$this->fail('Exception expected due to being unenrolled from the course.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
示例5: get_seplments
/**
* Returns an array of courses the user is enrolled in, and for each course all of the seplments that the user can
* view within that course.
*
* @param array $courseids An optional array of course ids. If provided only seplments within the given course
* will be returned. If the user is not enrolled in a given course a warning will be generated and returned.
* @param array $capabilities An array of additional capability checks you wish to be made on the course context.
* @return An array of courses and warnings.
* @since Moodle 2.4
*/
public static function get_seplments($courseids = array(), $capabilities = array())
{
global $USER, $DB, $CFG;
require_once "{$CFG->dirroot}/mod/sepl/locallib.php";
$params = self::validate_parameters(self::get_seplments_parameters(), array('courseids' => $courseids, 'capabilities' => $capabilities));
$warnings = array();
$fields = 'sortorder,shortname,fullname,timemodified';
$courses = enrol_get_users_courses($USER->id, true, $fields);
// Used to test for ids that have been requested but can't be returned.
if (count($params['courseids']) > 0) {
foreach ($params['courseids'] as $courseid) {
if (!in_array($courseid, array_keys($courses))) {
unset($courses[$courseid]);
$warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => '2', 'message' => 'User is not enrolled or does not have requested capability');
}
}
}
foreach ($courses as $id => $course) {
if (count($params['courseids']) > 0 && !in_array($id, $params['courseids'])) {
unset($courses[$id]);
}
$context = context_course::instance($id);
try {
self::validate_context($context);
} catch (Exception $e) {
unset($courses[$id]);
$warnings[] = array('item' => 'course', 'itemid' => $id, 'warningcode' => '1', 'message' => 'No access rights in course context ' . $e->getMessage() . $e->getTraceAsString());
continue;
}
if (count($params['capabilities']) > 0 && !has_all_capabilities($params['capabilities'], $context)) {
unset($courses[$id]);
}
}
$extrafields = 'm.id as seplmentid, ' . 'm.course, ' . 'm.nosubmissions, ' . 'm.submissiondrafts, ' . 'm.sendnotifications, ' . 'm.sendlatenotifications, ' . 'm.sendstudentnotifications, ' . 'm.duedate, ' . 'm.allowsubmissionsfromdate, ' . 'm.grade, ' . 'm.timemodified, ' . 'm.completionsubmit, ' . 'm.cutoffdate, ' . 'm.teamsubmission, ' . 'm.requireallteammemberssubmit, ' . 'm.teamsubmissiongroupingid, ' . 'm.blindmarking, ' . 'm.revealidentities, ' . 'm.attemptreopenmethod, ' . 'm.maxattempts, ' . 'm.markingworkflow, ' . 'm.markingallocation, ' . 'm.requiresubmissionstatement, ' . 'm.intro, ' . 'm.introformat';
$coursearray = array();
foreach ($courses as $id => $course) {
$seplmentarray = array();
// Get a list of seplments for the course.
if ($modules = get_coursemodules_in_course('sepl', $courses[$id]->id, $extrafields)) {
foreach ($modules as $module) {
$context = context_module::instance($module->id);
try {
self::validate_context($context);
require_capability('mod/sepl:view', $context);
} catch (Exception $e) {
$warnings[] = array('item' => 'module', 'itemid' => $module->id, 'warningcode' => '1', 'message' => 'No access rights in module context');
continue;
}
$configrecords = $DB->get_recordset('sepl_plugin_config', array('seplment' => $module->seplmentid));
$configarray = array();
foreach ($configrecords as $configrecord) {
$configarray[] = array('id' => $configrecord->id, 'seplment' => $configrecord->seplment, 'plugin' => $configrecord->plugin, 'subtype' => $configrecord->subtype, 'name' => $configrecord->name, 'value' => $configrecord->value);
}
$configrecords->close();
$seplment = array('id' => $module->seplmentid, 'cmid' => $module->id, 'course' => $module->course, 'name' => $module->name, 'nosubmissions' => $module->nosubmissions, 'submissiondrafts' => $module->submissiondrafts, 'sendnotifications' => $module->sendnotifications, 'sendlatenotifications' => $module->sendlatenotifications, 'sendstudentnotifications' => $module->sendstudentnotifications, 'duedate' => $module->duedate, 'allowsubmissionsfromdate' => $module->allowsubmissionsfromdate, 'grade' => $module->grade, 'timemodified' => $module->timemodified, 'completionsubmit' => $module->completionsubmit, 'cutoffdate' => $module->cutoffdate, 'teamsubmission' => $module->teamsubmission, 'requireallteammemberssubmit' => $module->requireallteammemberssubmit, 'teamsubmissiongroupingid' => $module->teamsubmissiongroupingid, 'blindmarking' => $module->blindmarking, 'revealidentities' => $module->revealidentities, 'attemptreopenmethod' => $module->attemptreopenmethod, 'maxattempts' => $module->maxattempts, 'markingworkflow' => $module->markingworkflow, 'markingallocation' => $module->markingallocation, 'requiresubmissionstatement' => $module->requiresubmissionstatement, 'configs' => $configarray);
// Return or not intro and file attachments depending on the plugin settings.
$sepl = new sepl($context, null, null);
if ($sepl->show_intro()) {
list($seplment['intro'], $seplment['introformat']) = external_format_text($module->intro, $module->introformat, $context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0);
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0, 'timemodified', false)) {
$seplment['introattachments'] = array();
foreach ($files as $file) {
$filename = $file->get_filename();
$seplment['introattachments'][] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0, '/', $filename)->out(false));
}
}
}
$seplmentarray[] = $seplment;
}
}
$coursearray[] = array('id' => $courses[$id]->id, 'fullname' => $courses[$id]->fullname, 'shortname' => $courses[$id]->shortname, 'timemodified' => $courses[$id]->timemodified, 'seplments' => $seplmentarray);
}
$result = array('courses' => $coursearray, 'warnings' => $warnings);
return $result;
}
示例6: test_get_subwiki_files_visible_groups_individual
/**
* Test get_subwiki_files using an individual wiki with visible groups.
*/
public function test_get_subwiki_files_visible_groups_individual()
{
// Create testing data.
$this->create_individual_wikis_with_groups();
$this->setUser($this->student);
// Add a file as subwiki attachment in the student group 1 subwiki.
$fs = get_file_storage();
$contextwiki = context_module::instance($this->wikivisind->cmid);
$file = array('component' => 'mod_wiki', 'filearea' => 'attachments', 'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid, 'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
$content = 'IMAGE';
$fs->create_file_from_string($file, $content);
$expectedfile = array('filename' => $file['filename'], 'filepath' => $file['filepath'], 'mimetype' => 'image/jpeg', 'filesize' => strlen($content), 'timemodified' => $file['timemodified'], 'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'], $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']));
// Call the WS and check that it returns this file.
$result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id);
$result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
$this->assertCount(1, $result['files']);
$this->assertEquals($expectedfile, $result['files'][0]);
// Now check that a teacher can see it too.
$this->setUser($this->teacher);
$result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
$this->assertCount(1, $result['files']);
$this->assertEquals($expectedfile, $result['files'][0]);
}
示例7: get_assignments
/**
* Returns an array of courses the user is enrolled, and for each course all of the assignments that the user can
* view within that course.
*
* @param array $courseids An optional array of course ids. If provided only assignments within the given course
* will be returned. If the user is not enrolled in or can't view a given course a warning will be generated and returned.
* @param array $capabilities An array of additional capability checks you wish to be made on the course context.
* @param bool $includenotenrolledcourses Wheter to return courses that the user can see even if is not enroled in.
* This requires the parameter $courseids to not be empty.
* @return An array of courses and warnings.
* @since Moodle 2.4
*/
public static function get_assignments($courseids = array(), $capabilities = array(), $includenotenrolledcourses = false)
{
global $USER, $DB, $CFG;
$params = self::validate_parameters(self::get_assignments_parameters(), array('courseids' => $courseids, 'capabilities' => $capabilities, 'includenotenrolledcourses' => $includenotenrolledcourses));
$warnings = array();
$courses = array();
$fields = 'sortorder,shortname,fullname,timemodified';
// If the courseids list is empty, we return only the courses where the user is enrolled in.
if (empty($params['courseids'])) {
$courses = enrol_get_users_courses($USER->id, true, $fields);
$courseids = array_keys($courses);
} else {
if ($includenotenrolledcourses) {
// In this case, we don't have to check here for enrolmnents. Maybe the user can see the course even if is not enrolled.
$courseids = $params['courseids'];
} else {
// We need to check for enrolments.
$mycourses = enrol_get_users_courses($USER->id, true, $fields);
$mycourseids = array_keys($mycourses);
foreach ($params['courseids'] as $courseid) {
if (!in_array($courseid, $mycourseids)) {
unset($courses[$courseid]);
$warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => '2', 'message' => 'User is not enrolled or does not have requested capability');
} else {
$courses[$courseid] = $mycourses[$courseid];
}
}
$courseids = array_keys($courses);
}
}
foreach ($courseids as $cid) {
try {
$context = context_course::instance($cid);
self::validate_context($context);
// Check if this course was already loaded (by enrol_get_users_courses).
if (!isset($courses[$cid])) {
$courses[$cid] = get_course($cid);
}
$courses[$cid]->contextid = $context->id;
} catch (Exception $e) {
unset($courses[$cid]);
$warnings[] = array('item' => 'course', 'itemid' => $cid, 'warningcode' => '1', 'message' => 'No access rights in course context ' . $e->getMessage());
continue;
}
if (count($params['capabilities']) > 0 && !has_all_capabilities($params['capabilities'], $context)) {
unset($courses[$cid]);
}
}
$extrafields = 'm.id as assignmentid, ' . 'm.course, ' . 'm.nosubmissions, ' . 'm.submissiondrafts, ' . 'm.sendnotifications, ' . 'm.sendlatenotifications, ' . 'm.sendstudentnotifications, ' . 'm.duedate, ' . 'm.allowsubmissionsfromdate, ' . 'm.grade, ' . 'm.timemodified, ' . 'm.completionsubmit, ' . 'm.cutoffdate, ' . 'm.teamsubmission, ' . 'm.requireallteammemberssubmit, ' . 'm.teamsubmissiongroupingid, ' . 'm.blindmarking, ' . 'm.revealidentities, ' . 'm.attemptreopenmethod, ' . 'm.maxattempts, ' . 'm.markingworkflow, ' . 'm.markingallocation, ' . 'm.requiresubmissionstatement, ' . 'm.preventsubmissionnotingroup, ' . 'm.intro, ' . 'm.introformat';
$coursearray = array();
foreach ($courses as $id => $course) {
$assignmentarray = array();
// Get a list of assignments for the course.
if ($modules = get_coursemodules_in_course('assign', $courses[$id]->id, $extrafields)) {
foreach ($modules as $module) {
$context = context_module::instance($module->id);
try {
self::validate_context($context);
require_capability('mod/assign:view', $context);
} catch (Exception $e) {
$warnings[] = array('item' => 'module', 'itemid' => $module->id, 'warningcode' => '1', 'message' => 'No access rights in module context');
continue;
}
$configrecords = $DB->get_recordset('assign_plugin_config', array('assignment' => $module->assignmentid));
$configarray = array();
foreach ($configrecords as $configrecord) {
$configarray[] = array('id' => $configrecord->id, 'assignment' => $configrecord->assignment, 'plugin' => $configrecord->plugin, 'subtype' => $configrecord->subtype, 'name' => $configrecord->name, 'value' => $configrecord->value);
}
$configrecords->close();
$assignment = array('id' => $module->assignmentid, 'cmid' => $module->id, 'course' => $module->course, 'name' => $module->name, 'nosubmissions' => $module->nosubmissions, 'submissiondrafts' => $module->submissiondrafts, 'sendnotifications' => $module->sendnotifications, 'sendlatenotifications' => $module->sendlatenotifications, 'sendstudentnotifications' => $module->sendstudentnotifications, 'duedate' => $module->duedate, 'allowsubmissionsfromdate' => $module->allowsubmissionsfromdate, 'grade' => $module->grade, 'timemodified' => $module->timemodified, 'completionsubmit' => $module->completionsubmit, 'cutoffdate' => $module->cutoffdate, 'teamsubmission' => $module->teamsubmission, 'requireallteammemberssubmit' => $module->requireallteammemberssubmit, 'teamsubmissiongroupingid' => $module->teamsubmissiongroupingid, 'blindmarking' => $module->blindmarking, 'revealidentities' => $module->revealidentities, 'attemptreopenmethod' => $module->attemptreopenmethod, 'maxattempts' => $module->maxattempts, 'markingworkflow' => $module->markingworkflow, 'markingallocation' => $module->markingallocation, 'requiresubmissionstatement' => $module->requiresubmissionstatement, 'preventsubmissionnotingroup' => $module->preventsubmissionnotingroup, 'configs' => $configarray);
// Return or not intro and file attachments depending on the plugin settings.
$assign = new assign($context, null, null);
if ($assign->show_intro()) {
list($assignment['intro'], $assignment['introformat']) = external_format_text($module->intro, $module->introformat, $context->id, 'mod_assign', 'intro', null);
$assignment['introfiles'] = external_util::get_area_files($context->id, 'mod_assign', 'intro', false, false);
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0, 'timemodified', false)) {
$assignment['introattachments'] = array();
foreach ($files as $file) {
$filename = $file->get_filename();
$assignment['introattachments'][] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA, 0, '/', $filename)->out(false));
}
}
}
$assignmentarray[] = $assignment;
}
}
$coursearray[] = array('id' => $courses[$id]->id, 'fullname' => external_format_string($courses[$id]->fullname, $course->contextid), 'shortname' => external_format_string($courses[$id]->shortname, $course->contextid), 'timemodified' => $courses[$id]->timemodified, 'assignments' => $assignmentarray);
//.........这里部分代码省略.........
示例8: book_export_contents
/**
* Export book resource contents
*
* @param stdClass $cm Course module object
* @param string $baseurl Base URL for file downloads
* @return array of file content
*/
function book_export_contents($cm, $baseurl)
{
global $DB;
$contents = array();
$context = context_module::instance($cm->id);
$book = $DB->get_record('book', array('id' => $cm->instance), '*', MUST_EXIST);
$fs = get_file_storage();
$chapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum');
$structure = array();
$currentchapter = 0;
foreach ($chapters as $chapter) {
if ($chapter->hidden) {
continue;
}
// Generate the book structure.
$thischapter = array("title" => format_string($chapter->title, true, array('context' => $context)), "href" => $chapter->id . "/index.html", "level" => 0, "subitems" => array());
// Main chapter.
if (!$chapter->subchapter) {
$currentchapter = $chapter->pagenum;
$structure[$currentchapter] = $thischapter;
} else {
// Subchapter.
$thischapter['level'] = 1;
$structure[$currentchapter]["subitems"][] = $thischapter;
}
// Export the chapter contents.
// Main content (html).
$filename = 'index.html';
$chapterindexfile = array();
$chapterindexfile['type'] = 'file';
$chapterindexfile['filename'] = $filename;
// Each chapter in a subdirectory.
$chapterindexfile['filepath'] = "/{$chapter->id}/";
$chapterindexfile['filesize'] = 0;
$chapterindexfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_book', 'chapter', $chapter->id, '/', 'index.html')->out(false);
$chapterindexfile['timecreated'] = $book->timecreated;
$chapterindexfile['timemodified'] = $book->timemodified;
$chapterindexfile['content'] = format_string($chapter->title, true, array('context' => $context));
$chapterindexfile['sortorder'] = 0;
$chapterindexfile['userid'] = null;
$chapterindexfile['author'] = null;
$chapterindexfile['license'] = null;
$contents[] = $chapterindexfile;
// Chapter files (images usually).
$files = $fs->get_area_files($context->id, 'mod_book', 'chapter', $chapter->id, 'sortorder DESC, id ASC', false);
foreach ($files as $fileinfo) {
$file = array();
$file['type'] = 'file';
$file['filename'] = $fileinfo->get_filename();
$file['filepath'] = "/{$chapter->id}" . $fileinfo->get_filepath();
$file['filesize'] = $fileinfo->get_filesize();
$file['fileurl'] = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_book', 'chapter', $chapter->id, $fileinfo->get_filepath(), $fileinfo->get_filename())->out(false);
$file['timecreated'] = $fileinfo->get_timecreated();
$file['timemodified'] = $fileinfo->get_timemodified();
$file['sortorder'] = $fileinfo->get_sortorder();
$file['userid'] = $fileinfo->get_userid();
$file['author'] = $fileinfo->get_author();
$file['license'] = $fileinfo->get_license();
$contents[] = $file;
}
}
// First content is the structure in encoded JSON format.
$structurefile = array();
$structurefile['type'] = 'content';
$structurefile['filename'] = 'structure';
$structurefile['filepath'] = "/";
$structurefile['filesize'] = 0;
$structurefile['fileurl'] = null;
$structurefile['timecreated'] = $book->timecreated;
$structurefile['timemodified'] = $book->timemodified;
$structurefile['content'] = json_encode(array_values($structure));
$structurefile['sortorder'] = 0;
$structurefile['userid'] = null;
$structurefile['author'] = null;
$structurefile['license'] = null;
// Add it as first element.
array_unshift($contents, $structurefile);
return $contents;
}
示例9: get_user_badges
/**
* Returns the list of badges awarded to a user.
*
* @param int $userid user id
* @param int $courseid course id
* @param int $page page of records to return
* @param int $perpage number of records to return per page
* @param string $search a simple string to search for
* @param bool $onlypublic whether to return only public badges
* @return array array containing warnings and the awarded badges
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false)
{
global $CFG, $USER;
$warnings = array();
$params = array('userid' => $userid, 'courseid' => $courseid, 'page' => $page, 'perpage' => $perpage, 'search' => $search, 'onlypublic' => $onlypublic);
$params = self::validate_parameters(self::get_user_badges_parameters(), $params);
if (empty($CFG->enablebadges)) {
throw new moodle_exception('badgesdisabled', 'badges');
}
if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) {
throw new moodle_exception('coursebadgesdisabled', 'badges');
}
// Default value for userid.
if (empty($params['userid'])) {
$params['userid'] = $USER->id;
}
// Validate the user.
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
core_user::require_active_user($user);
$usercontext = context_user::instance($user->id);
self::validate_context($usercontext);
if ($USER->id != $user->id) {
require_capability('moodle/badges:viewotherbadges', $usercontext);
// We are looking other user's badges, we must retrieve only public badges.
$params['onlypublic'] = true;
}
$userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'], $params['onlypublic']);
$result = array();
$result['badges'] = array();
$result['warnings'] = $warnings;
foreach ($userbadges as $badge) {
$context = $badge->type == BADGE_TYPE_SITE ? context_system::instance() : context_course::instance($badge->courseid);
$badge->badgeurl = moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1')->out(false);
// Return all the information if we are requesting our own badges.
// Or, if we have permissions for configuring badges in the badge context.
if ($USER->id == $user->id or has_capability('moodle/badges:configuredetails', $context)) {
$result['badges'][] = (array) $badge;
} else {
$result['badges'][] = array('name' => $badge->name, 'description' => $badge->description, 'badgeurl' => $badge->badgeurl, 'issuername' => $badge->issuername, 'issuerurl' => $badge->issuerurl, 'issuercontact' => $badge->issuercontact, 'uniquehash' => $badge->uniquehash, 'dateissued' => $badge->dateissued, 'dateexpire' => $badge->dateexpire);
}
}
return $result;
}
示例10: fill_entry_details
/**
* Fill in an entry object.
*
* This adds additional required fields for the external function to return.
*
* @param stdClass $entry The entry.
* @param context $context The context the entry belongs to.
* @return void
*/
protected static function fill_entry_details($entry, $context)
{
global $PAGE;
$canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
// Format concept and definition.
$entry->concept = external_format_string($entry->concept, $context->id);
list($entry->definition, $entry->definitionformat) = external_format_text($entry->definition, $entry->definitionformat, $context->id, 'mod_glossary', 'entry', $entry->id);
// Author details.
$user = mod_glossary_entry_query_builder::get_user_from_record($entry);
$userpicture = new user_picture($user);
$userpicture->size = 1;
$entry->userfullname = fullname($user, $canviewfullnames);
$entry->userpictureurl = $userpicture->get_url($PAGE)->out(false);
// Fetch attachments.
$entry->attachment = !empty($entry->attachment) ? 1 : 0;
$entry->attachments = array();
if ($entry->attachment) {
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id, 'filename', false)) {
foreach ($files as $file) {
$filename = $file->get_filename();
$fileurl = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_glossary', 'attachment', $entry->id, '/', $filename);
$entry->attachments[] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => $fileurl->out(false));
}
}
}
}
示例11: get_blocked_users
/**
* Retrieve a list of users blocked
*
* @param int $userid the user whose blocked users we want to retrieve
* @return external_description
* @since 2.9
*/
public static function get_blocked_users($userid)
{
global $CFG, $USER;
require_once $CFG->dirroot . "/message/lib.php";
// Warnings array, it can be empty at the end but is mandatory.
$warnings = array();
// Validate params.
$params = array('userid' => $userid);
$params = self::validate_parameters(self::get_blocked_users_parameters(), $params);
$userid = $params['userid'];
// Validate context.
$context = context_system::instance();
self::validate_context($context);
// Check if private messaging between users is allowed.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
$user = core_user::get_user($userid, 'id', MUST_EXIST);
// Check if we have permissions for retrieve the information.
if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
throw new moodle_exception('accessdenied', 'admin');
}
// Now, we can get safely all the blocked users.
$users = message_get_blocked_users($user);
$blockedusers = array();
foreach ($users as $user) {
$newuser = array('id' => $user->id, 'fullname' => fullname($user));
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1')->out(false);
$blockedusers[] = $newuser;
}
$results = array('users' => $blockedusers, 'warnings' => $warnings);
return $results;
}
示例12: test_mod_forum_get_forum_discussion_posts
/**
* Test get forum posts
*/
public function test_mod_forum_get_forum_discussion_posts()
{
global $CFG, $PAGE;
$this->resetAfterTest(true);
// Set the CFG variable to allow track forums.
$CFG->forum_trackreadposts = true;
// Create a user who can track forums.
$record = new stdClass();
$record->trackforums = true;
$user1 = self::getDataGenerator()->create_user($record);
// Create a bunch of other users to post.
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Set the first created user to the test user.
self::setUser($user1);
// Create course to add the module.
$course1 = self::getDataGenerator()->create_course();
// Forum with tracking off.
$record = new stdClass();
$record->course = $course1->id;
$record->trackingtype = FORUM_TRACKING_OFF;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
$forum1context = context_module::instance($forum1->cmid);
// Add discussions to the forums.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user2->id;
$record->forum = $forum1->id;
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Add 2 replies to the discussion 1 from different users.
$record = new stdClass();
$record->discussion = $discussion1->id;
$record->parent = $discussion1->firstpost;
$record->userid = $user2->id;
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
$filename = 'shouldbeanimage.jpg';
// Add a fake inline image to the post.
$filerecordinline = array('contextid' => $forum1context->id, 'component' => 'mod_forum', 'filearea' => 'post', 'itemid' => $discussion1reply1->id, 'filepath' => '/', 'filename' => $filename);
$fs = get_file_storage();
$timepost = time();
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
$record->parent = $discussion1reply1->id;
$record->userid = $user3->id;
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
// Following line enrol and assign default role id to the user.
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
// Delete one user, to test that we still receive posts by this user.
delete_user($user3);
// Create what we expect to be returned when querying the discussion.
$expectedposts = array('posts' => array(), 'warnings' => array());
// User pictures are initially empty, we should get the links once the external function is called.
$expectedposts['posts'][] = array('id' => $discussion1reply2->id, 'discussion' => $discussion1reply2->discussion, 'parent' => $discussion1reply2->parent, 'userid' => (int) $discussion1reply2->userid, 'created' => $discussion1reply2->created, 'modified' => $discussion1reply2->modified, 'mailed' => $discussion1reply2->mailed, 'subject' => $discussion1reply2->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply2->messagetrust, 'attachment' => $discussion1reply2->attachment, 'totalscore' => $discussion1reply2->totalscore, 'mailnow' => $discussion1reply2->mailnow, 'children' => array(), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user3), 'userpictureurl' => '');
$expectedposts['posts'][] = array('id' => $discussion1reply1->id, 'discussion' => $discussion1reply1->discussion, 'parent' => $discussion1reply1->parent, 'userid' => (int) $discussion1reply1->userid, 'created' => $discussion1reply1->created, 'modified' => $discussion1reply1->modified, 'mailed' => $discussion1reply1->mailed, 'subject' => $discussion1reply1->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply1->messagetrust, 'attachment' => $discussion1reply1->attachment, 'messageinlinefiles' => array(array('filename' => $filename, 'filepath' => '/', 'filesize' => '27', 'fileurl' => moodle_url::make_webservice_pluginfile_url($forum1context->id, 'mod_forum', 'post', $discussion1reply1->id, '/', $filename), 'timemodified' => $timepost, 'mimetype' => 'image/jpeg')), 'totalscore' => $discussion1reply1->totalscore, 'mailnow' => $discussion1reply1->mailnow, 'children' => array($discussion1reply2->id), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user2), 'userpictureurl' => '');
// Test a discussion with two additional posts (total 3 posts).
$posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertEquals(3, count($posts['posts']));
// Generate here the pictures because we need to wait to the external function to init the theme.
$userpicture = new user_picture($user3);
$userpicture->size = 1;
// Size f1.
$expectedposts['posts'][0]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
$userpicture = new user_picture($user2);
$userpicture->size = 1;
// Size f1.
$expectedposts['posts'][1]['userpictureurl'] = $userpicture->get_url($PAGE)->out(false);
// Unset the initial discussion post.
array_pop($posts['posts']);
$this->assertEquals($expectedposts, $posts);
// Test discussion without additional posts. There should be only one post (the one created by the discussion).
$posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertEquals(1, count($posts['posts']));
}
示例13: get_item_ratings
/**
* Retrieve a list of ratings for a given item (forum post etc)
*
* @param string $contextlevel course, module, user...
* @param int $instanceid the instance if for the context element
* @param string $component the name of the component
* @param string $ratingarea rating area
* @param int $itemid the item id
* @param int $scaleid the scale id
* @param string $sort sql order (firstname, rating or timemodified)
* @return array Result and possible warnings
* @throws moodle_exception
* @since Moodle 2.9
*/
public static function get_item_ratings($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $sort)
{
global $USER;
$warnings = array();
$arrayparams = array('contextlevel' => $contextlevel, 'instanceid' => $instanceid, 'component' => $component, 'ratingarea' => $ratingarea, 'itemid' => $itemid, 'scaleid' => $scaleid, 'sort' => $sort);
// Validate and normalize parameters.
$params = self::validate_parameters(self::get_item_ratings_parameters(), $arrayparams);
$context = self::get_context_from_params($params);
self::validate_context($context);
// Minimal capability required.
$callbackparams = array('contextid' => $context->id, 'component' => $component, 'ratingarea' => $ratingarea, 'itemid' => $itemid, 'scaleid' => $scaleid);
if (!has_capability('moodle/rating:view', $context) || !component_callback($component, 'rating_can_see_item_ratings', array($callbackparams), true)) {
throw new moodle_exception('noviewrate', 'rating');
}
list($context, $course, $cm) = get_context_info_array($context->id);
// Can we see all ratings?
$canviewallratings = has_capability('moodle/rating:viewall', $context);
// Create the Sql sort order string.
switch ($params['sort']) {
case 'firstname':
$sqlsort = "u.firstname ASC";
break;
case 'rating':
$sqlsort = "r.rating ASC";
break;
default:
$sqlsort = "r.timemodified ASC";
}
$ratingoptions = new stdClass();
$ratingoptions->context = $context;
$ratingoptions->component = $params['component'];
$ratingoptions->ratingarea = $params['ratingarea'];
$ratingoptions->itemid = $params['itemid'];
$ratingoptions->sort = $sqlsort;
$rm = new rating_manager();
$ratings = $rm->get_all_ratings_for_item($ratingoptions);
$scalemenu = make_grades_menu($params['scaleid']);
// If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
// We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
$maxrating = max(array_keys($scalemenu));
$results = array();
foreach ($ratings as $rating) {
if ($canviewallratings || $USER->id == $rating->userid) {
if ($rating->rating > $maxrating) {
$rating->rating = $maxrating;
}
$profileimageurl = '';
// We can have ratings from deleted users. In this case, those users don't have a valid context.
$usercontext = context_user::instance($rating->userid, IGNORE_MISSING);
if ($usercontext) {
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
}
$result = array();
$result['id'] = $rating->id;
$result['userid'] = $rating->userid;
$result['userpictureurl'] = $profileimageurl;
$result['userfullname'] = fullname($rating);
$result['rating'] = $scalemenu[$rating->rating];
$result['timemodified'] = $rating->timemodified;
$results[] = $result;
}
}
return array('ratings' => $results, 'warnings' => $warnings);
}
示例14: get_scorms_by_courses
/**
* Returns a list of scorms in a provided list of courses,
* if no list is provided all scorms that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array the scorm details
* @since Moodle 3.0
*/
public static function get_scorms_by_courses($courseids = array())
{
global $CFG;
$returnedscorms = array();
$warnings = array();
$params = self::validate_parameters(self::get_scorms_by_courses_parameters(), array('courseids' => $courseids));
$courses = array();
if (empty($params['courseids'])) {
$courses = enrol_get_my_courses();
$params['courseids'] = array_keys($courses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses);
// Get the scorms in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$scorms = get_all_instances_in_courses("scorm", $courses);
$fs = get_file_storage();
foreach ($scorms as $scorm) {
$context = context_module::instance($scorm->coursemodule);
// Entry to return.
$module = array();
// First, we return information that any user can see in (or can deduce from) the web interface.
$module['id'] = $scorm->id;
$module['coursemodule'] = $scorm->coursemodule;
$module['course'] = $scorm->course;
$module['name'] = external_format_string($scorm->name, $context->id);
list($module['intro'], $module['introformat']) = external_format_text($scorm->intro, $scorm->introformat, $context->id, 'mod_scorm', 'intro', $scorm->id);
// Check if the SCORM open and return warnings if so.
list($open, $openwarnings) = scorm_get_availability_status($scorm, true, $context);
if (!$open) {
foreach ($openwarnings as $warningkey => $warningdata) {
$warnings[] = array('item' => 'scorm', 'itemid' => $scorm->id, 'warningcode' => $warningkey, 'message' => get_string($warningkey, 'scorm', $warningdata));
}
} else {
$module['packagesize'] = 0;
// SCORM size.
if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) {
$module['packagesize'] = $packagefile->get_filesize();
// Download URL.
$module['packageurl'] = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)->out(false);
}
}
$module['protectpackagedownloads'] = get_config('scorm', 'protectpackagedownloads');
$viewablefields = array('version', 'maxgrade', 'grademethod', 'whatgrade', 'maxattempt', 'forcecompleted', 'forcenewattempt', 'lastattemptlock', 'displayattemptstatus', 'displaycoursestructure', 'sha1hash', 'md5hash', 'revision', 'launch', 'skipview', 'hidebrowse', 'hidetoc', 'nav', 'navpositionleft', 'navpositiontop', 'auto', 'popup', 'width', 'height', 'timeopen', 'timeclose', 'displayactivityname', 'scormtype', 'reference');
// Check additional permissions for returning optional private settings.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('updatefreq', 'options', 'completionstatusrequired', 'completionscorerequired', 'autocommit', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
$viewablefields = array_merge($viewablefields, $additionalfields);
}
foreach ($viewablefields as $field) {
$module[$field] = $scorm->{$field};
}
}
$returnedscorms[] = $module;
}
}
$result = array();
$result['scorms'] = $returnedscorms;
$result['warnings'] = $warnings;
return $result;
}
示例15: test_mod_forum_get_forum_discussion_posts
/**
* Test get forum posts
*/
public function test_mod_forum_get_forum_discussion_posts()
{
global $CFG;
$this->resetAfterTest(true);
// Set the CFG variable to allow track forums.
$CFG->forum_trackreadposts = true;
// Create a user who can track forums.
$record = new stdClass();
$record->trackforums = true;
$user1 = self::getDataGenerator()->create_user($record);
// Create a bunch of other users to post.
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Set the first created user to the test user.
self::setUser($user1);
// Create course to add the module.
$course1 = self::getDataGenerator()->create_course();
// Forum with tracking off.
$record = new stdClass();
$record->course = $course1->id;
$record->trackingtype = FORUM_TRACKING_OFF;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
$forum1context = context_module::instance($forum1->cmid);
// Add discussions to the forums.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user2->id;
$record->forum = $forum1->id;
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Add 2 replies to the discussion 1 from different users.
$record = new stdClass();
$record->discussion = $discussion1->id;
$record->parent = $discussion1->firstpost;
$record->userid = $user2->id;
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
$record->parent = $discussion1reply1->id;
$record->userid = $user3->id;
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
// Following line enrol and assign default role id to the user.
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user3->id, $course1->id);
// Create what we expect to be returned when querying the discussion.
$expectedposts = array('posts' => array(), 'warnings' => array());
$userpictureurl = moodle_url::make_webservice_pluginfile_url(context_user::instance($discussion1reply2->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false);
$expectedposts['posts'][] = array('id' => $discussion1reply2->id, 'discussion' => $discussion1reply2->discussion, 'parent' => $discussion1reply2->parent, 'userid' => (int) $discussion1reply2->userid, 'created' => $discussion1reply2->created, 'modified' => $discussion1reply2->modified, 'mailed' => $discussion1reply2->mailed, 'subject' => $discussion1reply2->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply2->messagetrust, 'attachment' => $discussion1reply2->attachment, 'totalscore' => $discussion1reply2->totalscore, 'mailnow' => $discussion1reply2->mailnow, 'children' => array(), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user3), 'userpictureurl' => $userpictureurl);
$userpictureurl = moodle_url::make_webservice_pluginfile_url(context_user::instance($discussion1reply1->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false);
$expectedposts['posts'][] = array('id' => $discussion1reply1->id, 'discussion' => $discussion1reply1->discussion, 'parent' => $discussion1reply1->parent, 'userid' => (int) $discussion1reply1->userid, 'created' => $discussion1reply1->created, 'modified' => $discussion1reply1->modified, 'mailed' => $discussion1reply1->mailed, 'subject' => $discussion1reply1->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply1->messagetrust, 'attachment' => $discussion1reply1->attachment, 'totalscore' => $discussion1reply1->totalscore, 'mailnow' => $discussion1reply1->mailnow, 'children' => array($discussion1reply2->id), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user2), 'userpictureurl' => $userpictureurl);
// Test a discussion with two additional posts (total 3 posts).
$posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertEquals(3, count($posts['posts']));
// Unset the initial discussion post.
array_pop($posts['posts']);
$this->assertEquals($expectedposts, $posts);
// Test discussion without additional posts. There should be only one post (the one created by the discussion).
$posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC');
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertEquals(1, count($posts['posts']));
}