/** * Returns whether the discussion group is visible by the current user or not. * * @since Moodle 2.8, 2.7.1, 2.6.4 * @param cm_info $cm The discussion course module * @param int $discussiongroupid The discussion groupid * @return bool */ function forum_is_user_group_discussion(cm_info $cm, $discussiongroupid) { if ($discussiongroupid == -1 || $cm->effectivegroupmode != SEPARATEGROUPS) { return true; } if (isguestuser()) { return false; } if (has_capability('moodle/site:accessallgroups', context_module::instance($cm->id)) || in_array($discussiongroupid, $cm->get_modinfo()->get_groups($cm->groupingid))) { return true; } return false; }
/** * Obtains shared data that is used in print_section when displaying a * course-module entry. * * Calls format_text or format_string as appropriate, and obtains the correct icon. * * This data is also used in other areas of the code. * @param cm_info $cm Course-module data (must come from get_fast_modinfo) * @param object $course Moodle course object * @return array An array with the following values in this order: * $content (optional extra content for after link), * $instancename (text of link) */ function get_print_section_cm_text(cm_info $cm, $course) { global $OUTPUT; // Get content from modinfo if specified. Content displays either // in addition to the standard link (below), or replaces it if // the link is turned off by setting ->url to null. if (($content = $cm->get_content()) !== '') { // Improve filter performance by preloading filter setttings for all // activities on the course (this does nothing if called multiple // times) filter_preload_activities($cm->get_modinfo()); // Get module context $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id); $labelformatoptions = new stdClass(); $labelformatoptions->noclean = true; $labelformatoptions->overflowdiv = true; $labelformatoptions->context = $modulecontext; $content = format_text($content, FORMAT_HTML, $labelformatoptions); } else { $content = ''; } // Get course context $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $stringoptions = new stdClass(); $stringoptions->context = $coursecontext; $instancename = format_string($cm->name, true, $stringoptions); return array($content, $instancename); }
/** * For an activity LO, retrieves the moodle id of the following activity/resource (course_module) in the section of the activity * or null if this is the last activity in that section * @param \cm_info $rawData Moodle internal info for a course module * @return string|NULL id of the following activity/resource (Course_module) */ private function getFollowingSib($rawData) { $course_info = $rawData->get_modinfo(); //get the course_modinfo object with all data of the course $section = $course_info->get_section_info($rawData->sectionnum); $cmsId = explode(',', $section->sequence); //array with the ids of the cms in that section $position = array_keys($cmsId, $rawData->id); //position of the cm in the sequence $pos = $position[0]; $num_mods = count($cmsId); while (true) { // if($position[0]==(count($cmsId)-1)){ //it is the last in the section, not previous cm if ($pos == $num_mods - 1) { //it is the last in the section, not previous cm $FollowingSib = null; break; } else { // get the previous cm in the sequence $FollowingSib = $cmsId[$pos + 1]; // check if type is ignored $factory = Intuitel::getAdaptorInstance()->getGuessedFactory($course_info->get_cm($FollowingSib)); if (!$factory->toBeIgnored()) { break; } } $pos++; } // end of while return $FollowingSib; }
/** * Checks if an activity is visible to the given user. * * Unlike other checks in the availability system, this check includes the * $cm->visible flag. It is equivalent to $cm->uservisible. * * If you have already checked (or do not care whether) the user has access * to the course, you can set $checkcourse to false to save it checking * course access. * * When checking for the current user, you should generally not call * this function. Instead, use get_fast_modinfo to get a cm_info object, * then simply check the $cm->uservisible flag. This function is intended * to obtain that information for a separate course-module object that * wasn't loaded with get_fast_modinfo, or for a different user. * * This function has a performance cost unless the availability system is * disabled, and you supply a $cm object with necessary fields, and you * don't check course access. * * @param int|\stdClass|\cm_info $cmorid Object or id representing activity * @param int $userid User id (0 = current user) * @param bool $checkcourse If true, checks whether the user has course access * @return bool True if the activity is visible to the specified user * @throws \moodle_exception If the cmid doesn't exist */ public static function is_user_visible($cmorid, $userid = 0, $checkcourse = true) { global $USER, $DB, $CFG; // Evaluate user id. if (!$userid) { $userid = $USER->id; } // If this happens to be already called with a cm_info for the right user // then just return uservisible. if ($cmorid instanceof \cm_info && $cmorid->get_modinfo()->userid == $userid) { return $cmorid->uservisible; } // If the $cmorid isn't an object or doesn't have required fields, load it. if (is_object($cmorid) && isset($cmorid->course) && isset($cmorid->visible)) { $cm = $cmorid; } else { if (is_object($cmorid)) { $cmorid = $cmorid->id; } $cm = $DB->get_record('course_modules', array('id' => $cmorid)); if (!$cm) { // In some error cases, the course module may not exist. debugging('info_module::is_user_visible called with invalid cmid ' . $cmorid, DEBUG_DEVELOPER); return false; } } // If requested, check user can access the course. if ($checkcourse) { $coursecontext = \context_course::instance($cm->course); if (!is_enrolled($coursecontext, $userid, '', true) && !has_capability('moodle/course:view', $coursecontext, $userid)) { return false; } } // If availability is disabled, then all we need to do is check the visible flag. if (!$CFG->enableavailability && $cm->visible) { return true; } // When availability is enabled, access can depend on 3 things: // 1. $cm->visible // 2. $cm->availability // 3. $section->availability (for activity section and possibly for // parent sections) // As a result we cannot take short cuts any longer and must get // standard modinfo. $modinfo = get_fast_modinfo($cm->course, $userid); $cms = $modinfo->get_cms(); if (!isset($cms[$cm->id])) { // In some cases this might get called with a cmid that is no longer // available, for example when a module is hidden at system level. debugging('info_module::is_user_visible called with invalid cmid ' . $cm->id, DEBUG_DEVELOPER); return false; } return $cms[$cm->id]->uservisible; }
/** * Checks if an activity is visible to the given user. * * Unlike other checks in the availability system, this check includes the * $cm->visible flag and also (if enabled) the groupmembersonly feature. * It is equivalent to $cm->uservisible. * * If you have already checked (or do not care whether) the user has access * to the course, you can set $checkcourse to false to save it checking * course access. * * When checking for the current user, you should generally not call * this function. Instead, use get_fast_modinfo to get a cm_info object, * then simply check the $cm->uservisible flag. This function is intended * to obtain that information for a separate course-module object that * wasn't loaded with get_fast_modinfo, or for a different user. * * This function has a performance cost unless the availability system is * disabled, and you supply a $cm object with necessary fields, and you * don't check course access. * * @param int|\stdClass|\cm_info $cmorid Object or id representing activity * @param int $userid User id (0 = current user) * @param bool $checkcourse If true, checks whether the user has course access * @return bool True if the activity is visible to the specified user * @throws \moodle_exception If the cmid doesn't exist */ public static function is_user_visible($cmorid, $userid = 0, $checkcourse = true) { global $USER, $DB, $CFG; // Evaluate user id. if (!$userid) { $userid = $USER->id; } // If this happens to be already called with a cm_info for the right user // then just return uservisible. if ($cmorid instanceof \cm_info && $cmorid->get_modinfo()->userid == $userid) { return $cmorid->uservisible; } // If the $cmorid isn't an object or doesn't have required fields, load it. if (is_object($cmorid) && isset($cmorid->course) && isset($cmorid->visible)) { $cm = $cmorid; } else { if (is_object($cmorid)) { $cmorid = $cmorid->id; } $cm = $DB->get_record('course_modules', array('id' => $cmorid), '*', MUST_EXIST); } // Check the groupmembersonly feature. if (!groups_course_module_visible($cm, $userid)) { return false; } // If requested, check user can access the course. if ($checkcourse) { $coursecontext = \context_course::instance($cm->course); if (!is_enrolled($coursecontext, $userid, '', true) && !has_capability('moodle/course:view', $coursecontext, $userid)) { return false; } } // If availability is disabled, then all we need to do is check the visible flag. if (!$CFG->enableavailability && $cm->visible) { return true; } // When availability is enabled, access can depend on 3 things: // 1. $cm->visible // 2. $cm->availability // 3. $section->availability (for activity section and possibly for // parent sections) // As a result we cannot take short cuts any longer and must get // standard modinfo. $modinfo = get_fast_modinfo($cm->course, $userid); return $modinfo->get_cm($cm->id)->uservisible; }