module_blog.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a blog module for Sloodle.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @contributor Peter R. Bloomfield
  11. */
  12. /** The Sloodle module base. */
  13. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  14. /** General Sloodle functions. */
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. /** Blog visibility identifier for private posts. (These can only be viewed by the person who posted them) */
  17. define('SLOODLE_BLOG_VISIBILITY_PRIVATE', 'draft');
  18. /** Blog visibility identifier for site posts. (These can be viewed by anybody who is registered on the site) */
  19. define('SLOODLE_BLOG_VISIBILITY_SITE', 'site');
  20. /** Blog visibility identifier for public posts. (These can be viewed by anybody) */
  21. define('SLOODLE_BLOG_VISIBILITY_PUBLIC', 'public');
  22. /**
  23. * The Sloodle blog module class.
  24. * @package sloodle
  25. */
  26. class SloodleModuleBlog extends SloodleModule
  27. {
  28. // DATA //
  29. //... None...
  30. // FUNCTIONS //
  31. /**
  32. * Constructor
  33. */
  34. function SloodleModuleBlog(&$_session)
  35. {
  36. $constructor = get_parent_class($this);
  37. parent::$constructor($_session);
  38. }
  39. /**
  40. * Checks if blogging is enabled on the site.
  41. * @return bool True if blogging is enabled, or false otherwise
  42. */
  43. function is_enabled()
  44. {
  45. // If blog level is not specified, then blogging is disabled
  46. global $CFG;
  47. if (empty($CFG->bloglevel)) return false;
  48. return true;
  49. }
  50. /**
  51. * Checks if the currently logged-in user has permission to create blog entries.
  52. * @return bool True if user has permission, or false otherwise
  53. */
  54. function user_can_write()
  55. {
  56. return has_capability('moodle/blog:create', get_context_instance(CONTEXT_SYSTEM, SITEID));
  57. }
  58. /**
  59. * Attempts to write a new blog entry to the database, attributed to the user currently loaded in the {@link SloodleSession}.
  60. * All of the text provided in parameters _must_ be database-safe!
  61. * @param string $subject The subject line of the post
  62. * @param string $body The body of the post
  63. * @param string $visibility The visibility identifier of the post (defaults to site level)
  64. * @return bool True if successful or false otherwise
  65. */
  66. function add_entry($subject, $body, $visibility = '')
  67. {
  68. // Make sure a user is loaded
  69. if (!isset($this->_session)) return false;
  70. if (!$this->_session->user->is_user_loaded()) return false;
  71. // Make sure the parameters are not empty
  72. if (empty($subject) || empty($body)) return false;
  73. if (empty($visibility)) $visibility = SLOODLE_BLOG_VISIBILITY_SITE;
  74. // Convert 'private' to 'draft' visibility
  75. if (strcasecmp($visibility, 'private') == 0) $visibility = SLOODLE_BLOG_VISIBILITY_PRIVATE;
  76. // Create a new database record
  77. $blogEntry = new stdClass();
  78. $blogEntry->subject = $subject;
  79. $blogEntry->summary = $body;
  80. $blogEntry->module = 'blog';
  81. $blogEntry->userid = $this->_session->user->get_user_id();
  82. $blogEntry->format = 1;
  83. $blogEntry->publishstate = $visibility;
  84. $blogEntry->lastmodified = time();
  85. $blogEntry->created = time();
  86. $blogEntry->courseid = 0;
  87. $blogEntry->groupid = 0;
  88. $blogEntry->moduleid = 0;
  89. $blogEntry->coursemoduleid = 0;
  90. $blogEntry->uniquehash = '';
  91. $blogEntry->rating = 0;
  92. // Attempt to insert it into the database
  93. $blogEntry->id = sloodle_insert_record('post', $blogEntry);
  94. if (!$blogEntry->id) return false;
  95. // Log the entry
  96. add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$blogEntry->userid.'&postid='.$blogEntry->id, "Entry posted from SL via Sloodle: \"{$blogEntry->subject}\"", $blogEntry->userid);
  97. return true;
  98. }
  99. /**
  100. * Gets the identified post.
  101. * @param mixed $id The unique site-wide identifier of the post.
  102. * @return SloodleBlogPost|bool Returns a {@link SloodleBlogPost} object, or FALSE if unsuccessful.
  103. */
  104. function get_post($id)
  105. {
  106. // Attempt to fetch the data from the database
  107. $rec = sloodle_get_record('post', id, $id);
  108. if (!$rec) return false;
  109. $post = new SloodleBlogPost();
  110. $post->load_data($rec);
  111. return true;
  112. }
  113. /**
  114. * Gets the most recent public post.
  115. * @return SloodleBlogPost|bool Returns a {@link SloodleBlogPost} object, or FALSE if unsuccessful.
  116. */
  117. function get_latest_public_post()
  118. {
  119. // Attempt to fetch the data from the database
  120. $rec = sloodle_get_records_select_params('post', "publishstate = 'public'", array(), 'created DESC', '*', 0, 1);
  121. if (!$rec) return false;
  122. $post = new SloodleBlogPost();
  123. $post->load_data($rec[0]);
  124. return true;
  125. }
  126. /**
  127. * Gets the most recent public/site level post.
  128. * @return SloodleBlogPost|bool Returns a {@link SloodleBlogPost} object, or FALSE if unsuccessful.
  129. */
  130. function get_latest_site_post()
  131. {
  132. // Attempt to fetch the data from the database
  133. $rec = sloodle_get_records_select_params('post', "publishstate = 'site' OR publishstate = 'public'", array(), 'created DESC', '*', 0, 1);
  134. if (!$rec) return false;
  135. $post = new SloodleBlogPost();
  136. $post->load_data($rec[0]);
  137. return true;
  138. }
  139. /**
  140. * Gets the most recent post by a particular user, regardless of visibility.
  141. * @param SloodleUser $user The user to search for. Uses the current {@link SloodleSession} user if unspecified.
  142. * @return SloodleBlogPost|bool Returns a {@link SloodleBlogPost} object, or FALSE if unsuccessful.
  143. */
  144. function get_latest_user_post($user = null)
  145. {
  146. // Get the user data from the Session, if needed
  147. if ($user == null) {
  148. $user = $this->_session->user;
  149. if (!$user->is_user_loaded()) return false;
  150. }
  151. // Attempt to fetch the data from the database
  152. $userid = (int)$user->get_user_id();
  153. $rec = sloodle_get_records_select_params('post', "userid = ?", array($userid), 'created DESC', '*', 0, 1);
  154. if (!$rec) return false;
  155. $post = new SloodleBlogPost();
  156. $post->load_data($rec[0]);
  157. return true;
  158. }
  159. // ACCESSORS //
  160. /**
  161. * Gets the name of this module instance.
  162. * @return string The name of this controller
  163. */
  164. function get_name()
  165. {
  166. return 'Blog';
  167. }
  168. /**
  169. * Gets the intro description of this module instance, if available.
  170. * @return string The intro description of this controller
  171. */
  172. function get_intro()
  173. {
  174. return '';
  175. }
  176. /**
  177. * Gets the identifier of the course this controller belongs to.
  178. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  179. */
  180. function get_course_id()
  181. {
  182. return 0;
  183. }
  184. /**
  185. * Gets the time at which this instance was created, or 0 if unknown.
  186. * @return int Timestamp
  187. */
  188. function get_creation_time()
  189. {
  190. return 0;
  191. }
  192. /**
  193. * Gets the time at which this instance was last modified, or 0 if unknown.
  194. * @return int Timestamp
  195. */
  196. function get_modification_time()
  197. {
  198. return 0;
  199. }
  200. /**
  201. * Gets the short type name of this instance.
  202. * @return string
  203. */
  204. function get_type()
  205. {
  206. return 'blog';
  207. }
  208. /**
  209. * Gets the full type name of this instance, according to the current language pack, if available.
  210. * Note: should be overridden by sub-classes.
  211. * @return string Full type name if possible, or the short name otherwise.
  212. */
  213. function get_type_full()
  214. {
  215. return 'Blog';
  216. }
  217. }
  218. /**
  219. * Represents a single blog post
  220. * @package sloodle
  221. */
  222. class SloodleBlogPost
  223. {
  224. /**
  225. * Constructor - initialises members.
  226. * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  227. * @param string $subject The subject line of this post
  228. * @param string $body The body text of this post
  229. * @param SloodleUser $user The user who posted this entry
  230. * @param string $visibility The visibility identifier of this post
  231. * @param int $timestamp_created Time when this post was first made
  232. * @param int $timestamp_modified Time when this post was last modified
  233. */
  234. function SloodleChatMessage($id=0, $subject='', $body='', $user=null, $visibility='', $timestamp_created=0, $timestamp_modified=0)
  235. {
  236. $this->id = $id;
  237. $this->subject = $subject;
  238. $this->body = $body;
  239. $this->user = $user;
  240. $this->visibility = $visibility;
  241. $this->timestamp_created = $timestamp_created;
  242. $this->timestamp_modified = $timestamp_modified;
  243. }
  244. /**
  245. * Accessor - set all members in a single call.
  246. * @param mixed $id The ID of this message - type depends on VLE, but is typically an integer
  247. * @param string $subject The subject line of this post
  248. * @param string $body The body text of this post
  249. * @param SloodleUser $user The user who posted this entry
  250. * @param string $visibility The visibility identifier of this post
  251. * @param int $timestamp_created Time when this post was first made
  252. * @param int $timestamp_modified Time when this post was last modified
  253. * @return void
  254. */
  255. function set($id, $subject, $body, $user, $visibility, $timestamp_created, $timestamp_modified)
  256. {
  257. $this->id = $id;
  258. $this->subject = $subject;
  259. $this->body = $body;
  260. $this->user = $user;
  261. $this->visibility = $visibility;
  262. $this->timestamp_created = $timestamp_created;
  263. $this->timestamp_modified = $timestamp_modified;
  264. }
  265. /**
  266. * Loads the data from a post record object.
  267. * @param object $rec Database record object contain the data required
  268. * @return void
  269. */
  270. function load_data($rec)
  271. {
  272. $this->id = $rec->id;
  273. $this->subject = $rec->subject;
  274. $this->body = $rec->summary;
  275. $this->visibility = $rec->publishstate;
  276. $this->timestamp_created = $created;
  277. $this->timestamp_modified = $lastmodified;
  278. $this->user = new SloodleUser();
  279. $this->user->load_user((int)$rec->userid);
  280. }
  281. /**
  282. * The ID of the message.
  283. * The type depends on the VLE, but typically is an integer.
  284. * @var mixed
  285. * @access public
  286. */
  287. var $id = 0;
  288. /**
  289. * The subject of the post.
  290. * @var string
  291. * @access public
  292. */
  293. var $subject = '';
  294. /**
  295. * The body of the post.
  296. * @var string
  297. * @access public
  298. */
  299. var $body = '';
  300. /**
  301. * The user who wrote this message.
  302. * @var SloodleUser
  303. * @access public
  304. */
  305. var $user = null;
  306. /**
  307. * The visibility of this post.
  308. * @var string
  309. * @access public
  310. */
  311. var $visibility = 0;
  312. /**
  313. * Timestamp of when this message was created.
  314. * @var int
  315. * @access public
  316. */
  317. var $timestamp_created = 0;
  318. /**
  319. * Timestamp of when this message was last modified.
  320. * @var int
  321. * @access public
  322. */
  323. var $timestamp_modified = 0;
  324. }
  325. ?>