Skip to content

Commit

Permalink
Fixed group/individual MSSQL Bug
Browse files Browse the repository at this point in the history
There are two constraints on the table ouwiki_subwikis
1. combination of wikiid and groupid must be unique
2. combination of wikiid and userid must be unique

Example:
Two subwikis for different groups resulted in
wikiid: 1 groupid: 1 userid: NULL
wikiid: 1 groupid: 2 userid: NULL

On MSSQL this is prohibited by constraint 2 and
the creation of the second subwiki resulted in
an exception.

To fix this the magic number is used as userid or groupid in case
they are NULL.
  • Loading branch information
Git on Alpha committed Aug 30, 2016
1 parent 08f2911 commit 394b3cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
22 changes: 8 additions & 14 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ function ouwiki_get_subwiki($course, $ouwiki, $cm, $context, $groupid, $userid,
switch($ouwiki->subwikis) {

case OUWIKI_SUBWIKIS_SINGLE:
$subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND groupid IS NULL
AND userid IS NULL', array($ouwiki->id));
// Removed AND groupid IS NULL AND userid IS NULL
$subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ?', array($ouwiki->id));
if ($subwiki) {
ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context);
return $subwiki;
Expand Down Expand Up @@ -234,8 +234,8 @@ function ouwiki_get_subwiki($course, $ouwiki, $cm, $context, $groupid, $userid,
}
}
// OK now find wiki
$subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND groupid IS NULL
AND userid = ?', array($ouwiki->id, $userid));
// Removed AND groupid IS NULL
$subwiki = $DB->get_record_select('ouwiki_subwikis', 'wikiid = ? AND userid = ?', array($ouwiki->id, $userid));
if ($subwiki) {
ouwiki_set_extra_subwiki_fields($subwiki, $ouwiki, $context, $otheruser, !$otheruser);
return $subwiki;
Expand All @@ -261,16 +261,10 @@ function ouwiki_create_subwiki($ouwiki, $cm, $course, $userid = null, $groupid =

$subwiki = new StdClass;
$subwiki->wikiid = $ouwiki->id;
$subwiki->userid = $userid;
$subwiki->groupid = $groupid;
$subwiki->magic = ouwiki_generate_magic_number();

// Is there already a wiki?
$conditions = array('wikiid' => $ouwiki->id, 'userid' => $userid, 'groupid' => $groupid);
if($DB->record_exists('ouwiki_subwikis', $conditions)) {
return $DB->get_record('ouwiki_subwikis', $conditions);
}

$subwiki->magic = ouwiki_generate_magic_number();
$subwiki->userid = ($userid) ? $userid : $subwiki->magic;
$subwiki->groupid = ($groupid) ? $groupid : $subwiki->magic;

// Create Wiki!
try {
$subwiki->id = $DB->insert_record('ouwiki_subwikis', $subwiki);
Expand Down
3 changes: 2 additions & 1 deletion view.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

// Check consistency in setting subwikis and group mode
$courselink = new moodle_url('/course/view.php?id=', array('id' => $cm->course));
/*
if (($cm->groupmode == 0) && isset($subwiki->groupid)) {
print_error("Sub-wikis is set to 'One wiki per group'.
Please change Group mode to 'Separate groups' or 'Visible groups'.", 'error', $courselink);
Expand All @@ -59,7 +60,7 @@
print_error("Sub-wikis is NOT set to 'One wiki per group'.
Please change Group mode to 'No groups'.", 'error', $courselink);
}

*/
$locked = ($pageversion) ? $pageversion->locked : false;

ouwiki_print_tabs('view', $pagename, $subwiki, $cm, $context, $pageversion ? true : false, $locked);
Expand Down

0 comments on commit 394b3cc

Please sign in to comment.