/**
* Returns a list of glossaries in a provided list of courses.
*
* If no list is provided all glossaries that the user can view will be returned.
*
* @param array $courseids the course IDs.
* @return array of glossaries
* @since Moodle 3.1
*/
public static function get_glossaries_by_courses($courseids = array())
{
$params = self::validate_parameters(self::get_glossaries_by_courses_parameters(), array('courseids' => $courseids));
$warnings = array();
$courses = array();
$courseids = $params['courseids'];
if (empty($courseids)) {
$courses = enrol_get_my_courses();
$courseids = array_keys($courses);
}
// Array to store the glossaries to return.
$glossaries = array();
// Ensure there are courseids to loop through.
if (!empty($courseids)) {
list($courses, $warnings) = external_util::validate_courses($courseids, $courses);
// Get the glossaries in these courses, this function checks users visibility permissions.
$glossaries = get_all_instances_in_courses('glossary', $courses);
foreach ($glossaries as $glossary) {
$context = context_module::instance($glossary->coursemodule);
$glossary->name = external_format_string($glossary->name, $context->id);
list($glossary->intro, $glossary->introformat) = external_format_text($glossary->intro, $glossary->introformat, $context->id, 'mod_glossary', 'intro', null);
}
}
$result = array();
$result['glossaries'] = $glossaries;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns a list of wikis in a provided list of courses,
* if no list is provided all wikis that the user can view will be returned.
*
* @param array $courseids The courses IDs.
* @return array Containing a list of warnings and a list of wikis.
* @since Moodle 3.1
*/
public static function get_wikis_by_courses($courseids = array())
{
$returnedwikis = array();
$warnings = array();
$params = self::validate_parameters(self::get_wikis_by_courses_parameters(), array('courseids' => $courseids));
$mycourses = array();
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
// Get the wikis in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$wikis = get_all_instances_in_courses('wiki', $courses);
foreach ($wikis as $wiki) {
$context = context_module::instance($wiki->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'] = $wiki->id;
$module['coursemodule'] = $wiki->coursemodule;
$module['course'] = $wiki->course;
$module['name'] = external_format_string($wiki->name, $context->id);
$viewablefields = [];
if (has_capability('mod/wiki:viewpage', $context)) {
list($module['intro'], $module['introformat']) = external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', $wiki->id);
$viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode', 'groupingid');
}
// Check additional permissions for returning optional private settings.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('timecreated', 'timemodified');
$viewablefields = array_merge($viewablefields, $additionalfields);
}
foreach ($viewablefields as $field) {
$module[$field] = $wiki->{$field};
}
// Check if user can add new pages.
$module['cancreatepages'] = wiki_can_create_pages($context);
$returnedwikis[] = $module;
}
}
$result = array();
$result['wikis'] = $returnedwikis;
$result['warnings'] = $warnings;
return $result;
}
/**
* Get the feedback text that should be show to a student who got the given grade in the given quiz.
*
* @param int $quizid quiz instance id
* @param float $grade the grade to check
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_quiz_feedback_for_grade($quizid, $grade)
{
global $DB;
$params = array('quizid' => $quizid, 'grade' => $grade);
$params = self::validate_parameters(self::get_quiz_feedback_for_grade_parameters(), $params);
$warnings = array();
list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']);
$result = array();
$result['feedbacktext'] = '';
$result['feedbacktextformat'] = FORMAT_MOODLE;
$feedback = quiz_feedback_record_for_grade($params['grade'], $quiz);
if (!empty($feedback->feedbacktext)) {
list($text, $format) = external_format_text($feedback->feedbacktext, $feedback->feedbacktextformat, $context->id, 'mod_quiz', 'feedback', $feedback->id);
$result['feedbacktext'] = $text;
$result['feedbacktextformat'] = $format;
}
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns a list of databases in a provided list of courses,
* if no list is provided all databases that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array the database details
* @since Moodle 2.9
*/
public static function get_databases_by_courses($courseids = array())
{
global $CFG;
$params = self::validate_parameters(self::get_databases_by_courses_parameters(), array('courseids' => $courseids));
$warnings = array();
if (!empty($params['courseids'])) {
$courses = array();
$courseids = $params['courseids'];
} else {
$courses = enrol_get_my_courses();
$courseids = array_keys($courses);
}
// Array to store the databases to return.
$arrdatabases = array();
// Ensure there are courseids to loop through.
if (!empty($courseids)) {
// Array of the courses we are going to retrieve the databases from.
$dbcourses = array();
// Go through the courseids.
foreach ($courseids as $cid) {
// Check the user can function in this context.
try {
$context = context_course::instance($cid);
self::validate_context($context);
// Check if this course was already loaded (by enrol_get_my_courses).
if (!isset($courses[$cid])) {
$courses[$cid] = get_course($cid);
}
$dbcourses[$cid] = $courses[$cid];
} catch (Exception $e) {
$warnings[] = array('item' => 'course', 'itemid' => $cid, 'warningcode' => '1', 'message' => 'No access rights in course context ' . $e->getMessage());
}
}
// Get the databases in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$databases = get_all_instances_in_courses("data", $dbcourses);
foreach ($databases as $database) {
$datacontext = context_module::instance($database->coursemodule);
// Entry to return.
$newdb = array();
// First, we return information that any user can see in the web interface.
$newdb['id'] = $database->id;
$newdb['coursemodule'] = $database->coursemodule;
$newdb['course'] = $database->course;
$newdb['name'] = $database->name;
// Format intro.
list($newdb['intro'], $newdb['introformat']) = external_format_text($database->intro, $database->introformat, $datacontext->id, 'mod_data', 'intro', null);
// This information should be only available if the user can see the database entries.
if (has_capability('mod/data:viewentry', $datacontext)) {
$viewablefields = array('comments', 'timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview');
// This is for avoid a long repetitive list and for
// checking that we are retrieving all the required fields.
foreach ($viewablefields as $field) {
// We do not use isset because it won't work for existing null values.
if (!property_exists($database, $field)) {
throw new invalid_response_exception('Missing database module required field: ' . $field);
}
$newdb[$field] = $database->{$field};
}
}
// Check additional permissions for returning optional private settings.
// I avoid intentionally to use can_[add|update]_moduleinfo.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
// This is for avoid a long repetitive list.
foreach ($additionalfields as $field) {
if (property_exists($database, $field)) {
$newdb[$field] = $database->{$field};
}
}
}
$arrdatabases[] = $newdb;
}
}
$result = array();
$result['databases'] = $arrdatabases;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns a list of chats in a provided list of courses,
* if no list is provided all chats that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array of chats details
* @since Moodle 3.0
*/
public static function get_chats_by_courses($courseids = array())
{
global $CFG;
$returnedchats = array();
$warnings = array();
$params = self::validate_parameters(self::get_chats_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 chats in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$chats = get_all_instances_in_courses("chat", $courses);
foreach ($chats as $chat) {
$chatcontext = context_module::instance($chat->coursemodule);
// Entry to return.
$chatdetails = array();
// First, we return information that any user can see in the web interface.
$chatdetails['id'] = $chat->id;
$chatdetails['coursemodule'] = $chat->coursemodule;
$chatdetails['course'] = $chat->course;
$chatdetails['name'] = external_format_string($chat->name, $chatcontext->id);
// Format intro.
list($chatdetails['intro'], $chatdetails['introformat']) = external_format_text($chat->intro, $chat->introformat, $chatcontext->id, 'mod_chat', 'intro', null);
if (has_capability('mod/chat:chat', $chatcontext)) {
$chatdetails['chatmethod'] = $CFG->chat_method;
$chatdetails['keepdays'] = $chat->keepdays;
$chatdetails['studentlogs'] = $chat->studentlogs;
$chatdetails['chattime'] = $chat->chattime;
$chatdetails['schedule'] = $chat->schedule;
}
if (has_capability('moodle/course:manageactivities', $chatcontext)) {
$chatdetails['timemodified'] = $chat->timemodified;
$chatdetails['section'] = $chat->section;
$chatdetails['visible'] = $chat->visible;
$chatdetails['groupmode'] = $chat->groupmode;
$chatdetails['groupingid'] = $chat->groupingid;
}
$returnedchats[] = $chatdetails;
}
}
$result = array();
$result['chats'] = $returnedchats;
$result['warnings'] = $warnings;
return $result;
}
/**
* Format the text using the external API.
* This function should we used when text formatting is required in external functions.
*
* @return array an array containing the text formatted and the text format
*/
public function format_external_text()
{
if ($this->editorcontext === null) {
// Switch on the event type to decide upon the appropriate context to use for this event.
$this->editorcontext = $this->properties->context;
if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') {
// We don't have a context here, do a normal format_text.
return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id);
}
}
// Work out the item id for the editor, if this is a repeated event then the files will be associated with the original.
if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
$itemid = $this->properties->repeatid;
} else {
$itemid = $this->properties->id;
}
return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id, 'calendar', 'event_description', $itemid);
}
开发者ID:stronk7,项目名称:moodle,代码行数:24,代码来源:lib.php
示例15: 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);
}
/**
* Returns a list of books in a provided list of courses,
* if no list is provided all books that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array of books details
* @since Moodle 3.0
*/
public static function get_books_by_courses($courseids = array())
{
global $CFG;
$returnedbooks = array();
$warnings = array();
$params = self::validate_parameters(self::get_books_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 books in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$books = get_all_instances_in_courses("book", $courses);
foreach ($books as $book) {
$context = context_module::instance($book->coursemodule);
// Entry to return.
$bookdetails = array();
// First, we return information that any user can see in the web interface.
$bookdetails['id'] = $book->id;
$bookdetails['coursemodule'] = $book->coursemodule;
$bookdetails['course'] = $book->course;
$bookdetails['name'] = external_format_string($book->name, $context->id);
// Format intro.
list($bookdetails['intro'], $bookdetails['introformat']) = external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null);
$bookdetails['numbering'] = $book->numbering;
$bookdetails['navstyle'] = $book->navstyle;
$bookdetails['customtitles'] = $book->customtitles;
if (has_capability('moodle/course:manageactivities', $context)) {
$bookdetails['revision'] = $book->revision;
$bookdetails['timecreated'] = $book->timecreated;
$bookdetails['timemodified'] = $book->timemodified;
$bookdetails['section'] = $book->section;
$bookdetails['visible'] = $book->visible;
$bookdetails['groupmode'] = $book->groupmode;
$bookdetails['groupingid'] = $book->groupingid;
}
$returnedbooks[] = $bookdetails;
}
}
$result = array();
$result['books'] = $returnedbooks;
$result['warnings'] = $warnings;
return $result;
}
请发表评论