Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/notes/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00010 define('NOTES_STATE_DRAFT', 'draft');
00011 define('NOTES_STATE_PUBLIC', 'public');
00012 define('NOTES_STATE_SITE', 'site');
00013 
00017 define('NOTES_SHOW_FULL', 0x07);
00018 define('NOTES_SHOW_HEAD', 0x02);
00019 define('NOTES_SHOW_BODY', 0x01);
00020 define('NOTES_SHOW_FOOT', 0x04);
00021 
00034 function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
00035     global $DB;
00036 
00037     // setup filters
00038     $selects = array();
00039     $params = array();
00040     if ($courseid) {
00041         $selects[] = 'courseid=?';
00042         $params[]  = $courseid;
00043     }
00044     if ($userid) {
00045         $selects[] = 'userid=?';
00046         $params[]  = $userid;
00047     }
00048     if ($author) {
00049         $selects[] = 'usermodified=?';
00050         $params[]  = $author;
00051     }
00052     if ($state) {
00053         $selects[] = 'publishstate=?';
00054         $params[]  = $state;
00055     }
00056     $selects[] = "module=?";
00057     $params[]  = 'notes';
00058 
00059     $select = implode(' AND ', $selects);
00060     $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
00061     // retrieve data
00062     return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
00063 }
00064 
00071 function note_load($note_id) {
00072     global $DB;
00073 
00074     $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
00075     return $DB->get_record('post', array('id'=>$note_id, 'module'=>'notes'), $fields);
00076 }
00077 
00085 function note_save(&$note) {
00086     global $USER, $DB;
00087 
00088     // setup & clean fields
00089     $note->module       = 'notes';
00090     $note->lastmodified = time();
00091     $note->usermodified = $USER->id;
00092     if (empty($note->format)) {
00093         $note->format = FORMAT_PLAIN;
00094     }
00095     if (empty($note->publishstate)) {
00096         $note->publishstate = NOTES_STATE_PUBLIC;
00097     }
00098     // save data
00099     if (empty($note->id)) {
00100         // insert new note
00101         $note->created = $note->lastmodified;
00102         $id = $DB->insert_record('post', $note);
00103         $note = $DB->get_record('post', array('id'=>$id));
00104     } else {
00105         // update old note
00106         $DB->update_record('post', $note);
00107     }
00108     unset($note->module);
00109     return true;
00110 }
00111 
00118 function note_delete($noteid) {
00119     global $DB;
00120 
00121     return $DB->delete_records('post', array('id'=>$noteid, 'module'=>'notes'));
00122 }
00123 
00130 function note_get_state_name($state) {
00131     // cache state names
00132     static $states;
00133     if (empty($states)) {
00134         $states = note_get_state_names();
00135     }
00136     if (isset($states[$state])) {
00137         return $states[$state];
00138     } else {
00139         return null;
00140     }
00141 }
00142 
00148 function note_get_state_names() {
00149     return array(
00150         NOTES_STATE_DRAFT => get_string('personal', 'notes'),
00151         NOTES_STATE_PUBLIC => get_string('course', 'notes'),
00152         NOTES_STATE_SITE => get_string('site', 'notes'),
00153     );
00154 }
00155 
00162 function note_print($note, $detail = NOTES_SHOW_FULL) {
00163     global $CFG, $USER, $DB, $OUTPUT;
00164 
00165     if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
00166         debugging("User $note->userid not found");
00167         return;
00168     }
00169     if (!$author = $DB->get_record('user', array('id'=>$note->usermodified))) {
00170         debugging("User $note->usermodified not found");
00171         return;
00172     }
00173     $context = get_context_instance(CONTEXT_COURSE, $note->courseid);
00174     $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00175 
00176     $authoring = new stdClass();
00177     $authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
00178     $authoring->date = userdate($note->lastmodified);
00179 
00180     echo '<div class="notepost '. $note->publishstate . 'notepost' .
00181         ($note->usermodified == $USER->id ? ' ownnotepost' : '')  .
00182         '" id="note-'. $note->id .'">';
00183 
00184     // print note head (e.g. author, user refering to, etc)
00185     if ($detail & NOTES_SHOW_HEAD) {
00186         echo '<div class="header">';
00187         echo '<div class="user">';
00188         echo $OUTPUT->user_picture($user, array('courseid'=>$note->courseid));
00189         echo fullname($user) . '</div>';
00190         echo '<div class="info">' .
00191             get_string('bynameondate', 'notes', $authoring) .
00192             ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
00193         echo '</div>';
00194     }
00195 
00196     // print note content
00197     if ($detail & NOTES_SHOW_BODY) {
00198         echo '<div class="content">';
00199         echo format_text($note->content, $note->format, array('overflowdiv'=>true));
00200         echo '</div>';
00201     }
00202 
00203     // print note options (e.g. delete, edit)
00204     if ($detail & NOTES_SHOW_FOOT) {
00205         if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate == NOTES_STATE_SITE ||
00206             has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
00207             echo '<div class="footer"><p>';
00208             echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?id='.$note->id. '">'. get_string('edit') .'</a> | ';
00209             echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?id='.$note->id. '">'. get_string('delete') .'</a>';
00210             echo '</p></div>';
00211         }
00212     }
00213     echo '</div>';
00214 }
00215 
00222 function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
00223 
00225     echo '<div class="notelist">';
00226     foreach ($notes as $note) {
00227         note_print($note, $detail);
00228     }
00229     echo '</div>';
00230 }
00231 
00243 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
00244     global $CFG;
00245 
00246     if ($header) {
00247         echo '<h3 class="notestitle">' . $header . '</h3>';
00248         echo '<div class="notesgroup">';
00249     }
00250     if ($addcourseid) {
00251         if ($userid) {
00252            echo '<p><a href="'. $CFG->wwwroot .'/notes/edit.php?courseid=' . $addcourseid . '&amp;userid=' . $userid . '&amp;publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
00253         } else {
00254            echo '<p><a href="'. $CFG->wwwroot .'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
00255         }
00256     }
00257     if ($viewnotes) {
00258         $notes = note_list($courseid, $userid, $state, $author);
00259         if ($notes) {
00260             note_print_list($notes);
00261         }
00262     } else {
00263         echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
00264     }
00265     if ($header) {
00266         echo '</div>';  // notesgroup
00267     }
00268 }
00269 
00275 function note_delete_all($courseid) {
00276     global $DB;
00277 
00278     return $DB->delete_records('post', array('module'=>'notes', 'courseid'=>$courseid));
00279 }
00280 
00287 function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
00288     return array('notes-*'=>get_string('page-notes-x', 'notes'));
00289 }
 All Data Structures Namespaces Files Functions Variables Enumerations