Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
quiz Class Reference

A class encapsulating a quiz and the questions it contains, and making the information available to scripts like view.php. More...

Public Member Functions

 __construct ($quiz, $cm, $course, $getcontext=true)
 Constructor, assuming we already have the necessary data loaded. More...
 
 attempt_url ($attemptid, $page=0)
 
 cannot_review_message ($when, $short=false)
 If $reviewoptions->attempt is false, meaning that students can't review this attempt at the moment, return an appropriate string explaining why. More...
 
 confirm_start_attempt_message ($notused)
 
 create_attempt_object ($attemptdata)
 Create a quiz_attempt for an attempt at this quiz. More...
 
 edit_url ()
 
 get_access_manager ($timenow)
 Return quiz_access_manager and instance of the quiz_access_manager class for this quiz at this time. More...
 
 get_all_question_types_used ($includepotential=false)
 Return all the question types used in this quiz. More...
 
 get_cm ()
 Get the course-module object for this quiz. More...
 
 get_cmid ()
 Get the course-module id for this quiz. More...
 
 get_context ()
 Get the quiz context. More...
 
 get_course ()
 Get the course settings object that this quiz belongs to. More...
 
 get_courseid ()
 Get the id of the course this quiz belongs to. More...
 
 get_navigation_method ()
 Get the navigation method in use. More...
 
 get_num_attempts_allowed ()
 
 get_question ($id)
 
 get_questions ($questionids=null)
 
 get_quiz ()
 Get the quiz settings object. More...
 
 get_quiz_name ()
 Get the quiz name. More...
 
 get_quizid ()
 Get this quiz's id (in the quiz table). More...
 
 get_sections ()
 Get all the sections in this quiz. More...
 
 get_structure ()
 Get an instance of the mod_quiz\structure class for this quiz. More...
 
 has_capability ($capability, $userid=null, $doanything=true)
 Wrapper round the has_capability funciton that automatically passes in the quiz context. More...
 
 has_questions ()
 
 is_participant ($userid)
 Checks user enrollment in the current course. More...
 
 is_preview_user ()
 
 load_questions ($questionids=null)
 Fully load some or all of the questions for this quiz. More...
 
 navigation ($title)
 Probably not used any more, but left for backwards compatibility. More...
 
 preload_questions ()
 Load just basic information about all the questions in this quiz.
 
 require_capability ($capability, $userid=null, $doanything=true)
 Wrapper round the require_capability function that automatically passes in the quiz context. More...
 
 review_url ($attemptid)
 
 show_only_active_users ()
 Check is only active users in course should be shown. More...
 
 start_attempt_url ($page=0)
 Get the URL to start/continue an attempt. More...
 
 summary_url ($attemptid)
 
 view_url ()
 

Static Public Member Functions

static create ($quizid, $userid=null)
 Static function to create a new quiz object for a specific user. More...
 

Protected Member Functions

 ensure_question_loaded ($id)
 Check that the definition of a particular question is loaded, and if not throw an exception. More...
 

Protected Attributes

quiz_access_manager $accessmanager = null
 the access manager for this quiz.
 
stdClass $cm
 the course_module settings from the database.
 
context $context
 the quiz context.
 
stdClass $course
 the course settings from the database.
 
bool $ispreviewuser = null
 whether the current user has capability mod/quiz:preview.
 
stdClass[] $questions = null
 of questions augmented with slot information.
 
stdClass $quiz
 the quiz settings from the database.
 
stdClass[] $sections = null
 of quiz_section rows.
 

Detailed Description

A class encapsulating a quiz and the questions it contains, and making the information available to scripts like view.php.

Initially, it only loads a minimal amout of information about each question - loading extra information only when necessary or when asked. The class tracks which questions are loaded.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Since
Moodle 2.0

Constructor & Destructor Documentation

◆ __construct()

quiz::__construct (   $quiz,
  $cm,
  $course,
  $getcontext = true 
)

Constructor, assuming we already have the necessary data loaded.

Parameters
object$quizthe row from the quiz table.
object$cmthe course_module object for this quiz.
object$coursethe row from the course table for the course we belong to.
bool$getcontextintended for testing - stops the constructor getting the context.

Member Function Documentation

◆ attempt_url()

quiz::attempt_url (   $attemptid,
  $page = 0 
)
Parameters
int$attemptidthe id of an attempt.
int$pageoptional page number to go to in the attempt.
Return values
stringthe URL of that attempt.

◆ cannot_review_message()

quiz::cannot_review_message (   $when,
  $short = false 
)

If $reviewoptions->attempt is false, meaning that students can't review this attempt at the moment, return an appropriate string explaining why.

Parameters
int$whenOne of the mod_quiz_display_options::DURING, IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
bool$shortif true, return a shorter string.
Return values
stringan appropraite message.

◆ confirm_start_attempt_message()

quiz::confirm_start_attempt_message (   $notused)
Parameters
bool$notusednot used.
Return values
stringan empty string.
Deprecated:
since 3.1. This sort of functionality is now entirely handled by quiz access rules.

◆ create()

static quiz::create (   $quizid,
  $userid = null 
)
static

Static function to create a new quiz object for a specific user.

Parameters
int$quizidthe the quiz id.
int | null$useridthe the userid (optional). If passed, relevant overrides are applied.
Return values
quizthe new quiz object.

◆ create_attempt_object()

quiz::create_attempt_object (   $attemptdata)

Create a quiz_attempt for an attempt at this quiz.

Parameters
object$attemptdatarow from the quiz_attempts table.
Return values
quiz_attemptthe new quiz_attempt object.

◆ edit_url()

quiz::edit_url ( )
Return values
stringthe URL of this quiz's edit page.

◆ ensure_question_loaded()

quiz::ensure_question_loaded (   $id)
protected

Check that the definition of a particular question is loaded, and if not throw an exception.

Parameters
int$ida question id.

◆ get_access_manager()

quiz::get_access_manager (   $timenow)

Return quiz_access_manager and instance of the quiz_access_manager class for this quiz at this time.

Parameters
int$timenowthe current time as a unix timestamp.
Return values
quiz_access_managerand instance of the quiz_access_manager class for this quiz at this time.

◆ get_all_question_types_used()

quiz::get_all_question_types_used (   $includepotential = false)

Return all the question types used in this quiz.

Parameters
boolean$includepotentialif the quiz include random questions, setting this flag to true will make the function to return all the possible question types in the random questions category.
Return values
arraya sorted array including the different question types.
Since
Moodle 3.1

◆ get_cm()

quiz::get_cm ( )

Get the course-module object for this quiz.

Return values
objectthe course_module object.

◆ get_cmid()

quiz::get_cmid ( )

Get the course-module id for this quiz.

Return values
intthe course_module id.

◆ get_context()

quiz::get_context ( )

Get the quiz context.

Return values
contextthe module context for this quiz.

◆ get_course()

quiz::get_course ( )

Get the course settings object that this quiz belongs to.

Return values
objectthe row of the course table.

◆ get_courseid()

quiz::get_courseid ( )

Get the id of the course this quiz belongs to.

Return values
intthe course id.

◆ get_navigation_method()

quiz::get_navigation_method ( )

Get the navigation method in use.

Return values
intQUIZ_NAVMETHOD_FREE or QUIZ_NAVMETHOD_SEQ.

◆ get_num_attempts_allowed()

quiz::get_num_attempts_allowed ( )
Return values
intthe number of attempts allowed at this quiz (0 = infinite).

◆ get_question()

quiz::get_question (   $id)
Parameters
int$idthe question id.
Return values
stdClassthe question object with that id.

◆ get_questions()

quiz::get_questions (   $questionids = null)
Parameters
array | null$questionidsquestion ids of the questions to load. null for all.
Return values
stdClass[]the question data objects.

◆ get_quiz()

quiz::get_quiz ( )

Get the quiz settings object.

Return values
stdClassthe row of the quiz table.

◆ get_quiz_name()

quiz::get_quiz_name ( )

Get the quiz name.

Return values
stringthe name of this quiz.

◆ get_quizid()

quiz::get_quizid ( )

Get this quiz's id (in the quiz table).

Return values
intthe quiz id.

◆ get_sections()

quiz::get_sections ( )

Get all the sections in this quiz.

Return values
array0, 1, 2, ... => quiz_sections row from the database.

◆ get_structure()

quiz::get_structure ( )

Get an instance of the mod_quiz\structure class for this quiz.

Return values
mod_quiz

◆ has_capability()

quiz::has_capability (   $capability,
  $userid = null,
  $doanything = true 
)

Wrapper round the has_capability funciton that automatically passes in the quiz context.

Parameters
string$capabilitythe name of the capability to check. For example mod/quiz:view.
int | null$useridA user id. By default (null) checks the permissions of the current user.
bool$doanythingIf false, ignore effect of admin role assignment.
Return values
booleantrue if the user has this capability. Otherwise false.

◆ has_questions()

quiz::has_questions ( )
Return values
boolwhether any questions have been added to this quiz.

◆ is_participant()

quiz::is_participant (   $userid)

Checks user enrollment in the current course.

Parameters
int$useridthe id of the user to check.
Return values
boolwhether the user is enrolled.

◆ is_preview_user()

quiz::is_preview_user ( )
Return values
boolwether the current user is someone who previews the quiz, rather than attempting it.

◆ load_questions()

quiz::load_questions (   $questionids = null)

Fully load some or all of the questions for this quiz.

You must call preload_questions() first.

Parameters
array | null$questionidsquestion ids of the questions to load. null for all.

◆ navigation()

quiz::navigation (   $title)

Probably not used any more, but left for backwards compatibility.

Parameters
string$titlethe name of this particular quiz page.
Return values
stringalways returns ''.

◆ require_capability()

quiz::require_capability (   $capability,
  $userid = null,
  $doanything = true 
)

Wrapper round the require_capability function that automatically passes in the quiz context.

Parameters
string$capabilitythe name of the capability to check. For example mod/quiz:view.
int | null$useridA user id. By default (null) checks the permissions of the current user.
bool$doanythingIf false, ignore effect of admin role assignment.

◆ review_url()

quiz::review_url (   $attemptid)
Parameters
int$attemptidthe id of an attempt.
Return values
stringthe URL of the review of that attempt.

◆ show_only_active_users()

quiz::show_only_active_users ( )

Check is only active users in course should be shown.

Return values
booltrue if only active users should be shown.

◆ start_attempt_url()

quiz::start_attempt_url (   $page = 0)

Get the URL to start/continue an attempt.

Parameters
int$pagepage in the attempt to start on (optional).
Return values
moodle_urlthe URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.

◆ summary_url()

quiz::summary_url (   $attemptid)
Parameters
int$attemptidthe id of an attempt.
Return values
stringthe URL of the review of that attempt.

◆ view_url()

quiz::view_url ( )
Return values
stringthe URL of this quiz's view page.

The documentation for this class was generated from the following file: