Source for file module_blog.php

Documentation is available at module_blog.php

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

Documentation generated on Mon, 07 Jul 2008 12:32:50 +0100 by phpDocumentor 1.4.0