From 1896b8009a6fdd55ea516b2fec21c3bffbc447ff Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 6 Oct 2016 22:46:08 +0100 Subject: [PATCH] MDL-56307 course: API changes to support retrieve by time --- comment/locallib.php | 46 ++++++++++++++++++++++++++++++++ lib/filestorage/file_storage.php | 9 ++++++- lib/gradelib.php | 2 ++ rating/lib.php | 34 +++++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/comment/locallib.php b/comment/locallib.php index 14a496513c3..a42d2b0f3ff 100644 --- a/comment/locallib.php +++ b/comment/locallib.php @@ -217,4 +217,50 @@ class comment_manager { } return true; } + + /** + * Get comments created since a given time. + * + * @param stdClass $course course object + * @param stdClass $context context object + * @param string $component component name + * @param int $since the time to check + * @param stdClass $cm course module object + * @return array list of comments db records since the given timelimit + * @since Moodle 3.2 + */ + public function get_component_comments_since($course, $context, $component, $since, $cm = null) { + global $DB; + + $commentssince = array(); + $where = 'contextid = ? AND component = ? AND timecreated > ?'; + $comments = $DB->get_records_select('comments', $where, array($context->id, $component, $since)); + // Check item by item if we have permissions. + $managersviewstatus = array(); + foreach ($comments as $comment) { + // Check if the manager for the item is cached. + if (!isset($managersviewstatus[$comment->commentarea]) or + !isset($managersviewstatus[$comment->commentarea][$comment->itemid])) { + + $args = new stdClass; + $args->area = $comment->commentarea; + $args->itemid = $comment->itemid; + $args->context = $context; + $args->course = $course; + $args->client_id = 0; + $args->component = $component; + if (!empty($cm)) { + $args->cm = $cm; + } + + $manager = new comment($args); + $managersviewstatus[$comment->commentarea][$comment->itemid] = $manager->can_view(); + } + + if ($managersviewstatus[$comment->commentarea][$comment->itemid]) { + $commentssince[$comment->id] = $comment; + } + } + return $commentssince; + } } diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index a5bc30c0d34..1c4adeede33 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -798,9 +798,12 @@ class file_storage { * @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 string $extrasql a fragment of SQL to append to the WHERE part of the query + * @param array $extraconditions an array of additional condition values for the $extrasql * @return stored_file[] array of stored_files indexed by pathanmehash */ - public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "itemid, filepath, filename", $includedirs = true) { + public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "itemid, filepath, filename", + $includedirs = true, $extrasql = '', $extraconditions = array()) { global $DB; $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea); @@ -819,6 +822,10 @@ class file_storage { AND f.component = :component AND f.filearea = :filearea $itemidsql"; + if (!empty($extrasql)) { + $sql .= " $extrasql"; + $conditions = array_merge($conditions, $extraconditions); + } if (!empty($sort)) { $sql .= " ORDER BY {$sort}"; } diff --git a/lib/gradelib.php b/lib/gradelib.php index 4b1cf8246d8..2417784bb9f 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -583,6 +583,8 @@ function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $use $grade->feedback = $grade_grades[$userid]->feedback; $grade->feedbackformat = $grade_grades[$userid]->feedbackformat; $grade->usermodified = $grade_grades[$userid]->usermodified; + $grade->datesubmitted = $grade_grades[$userid]->get_datesubmitted(); + $grade->dategraded = $grade_grades[$userid]->get_dategraded(); // create text representation of grade if (in_array($grade_item->id, $needsupdate)) { diff --git a/rating/lib.php b/rating/lib.php index b2f334776a2..f70fbb3ffc8 100644 --- a/rating/lib.php +++ b/rating/lib.php @@ -1174,6 +1174,40 @@ class rating_manager { } return $result; } + + /** + * Get ratings created since a given time. + * + * @param stdClass $context context object + * @param string $component component name + * @param int $since the time to check + * @return array list of ratings db records since the given timelimit + * @since Moodle 3.2 + */ + public function get_component_ratings_since($context, $component, $since) { + global $DB, $USER; + + $ratingssince = array(); + $where = 'contextid = ? AND component = ? AND (timecreated > ? OR timemodified > ?)'; + $ratings = $DB->get_records_select('rating', $where, array($context->id, $component, $since, $since)); + // Check area by area if we have permissions. + $permissions = array(); + $rm = new rating_manager(); + + foreach ($ratings as $rating) { + // Check if the permission array for the area is cached. + if (!isset($permissions[$rating->ratingarea])) { + $permissions[$rating->ratingarea] = $rm->get_plugin_permissions_array($context->id, $component, + $rating->ratingarea); + } + + if (($permissions[$rating->ratingarea]['view'] and $rating->userid == $USER->id) or + ($permissions[$rating->ratingarea]['viewany'] or $permissions[$rating->ratingarea]['viewall'])) { + $ratingssince[$rating->id] = $rating; + } + } + return $ratingssince; + } } // End rating_manager class definition. /**