/** * 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 surveys in a provided list of courses, * if no list is provided all surveys that the user can view will be returned. * * @param array $courseids the course ids * @return array of surveys details * @since Moodle 3.0 */ public static function get_surveys_by_courses($courseids = array()) { global $CFG, $USER, $DB; $returnedsurveys = array(); $warnings = array(); $params = self::validate_parameters(self::get_surveys_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 surveys in this course, this function checks users visibility permissions. // We can avoid then additional validate_context calls. $surveys = get_all_instances_in_courses("survey", $courses); foreach ($surveys as $survey) { $context = context_module::instance($survey->coursemodule); // Entry to return. $surveydetails = array(); // First, we return information that any user can see in the web interface. $surveydetails['id'] = $survey->id; $surveydetails['coursemodule'] = $survey->coursemodule; $surveydetails['course'] = $survey->course; $surveydetails['name'] = external_format_string($survey->name, $context->id); if (has_capability('mod/survey:participate', $context)) { $trimmedintro = trim($survey->intro); if (empty($trimmedintro)) { $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template)); $survey->intro = get_string($tempo, "survey"); } // Format intro. list($surveydetails['intro'], $surveydetails['introformat']) = external_format_text($survey->intro, $survey->introformat, $context->id, 'mod_survey', 'intro', null); $surveydetails['introfiles'] = external_util::get_area_files($context->id, 'mod_survey', 'intro', false, false); $surveydetails['template'] = $survey->template; $surveydetails['days'] = $survey->days; $surveydetails['questions'] = $survey->questions; $surveydetails['surveydone'] = survey_already_done($survey->id, $USER->id) ? 1 : 0; } if (has_capability('moodle/course:manageactivities', $context)) { $surveydetails['timecreated'] = $survey->timecreated; $surveydetails['timemodified'] = $survey->timemodified; $surveydetails['section'] = $survey->section; $surveydetails['visible'] = $survey->visible; $surveydetails['groupmode'] = $survey->groupmode; $surveydetails['groupingid'] = $survey->groupingid; } $returnedsurveys[] = $surveydetails; } } $result = array(); $result['surveys'] = $returnedsurveys; $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); $module['introfiles'] = external_util::get_area_files($context->id, 'mod_wiki', 'intro', false, false); $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; }
/** * Returns a list of forums in a provided list of courses, * if no list is provided all forums that the user can view * will be returned. * * @param array $courseids the course ids * @return array the forum details * @since Moodle 2.5 */ public static function get_forums_by_courses($courseids = array()) { global $CFG; require_once($CFG->dirroot . "/mod/forum/lib.php"); $params = self::validate_parameters(self::get_forums_by_courses_parameters(), array('courseids' => $courseids)); if (empty($params['courseids'])) { $params['courseids'] = array_keys(enrol_get_my_courses()); } // Array to store the forums to return. $arrforums = array(); $warnings = array(); // Ensure there are courseids to loop through. if (!empty($params['courseids'])) { list($courses, $warnings) = external_util::validate_courses($params['courseids']); // Get the forums in this course. This function checks users visibility permissions. $forums = get_all_instances_in_courses("forum", $courses); foreach ($forums as $forum) { $course = $courses[$forum->course]; $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id); $context = context_module::instance($cm->id); // Skip forums we are not allowed to see discussions. if (!has_capability('mod/forum:viewdiscussion', $context)) { continue; } // Format the intro before being returning using the format setting. list($forum->intro, $forum->introformat) = external_format_text($forum->intro, $forum->introformat, $context->id, 'mod_forum', 'intro', 0); // Discussions count. This function does static request cache. $forum->numdiscussions = forum_count_discussions($forum, $cm, $course); $forum->cmid = $forum->coursemodule; $forum->cancreatediscussions = forum_user_can_post_discussion($forum, null, -1, $cm, $context); // Add the forum to the array to return. $arrforums[$forum->id] = $forum; } } return $arrforums; }
/** * Returns a list of quizzes in a provided list of courses, * if no list is provided all quizzes that the user can view will be returned. * * @param array $courseids Array of course ids * @return array of quizzes details * @since Moodle 3.1 */ public static function get_quizzes_by_courses($courseids = array()) { global $USER; $warnings = array(); $returnedquizzes = array(); $params = array('courseids' => $courseids); $params = self::validate_parameters(self::get_quizzes_by_courses_parameters(), $params); $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 quizzes in this course, this function checks users visibility permissions. // We can avoid then additional validate_context calls. $quizzes = get_all_instances_in_courses("quiz", $courses); foreach ($quizzes as $quiz) { $context = context_module::instance($quiz->coursemodule); // Update quiz with override information. $quiz = quiz_update_effective_access($quiz, $USER->id); // Entry to return. $quizdetails = array(); // First, we return information that any user can see in the web interface. $quizdetails['id'] = $quiz->id; $quizdetails['coursemodule'] = $quiz->coursemodule; $quizdetails['course'] = $quiz->course; $quizdetails['name'] = external_format_string($quiz->name, $context->id); if (has_capability('mod/quiz:view', $context)) { // Format intro. list($quizdetails['intro'], $quizdetails['introformat']) = external_format_text($quiz->intro, $quiz->introformat, $context->id, 'mod_quiz', 'intro', null); $viewablefields = array('timeopen', 'timeclose', 'grademethod', 'section', 'visible', 'groupmode', 'groupingid'); $timenow = time(); $quizobj = quiz::create($quiz->id, $USER->id); $accessmanager = new quiz_access_manager($quizobj, $timenow, has_capability('mod/quiz:ignoretimelimits', $context, null, false)); // Fields the user could see if have access to the quiz. if (!$accessmanager->prevent_access()) { // Some times this function returns just empty. $hasfeedback = quiz_has_feedback($quiz); $quizdetails['hasfeedback'] = !empty($hasfeedback) ? 1 : 0; $quizdetails['hasquestions'] = (int) $quizobj->has_questions(); $quizdetails['autosaveperiod'] = get_config('quiz', 'autosaveperiod'); $additionalfields = array('timelimit', 'attempts', 'attemptonlast', 'grademethod', 'decimalpoints', 'questiondecimalpoints', 'reviewattempt', 'reviewcorrectness', 'reviewmarks', 'reviewspecificfeedback', 'reviewgeneralfeedback', 'reviewrightanswer', 'reviewoverallfeedback', 'questionsperpage', 'navmethod', 'sumgrades', 'grade', 'browsersecurity', 'delay1', 'delay2', 'showuserpicture', 'showblocks', 'completionattemptsexhausted', 'completionpass', 'overduehandling', 'graceperiod', 'preferredbehaviour', 'canredoquestions'); $viewablefields = array_merge($viewablefields, $additionalfields); } // Fields only for managers. if (has_capability('moodle/course:manageactivities', $context)) { $additionalfields = array('shuffleanswers', 'timecreated', 'timemodified', 'password', 'subnet'); $viewablefields = array_merge($viewablefields, $additionalfields); } foreach ($viewablefields as $field) { $quizdetails[$field] = $quiz->{$field}; } } $returnedquizzes[] = $quizdetails; } } $result = array(); $result['quizzes'] = $returnedquizzes; $result['warnings'] = $warnings; return $result; }
/** * Return a list of administration options in a set of courses that are available or not for the current user. * * @param array $courseids a list of course ids * @return array of warnings and the options availability * @since Moodle 3.2 * @throws moodle_exception */ public static function get_user_administration_options($courseids) { global $CFG; require_once $CFG->dirroot . '/course/lib.php'; // Parameter validation. $params = self::validate_parameters(self::get_user_administration_options_parameters(), array('courseids' => $courseids)); $courseoptions = array(); list($courses, $warnings) = external_util::validate_courses($params['courseids'], array(), true); if (!empty($courses)) { foreach ($courses as $course) { $adminoptions = course_get_user_administration_options($course, $course->context); $options = array(); foreach ($adminoptions as $name => $available) { $options[] = array('name' => $name, 'available' => $available); } $courseoptions[] = array('id' => $course->id, 'options' => $options); } } $result = array('courses' => $courseoptions, '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; }
/** * Validate courses can re-use an array of prefetched courses. */ public function test_validate_courses_prefetch() { $this->resetAfterTest(true); $c1 = $this->getDataGenerator()->create_course(); $c2 = $this->getDataGenerator()->create_course(); $c3 = $this->getDataGenerator()->create_course(); $c4 = $this->getDataGenerator()->create_course(); $u1 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($u1->id, $c1->id); $this->getDataGenerator()->enrol_user($u1->id, $c2->id); $courseids = array($c1->id, $c2->id, $c3->id); $courses = array($c2->id => $c2, $c3->id => $c3, $c4->id => $c4); $this->setUser($u1); list($courses, $warnings) = external_util::validate_courses($courseids, $courses); $this->assertCount(2, $courses); $this->assertCount(1, $warnings); $this->assertArrayHasKey($c1->id, $courses); $this->assertSame($c2, $courses[$c2->id]); $this->assertArrayNotHasKey($c3->id, $courses); // The extra course passed is not returned. $this->assertArrayNotHasKey($c4->id, $courses); }
/** * 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; }
/** * 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; }
/** * 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(); $mycourses = array(); if (empty($params['courseids'])) { $mycourses = enrol_get_my_courses(); $params['courseids'] = array_keys($mycourses); } // Array to store the databases to return. $arrdatabases = array(); // Ensure there are courseids to loop through. if (!empty($params['courseids'])) { list($dbcourses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses); // 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'] = external_format_string($database->name, $datacontext->id); // Format intro. list($newdb['intro'], $newdb['introformat']) = external_format_text($database->intro, $database->introformat, $datacontext->id, 'mod_data', 'intro', null); $newdb['introfiles'] = external_util::get_area_files($datacontext->id, 'mod_data', 'intro', false, false); // 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', $datacontext)) { $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'manageapproved', 'scale', 'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification', 'timemodified'); // 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 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(); $modes = 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); $glossary->introfiles = external_util::get_area_files($context->id, 'mod_glossary', 'intro', false, false); // Make sure we have a number of entries per page. if (!$glossary->entbypage) { $glossary->entbypage = $CFG->glossary_entbypage; } // Add the list of browsing modes. if (!isset($modes[$glossary->displayformat])) { $modes[$glossary->displayformat] = self::get_browse_modes_from_display_format($glossary->displayformat); } $glossary->browsemodes = $modes[$glossary->displayformat]; $glossary->canaddentry = has_capability('mod/glossary:write', $context) ? 1 : 0; } } $result = array(); $result['glossaries'] = $glossaries; $result['warnings'] = $warnings; return $result; }
/** * Returns a list of external tools in a provided list of courses, * if no list is provided all external tools that the user can view will be returned. * * @param array $courseids the course ids * @return array the lti details * @since Moodle 3.0 */ public static function get_ltis_by_courses($courseids = array()) { global $CFG; $returnedltis = array(); $warnings = array(); $params = self::validate_parameters(self::get_ltis_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 ltis in this course, this function checks users visibility permissions. // We can avoid then additional validate_context calls. $ltis = get_all_instances_in_courses("lti", $courses); foreach ($ltis as $lti) { $context = context_module::instance($lti->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'] = $lti->id; $module['coursemodule'] = $lti->coursemodule; $module['course'] = $lti->course; $module['name'] = external_format_string($lti->name, $context->id); $viewablefields = []; if (has_capability('mod/lti:view', $context)) { list($module['intro'], $module['introformat']) = external_format_text($lti->intro, $lti->introformat, $context->id, 'mod_lti', 'intro', $lti->id); $module['introfiles'] = external_util::get_area_files($context->id, 'mod_lti', 'intro', false, false); $viewablefields = array('launchcontainer', 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon'); } // Check additional permissions for returning optional private settings. if (has_capability('moodle/course:manageactivities', $context)) { $additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl', 'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster', 'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade', 'resourcekey', 'password', 'debuglaunch', 'servicesalt', 'visible', 'groupmode', 'groupingid'); $viewablefields = array_merge($viewablefields, $additionalfields); } foreach ($viewablefields as $field) { $module[$field] = $lti->{$field}; } $returnedltis[] = $module; } } $result = array(); $result['ltis'] = $returnedltis; $result['warnings'] = $warnings; return $result; }
/** * Return activities overview for the given courses. * * @param array $courseids a list of course ids * @return array of warnings and the activities overview * @since Moodle 3.2 * @throws moodle_exception */ public static function get_activities_overview($courseids) { global $USER; // Parameter validation. $params = self::validate_parameters(self::get_activities_overview_parameters(), array('courseids' => $courseids)); $courseoverviews = array(); list($courses, $warnings) = external_util::validate_courses($params['courseids']); if (!empty($courses)) { // Add lastaccess to each course (required by print_overview function). // We need the complete user data, the ws server does not load a complete one. $user = get_complete_user_data('id', $USER->id); foreach ($courses as $course) { if (isset($user->lastcourseaccess[$course->id])) { $course->lastaccess = $user->lastcourseaccess[$course->id]; } else { $course->lastaccess = 0; } } $overviews = array(); if ($modules = get_plugin_list_with_function('mod', 'print_overview')) { foreach ($modules as $fname) { $fname($courses, $overviews); } } // Format output. foreach ($overviews as $courseid => $modules) { $courseoverviews[$courseid]['id'] = $courseid; $courseoverviews[$courseid]['overviews'] = array(); foreach ($modules as $modname => $overviewtext) { $courseoverviews[$courseid]['overviews'][] = array('module' => $modname, 'overviewtext' => $overviewtext); } } } $result = array('courses' => $courseoverviews, 'warnings' => $warnings); return $result; }
/** * Returns a list of choices in a provided list of courses, * if no list is provided all choices that the user can view will be returned. * * @param array $courseids the course ids * @return array of choices details * @since Moodle 3.0 */ public static function get_choices_by_courses($courseids = array()) { global $CFG; $returnedchoices = array(); $warnings = array(); $params = self::validate_parameters(self::get_choices_by_courses_parameters(), array('courseids' => $courseids)); if (empty($params['courseids'])) { $params['courseids'] = array_keys(enrol_get_my_courses()); } // Ensure there are courseids to loop through. if (!empty($params['courseids'])) { list($courses, $warnings) = external_util::validate_courses($params['courseids']); // Get the choices in this course, this function checks users visibility permissions. // We can avoid then additional validate_context calls. $choices = get_all_instances_in_courses("choice", $courses); foreach ($choices as $choice) { $context = context_module::instance($choice->coursemodule); // Entry to return. $choicedetails = array(); // First, we return information that any user can see in the web interface. $choicedetails['id'] = $choice->id; $choicedetails['coursemodule'] = $choice->coursemodule; $choicedetails['course'] = $choice->course; $choicedetails['name'] = external_format_string($choice->name, $context->id); // Format intro. list($choicedetails['intro'], $choicedetails['introformat']) = external_format_text($choice->intro, $choice->introformat, $context->id, 'mod_choice', 'intro', null); if (has_capability('mod/choice:choose', $context)) { $choicedetails['publish'] = $choice->publish; $choicedetails['showresults'] = $choice->showresults; $choicedetails['showpreview'] = $choice->showpreview; $choicedetails['timeopen'] = $choice->timeopen; $choicedetails['timeclose'] = $choice->timeclose; $choicedetails['display'] = $choice->display; $choicedetails['allowupdate'] = $choice->allowupdate; $choicedetails['allowmultiple'] = $choice->allowmultiple; $choicedetails['limitanswers'] = $choice->limitanswers; $choicedetails['showunanswered'] = $choice->showunanswered; $choicedetails['includeinactive'] = $choice->includeinactive; } if (has_capability('moodle/course:manageactivities', $context)) { $choicedetails['timemodified'] = $choice->timemodified; $choicedetails['completionsubmit'] = $choice->completionsubmit; $choicedetails['section'] = $choice->section; $choicedetails['visible'] = $choice->visible; $choicedetails['groupmode'] = $choice->groupmode; $choicedetails['groupingid'] = $choice->groupingid; } $returnedchoices[] = $choicedetails; } } $result = array(); $result['choices'] = $returnedchoices; $result['warnings'] = $warnings; return $result; }
/** * Returns a list of IMSCP packages in a provided list of courses, * if no list is provided all IMSCP packages that the user can view will be returned. * * @param array $courseids the course ids * @return array of IMSCP packages details and possible warnings * @since Moodle 3.0 */ public static function get_imscps_by_courses($courseids = array()) { global $CFG; $returnedimscps = array(); $warnings = array(); $params = self::validate_parameters(self::get_imscps_by_courses_parameters(), array('courseids' => $courseids)); if (empty($params['courseids'])) { $params['courseids'] = array_keys(enrol_get_my_courses()); } // Ensure there are courseids to loop through. if (!empty($params['courseids'])) { list($courses, $warnings) = external_util::validate_courses($params['courseids']); // Get the imscps in this course, this function checks users visibility permissions. // We can avoid then additional validate_context calls. $imscps = get_all_instances_in_courses("imscp", $courses); foreach ($imscps as $imscp) { $context = context_module::instance($imscp->coursemodule); // Entry to return. $imscpdetails = array(); // First, we return information that any user can see in the web interface. $imscpdetails['id'] = $imscp->id; $imscpdetails['coursemodule'] = $imscp->coursemodule; $imscpdetails['course'] = $imscp->course; $imscpdetails['name'] = format_string($imscp->name, true, array('context' => $context)); if (has_capability('mod/imscp:view', $context)) { // Format intro. list($imscpdetails['intro'], $imscpdetails['introformat']) = external_format_text($imscp->intro, $imscp->introformat, $context->id, 'mod_imscp', 'intro', null); } if (has_capability('moodle/course:manageactivities', $context)) { $imscpdetails['revision'] = $imscp->revision; $imscpdetails['keepold'] = $imscp->keepold; $imscpdetails['structure'] = $imscp->structure; $imscpdetails['timemodified'] = $imscp->timemodified; $imscpdetails['section'] = $imscp->section; $imscpdetails['visible'] = $imscp->visible; $imscpdetails['groupmode'] = $imscp->groupmode; $imscpdetails['groupingid'] = $imscp->groupingid; } $returnedimscps[] = $imscpdetails; } } $result = array(); $result['imscps'] = $returnedimscps; $result['warnings'] = $warnings; return $result; }