Skip to content

Commit

Permalink
Merge branch 'MDL-55415-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Sep 6, 2016
2 parents f0ca10f + b9050b1 commit 05842d7
Show file tree
Hide file tree
Showing 7 changed files with 643 additions and 67 deletions.
156 changes: 156 additions & 0 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2611,4 +2611,160 @@ public static function get_activities_overview_returns() {
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.2
*/
public static function get_user_navigation_options_parameters() {
return new external_function_parameters(
array(
'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'Course id.')),
)
);
}

/**
* Return a list of navigation options in a set of courses that are avaialable 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_navigation_options($courseids) {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');

// Parameter validation.
$params = self::validate_parameters(self::get_user_navigation_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) {
// Fix the context for the frontpage.
if ($course->id == SITEID) {
$course->context = context_system::instance();
}
$navoptions = course_get_user_navigation_options($course->context, $course);
$options = array();
foreach ($navoptions 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 description of method result value
*
* @return external_description
* @since Moodle 3.2
*/
public static function get_user_navigation_options_returns() {
return new external_single_structure(
array(
'courses' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Course id'),
'options' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_ALPHANUMEXT, 'Option name'),
'available' => new external_value(PARAM_BOOL, 'Whether the option is available or not'),
)
)
)
)
), 'List of courses'
),
'warnings' => new external_warnings()
)
);
}

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.2
*/
public static function get_user_administration_options_parameters() {
return new external_function_parameters(
array(
'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'Course id.')),
)
);
}

/**
* 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 description of method result value
*
* @return external_description
* @since Moodle 3.2
*/
public static function get_user_administration_options_returns() {
return self::get_user_navigation_options_returns();
}
}
104 changes: 104 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4073,3 +4073,107 @@ function course_get_tagged_course_modules($tag, $exclusivemode = false, $fromcon
$exclusivemode, $fromcontextid, $contextid, $recursivecontext, $page, $totalpages);
}
}

/**
* Return an object with the list of navigation options in a course that are avaialable or not for the current user.
* This function also handles the frontpage course.
*
* @param stdClass $context context object (it can be a course context or the system context for frontpage settings)
* @param stdClass $course the course where the settings are being rendered (only used when $context is set to frontpage)
* @return stdClass the navigation options in a course and their availability status
* @since Moodle 3.2
*/
function course_get_user_navigation_options($context, $course = null) {
global $CFG;

$isloggedin = isloggedin();
$isguestuser = isguestuser();
$isfrontpage = $context->contextlevel == CONTEXT_SYSTEM;

if ($isfrontpage) {
$sitecontext = $context;
} else {
$sitecontext = context_system::instance();
}

$options = new stdClass;
$options->blogs = !empty($CFG->enableblogs) &&
($CFG->bloglevel == BLOG_GLOBAL_LEVEL ||
($CFG->bloglevel == BLOG_SITE_LEVEL and ($isloggedin and !$isguestuser)))
&& has_capability('moodle/blog:view', $sitecontext);

$options->notes = !empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $context);

// Frontpage settings?
if ($isfrontpage) {
if ($course->id == SITEID) {
$options->participants = has_capability('moodle/site:viewparticipants', $sitecontext);
} else {
$options->participants = has_capability('moodle/course:viewparticipants', context_course::instance($course->id));
}

$options->badges = !empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $sitecontext);
$options->tags = !empty($CFG->usetags) && $isloggedin;
$options->search = !empty($CFG->enableglobalsearch) && has_capability('moodle/search:query', $sitecontext);
$options->calendar = $isloggedin;
} else {
$options->participants = has_capability('moodle/course:viewparticipants', $context);
$options->badges = !empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) &&
has_capability('moodle/badges:viewbadges', $context);
}
return $options;
}

/**
* Return an object with the list of administration options in a course that are available or not for the current user.
* This function also handles the frontpage settings.
*
* @param stdClass $course course object (for frontpage it should be a clone of $SITE)
* @param stdClass $context context object (course context)
* @return stdClass the administration options in a course and their availability status
* @since Moodle 3.2
*/
function course_get_user_administration_options($course, $context) {
global $CFG;
$isfrontpage = $course->id == SITEID;

$options = new stdClass;
$options->update = has_capability('moodle/course:update', $context);
$options->filters = has_capability('moodle/filter:manage', $context) &&
count(filter_get_available_in_context($context)) > 0;
$options->reports = has_capability('moodle/site:viewreports', $context);
$options->backup = has_capability('moodle/backup:backupcourse', $context);
$options->restore = has_capability('moodle/restore:restorecourse', $context);
$options->files = $course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $context);

if (!$isfrontpage) {
$options->tags = has_capability('moodle/course:tag', $context);
$options->gradebook = has_capability('moodle/grade:manage', $context);
$options->outcomes = !empty($CFG->enableoutcomes) && has_capability('moodle/course:update', $context);
$options->badges = !empty($CFG->enablebadges);
$options->import = has_capability('moodle/restore:restoretargetimport', $context);
$options->publish = has_capability('moodle/course:publish', $context);
$options->reset = has_capability('moodle/course:reset', $context);
$options->roles = has_capability('moodle/role:switchroles', $context);

// Add view grade report is permitted.
$grades = false;
if (has_capability('moodle/grade:viewall', $context)) {
$grades = true;
} else if (!empty($course->showgrades)) {
$reports = core_component::get_plugin_list('gradereport');
if (is_array($reports) && count($reports) > 0) { // Get all installed reports.
arsort($reports); // User is last, we want to test it first.
foreach ($reports as $plugin => $plugindir) {
if (has_capability('gradereport/'.$plugin.':view', $context)) {
// Stop when the first visible plugin is found.
$grades = true;
break;
}
}
}
}
$options->grades = $grades;
}
return $options;
}
Loading

0 comments on commit 05842d7

Please sign in to comment.