Skip to content

Commit

Permalink
MDL-14679 towards notes conversion and regression
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 31, 2008
1 parent 27af904 commit b3829d0
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ function init_full() {
if ($this->id == $COURSE->id) {
$this->courserecord = $COURSE;
} else {
$this->courserecord = $DB->get_record('course', 'id', $this->id);
$this->courserecord = $DB->get_record('course', array('id'=>$this->id));
}

if(empty($this->courserecord) && !defined('ADMIN_STICKYBLOCKS')) {
Expand Down
22 changes: 11 additions & 11 deletions notes/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$userid = required_param('user', PARAM_INT);

/// locate course information
if (!($course = get_record('course', 'id', $courseid))) {
if (!($course = $DB->get_record('course', array('id'=>$courseid)))) {
print_error('Incorrect course id found');
}

Expand All @@ -23,7 +23,7 @@


/// locate user information
if (!($user = get_record('user', 'id', $userid))) {
if (!($user = $DB->get_record('user', array('id'=>$userid)))) {
print_error('Incorrect user id found');
}

Expand All @@ -39,12 +39,12 @@
}

/// if data was submitted and validated, then save it to database
if ($formdata = $noteform->get_data()) {
if ($formdata = $noteform->get_data(false)) {
$note = new object();
$note->courseid = $formdata->course;
$note->content = $formdata->content;
$note->format = FORMAT_PLAIN;
$note->userid = $formdata->user;
$note->courseid = $formdata->course;
$note->content = $formdata->content;
$note->format = FORMAT_PLAIN;
$note->userid = $formdata->user;
$note->publishstate = $formdata->publishstate;
if (note_save($note)) {
add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'add note');
Expand All @@ -53,15 +53,15 @@
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid);
}

if($noteform->is_submitted()) {
if ($noteform->is_submitted()) {
// if data was submitted with errors, then use it as default for new form
$note = $noteform->get_submitted_data(false);
} else {
// if data was not submitted yet, then use default values
$note = new object();
$note->id = 0;
$note->course = $courseid;
$note->user = $userid;
$note->id = 0;
$note->course = $courseid;
$note->user = $userid;
$note->publishstate = optional_param('state', NOTES_STATE_PUBLIC, PARAM_ALPHA);
}
$noteform->set_data($note);
Expand Down
6 changes: 3 additions & 3 deletions notes/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
}

// locate course information
if (!$course = get_record('course', 'id', $note->courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
print_error('Incorrect course id found');
}

// locate user information
if (!$user = get_record('user', 'id', $note->userid)) {
if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
print_error('Incorrect user id found');
}

Expand All @@ -32,7 +32,7 @@
print_error('You may not delete this note');
}

if (data_submitted() && confirm_sesskey()) {
if (data_submitted(false) && confirm_sesskey()) {
//if data was submitted and is valid, then delete note
$returnurl = $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&user=' . $note->userid;
if (note_delete($noteid)) {
Expand Down
16 changes: 8 additions & 8 deletions notes/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
}

/// locate course information
if (!$course = get_record('course', 'id', $note->courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
print_error('Incorrect course id found');
}

/// locate user information
if (!$user = get_record('user', 'id', $note->userid)) {
if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
print_error('Incorrect user id found');
}

Expand Down Expand Up @@ -45,10 +45,10 @@

/// if data was submitted and validated, then save it to database
if ($formdata = $noteform->get_data()){
$note->courseid = $formdata->course;
$note->userid = $formdata->user;
$note->content = $formdata->content;
$note->format = FORMAT_PLAIN;
$note->courseid = $formdata->course;
$note->userid = $formdata->user;
$note->content = $formdata->content;
$note->format = FORMAT_PLAIN;
$note->publishstate = $formdata->publishstate;
if (note_save($note)) {
add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id, 'update note');
Expand All @@ -63,9 +63,9 @@
$note = $noteform->get_submitted_data(false);
} else {
// if data was not submitted yet, then used values retrieved from the database
$note->user = $note->userid;
$note->user = $note->userid;
$note->course = $note->courseid;
$note->note = $note->id;
$note->note = $note->id;
}
$noteform->set_data($note);
$strnotes = get_string('editnote', 'notes');
Expand Down
4 changes: 2 additions & 2 deletions notes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
}

/// locate course information
if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('Incorrect course id specified');
}

/// locate user information
if ($userid) {
if (!$user = get_record('user', 'id', $userid)) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error('Incorrect user id specified');
}
$filtertype = 'user';
Expand Down
71 changes: 45 additions & 26 deletions notes/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,34 @@
* @return array of note objects
*/
function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
global $DB;

// setup filters
$selects = array();
if($courseid) {
$selects[] = 'courseid=' . $courseid;
$params = array();
if ($courseid) {
$selects[] = 'courseid=?';
$params[] = $courseid;
}
if($userid) {
$selects[] = 'userid=' . $userid;
if ($userid) {
$selects[] = 'userid=?';
$params[] = $userid;
}
if($author) {
$selects[] = 'usermodified=' . $author;
if ($author) {
$selects[] = 'usermodified=?';
$params[] = $author;
}
if($state) {
$selects[] = "publishstate='$state'";
if ($state) {
$selects[] = 'publishstate=?';
$params[] = $state;
}
$selects[] = "module='notes'";
$selects[] = "module=?";
$params[] = 'notes';

$select = implode(' AND ', $selects);
$fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
// retrieve data
$rs =& get_recordset_select('post', $select, $order, $fields, $limitfrom, $limitnum);
return recordset_to_array($rs);
return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
}

/**
Expand All @@ -61,8 +69,10 @@ function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='las
* @return note object
*/
function note_load($note_id) {
global $DB;

$fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
return get_record_select('post', "id=$note_id AND module='notes'", $fields);
return $DB->get_record('post', array('id'=>$note_id, 'module'=>'notes'), $fields);
}

/**
Expand All @@ -73,9 +83,10 @@ function note_load($note_id) {
* @return boolean true if the object was saved; false otherwise
*/
function note_save(&$note) {
global $USER;
global $USER, $DB;

// setup & clean fields
$note->module = 'notes';
$note->module = 'notes';
$note->lastmodified = time();
$note->usermodified = $USER->id;
if(empty($note->format)) {
Expand All @@ -88,15 +99,15 @@ function note_save(&$note) {
if(empty($note->id)) {
// insert new note
$note->created = $note->lastmodified;
if($id = insert_record('post', $note)) {
if ($id = $DB->insert_record('post', $note)) {
$note->id = $id;
$result = true;
} else {
$result = false;
}
} else {
// update old note
$result = update_record('post', $note);
$result = $DB->update_record('post', $note);
}
unset($note->module);
return $result;
Expand All @@ -109,7 +120,9 @@ function note_save(&$note) {
* @return boolean true if the object was deleted; false otherwise
*/
function note_delete($noteid) {
return delete_records_select('post', "id=$noteid AND module='notes'");
global $DB;

return $DB->delete_records('post', array('id'=>$noteid, 'module'=>'notes'));
}

/**
Expand All @@ -124,7 +137,11 @@ function note_get_state_name($state) {
if (empty($states)) {
$states = note_get_state_names();
}
return @$states[$state];
if (isset($states[$state])) {
return $states[$state];
} else {
return null;
}
}

/**
Expand All @@ -147,20 +164,20 @@ function note_get_state_names() {
* @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
*/
function note_print($note, $detail = NOTES_SHOW_FULL) {
global $CFG, $USER, $DB;

global $CFG, $USER;
if (!$user = get_record('user','id',$note->userid)) {
if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
debugging("User $note->userid not found");
return;
}
if (!$author = get_record('user','id',$note->usermodified)) {
if (!$author = $DB->get_record('user', array('id'=>$note->usermodified))) {
debugging("User $note->usermodified not found");
return;
}
$context = get_context_instance(CONTEXT_COURSE, $note->courseid);
$sitecontext = get_context_instance(CONTEXT_SYSTEM);

$authoring = new object;
$authoring = new object();
$authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
$authoring->date = userdate($note->lastmodified);

Expand Down Expand Up @@ -227,9 +244,9 @@ function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
* @param string $state state of the notes (i.e. draft, public, site) ('' means any)
* @param int $author id of the user who modified the note last time (0 means any)
*/
function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)
{
function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
global $CFG;

if ($header) {
echo '<h3 class="notestitle">' . $header . '</h3>';
echo '<div class="notesgroup">';
Expand All @@ -242,7 +259,7 @@ function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $coursei
}
}
if ($viewnotes) {
$notes =& note_list($courseid, $userid, $state, $author);
$notes = note_list($courseid, $userid, $state, $author);
if ($notes) {
note_print_list($notes);
}
Expand All @@ -260,6 +277,8 @@ function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $coursei
* @return bool success
*/
function note_delete_all($courseid) {
return delete_records('post', 'module', 'notes', 'courseid', $courseid);
global $DB;

return $DB->delete_records('post', array('module'=>'notes', 'courseid'=>$courseid));
}
?>
2 changes: 1 addition & 1 deletion rss/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}


if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
rss_not_found();
}

Expand Down

0 comments on commit b3829d0

Please sign in to comment.