Skip to content

Commit

Permalink
Merge branch 'MDL-26371_distinct_text' of git://github.com/stronk7/mo…
Browse files Browse the repository at this point in the history
…odle
  • Loading branch information
skodak committed Feb 14, 2011
2 parents 251e5a8 + e07c51c commit cbbc272
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
24 changes: 15 additions & 9 deletions enrol/category/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,22 @@ function enrol_category_sync_full() {

// first of all add necessary enrol instances to all courses
$parentcat = $DB->sql_concat("cat.path", "'/%'");
$sql = "SELECT DISTINCT c.*
// need whole course records to be used by add_instance(), use inner view (ci) to
// get distinct records only.
// TODO: Moodle 2.1. Improve enrol API to accept courseid / courserec
$sql = "SELECT c.*
FROM {course} c
JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :courselevel)
JOIN (SELECT DISTINCT cctx.path
FROM {course_categories} cc
JOIN {context} cctx ON (cctx.instanceid = cc.id AND cctx.contextlevel = :catlevel)
JOIN {role_assignments} ra ON (ra.contextid = cctx.id AND ra.roleid $roleids)
) cat ON (ctx.path LIKE $parentcat)
LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'category')
WHERE e.id IS NULL";
JOIN (
SELECT DISTINCT c.id
FROM {course} c
JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :courselevel)
JOIN (SELECT DISTINCT cctx.path
FROM {course_categories} cc
JOIN {context} cctx ON (cctx.instanceid = cc.id AND cctx.contextlevel = :catlevel)
JOIN {role_assignments} ra ON (ra.contextid = cctx.id AND ra.roleid $roleids)
) cat ON (ctx.path LIKE $parentcat)
LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'category')
WHERE e.id IS NULL) ci ON (c.id = ci.id)";

$rs = $DB->get_recordset_sql($sql, $params);
foreach($rs as $course) {
Expand Down
14 changes: 12 additions & 2 deletions mod/glossary/sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
default:
$sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
$sqlfrom = "FROM {glossary_entries} ge";
// For cases needing inner view
$sqlwrapheader = '';
$sqlwrapfooter = '';

$where = '';
$fullpivot = 0;
Expand Down Expand Up @@ -203,7 +206,13 @@
} else {
$searchcond = implode(" AND ", $searchcond);

$sqlselect = "SELECT DISTINCT ge.*, ge.concept AS glossarypivot";
// Need one inner view here to avoid distinct + text
$sqlwrapheader = 'SELECT ge.*, ge.concept AS glossarypivot
FROM {glossary_entries} ge
JOIN ( ';
$sqlwrapfooter = ' ) gei ON (ge.id = gei.id)';

$sqlselect = "SELECT DISTINCT ge.id";
$sqlfrom = "FROM {glossary_entries} ge
LEFT JOIN {glossary_alias} al ON al.entryid = ge.id";
$where = "AND ($searchcond)";
Expand Down Expand Up @@ -262,5 +271,6 @@
$limitnum = $entriesbypage;
}

$allentries = $DB->get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby", $params, $limitfrom, $limitnum);
$query = "$sqlwrapheader $sqlselect $sqlfrom $sqlwhere $sqlwrapfooter $sqlorderby";
$allentries = $DB->get_records_sql($query, $params, $limitfrom, $limitnum);

17 changes: 11 additions & 6 deletions mod/lesson/essay.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@
} else {
$queryadd = '';
$params = array ("lessonid" => $lesson->id);
if (!$users = $DB->get_records_sql("SELECT DISTINCT u.id, u.*
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
u.id = a.userid
ORDER BY u.lastname", $params)) {
// Need to use inner view to avoid distinct + text
if (!$users = $DB->get_records_sql("
SELECT u.*
FROM {user} u
JOIN (
SELECT DISTINCT u.id
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
u.id = a.userid) ui ON (u.id = ui.id)
ORDER BY u.lastname", $params)) {
print_error('cannotfinduser', 'lesson');
}
}
Expand Down
6 changes: 4 additions & 2 deletions mod/lesson/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/lesson:manage', $context);

$ufields = user_picture::fields('u'); // These fields are enough
$params = array("lessonid" => $lesson->id);
// TODO: Improve this. Fetching all students always is crazy!
if (!empty($cm->groupingid)) {
$params["groupid"] = $cm->groupingid;
$sql = "SELECT DISTINCT u.id, u.*
$sql = "SELECT DISTINCT $ufields
FROM {lesson_attempts} a
INNER JOIN {user} u ON u.id = a.userid
INNER JOIN {groups_members} gm ON gm.userid = u.id
INNER JOIN {groupings_groups} gg ON gm.groupid = :groupid
WHERE a.lessonid = :lessonid
ORDER BY u.lastname";
} else {
$sql = "SELECT DISTINCT u.id, u.*
$sql = "SELECT DISTINCT $ufields
FROM {user} u,
{lesson_attempts} a
WHERE a.lessonid = :lessonid and
Expand Down

0 comments on commit cbbc272

Please sign in to comment.