Source for file module_choice.php
Documentation is available at module_choice.php
// This file is part of the Sloodle project (www.sloodle.org)
* This file defines a choice module for Sloodle.
* @copyright Copyright (c) 2008 Sloodle (various contributors)
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
* @contributor Peter R. Bloomfield
/** The Sloodle module base. */
require_once(SLOODLE_LIBROOT.
'/modules/module_base.php');
/** General Sloodle functions. */
require_once(SLOODLE_LIBROOT.
'/general.php');
/** Include the standard Moodle choice module library. */
require_once($CFG->dirroot.
'/mod/choice/lib.php');
* The Sloodle choice module class.
* Internal for Moodle only - course module instance.
* Corresponds to one record from the Moodle 'course_modules' table.
* Internal only - Moodle choice module instance database object.
* Corresponds to one record from the Moodle 'choice' table.
* The number of (non-admin) users on the course who have not yet answered this choice.
* The options available for this choice, as an associative array of IDs to {@link SloodleChoiceOption} objects.
parent::$constructor($_session);
* Loads data from the database.
* Note: even if the function fails, it may still have overwritten some or all existing data in the object.
* @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)
* @return bool True if successful, or false otherwise
// Make sure the ID is valid
if ($id <=
0) return false;
// Fetch the course module data
if (!($this->cm =
get_coursemodule_from_id('choice', $id))) {
sloodle_debug("Failed to load course module instance #$id.<br/>");
// Make sure the module is visible
if ($this->cm->visible ==
0) {
sloodle_debug("Error: course module instance #$id not visible.<br/>");
// Load from the primary table: choice instance
sloodle_debug("Failed to load choice with instance ID #{$cm->instance}.<br/>
");
foreach ($options as $opt) {
// Create our option object and add our data
$this->options[$opt->id]->id =
$opt->id;
$this->options[$opt->id]->text =
$opt->text;
$this->options[$opt->id]->maxselections =
$opt->maxanswers;
$this->options[$opt->id]->numselections = (int)
count_records('choice_answers', 'optionid', $opt->id);
$this->options[$opt->id]->timemodified = (int)
$opt->timemodified;
// Determine how many people on the course have not yet answered
$users =
get_course_users($this->cm->course);
$num_users =
count($users);
* Selects an option in this choice on behalf of the specified user.
* Logs the user in to the VLE if necessary.
* If a general error occurs, FALSE will be returned.
* Otherwise, an integer {@link http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes status code} will be returned.
* The following status codes are typical responses:
* * 10011 = added new choice selection
* * 10012 = updated existing choice selection
* * 10013 = user previously selected same option
* * -10011 = User already made a selection, and re-selection is not allowed
* * -10012 = max number of selections for this option already made
* * -10013 = choice is not yet open
* * -10014 = choice is already closed
* @param mixed $optionid The unique site-wide identifier of the option to be selected
* @param mixed $user A SloodleUser identifying the current user; if omitted, the current {@link SloodleSession} will be used.
// Fetch a user if necessary
// Make sure we have a session user
if (!isset
($this->_session->user)) return false;
// Make sure we have a user loaded and logged-in
if (!$user->is_user_loaded()) return false;
if (!$user->login()) return false;
// Make sure the user is permitted to select from this choice
if (!has_capability('mod/choice:choose', get_context_instance(CONTEXT_MODULE, $this->cm->id))) return -
331;
// Make sure the choice is open
if ($this->is_late()) return -
10014;
// Has the user already made a selection for this choice?
$update_selection =
false;
$previous_selection =
get_record('choice_answers', 'choiceid', $this->moodle_choice_instance->id, 'userid', $user->get_user_id());
if ($previous_selection) {
// Was it a selection of the same option?
if ($previous_selection->optionid ==
$optionid) {
// Yes - that's fine. Nothing to do.
// No - are re-selections allowed?
$update_selection =
true;
// Fetch the option record
if (!$option) return false;
// Make sure the maximum selections for the given option have not yet been made
$numselections =
count_records('choice_answers', 'optionid', $optionid);
if (!$numselections) return false;
if ($numselections >=
$option->maxanswers) return -
10012;
// If necessary, delete the existing selection
if ($update_selection) delete_records('choice_answers', 'choiceid', $this->moodle_choice_instance->id, 'userid', $user->get_user_id());
$selection =
new stdClass();
$selection->userid =
$user->get_user_id();
$selection->optionid =
$optionid;
$selection->timemodified =
time();
if (!insert_record('choice_answers', $selection)) return false;
if ($update_selection) return 10012;
* Gets the name of this module instance.
* @return string The name of this controller
* Gets the intro description of this module instance, if available.
* @return string The intro description of this controller
* Gets the identifier of the course this controller belongs to.
* @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
* Gets the time at which this instance was created, or 0 if unknown.
* Gets the time at which this instance was last modified, or 0 if unknown.
* Gets the short type name of this instance.
* Gets the full type name of this instance, according to the current language pack, if available.
* Note: should be overridden by sub-classes.
* @return string Full type name if possible, or the short name otherwise.
return get_string('modulename', 'choice');
* Gets the time at which this choice opens.
* @return int Timestamp. 0 if choice has no opening time.
* Gets the time at which this choice closes.
* @return int Timestamp. 0 if choice has no closing time.
* Determines if the choice is currently open.
* @param int $timestamp The time to test. Uses the current time if none is given.
function is_open($timestamp =
null)
// Use the current time if necessary
if ($timestamp ===
null) $timestamp =
time();
// Check against the opening and closing times
if ($open >
0 &&
$open >
$timestamp) return false;
if ($close >
0 &&
$close <
$timestamp) return false;
* Determines if the choice has not opened yet.
* @param int $timestamp The time to test. Uses the current time if none is given.
// Use the current time if necessary
if ($timestamp ===
null) $timestamp =
time();
// Check against the opening time
if ($open ==
0) return false; // No opening time - can never be early
return ($open >
$timestamp);
* Determines if the choice has already closed.
* @param int $timestamp The time to test. Uses the current time if none is given.
function is_late($timestamp =
null)
// Use the current time if necessary
if ($timestamp ===
null) $timestamp =
time();
// Check against the closing time
if ($close ==
0) return false; // No opening time - can never be early
return ($close <
$timestamp);
* Checks if users are allowed to re-select their answer in this choice.
* Checks if results are to be shown.
* (Some choices only allow results after the choice is closed).
* Gets the number of people who have not yet answered the choice.
* Counts all users on the course, including students and teachers.
* Class to represent a single available option for a choice.
* The ID of the option (should be unique across the site).
* The text of this option.
* Number of selections so far of this option.
* Maximum allowed number of selections for this option.
* Note: will be -1 if there is no limit.
* Timestamp of when this option was last modified.
Documentation generated on Mon, 07 Jul 2008 12:32:55 +0100 by phpDocumentor 1.4.0