/** * Test remove_course_content deletes course contents * TODO Add asserts to verify other data related to course is deleted as well. */ public function test_remove_course_contents() { $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); $gen = $this->getDataGenerator()->get_plugin_generator('core_notes'); $note = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id)); $this->assertNotEquals(false, note_load($note->id)); remove_course_contents($course->id, false); $this->assertFalse(note_load($note->id)); }
<?php // $Id$ require_once '../config.php'; require_once 'lib.php'; // retrieve parameters $noteid = required_param('id', PARAM_INT); // locate note information if (!($note = note_load($noteid))) { error('Incorrect note id specified'); } // locate course information if (!($course = get_record('course', 'id', $note->courseid))) { error('Incorrect course id found'); } // locate user information if (!($user = get_record('user', 'id', $note->userid))) { error('Incorrect user id found'); } // require login to access notes require_login($course); // locate context information $context = get_context_instance(CONTEXT_COURSE, $course->id); // check capability if (!has_capability('moodle/notes:manage', $context)) { print_error('nopermissiontodelete', 'notes'); } if (empty($CFG->enablenotes)) { print_error('notesdisabled', 'notes'); } if (data_submitted() && confirm_sesskey()) {
/** * Saves a note object. The note object is passed by reference and its fields (i.e. id) * might change during the save. * * @param stdClass $note object to save * @return boolean true if the object was saved; false otherwise */ function note_save(&$note) { global $USER, $DB; // Setup & clean fields. $note->module = 'notes'; $note->lastmodified = time(); $note->usermodified = $USER->id; if (empty($note->format)) { $note->format = FORMAT_PLAIN; } if (empty($note->publishstate)) { $note->publishstate = NOTES_STATE_PUBLIC; } // Save data. if (empty($note->id)) { // Insert new note. $note->created = $note->lastmodified; $id = $DB->insert_record('post', $note); $note = note_load($id); // Trigger event. $event = \core\event\note_created::create(array('objectid' => $note->id, 'courseid' => $note->courseid, 'relateduserid' => $note->userid, 'userid' => $note->usermodified, 'context' => context_course::instance($note->courseid), 'other' => array('publishstate' => $note->publishstate))); $event->trigger(); } else { // Update old note. $DB->update_record('post', $note); $note = note_load($note->id); // Trigger event. $event = \core\event\note_updated::create(array('objectid' => $note->id, 'courseid' => $note->courseid, 'relateduserid' => $note->userid, 'userid' => $note->usermodified, 'context' => context_course::instance($note->courseid), 'other' => array('publishstate' => $note->publishstate))); $event->trigger(); } unset($note->module); return true; }
/** * Update notes about users. * * @param array $notes An array of ids for the notes to update. * @return array fail infos. * @since Moodle 2.2 */ public static function update_notes($notes = array()) { global $CFG, $DB; $params = self::validate_parameters(self::update_notes_parameters(), array('notes' => $notes)); // Check if note system is enabled. if (!$CFG->enablenotes) { throw new moodle_exception('notesdisabled', 'notes'); } $warnings = array(); foreach ($params['notes'] as $note) { $notedetails = note_load($note['id']); if (isset($notedetails->id)) { // Ensure the current user is allowed to run this function. $context = context_course::instance($notedetails->courseid); self::validate_context($context); require_capability('moodle/notes:manage', $context); $dbnote = new stdClass(); $dbnote->id = $note['id']; $dbnote->content = $note['text']; $dbnote->format = external_validate_format($note['format']); // Get the state ('personal', 'course', 'site'). switch ($note['publishstate']) { case 'personal': $dbnote->publishstate = NOTES_STATE_DRAFT; break; case 'course': $dbnote->publishstate = NOTES_STATE_PUBLIC; break; case 'site': $dbnote->publishstate = NOTES_STATE_SITE; $dbnote->courseid = SITEID; break; default: $warnings[] = array('item' => 'note', 'itemid' => $note["id"], 'warningcode' => 'badparam', 'message' => 'Provided publishstate incorrect'); break; } if (!note_save($dbnote)) { $warnings[] = array('item' => 'note', 'itemid' => $note["id"], 'warningcode' => 'savedfailed', 'message' => 'Note could not be modified'); } } else { $warnings[] = array('item' => 'note', 'itemid' => $note["id"], 'warningcode' => 'badid', 'message' => 'Note does not exist'); } } return $warnings; }
/** * Deletes a note object based on its id. * * @param int|object $note id of the note to delete, or a note object which is to be deleted. * @return boolean true if the object was deleted; false otherwise */ function note_delete($note) { global $DB; if (is_int($note)) { $note = note_load($note); debugging('Warning: providing note_delete with a note object would improve performance.',DEBUG_DEVELOPER); } $logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid)); $logurl->set_anchor('note-' . $note->id); add_to_log($note->courseid, 'notes', 'delete', $logurl, 'delete note'); return $DB->delete_records('post', array('id'=>$note->id, 'module'=>'notes')); }
/** * Saves a note object. The note object is passed by reference and its fields (i.e. id) * might change during the save. * * @param note $note object to save * @return boolean true if the object was saved; false otherwise */ function note_save(&$note) { global $USER, $DB; // setup & clean fields $note->module = 'notes'; $note->lastmodified = time(); $note->usermodified = $USER->id; if (empty($note->format)) { $note->format = FORMAT_PLAIN; } if (empty($note->publishstate)) { $note->publishstate = NOTES_STATE_PUBLIC; } // save data if (empty($note->id)) { // insert new note $note->created = $note->lastmodified; $id = $DB->insert_record('post', $note); $note = note_load($id); } else { // update old note $DB->update_record('post', $note); $note = note_load($note->id); } unset($note->module); return true; }