diff --git a/db/services.php b/db/services.php index a9716b0..9511328 100755 --- a/db/services.php +++ b/db/services.php @@ -844,14 +844,6 @@ user.', 'type' => 'read', ), - 'local_mobile_core_user_update_picture' => array( - 'classname' => 'local_mobile_external', - 'methodname' => 'core_user_update_picture', - 'classpath' => 'local/mobile/externallib.php', - 'description' => 'Update or delete the user picture in the site', - 'type' => 'write', - 'capabilities' => 'moodle/user:editownprofile, moodle/user:editprofile', - ), 'local_mobile_tool_mobile_get_config' => array( 'classname' => 'local_mobile_external', 'methodname' => 'tool_mobile_get_config', @@ -859,12 +851,13 @@ 'description' => 'Returns a list of the site configurations, filtering by section.', 'type' => 'read', ), - 'local_mobile_core_course_check_updates' => array( + 'local_mobile_core_badges_get_user_badges' => array( 'classname' => 'local_mobile_external', - 'methodname' => 'core_course_check_updates', + 'methodname' => 'core_badges_get_user_badges', 'classpath' => 'local/mobile/externallib.php', - 'description' => 'Check if there is updates affecting the user for the given course and contexts.', + 'description' => 'Returns the list of badges awarded to a user.', 'type' => 'read', + 'capabilities' => 'moodle/badges:viewotherbadges', ), ); @@ -928,10 +921,9 @@ 'local_mobile_core_course_search_courses', 'local_mobile_core_course_get_user_navigation_options', 'local_mobile_core_course_get_user_administration_options', - 'local_mobile_core_user_update_picture', 'local_mobile_mod_assign_view_assign', 'local_mobile_tool_mobile_get_config', - 'local_mobile_core_course_check_updates', + 'local_mobile_core_badges_get_user_badges', 'local_mobile_core_enrol_get_course_enrolment_methods', 'local_mobile_core_group_get_activity_allowed_groups', 'local_mobile_core_group_get_activity_groupmode', diff --git a/externallib.php b/externallib.php index 185e8be..04b22c0 100644 --- a/externallib.php +++ b/externallib.php @@ -9994,104 +9994,6 @@ public static function core_course_get_user_administration_options_returns() { return self::core_course_get_user_navigation_options_returns(); } - /** - * Returns description of method parameters - * - * @return external_function_parameters - * @since Moodle 3.2 - */ - public static function core_user_update_picture_parameters() { - return new external_function_parameters( - array( - 'draftitemid' => new external_value(PARAM_INT, 'Id of the user draft file to use as image'), - 'delete' => new external_value(PARAM_BOOL, 'If we should delete the user picture', VALUE_DEFAULT, false), - 'userid' => new external_value(PARAM_INT, 'Id of the user, 0 for current user', VALUE_DEFAULT, 0) - ) - ); - } - - /** - * Update or delete the user picture in the site - * - * @param int $draftitemid id of the user draft file to use as image - * @param bool $delete if we should delete the user picture - * @param int $userid id of the user, 0 for current user - * @return array warnings and success status - * @since Moodle 3.2 - * @throws moodle_exception - */ - public static function core_user_update_picture($draftitemid, $delete = false, $userid = 0) { - global $CFG, $USER, $PAGE; - - $params = self::validate_parameters( - self::core_user_update_picture_parameters(), - array( - 'draftitemid' => $draftitemid, - 'delete' => $delete, - 'userid' => $userid - ) - ); - - $context = context_system::instance(); - self::validate_context($context); - - if (!empty($CFG->disableuserimages)) { - throw new moodle_exception('userimagesdisabled', 'admin'); - } - - if (empty($params['userid']) or $params['userid'] == $USER->id) { - $user = $USER; - require_capability('moodle/user:editownprofile', $context); - } else { - $user = core_user::get_user($params['userid'], '*', MUST_EXIST); - core_user::require_active_user($user); - $personalcontext = context_user::instance($user->id); - - require_capability('moodle/user:editprofile', $personalcontext); - if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins. - throw new moodle_exception('useradmineditadmin'); - } - } - - // Load the appropriate auth plugin. - $userauth = get_auth_plugin($user->auth); - if (is_mnet_remote_user($user) or !$userauth->can_edit_profile() or $userauth->edit_profile_url()) { - throw new moodle_exception('noprofileedit', 'auth'); - } - - $filemanageroptions = array('maxbytes' => $CFG->maxbytes, 'subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => 'web_image'); - $user->deletepicture = $params['delete']; - $user->imagefile = $params['draftitemid']; - $success = local_mobile_core_user_update_picture($user, $filemanageroptions); - - $result = array( - 'success' => $success, - 'warnings' => array(), - ); - if ($success) { - $userpicture = new user_picture(core_user::get_user($user->id)); - $userpicture->size = 1; // Size f1. - $result['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); - } - return $result; - } - - /** - * Returns description of method result value - * - * @return external_description - * @since Moodle 3.2 - */ - public static function core_user_update_picture_returns() { - return new external_single_structure( - array( - 'success' => new external_value(PARAM_BOOL, 'True if the image was updated, false otherwise.'), - 'profileimageurl' => new external_value(PARAM_URL, 'New profile user image url', VALUE_OPTIONAL), - 'warnings' => new external_warnings() - ) - ); - } - /** * Returns description of get_config() parameters. * @@ -10114,6 +10016,7 @@ public static function tool_mobile_get_config_parameters() { * @since Moodle 3.2 */ public static function tool_mobile_get_config($section = '') { + global $CFG, $SITE; $params = self::validate_parameters(self::tool_mobile_get_config_parameters(), array('section' => $section)); @@ -10188,129 +10091,152 @@ public static function tool_mobile_get_config_returns() { } /** - * Returns description of method parameters + * Describes the parameters for core_badges_get_user_badges. * - * @return external_function_parameters - * @since Moodle 3.2 + * @return external_external_function_parameters + * @since Moodle 3.1 */ - public static function core_course_check_updates_parameters() { - return new external_function_parameters( + public static function core_badges_get_user_badges_parameters() { + return new external_function_parameters ( array( - 'courseid' => new external_value(PARAM_INT, 'Course id to check'), - 'tocheck' => new external_multiple_structure( - new external_single_structure( - array( - 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level for the file location. - Only module supported right now.'), - 'id' => new external_value(PARAM_INT, 'Context instance id'), - 'since' => new external_value(PARAM_INT, 'Check updates since this time stamp'), - ) - ), - 'Instances to check' - ), - 'filter' => new external_multiple_structure( - new external_value(PARAM_ALPHANUM, 'Area name: configuration, fileareas, completion, ratings, comments, - gradeitems, outcomes'), - 'Check only for updates in these areas', VALUE_DEFAULT, array() - ) + 'userid' => new external_value(PARAM_INT, 'Badges only for this user id, empty for current user', VALUE_DEFAULT, 0), + 'courseid' => new external_value(PARAM_INT, 'Filter badges by course id, empty all the courses', VALUE_DEFAULT, 0), + 'page' => new external_value(PARAM_INT, 'The page of records to return.', VALUE_DEFAULT, 0), + 'perpage' => new external_value(PARAM_INT, 'The number of records to return per page', VALUE_DEFAULT, 0), + 'search' => new external_value(PARAM_RAW, 'A simple string to search for', VALUE_DEFAULT, ''), + 'onlypublic' => new external_value(PARAM_BOOL, 'Whether to return only public badges', VALUE_DEFAULT, false), ) ); } /** - * Check if there is updates affecting the user for the given course and contexts. - * Right now only modules are supported. - * This WS calls mod_core_course_check_updates_since for each module to check if there is any update the user should we aware of. + * Returns the list of badges awarded to a user. * - * @param int $courseid the list of modules to check - * @param array $tocheck the list of modules to check - * @param array $filter check only for updates in these areas - * @return array list of updates and warnings + * @param int $userid user id + * @param int $courseid course id + * @param int $page page of records to return + * @param int $perpage number of records to return per page + * @param string $search a simple string to search for + * @param bool $onlypublic whether to return only public badges + * @return array array containing warnings and the awarded badges + * @since Moodle 3.1 * @throws moodle_exception - * @since Moodle 3.2 */ - public static function core_course_check_updates($courseid, $tocheck, $filter = array()) { - global $CFG, $DB; + public static function core_badges_get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) { + global $CFG, $USER; + require_once($CFG->libdir . '/badgeslib.php'); - $params = self::validate_parameters( - self::core_course_check_updates_parameters(), - array( - 'courseid' => $courseid, - 'tocheck' => $tocheck, - 'filter' => $filter, - ) + $warnings = array(); + + $params = array( + 'userid' => $userid, + 'courseid' => $courseid, + 'page' => $page, + 'perpage' => $perpage, + 'search' => $search, + 'onlypublic' => $onlypublic, ); + $params = self::validate_parameters(self::core_badges_get_user_badges_parameters(), $params); - $course = get_course($params['courseid']); - $context = context_course::instance($course->id); - self::validate_context($context); + if (empty($CFG->enablebadges)) { + throw new moodle_exception('badgesdisabled', 'badges'); + } - list($instances, $warnings) = course_check_updates($course, $params['tocheck'], $filter); + if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) { + throw new moodle_exception('coursebadgesdisabled', 'badges'); + } - $instancesformatted = array(); - foreach ($instances as $instance) { - $updates = array(); - foreach ($instance['updates'] as $name => $data) { - if (empty($data->updated)) { - continue; - } - $updatedata = array( - 'name' => $name, - ); - if (!empty($data->timeupdated)) { - $updatedata['timeupdated'] = $data->timeupdated; - } - if (!empty($data->itemids)) { - $updatedata['itemids'] = $data->itemids; - } - $updates[] = $updatedata; - } - if (!empty($updates)) { - $instancesformatted[] = array( - 'contextlevel' => $instance['contextlevel'], - 'id' => $instance['id'], - 'updates' => $updates + // Default value for userid. + if (empty($params['userid'])) { + $params['userid'] = $USER->id; + } + + // Validate the user. + $user = core_user::get_user($params['userid'], '*', MUST_EXIST); + local_mobile_require_active_user($user); + + $usercontext = context_user::instance($user->id); + self::validate_context($usercontext); + + if ($USER->id != $user->id) { + require_capability('moodle/badges:viewotherbadges', $usercontext); + // We are looking other user's badges, we must retrieve only public badges. + $params['onlypublic'] = true; + } + + $userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'], + $params['onlypublic']); + + $result = array(); + $result['badges'] = array(); + $result['warnings'] = $warnings; + + foreach ($userbadges as $badge) { + $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid); + $badge->badgeurl = moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', + 'f1')->out(false); + // Return all the information if we are requesting our own badges. + // Or, if we have permissions for configuring badges in the badge context. + if ($USER->id == $user->id or has_capability('moodle/badges:configuredetails', $context)) { + $result['badges'][] = (array) $badge; + } else { + $result['badges'][] = array( + 'name' => $badge->name, + 'description' => $badge->description, + 'badgeurl' => $badge->badgeurl, + 'issuername' => $badge->issuername, + 'issuerurl' => $badge->issuerurl, + 'issuercontact' => $badge->issuercontact, + 'uniquehash' => $badge->uniquehash, + 'dateissued' => $badge->dateissued, + 'dateexpire' => $badge->dateexpire, ); } } - return array( - 'instances' => $instancesformatted, - 'warnings' => $warnings - ); + return $result; } /** - * Returns description of method result value + * Describes the core_badges_get_user_badges return value. * - * @return external_description - * @since Moodle 3.2 + * @return external_single_structure + * @since Moodle 3.1 */ - public static function core_course_check_updates_returns() { + public static function core_badges_get_user_badges_returns() { return new external_single_structure( array( - 'instances' => new external_multiple_structure( + 'badges' => new external_multiple_structure( new external_single_structure( array( - 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level'), - 'id' => new external_value(PARAM_INT, 'Instance id'), - 'updates' => new external_multiple_structure( - new external_single_structure( - array( - 'name' => new external_value(PARAM_ALPHANUMEXT, 'Name of the area updated.'), - 'timeupdated' => new external_value(PARAM_INT, 'Last time was updated', VALUE_OPTIONAL), - 'itemids' => new external_multiple_structure( - new external_value(PARAM_INT, 'Instance id'), - 'The ids of the items updated', - VALUE_OPTIONAL - ) - ) - ) - ) + 'id' => new external_value(PARAM_INT, 'Badge id.', VALUE_OPTIONAL), + 'name' => new external_value(PARAM_FILE, 'Badge name.'), + 'description' => new external_value(PARAM_NOTAGS, 'Badge description.'), + 'badgeurl' => new external_value(PARAM_URL, 'Badge URL.'), + 'timecreated' => new external_value(PARAM_INT, 'Time created.', VALUE_OPTIONAL), + 'timemodified' => new external_value(PARAM_INT, 'Time modified.', VALUE_OPTIONAL), + 'usercreated' => new external_value(PARAM_INT, 'User created.', VALUE_OPTIONAL), + 'usermodified' => new external_value(PARAM_INT, 'User modified.', VALUE_OPTIONAL), + 'issuername' => new external_value(PARAM_NOTAGS, 'Issuer name.'), + 'issuerurl' => new external_value(PARAM_URL, 'Issuer URL.'), + 'issuercontact' => new external_value(PARAM_RAW, 'Issuer contact.'), + 'expiredate' => new external_value(PARAM_INT, 'Expire date.', VALUE_OPTIONAL), + 'expireperiod' => new external_value(PARAM_INT, 'Expire period.', VALUE_OPTIONAL), + 'type' => new external_value(PARAM_INT, 'Type.', VALUE_OPTIONAL), + 'courseid' => new external_value(PARAM_INT, 'Course id.', VALUE_OPTIONAL), + 'message' => new external_value(PARAM_RAW, 'Message.', VALUE_OPTIONAL), + 'messagesubject' => new external_value(PARAM_TEXT, 'Message subject.', VALUE_OPTIONAL), + 'attachment' => new external_value(PARAM_INT, 'Attachment.', VALUE_OPTIONAL), + 'status' => new external_value(PARAM_INT, 'Status.', VALUE_OPTIONAL), + 'issuedid' => new external_value(PARAM_INT, 'Issued id.', VALUE_OPTIONAL), + 'uniquehash' => new external_value(PARAM_ALPHANUM, 'Unique hash.'), + 'dateissued' => new external_value(PARAM_INT, 'Date issued.'), + 'dateexpire' => new external_value(PARAM_INT, 'Date expire.'), + 'visible' => new external_value(PARAM_INT, 'Visible.', VALUE_OPTIONAL), ) ) ), - 'warnings' => new external_warnings() + 'warnings' => new external_warnings(), ) ); } diff --git a/futurelib.php b/futurelib.php index 01db701..78832b4 100644 --- a/futurelib.php +++ b/futurelib.php @@ -3190,10 +3190,6 @@ function course_get_user_navigation_options($context, $course = null) { $options->grades = $grades; } - if (\core_competency\api::is_enabled()) { - $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage'); - $options->competencies = has_any_capability($capabilities, $context); - } return $options; } } @@ -3242,659 +3238,3 @@ function course_get_user_administration_options($course, $context) { return $options; } } - -require_once("$CFG->dirroot/user/lib.php"); - -/** - * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms. - * - * @param stdClass $usernew An object that contains some information about the user being updated - * @param array $filemanageroptions - * @return bool True if the user was updated, false if it stayed the same. - */ -function local_mobile_core_user_update_picture(stdClass $usernew, $filemanageroptions = array()) { - global $CFG, $DB; - require_once("$CFG->libdir/gdlib.php"); - - $context = context_user::instance($usernew->id, MUST_EXIST); - $user = core_user::get_user($usernew->id, 'id, picture', MUST_EXIST); - - $newpicture = $user->picture; - // Get file_storage to process files. - $fs = get_file_storage(); - if (!empty($usernew->deletepicture)) { - // The user has chosen to delete the selected users picture. - $fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area. - $newpicture = 0; - - } else { - // Save newly uploaded file, this will avoid context mismatch for newly created users. - file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions); - if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) { - // Get file which was uploaded in draft area. - foreach ($iconfiles as $file) { - if (!$file->is_directory()) { - break; - } - } - // Copy file to temporary location and the send it for processing icon. - if ($iconfile = $file->copy_content_to_temp()) { - // There is a new image that has been uploaded. - // Process the new image and set the user to make use of it. - // NOTE: Uploaded images always take over Gravatar. - $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile); - // Delete temporary file. - @unlink($iconfile); - // Remove uploaded file. - $fs->delete_area_files($context->id, 'user', 'newicon'); - } else { - // Something went wrong while creating temp file. - // Remove uploaded file. - $fs->delete_area_files($context->id, 'user', 'newicon'); - return false; - } - } - } - - if ($newpicture != $user->picture) { - $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id)); - return true; - } else { - return false; - } -} - -require_once($CFG->dirroot . '/course/lib.php'); - -if (!function_exists('course_check_updates')) { - /** - * Check for course updates in the given context level instances (only modules supported right Now) - * - * @param stdClass $course course object - * @param array $tocheck instances to check for updates - * @param array $filter check only for updates in these areas - * @return array list of warnings and instances with updates information - * @since Moodle 3.2 - */ - function course_check_updates($course, $tocheck, $filter = array()) { - global $CFG, $DB; - - $instances = array(); - $warnings = array(); - $modulescallbacksupport = array(); - $modinfo = get_fast_modinfo($course); - - $supportedplugins = array( - 'mod_assign' => true, - 'mod_book' => true, - 'mod_choice' => true, - 'mod_folder' => array('content'), - 'mod_glossary' => true, - 'mod_imscp' => array('content'), - 'mod_label' => array(), - 'mod_lti' => true, - 'mod_page' => array('content'), - 'mod_quiz' => true, - 'mod_resource' => array('content'), - 'mod_scorm' => true, - 'mod_survey' => true, - 'mod_url' => array('content'), - 'mod_wiki' => true, - ); - - // Check instances. - foreach ($tocheck as $instance) { - if ($instance['contextlevel'] == 'module') { - // Check module visibility. - try { - $cm = $modinfo->get_cm($instance['id']); - } catch (Exception $e) { - $warnings[] = array( - 'item' => 'module', - 'itemid' => $instance['id'], - 'warningcode' => 'cmidnotincourse', - 'message' => 'This module id does not belong to this course.' - ); - continue; - } - - if (!$cm->uservisible) { - $warnings[] = array( - 'item' => 'module', - 'itemid' => $instance['id'], - 'warningcode' => 'nonuservisible', - 'message' => 'You don\'t have access to this module.' - ); - continue; - } - - if (!isset($supportedplugins['mod_' . $cm->modname])) { - $warnings[] = array( - 'item' => 'module', - 'itemid' => $instance['id'], - 'warningcode' => 'missingcallback', - 'message' => 'This module does not implement the check_updates_since callback: ' . $instance['contextlevel'], - ); - continue; - } - - $content = $supportedplugins['mod_' . $cm->modname]; - - if (is_array($content)) { - $instances[] = array( - 'contextlevel' => $instance['contextlevel'], - 'id' => $instance['id'], - 'updates' => course_check_module_updates_since($cm, $instance['since'], $content, $filter) - ); - } else { - $instances[] = array( - 'contextlevel' => $instance['contextlevel'], - 'id' => $instance['id'], - 'updates' => call_user_func($cm->modname . '_check_updates_since', $cm, $instance['since'], $filter) - ); - } - - } else { - $warnings[] = array( - 'item' => 'contextlevel', - 'itemid' => $instance['id'], - 'warningcode' => 'contextlevelnotsupported', - 'message' => 'Context level not yet supported ' . $instance['contextlevel'], - ); - } - } - return array($instances, $warnings); - } -} - - -if (!function_exists('course_check_module_updates_since')) { - /** - * Check module updates since a given time. - * This function checks for updates in the module config, file areas, completion, grades, comments and ratings. - * - * @param cm_info $cm course module data - * @param int $from the time to check - * @param array $fileareas additional file ares to check - * @param array $filter if we need to filter and return only selected updates - * @return stdClass object with the different updates - * @since Moodle 3.2 - */ - function course_check_module_updates_since($cm, $from, $fileareas = array(), $filter = array()) { - global $DB, $CFG, $USER; - - $context = $cm->context; - $mod = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); - - $updates = new stdClass(); - $course = get_course($cm->course); - $component = 'mod_' . $cm->modname; - - // Check changes in the module configuration. - if (isset($mod->timemodified) and (empty($filter) or in_array('configuration', $filter))) { - $updates->configuration = (object) array('updated' => false); - if ($updates->configuration->updated = $mod->timemodified > $from) { - $updates->configuration->timeupdated = $mod->timemodified; - } - } - - // Check for updates in files. - if (plugin_supports('mod', $cm->modname, FEATURE_MOD_INTRO)) { - $fileareas[] = 'intro'; - } - if (!empty($fileareas) and (empty($filter) or in_array('fileareas', $filter))) { - $fs = get_file_storage(); - $files = local_mobile_fs_get_area_files($context->id, $component, $fileareas, false, "filearea, timemodified DESC", true, $from); - foreach ($fileareas as $filearea) { - $updates->{$filearea . 'files'} = (object) array('updated' => false); - } - foreach ($files as $file) { - $updates->{$file->get_filearea() . 'files'}->updated = true; - $updates->{$file->get_filearea() . 'files'}->itemids[] = $file->get_id(); - } - } - - // Check completion. - $supportcompletion = plugin_supports('mod', $cm->modname, FEATURE_COMPLETION_HAS_RULES); - $supportcompletion = $supportcompletion or plugin_supports('mod', $cm->modname, FEATURE_COMPLETION_TRACKS_VIEWS); - if ($supportcompletion and (empty($filter) or in_array('completion', $filter))) { - $updates->completion = (object) array('updated' => false); - $completion = new completion_info($course); - // Use wholecourse to cache all the modules the first time. - $completiondata = $completion->get_data($cm, true); - if ($updates->completion->updated = !empty($completiondata->timemodified) && $completiondata->timemodified > $from) { - $updates->completion->timemodified = $completiondata->timemodified; - } - } - - // Check grades. - $supportgrades = plugin_supports('mod', $cm->modname, FEATURE_GRADE_HAS_GRADE); - $supportgrades = $supportgrades or plugin_supports('mod', $cm->modname, FEATURE_GRADE_OUTCOMES); - if ($supportgrades and (empty($filter) or (in_array('gradeitems', $filter) or in_array('outcomes', $filter)))) { - require_once($CFG->libdir . '/gradelib.php'); - $grades = grade_get_grades($course->id, 'mod', $cm->modname, $mod->id, $USER->id); - - if (empty($filter) or in_array('gradeitems', $filter)) { - $updates->gradeitems = (object) array('updated' => false); - foreach ($grades->items as $gradeitem) { - foreach ($gradeitem->grades as $grade) { - if ($grade->datesubmitted > $from or $grade->dategraded > $from) { - $updates->gradeitems->updated = true; - $updates->gradeitems->itemids[] = $gradeitem->id; - } - } - } - } - } - - return $updates; - } -} - - -/** - * Returns all area files (optionally limited by itemid) - * - * @param int $contextid context ID - * @param string $component component - * @param mixed $filearea file area/s, you cannot specify multiple fileareas as well as an itemid - * @param int $itemid item ID or all files if not specified - * @param string $sort A fragment of SQL to use for sorting - * @param bool $includedirs whether or not include directories - * @param int $updatedsince return files updated since this time - * @return stored_file[] array of stored_files indexed by pathanmehash - */ -function local_mobile_fs_get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "itemid, filepath, filename", - $includedirs = true, $updatedsince = 0) { - global $DB; - - $fs = get_file_storage(); - list($areasql, $conditions) = $DB->get_in_or_equal($filearea, SQL_PARAMS_NAMED); - $conditions['contextid'] = $contextid; - $conditions['component'] = $component; - - if ($itemid !== false && is_array($filearea)) { - throw new coding_exception('You cannot specify multiple fileareas as well as an itemid.'); - } else if ($itemid !== false) { - $itemidsql = ' AND f.itemid = :itemid '; - $conditions['itemid'] = $itemid; - } else { - $itemidsql = ''; - } - - $updatedsincesql = ''; - if (!empty($updatedsince)) { - $conditions['time'] = $updatedsince; - $updatedsincesql = 'AND f.timemodified > :time'; - } - - $sql = "SELECT ".self::instance_sql_fields('f', 'r')." - FROM {files} f - LEFT JOIN {files_reference} r - ON f.referencefileid = r.id - WHERE f.contextid = :contextid - AND f.component = :component - AND f.filearea $areasql - $updatedsincesql - $itemidsql"; - if (!empty($sort)) { - $sql .= " ORDER BY {$sort}"; - } - - $result = array(); - $filerecords = $DB->get_records_sql($sql, $conditions); - foreach ($filerecords as $filerecord) { - if (!$includedirs and $filerecord->filename === '.') { - continue; - } - $result[$filerecord->pathnamehash] = $fs->get_file_instance($filerecord); - } - return $result; -} - - -require_once("$CFG->dirroot/mod/assign/lib.php"); -if (function_exists('assign_check_updates_since')) { - - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function assign_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $USER, $CFG; - require_once($CFG->dirroot . '/mod/assign/locallib.php'); - - $updates = new stdClass(); - $updates = course_check_module_updates_since($cm, $from, array(ASSIGN_INTROATTACHMENT_FILEAREA), $filter); - - // Check if there is a new submission by the user or new grades. - $select = 'assignment = :id AND userid = :userid AND (timecreated > :since1 OR timemodified > :since2)'; - $params = array('id' => $cm->instance, 'userid' => $USER->id, 'since1' => $from, 'since2' => $from); - $updates->submissions = (object) array('updated' => false); - $submissions = $DB->get_records_select('assign_submission', $select, $params, '', 'id'); - if (!empty($submissions)) { - $updates->submissions->updated = true; - $updates->submissions->itemids = array_keys($submissions); - } - - $updates->grades = (object) array('updated' => false); - $grades = $DB->get_records_select('assign_grades', $select, $params, '', 'id'); - if (!empty($grades)) { - $updates->grades->updated = true; - $updates->grades->itemids = array_keys($grades); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/book/lib.php"); -if (function_exists('book_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function book_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB; - - $context = $cm->context; - $updates = new stdClass(); - if (!has_capability('mod/book:read', $context)) { - return $updates; - } - $updates = course_check_module_updates_since($cm, $from, array('content'), $filter); - - $select = 'bookid = :id AND (timecreated > :since1 OR timemodified > :since2)'; - $params = array('id' => $cm->instance, 'since1' => $from, 'since2' => $from); - if (!has_capability('mod/book:viewhiddenchapters', $context)) { - $select .= ' AND hidden = 0'; - } - $updates->entries = (object) array('updated' => false); - $entries = $DB->get_records_select('book_chapters', $select, $params, '', 'id'); - if (!empty($entries)) { - $updates->entries->updated = true; - $updates->entries->itemids = array_keys($entries); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/choice/lib.php"); -if (function_exists('choice_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function choice_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB; - - $updates = new stdClass(); - $choice = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); - list($available, $warnings) = choice_get_availability_status($choice); - if (!$available) { - return $updates; - } - - $updates = course_check_module_updates_since($cm, $from, array(), $filter); - - if (!choice_can_view_results($choice)) { - return $updates; - } - // Check if there are new responses in the choice. - $updates->answers = (object) array('updated' => false); - $select = 'choiceid = :id AND timemodified > :since'; - $params = array('id' => $choice->id, 'since' => $from); - $answers = $DB->get_records_select('choice_answers', $select, $params, '', 'id'); - if (!empty($answers)) { - $updates->answers->updated = true; - $updates->answers->itemids = array_keys($answers); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/glossary/lib.php"); -if (function_exists('glossary_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function glossary_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB; - - $updates = course_check_module_updates_since($cm, $from, array('attachment', 'entry'), $filter); - - $updates->entries = (object) array('updated' => false); - $select = 'glossaryid = :id AND (timecreated > :since1 OR timemodified > :since2)'; - $params = array('id' => $cm->instance, 'since1' => $from, 'since2' => $from); - if (!has_capability('mod/glossary:approve', $cm->context)) { - $select .= ' AND approved = 1'; - } - - $entries = $DB->get_records_select('glossary_entries', $select, $params, '', 'id'); - if (!empty($entries)) { - $updates->entries->updated = true; - $updates->entries->itemids = array_keys($entries); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/lti/lib.php"); -if (function_exists('lti_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function lti_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $USER; - - $updates = course_check_module_updates_since($cm, $from, array(), $filter); - - // Check if there is a new submission. - $updates->submissions = (object) array('updated' => false); - $select = 'ltiid = :id AND userid = :userid AND (datesubmitted > :since1 OR dateupdated > :since2)'; - $params = array('id' => $cm->instance, 'userid' => $USER->id, 'since1' => $from, 'since2' => $from); - $submissions = $DB->get_records_select('lti_submission', $select, $params, '', 'id'); - if (!empty($submissions)) { - $updates->submissions->updated = true; - $updates->submissions->itemids = array_keys($submissions); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/quiz/lib.php"); -if (function_exists('quiz_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function quiz_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $USER, $CFG; - require_once($CFG->dirroot . '/mod/quiz/locallib.php'); - - $updates = course_check_module_updates_since($cm, $from, array(), $filter); - - // Check if questions were updated. - $updates->questions = (object) array('updated' => false); - $quizobj = quiz::create($cm->instance, $USER->id); - $quizobj->preload_questions(); - $quizobj->load_questions(); - $questionids = array_keys($quizobj->get_questions()); - if (!empty($questionids)) { - list($questionsql, $params) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED); - $select = 'id ' . $questionsql . ' AND (timemodified > :time1 OR timecreated > :time2)'; - $params['time1'] = $from; - $params['time2'] = $from; - $questions = $DB->get_records_select('question', $select, $params, '', 'id'); - if (!empty($questions)) { - $updates->questions->updated = true; - $updates->questions->itemids = array_keys($questions); - } - } - - // Check for new attempts or grades. - $updates->attempts = (object) array('updated' => false); - $updates->grades = (object) array('updated' => false); - $select = 'quiz = ? AND userid = ? AND timemodified > ?'; - $params = array($cm->instance, $USER->id, $from); - - $attempts = $DB->get_records_select('quiz_attempts', $select, $params, '', 'id'); - if (!empty($attempts)) { - $updates->attempts->updated = true; - $updates->attempts->itemids = array_keys($attempts); - } - $grades = $DB->get_records_select('quiz_grades', $select, $params, '', 'id'); - if (!empty($grades)) { - $updates->grades->updated = true; - $updates->grades->itemids = array_keys($grades); - } - - return $updates; - } -} - -require_once("$CFG->dirroot/mod/scorm/lib.php"); -if (function_exists('scorm_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function scorm_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $USER, $CFG; - require_once($CFG->dirroot . '/mod/scorm/locallib.php'); - - $scorm = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); - $updates = new stdClass(); - list($available, $warnings) = scorm_get_availability_status($scorm, true, $cm->context); - if (!$available) { - return $updates; - } - $updates = course_check_module_updates_since($cm, $from, array('package'), $filter); - - $updates->tracks = (object) array('updated' => false); - $select = 'scormid = ? AND userid = ? AND timemodified > ?'; - $params = array($scorm->id, $USER->id, $from); - $tracks = $DB->get_records_select('scorm_scoes_track', $select, $params, '', 'id'); - if (!empty($tracks)) { - $updates->tracks->updated = true; - $updates->tracks->itemids = array_keys($tracks); - } - return $updates; - } -} - -require_once("$CFG->dirroot/mod/survey/lib.php"); -if (function_exists('survey_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function survey_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $USER; - - $updates = new stdClass(); - if (!has_capability('mod/survey:participate', $cm->context)) { - return $updates; - } - $updates = course_check_module_updates_since($cm, $from, array(), $filter); - - $updates->answers = (object) array('updated' => false); - $select = 'survey = ? AND userid = ? AND time > ?'; - $params = array($cm->instance, $USER->id, $from); - $answers = $DB->get_records_select('survey_answers', $select, $params, '', 'id'); - if (!empty($answers)) { - $updates->answers->updated = true; - $updates->answers->itemids = array_keys($answers); - } - return $updates; - } -} - -require_once("$CFG->dirroot/mod/wiki/lib.php"); -if (function_exists('wiki_check_updates_since')) { - /** - * Check if the module has any update that affects the current user since a given time. - * - * @param cm_info $cm course module data - * @param int $from the time to check updates from - * @param array $filter if we need to check only specific updates - * @return stdClass an object with the different type of areas indicating if they were updated or not - * @since Moodle 3.2 - */ - function wiki_check_updates_since(cm_info $cm, $from, $filter = array()) { - global $DB, $CFG; - require_once($CFG->dirroot . '/mod/wiki/locallib.php'); - - $updates = new stdClass(); - if (!has_capability('mod/wiki:viewpage', $cm->context)) { - return $updates; - } - $updates = course_check_module_updates_since($cm, $from, array('attachments'), $filter); - - // Check only pages updated in subwikis the user can access. - $updates->pages = (object) array('updated' => false); - $wiki = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); - if ($subwikis = wiki_get_visible_subwikis($wiki, $cm, $cm->context)) { - $subwikisids = array(); - foreach ($subwikis as $subwiki) { - $subwikisids[] = $subwiki->id; - } - list($subwikissql, $params) = $DB->get_in_or_equal($subwikisids, SQL_PARAMS_NAMED); - $select = 'subwikiid ' . $subwikissql . ' AND (timemodified > :since1 OR timecreated > :since2)'; - $params['since1'] = $from; - $params['since2'] = $from; - $pages = $DB->get_records_select('wiki_pages', $select, $params, '', 'id'); - if (!empty($pages)) { - $updates->pages->updated = true; - $updates->pages->itemids = array_keys($pages); - } - } - return $updates; - } -}