Differences Between: [Versions 402 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core\moodlenet; 18 19 use core\oauth2\issuer; 20 21 /** 22 * Class containing static utilities (such as various checks) required by the MoodleNet API. 23 * 24 * @package core 25 * @copyright 2023 Michael Hawkins <michaelh@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 class utilities { 29 /** 30 * Check whether the specified issuer is configured as a MoodleNet instance that can be shared to. 31 * 32 * @param issuer $issuer The OAuth 2 issuer being validated. 33 * @return bool true if the issuer is enabled and available to share to. 34 */ 35 public static function is_valid_instance(issuer $issuer): bool { 36 global $CFG; 37 38 $issuerid = $issuer->get('id'); 39 $allowedissuer = get_config('moodlenet', 'oauthservice'); 40 41 return ($CFG->enablesharingtomoodlenet && $issuerid == $allowedissuer && $issuer->get('enabled') && 42 $issuer->get('servicetype') == 'moodlenet'); 43 } 44 45 /** 46 * Check whether a user has the capabilities required to share activities or courses to MoodleNet. 47 * 48 * @param \core\context\course $coursecontext Course context where the activity or course would be shared from. 49 * @param int $userid The user ID being checked. 50 * @param string $type The type of resource being checked (either 'activity' or 'course'). 51 * @return boolean 52 * @throws \coding_exception If an invalid resource type is provided. 53 */ 54 public static function can_user_share(\core\context\course $coursecontext, int $userid, string $type = 'activity'): bool { 55 if ($type === 'course') { 56 return (has_capability('moodle/moodlenet:sharecourse', $coursecontext, $userid) && 57 has_capability('moodle/backup:backupcourse', $coursecontext, $userid)); 58 } else if ($type === 'activity') { 59 return (has_capability('moodle/moodlenet:shareactivity', $coursecontext, $userid) && 60 has_capability('moodle/backup:backupactivity', $coursecontext, $userid)); 61 } 62 63 throw new \coding_exception('Invalid resource type'); 64 } 65 66 /** 67 * Get the support url. 68 * 69 * @return string 70 */ 71 public static function get_support_url(): string { 72 global $CFG; 73 $supporturl = ''; 74 75 if ($CFG->supportavailability && $CFG->supportavailability !== CONTACT_SUPPORT_DISABLED) { 76 if (!empty($CFG->supportpage)) { 77 $supporturl = $CFG->supportpage; 78 } else { 79 $supporturl = $CFG->wwwroot . '/user/contactsitesupport.php'; 80 } 81 } 82 83 return $supporturl; 84 } 85 86 /** 87 * Check the user has a valid capability in any course they are enrolled in. 88 * 89 * @param int $userid The user id to query 90 * @return string Returns 'yes' if capability found, 'no' if not 91 */ 92 public static function does_user_have_capability_in_any_course(int $userid): string { 93 // We are checking this way because we are not always in the course context and need 94 // a way to retrieve the user's courses to see if any of them have the correct capability. 95 $capabilities = [ 96 'moodle/moodlenet:sharecourse', 97 'moodle/moodlenet:shareactivity' 98 ]; 99 // We are using 'no' instead of false to avoid confusing a cache key 100 // that was not found (returns false) with a user who does not have the capablity. 101 $isallowed = 'no'; 102 $cache = \cache::make('core', 'moodlenet_usercanshare'); 103 $cachedvalue = $cache->get($userid); 104 105 if ($cachedvalue === false) { 106 foreach ($capabilities as $capability) { 107 // Find at least one course that contains a capability match. 108 $course = get_user_capability_course($capability, $userid, true, '', 'id', 1); 109 110 if (!empty($course)) { 111 // Set the cache to 'yes' and break out of the loop. 112 $isallowed = 'yes'; 113 break; 114 } 115 } 116 $cache->set($userid, $isallowed); 117 } else { 118 $isallowed = $cachedvalue; 119 } 120 121 return $isallowed; 122 } 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body