/** * here the position of the given item will be set to the value in $pos * @param object $moveitem * @param int $pos * @return boolean */ function feedback_move_item($moveitem, $pos) { if ($moveitem->position == $pos) { return true; } if (!($allitems = get_records('feedback_item', 'feedback', $moveitem->feedback, 'position'))) { return false; } if (is_array($allitems)) { $index = 1; foreach ($allitems as $item) { if ($item->id == $moveitem->id) { continue; } //the moving item is handled special if ($index == $pos) { $moveitem->position = $index; feedback_update_item($moveitem); $index++; } $item->position = $index; feedback_update_item($item); $index++; } if ($pos >= count($allitems)) { $moveitem->position = $index; feedback_update_item($moveitem); } return true; } return false; }
/** * here the position of the given item will be set to the value in $pos * * @global object * @param object $moveitem * @param int $pos * @return boolean */ function feedback_move_item($moveitem, $pos) { global $DB; $params = array('feedback'=>$moveitem->feedback); if (!$allitems = $DB->get_records('feedback_item', $params, 'position')) { return false; } if (is_array($allitems)) { $index = 1; foreach ($allitems as $item) { if ($index == $pos) { $index++; } if ($item->id == $moveitem->id) { $moveitem->position = $pos; feedback_update_item($moveitem); continue; } $item->position = $index; feedback_update_item($item); $index++; } return true; } return false; }
$newposition = $formdata->position; $formdata->position = $newposition + 1; if (!($newitemid = feedback_create_item($formdata))) { $SESSION->feedback->errors[] = get_string('item_creation_failed', 'feedback'); } else { $newitem = get_record('feedback_item', 'id', $newitemid); if (!feedback_move_item($newitem, $newposition)) { $SESSION->feedback->errors[] = get_string('item_creation_failed', 'feedback'); } else { redirect(htmlspecialchars('edit.php?id=' . $id)); } } } if (isset($formdata->updateitem) and $formdata->updateitem == 1) { //update the item and go back if (!feedback_update_item($item, $formdata)) { $SESSION->feedback->errors[] = get_string('item_update_failed', 'feedback'); } else { if (!feedback_move_item($item, $formdata->position)) { $SESSION->feedback->errors[] = get_string('item_update_failed', 'feedback'); } else { redirect(htmlspecialchars('edit.php?id=' . $id)); } } } //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// /// Print the page header $strfeedbacks = get_string("modulenameplural", "feedback"); $strfeedback = get_string("modulename", "feedback"); $buttontext = update_module_button($cm->id, $course->id, $strfeedback);