Permalink
Browse files

Merge branch 'MDL-39723-m25' of git://github.com/sammarshallou/moodle…

… into MOODLE_25_STABLE
  • Loading branch information...
stronk7 committed Jun 4, 2013
2 parents bf2360c + 695f6f3 commit 8c8b3eba2479e99a69a07ca77487d5954584dd49
Showing with 56 additions and 1 deletion.
  1. +1 −1 course/format/lib.php
  2. +24 −0 lib/datalib.php
  3. +26 −0 lib/tests/datalib_test.php
  4. +5 −0 lib/upgrade.txt
View
@@ -235,7 +235,7 @@ public function get_course() {
return null;
}
if ($this->course === false) {
- $this->course = $DB->get_record('course', array('id' => $this->courseid));
+ $this->course = get_course($this->courseid);
$options = $this->get_format_options();
foreach ($options as $optionname => $optionvalue) {
if (!isset($this->course->$optionname)) {
View
@@ -556,6 +556,30 @@ function get_site() {
}
}
+/**
+ * Gets a course object from database. If the course id corresponds to an
+ * already-loaded $COURSE or $SITE object, then the loaded object will be used,
+ * saving a database query.
+ *
+ * If it reuses an existing object, by default the object will be cloned. This
+ * means you can modify the object safely without affecting other code.
+ *
+ * @param int $courseid Course id
+ * @param bool $clone If true (default), makes a clone of the record
+ * @return stdClass A course object
+ * @throws dml_exception If not found in database
+ */
+function get_course($courseid, $clone = true) {
+ global $DB, $COURSE, $SITE;
+ if (!empty($COURSE->id) && $COURSE->id == $courseid) {
+ return $clone ? clone($COURSE) : $COURSE;
+ } else if (!empty($SITE->id) && $SITE->id == $courseid) {
+ return $clone ? clone($SITE) : $SITE;
+ } else {
+ return $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
+ }
+}
+
/**
* Returns list of courses, for whole site, or category
*
View
@@ -267,4 +267,30 @@ public function test_get_admins() {
get_admins(); // This should make just one query.
$this->assertEquals($odlread+1, $DB->perf_get_reads());
}
+
+ public function test_get_course() {
+ global $DB, $PAGE, $SITE;
+
+ $this->resetAfterTest(true);
+
+ // First test course will be current course ($COURSE).
+ $course1obj = $this->getDataGenerator()->create_course(array('shortname' => 'FROGS'));
+ $PAGE->set_course($course1obj);
+
+ // Second test course is not current course.
+ $course2obj = $this->getDataGenerator()->create_course(array('shortname' => 'ZOMBIES'));
+
+ // Check it does not make any queries when requesting the $COURSE/$SITE
+ $before = $DB->perf_get_queries();
+ $result = get_course($course1obj->id);
+ $this->assertEquals($before, $DB->perf_get_queries());
+ $this->assertEquals('FROGS', $result->shortname);
+ $result = get_course($SITE->id);
+ $this->assertEquals($before, $DB->perf_get_queries());
+
+ // Check it makes 1 query to request other courses.
+ $result = get_course($course2obj->id);
+ $this->assertEquals('ZOMBIES', $result->shortname);
+ $this->assertEquals($before + 1, $DB->perf_get_queries());
+ }
}
View
@@ -1,6 +1,11 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
+=== 2.5.1 ===
+
+* New get_course() function for use when obtaining the course record from database. Will
+ reuse existing $COURSE or $SITE globals if possible to improve performance.
+
=== 2.5 ===
* The database drivers (moodle_database and subclasses) aren't using anymore the ::columns property

0 comments on commit 8c8b3eb

Please sign in to comment.