Skip to content

Commit

Permalink
Merge branch 'MDL-39723-master' of git://github.com/sammarshallou/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 4, 2013
2 parents dd03ed1 + a260245 commit 79452da
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion course/format/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
24 changes: 24 additions & 0 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
26 changes: 26 additions & 0 deletions lib/tests/datalib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
5 changes: 5 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 79452da

Please sign in to comment.