Skip to content

Commit

Permalink
Import mostly working - does not spot removed modules, or properly sk…
Browse files Browse the repository at this point in the history
…ip manually added items
  • Loading branch information
davosmith committed Jan 14, 2011
1 parent 150e542 commit dfa2c92
Showing 1 changed file with 150 additions and 38 deletions.
188 changes: 150 additions & 38 deletions locallib.php
Expand Up @@ -10,7 +10,12 @@
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');

define("TEXT_INPUT_WIDTH", 45);
define("CHECKLIST_TEXT_INPUT_WIDTH", 45);
define("CHECKLIST_OPTIONAL_NO", 0);
define("CHECKLIST_OPTIONAL_YES", 1);
define("CHECKLIST_OPTIONAL_HEADING", 2);
define("CHECKLIST_OPTIONAL_DISABLED", 3);
define("CHECKLIST_OPTIONAL_HEADING_DISABLED", 4);

class checklist_class {
var $cm;
Expand Down Expand Up @@ -123,7 +128,16 @@ function get_items() {

function get_itemid_from_moduleid($moduleid) {
foreach ($this->items as $item) {
if ($item->moduleid == $moduleid) {
if (($item->moduleid == $moduleid) && ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING)) {
return $item->id;
}
}
return false;
}

function get_itemid_from_sectionid($sectionid) {
foreach ($this->items as $item) {
if (($item->moduleid == $sectionid) && ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING)) {
return $item->id;
}
}
Expand All @@ -133,13 +147,93 @@ function get_itemid_from_moduleid($moduleid) {
function update_items_from_course() {
$mods = get_fast_modinfo($this->course);

foreach ($mods->cms as $cm) {
if (!$this->get_itemid_from_moduleid($cm->id)) {
$this->additem($cm->name, 0, 0, false, false, $cm->id);
$nextpos = 1;
$section = 1;
reset($this->items);

while (array_key_exists($section, $mods->sections)) {
$sectionheading = 0;
while (list($itemid, $item) = each($this->items)) {
// Search from current position
echo 'Looking at: '.$item->displaytext.'<br/>';
if (($item->moduleid == $section) && ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING)) {
$sectionheading = $itemid;
echo 'found section: '.$section.'<br/>';
break;
}
}

if (!$sectionheading) {
// Search again from the start
foreach ($this->items as $item) {
echo 'for Looking at: '.$item->displaytext.'<br/>';
if (($item->moduleid == $section) && ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING)) {
$sectionheading = $itemid;
echo 'for found section: '.$section.'<br/>';
break;
}
}
reset($this->items);
}

if (!$sectionheading) {
echo 'adding section '.$section.'<br/>';
$name = get_string('section').' '.$section;
$sectionheading = $this->additem($name, 0, 0, false, false, $section, 2);
reset($this->items);
}

if ($this->items[$sectionheading]->position < $nextpos) {
$this->moveitemto($sectionheading, $nextpos);
reset($this->items);
}
$nextpos = $this->items[$sectionheading]->position + 1;

$foundit = false;
foreach($mods->sections[$section] as $cmid) {
while(list($itemid, $item) = each($this->items)) {
// Search list from current position (will usually be the next item)
echo 'item looking at '.$item->displaytext.'<br/>';
if (($item->moduleid == $cmid) && ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING)) {
echo 'found it!<br/>';
$foundit = true;
if ($item->position != $nextpos) {
echo 'reposition '.$item->displaytext.' => '.$nextpos.'<br/>';
$this->moveitemto($item->id, $nextpos);
reset($this->items);
}
break;
}
}
if (!$foundit) {
// Search list again from the start (just in case)
foreach($this->items as $item) {
echo 'item for looking at '.$item->displaytext.'<br/>';
if (($item->moduleid == $cmid) && ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING)) {
echo 'found it!<br/>';
$foundit = true;
if ($item->position != $nextpos) {
echo 'reposition '.$item->displaytext.' => '.$nextpos.'<br/>';
$this->moveitemto($item->id, $nextpos);
}
break;
}
}
reset($this->items);
}
if (!$foundit) {
$name = addslashes($mods->cms[$cmid]->name);
echo 'adding item '.$name.'<br/>';
$this->additem($name, 0, 0, $nextpos, false, $cmid);
reset($this->items);
}
$nextpos++;
}

$section++;
}

print_r($mods);
//print_r($mods);

// Get module ids + names for all items in the course
// Check against the items in the list
Expand Down Expand Up @@ -389,10 +483,10 @@ function view_progressbar() {
$completeitems = 0;
$allcompleteitems = 0;
foreach ($this->items as $item) {
if ($item->itemoptional == 2) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
continue;
}
if (!$item->itemoptional) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_NO) {
$requireditems++;
if ($teacherprogress) {
if ($item->teachermark == CHECKLIST_TEACHERMARK_YES) {
Expand Down Expand Up @@ -607,17 +701,17 @@ function view_items($viewother = false, $userreport = false) {
$itemcolour = 'itemblack';
}

if ($item->itemoptional == 2) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$optional = ' class="itemheading '.$itemcolour.'" ';
$spacerimg = $CFG->wwwroot.'/mod/checklist/images/check_spacer.gif';
} elseif ($item->itemoptional == 1) {
} elseif ($item->itemoptional == CHECKLIST_OPTIONAL_YES) {
$optional = ' class="itemoptional '.$itemcolour.'" ';
} else {
$optional = ' class="'.$itemcolour.'" ';
}
echo '<li>';
if ($showteachermark) {
if ($item->itemoptional == 2) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
//echo '<img src="'.$spacerimg.'" alt="" title="" />';
} else {
if ($viewother) {
Expand All @@ -637,7 +731,7 @@ function view_items($viewother = false, $userreport = false) {
}
}
if ($showcheckbox) {
if ($item->itemoptional == 2) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
//echo '<img src="'.$spacerimg.'" alt="" title="" />';
} else {
echo '<input type="checkbox" name="items[]" id='.$itemname.$checked.' value="'.$item->id.'" />';
Expand Down Expand Up @@ -707,7 +801,7 @@ function view_items($viewother = false, $userreport = false) {
echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
echo '<input type="hidden" name="itemid" value="'.$useritem->id.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" size="'.TEXT_INPUT_WIDTH.'" name="displaytext" value="'.s($text).'" id="updateitembox" />';
echo '<input type="text" size="'.CHECKLIST_TEXT_INPUT_WIDTH.'" name="displaytext" value="'.s($text).'" id="updateitembox" />';
echo '<input type="submit" name="updateitem" value="'.get_string('updateitem','checklist').'" />';
echo '<br />';
echo '<textarea name="displaytextnote" rows="3" cols="25">'.s($note).'</textarea>';
Expand Down Expand Up @@ -766,7 +860,7 @@ function view_items($viewother = false, $userreport = false) {
if ($showcheckbox) {
echo '<input type="checkbox" disabled="disabled" />';
}
echo '<input type="text" size="'.TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
echo '<input type="text" size="'.CHECKLIST_TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
echo '<input type="submit" name="additem" value="'.get_string('additem','checklist').'" />';
echo '<br />';
echo '<textarea name="displaytextnote" rows="3" cols="25"></textarea>';
Expand Down Expand Up @@ -913,16 +1007,20 @@ function view_edit_items() {
$nexticon = 'colour_red.gif';
}

$autoitem = ($this->checklist->autopopulate) && ($item->moduleid != 0);

echo '<li>';
if ($item->itemoptional == 1) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_YES) {
$title = '"'.get_string('optionalitem','checklist').'"';
echo '<a href="'.$baseurl.'makeheading">';
echo '<img src="'.$CFG->wwwroot.'/mod/checklist/images/empty_box.gif" alt='.$title.' title='.$title.' /></a>&nbsp;';
$optional = ' class="itemoptional '.$itemcolour.'" ';
} elseif ($item->itemoptional == 2) {
} elseif ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$title = '"'.get_string('headingitem','checklist').'"';
echo '<a href="'.$baseurl.'makerequired">';
echo '<img src="'.$CFG->wwwroot.'/mod/checklist/images/no_box.gif" alt='.$title.' title='.$title.' /></a>&nbsp;';
if (!$autoitem) { echo '<a href="'.$baseurl.'makerequired">'; }
echo '<img src="'.$CFG->wwwroot.'/mod/checklist/images/no_box.gif" alt='.$title.' title='.$title.' />';
if (!$autoitem) { echo '</a>'; }
echo '&nbsp;';
$optional = ' class="itemheading '.$itemcolour.'" ';
} else {
$title = '"'.get_string('requireditem','checklist').'"';
Expand All @@ -937,7 +1035,7 @@ function view_edit_items() {
echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
echo '<input type="hidden" name="itemid" value="'.$item->id.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" size="'.TEXT_INPUT_WIDTH.'" name="displaytext" value="'.s($item->displaytext).'" id="updateitembox" />';
echo '<input type="text" size="'.CHECKLIST_TEXT_INPUT_WIDTH.'" name="displaytext" value="'.s($item->displaytext).'" id="updateitembox" />';
if ($this->editdates) {
echo '<input type="hidden" name="editdates" value="on" />';
$this->print_edit_date($item->duetime);
Expand All @@ -963,18 +1061,20 @@ function view_edit_items() {
echo '<a href="'.$baseurl.'nextcolour">';
$title = '"'.get_string('changetextcolour','checklist').'"';
echo '<img src="'.$CFG->wwwroot.'/mod/checklist/images/'.$nexticon.'" alt='.$title.' title='.$title.' /></a>';

echo '<a href="'.$baseurl.'edititem">';
$title = '"'.get_string('edititem','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt='.$title.' title='.$title.' /></a>&nbsp;';

if ($item->indent > 0) {
if (!$autoitem) {
echo '<a href="'.$baseurl.'edititem">';
$title = '"'.get_string('edititem','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt='.$title.' title='.$title.' /></a>&nbsp;';
}

if (!$autoitem && $item->indent > 0) {
echo '<a href="'.$baseurl.'unindentitem">';
$title = '"'.get_string('unindentitem','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/left.gif" alt='.$title.' title='.$title.' /></a>';
}

if (($item->indent < CHECKLIST_MAX_INDENT) && (($lastindent+1) > $currindent)) {
if (!$autoitem && ($item->indent < CHECKLIST_MAX_INDENT) && (($lastindent+1) > $currindent)) {
echo '<a href="'.$baseurl.'indentitem">';
$title = '"'.get_string('indentitem','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/right.gif" alt='.$title.' title='.$title.' /></a>';
Expand All @@ -983,13 +1083,13 @@ function view_edit_items() {
echo '&nbsp;';

// TODO more complex checks to take into account indentation
if ($item->position > 1) {
if (!$autoitem && $item->position > 1) {
echo '<a href="'.$baseurl.'moveitemup">';
$title = '"'.get_string('moveitemup','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/up.gif" alt='.$title.' title='.$title.' /></a>';
}

if ($item->position < $lastitem) {
if (!$autoitem && $item->position < $lastitem) {
echo '<a href="'.$baseurl.'moveitemdown">';
$title = '"'.get_string('moveitemdown','checklist').'"';
echo '<img src="'.$CFG->pixpath.'/t/down.gif" alt='.$title.' title='.$title.' /></a>';
Expand Down Expand Up @@ -1020,7 +1120,7 @@ function view_edit_items() {
echo '<input type="hidden" name="indent" value="'.$item->indent.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="checkbox" disabled="disabled" />';
echo '<input type="text" size="'.TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
echo '<input type="text" size="'.CHECKLIST_TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
if ($this->editdates) {
echo '<input type="hidden" name="editdates" value="on" />';
$this->print_edit_date();
Expand Down Expand Up @@ -1054,7 +1154,7 @@ function view_edit_items() {
echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
echo '<input type="hidden" name="indent" value="'.$currindent.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" size="'.TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
echo '<input type="text" size="'.CHECKLIST_TEXT_INPUT_WIDTH.'" name="displaytext" value="" id="additembox" />';
if ($this->editdates) {
echo '<input type="hidden" name="editdates" value="on" />';
$this->print_edit_date();
Expand Down Expand Up @@ -1163,14 +1263,14 @@ function view_report() {
if ($this->showoptional) {
$itemstocount = array();
foreach ($this->items as $item) {
if ($item->itemoptional != 2) {
if ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING) {
$itemstocount[] = $item->id;
}
}
} else {
$itemstocount = array();
foreach ($this->items as $item) {
if (!$item->itemoptional) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_NO) {
$itemstocount[] = $item->id;
}
}
Expand Down Expand Up @@ -1241,7 +1341,7 @@ function view_report() {
$table->head[] = s($item->displaytext);
$table->level[] = ($item->indent < 3) ? $item->indent : 2;
$table->size[] = '80px';
$table->skip[] = (!$this->showoptional) && ($item->itemoptional == 1);
$table->skip[] = (!$this->showoptional) && ($item->itemoptional == CHECKLIST_OPTIONAL_YES);
}

$table->data = array();
Expand All @@ -1265,7 +1365,7 @@ function view_report() {
$checks = get_records_sql($sql);

foreach ($checks as $check) {
if ($check->itemoptional == 2) {
if ($check->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$row[] = array(false, false, true);
} else {
if ($check->usertimestamp > 0) {
Expand Down Expand Up @@ -1555,19 +1655,19 @@ function process_report_actions() {
}
}

function additem($displaytext, $userid=0, $indent=0, $position=false, $duetime=false, $moduleid=0) {
function additem($displaytext, $userid=0, $indent=0, $position=false, $duetime=false, $moduleid=0, $optional=0) {
$displaytext = trim($displaytext);
if ($displaytext == '') {
return;
return false;
}

if ($userid) {
if (!$this->canaddown()) {
return;
return false;
}
} else {
if (!$this->canedit()) {
return;
return false;
}
}

Expand All @@ -1581,7 +1681,7 @@ function additem($displaytext, $userid=0, $indent=0, $position=false, $duetime=f
}
$item->indent = $indent;
$item->userid = $userid;
$item->itemoptional = false;
$item->itemoptional = $optional;
$item->duetime = 0;
if ($duetime) {
$item->duetime = make_timestamp($duetime['year'], $duetime['month'], $duetime['day']);
Expand Down Expand Up @@ -1611,6 +1711,8 @@ function additem($displaytext, $userid=0, $indent=0, $position=false, $duetime=f
}
}
}

return $item->id;
}

function setevent($itemid, $add) {
Expand Down Expand Up @@ -1864,6 +1966,16 @@ function makeoptional($itemid, $optional, $heading=false) {
$optional = 0;
}

if ($this->items[$itemid]->moduleid) {
if ($this->items[$itemid]->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
return; // Topic headings must stay as headings
} elseif ($this->items[$itemid]->itemoptional == CHECKLIST_OPTIONAL_YES) {
$optional = 0; // Module links cannot become headings
} else {
$optional = 1;
}
}

$this->items[$itemid]->itemoptional = $optional;
$upditem = new stdClass;
$upditem->id = $itemid;
Expand Down

0 comments on commit dfa2c92

Please sign in to comment.