Source for file module_sloodleobject.php

Documentation is available at module_sloodleobject.php

  1. <?php
  2.     // This file is part of the Sloodle project (www.sloodle.org)
  3.     
  4.     /**
  5.     * This file defines a Sloodle Object assignment 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.     // Make sure the Sloodle Object assignment type is installed
  20.     $incfile $CFG->dirroot.'/mod/assignment/type/sloodleobject/assignment.class.php';
  21.     if (!file_exists($incfile)) return// Leaves this script, but does not terminate execution
  22.     
  23.     /** The Sloodle Object assignment type. */
  24.     require_once($incfile);
  25.     
  26.     /**
  27.     * The Sloodle Object assignment module class.
  28.     * @package sloodle
  29.     */
  30.     class SloodleModuleSloodleObject extends SloodleModule
  31.     {
  32.     // DATA //
  33.     
  34.         /**
  35.         * Internal for Moodle only - course module instance.
  36.         * Corresponds to one record from the Moodle 'course_modules' table.
  37.         * @var object 
  38.         * @access private
  39.         */
  40.         var $cm = null;
  41.     
  42.         /**
  43.         * Internal only - Moodle assignment module instance database object.
  44.         * Corresponds to one record from the Moodle 'assignment' table.
  45.         * @var object 
  46.         * @access private
  47.         */
  48.         var $moodle_assignment_instance = null;
  49.  
  50.         /**
  51.         * Internal only - the Moodle assignment structure.
  52.         * @var assignment_sloodleobject 
  53.         * @access private
  54.         */
  55.         var $assignment = null;
  56.                 
  57.         
  58.     // FUNCTIONS //
  59.     
  60.         /**
  61.         * Constructor
  62.         */
  63.         function SloodleModuleSloodleObject(&$_session)
  64.         {
  65.             $constructor get_parent_class($this);
  66.             parent::$constructor($_session);
  67.         }
  68.         
  69.         /**
  70.         * Loads data from the database.
  71.         * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  72.         * @param mixed $id The site-wide unique identifier for all modules. Type depends on VLE. On Moodle, it is an integer course module identifier ('id' field of 'course_modules' table)
  73.         * @return bool True if successful, or false otherwise
  74.         */
  75.         function load($id)
  76.         {
  77.             // Make sure the ID is valid
  78.             $id = (int)$id;
  79.             if ($id <= 0return false;
  80.             
  81.             // Fetch the course module data
  82.             if (!($this->cm = get_coursemodule_from_id('assignment'$id))) {
  83.                 sloodle_debug("Failed to load course module instance #$id.<br/>");
  84.                 return false;
  85.             }
  86.             // Make sure the module is visible
  87.             if ($this->cm->visible == 0{
  88.                 sloodle_debug("Error: course module instance #$id not visible.<br/>");
  89.                 return false;
  90.             }
  91.             
  92.             // Load from the primary table: assignment instance
  93.             if (!($this->moodle_assignment_instance = get_record('assignment''id'$this->cm->instance))) {
  94.                 sloodle_debug("Failed to load assignment with instance ID #{$cm->instance}.<br/>");
  95.                 return false;
  96.             }
  97.             
  98.             // Make sure this assignment is of the correct type
  99.             if ($this->moodle_assignment_instance->assignmenttype != 'sloodleobject'{
  100.                 sloodle_debug("ERROR assignment \"{$this->moodle_assignment_instance->name}\" is not of type 'sloodleobject' (actual type: '{$this->moodle_assignment_instance->assignmenttype}').");
  101.                 return false;
  102.             }
  103.             
  104.             // Attempt to construct the assignment object
  105.             $this->assignment = new assignment_sloodleobject($this->cm->id$this->moodle_assignment_instance$this->cm);
  106.             
  107.             return true;
  108.         }
  109.         
  110.         /**
  111.         * Checks if the specified user is permitted to submit to this assignment.
  112.         * (Note: this only checks permissions, and no other settings, such as submission times).
  113.         * @param SloodleUser $user The user to be checked
  114.         * @return bool True if the user has permission to submit to this assignment, or false otherwise.
  115.         */
  116.         function user_can_submit($user)
  117.         {
  118.             // Make sure a user is loaded
  119.             if (!$user->is_user_loaded()) return false;
  120.             // Login the current user, and check capabilities
  121.             if (!$user->login()) return false;
  122.             return has_capability('mod/assignment:submit'get_context_instance(CONTEXT_MODULE$this->cm->id));
  123.         }
  124.         
  125.         /**
  126.         * Checks if the specified user is permitted to view submissions to this assignment.
  127.         * @param SloodleUser $user The user to be checked
  128.         * @return bool True if the user has permission to view the submissions, or false otherwise.
  129.         */
  130.         function user_can_view($user)
  131.         {
  132.             // Make sure a user is loaded
  133.             if (!$user->is_user_loaded()) return false;
  134.             // Login the current user, and check capabilities
  135.             if (!$user->login()) return false;
  136.             return has_capability('mod/assignment:view'get_context_instance(CONTEXT_MODULE$this->cm->id));
  137.         }
  138.         
  139.         /**
  140.         * Checks if the specified user has submitted to this assignment already.
  141.         * @param SloodleUser $user The user to be checked
  142.         * @return bool True if the user has previously attempted this assignment, or false otherwise
  143.         */
  144.         function user_has_submitted($user)
  145.         {
  146.             // Make sure a user is loaded
  147.             if (!$user->is_user_loaded()) return false;
  148.             // Check for a submission by this user
  149.             if (record_exists('assignment_submissions''assignment'$this->moodle_assignment_instance->id'userid'$user->get_user_id())) return true;
  150.             return false;
  151.         }
  152.         
  153.         /**
  154.         * Checks if re-submissions are permitted.
  155.         * @return bool True if resubmissions are permitted, or false otherwise
  156.         */
  157.         function resubmit_allowed()
  158.         {
  159.             if (empty($this->moodle_assignment_instance->resubmit)) return false;
  160.             return true;
  161.         }
  162.         
  163.         /**
  164.         * Checks if an assignment submitted at the specified time would be too early for submission.
  165.         * @param int $timestamp A timestamp giving the time to check (if omitted, it defaults to the current timestamp)
  166.         * @return bool True if assignment would be too early, or false if it's OK.
  167.         */
  168.         function is_too_early($timestamp null)
  169.         {
  170.             // If no 'available' time is set, then nothing is too early
  171.             if (empty($this->moodle_assignment_instance->timeavailable|| $this->moodle_assignment_instance->timeavailable <= 0return false;
  172.             // Use the current timestamp if need be
  173.             if ($timestamp == null$timestamp time();
  174.             // Check the time
  175.             return ($timestamp $this->moodle_assignment_instance->timeavailable);
  176.         }
  177.         
  178.         /**
  179.         * Checks if an assignment submitted at the specified time would be too late for submission.
  180.         * @param int $timestamp A timestamp giving the time to check (if omitted, it defaults to the current timestamp)
  181.         * @return int 1 if assignment would be too late and cannot be accepted, 0 if it is OK, or -1 if it would be late but still accepted
  182.         */
  183.         function is_too_late($timestamp null)
  184.         {
  185.             // If no 'due' time is set, then nothing is too early
  186.             if (empty($this->moodle_assignment_instance->timedue|| $this->moodle_assignment_instance->timedue <= 0return false;
  187.             // Use the current timestamp if need be
  188.             if ($timestamp == null$timestamp time();
  189.             
  190.             // Check the time
  191.             if ($timestamp $this->moodle_assignment_instance->timedue{
  192.                 // It's late... check if late submissions are prevented
  193.                 if (empty($this->moodle_assignment_instance->preventlate)) {
  194.                     return -1;
  195.                 else {
  196.                     return 1;
  197.                 }
  198.             }
  199.             // It's OK
  200.             return 0;
  201.         }
  202.         
  203.         /**
  204.         * Add a new submission (or replace an existing one).
  205.         * Ignores all submission checks, such as permissions and time.
  206.         * @param SloodleUser $user The user making the submission
  207.         * @param string $obj_name Name of the object being submitted
  208.         * @param int $num_prims Number of prims in the object being submitted
  209.         * @param string $primdrop_name Name of the PrimDrop being submitted to
  210.         * @param string $primdrop_uuid UUID of the PrimDrop being submitted to
  211.         * @param string $primdrop_region Region of the PrimDrop being submitted to
  212.         * @param string $primdrop_pos Position vector (<x,y,z>) of the PrimDrop being submitted to
  213.         * @return bool True if successful, or false otherwise
  214.         */
  215.         function submit($user$obj_name$num_prims$primdrop_name$primdrop_uuid$primdrop_region$primdrop_pos)
  216.         {
  217.             // Make sure the user is loaded
  218.             if (!$user->is_user_loaded()) return false;
  219.             // Construct a submission object
  220.             $sloodle_submission new assignment_sloodleobject_submission();
  221.             $sloodle_submission->obj_name $obj_name;
  222.             $sloodle_submission->num_prims $num_prims;
  223.             $sloodle_submission->primdrop_name $primdrop_name;
  224.             $sloodle_submission->primdrop_uuid $primdrop_uuid;
  225.             $sloodle_submission->primdrop_region $primdrop_region;
  226.             $sloodle_submission->primdrop_pos $primdrop_pos;
  227.             
  228.             // Update the submission
  229.             return $this->assignment->update_submission($user->get_user_id()$sloodle_submission);
  230.         }
  231.         
  232.         
  233.     // ACCESSORS //
  234.     
  235.         /**
  236.         * Gets the name of this module instance.
  237.         * @return string The name of this controller
  238.         */
  239.         function get_name()
  240.         {
  241.             return $this->moodle_assignment_instance->name;
  242.         }
  243.         
  244.         /**
  245.         * Gets the intro description of this module instance, if available.
  246.         * @return string The intro description of this controller
  247.         */
  248.         function get_intro()
  249.         {
  250.             return $this->moodle_assignment_instance->description;
  251.         }
  252.         
  253.         /**
  254.         * Gets the identifier of the course this controller belongs to.
  255.         * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  256.         */
  257.         function get_course_id()
  258.         {
  259.             return (int)$this->moodle_assignment_instance->course;
  260.         }
  261.         
  262.         /**
  263.         * Gets the time at which this instance was created, or 0 if unknown.
  264.         * @return int Timestamp
  265.         */
  266.         function get_creation_time()
  267.         {
  268.             return 0;
  269.         }
  270.         
  271.         /**
  272.         * Gets the time at which this instance was last modified, or 0 if unknown.
  273.         * @return int Timestamp
  274.         */
  275.         function get_modification_time()
  276.         {
  277.             return (int)$this->moodle_assignment_instance->timemodified;
  278.         }
  279.         
  280.         
  281.         /**
  282.         * Gets the short type name of this instance.
  283.         * @return string 
  284.         */
  285.         function get_type()
  286.         {
  287.             return 'sloodleobject';
  288.         }
  289.  
  290.         /**
  291.         * Gets the full type name of this instance, according to the current language pack, if available.
  292.         * Note: should be overridden by sub-classes.
  293.         * @return string Full type name if possible, or the short name otherwise.
  294.         */
  295.         function get_type_full()
  296.         {
  297.             return get_string('typesloodleobject''assignment');
  298.         }
  299.  
  300.     }
  301.  
  302. ?>

Documentation generated on Mon, 07 Jul 2008 12:33:00 +0100 by phpDocumentor 1.4.0