Skip to content

Commit

Permalink
MDL-49231 mod_glossary: Adding a method to validate a glossary by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart authored and jleyva committed Dec 31, 2015
1 parent e70f58c commit 21e3002
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public static function fill_entry_details($entry, $context, $useridfield = 'user
}
}

/**
* Validate a glossary via ID.
*
* @param int $id The glossary ID.
* @return array Contains glossary, context, course and cm.
*/
public static function validate_glossary($id) {
global $DB;
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
return array($glossary, $context, $course, $cm);
}

/**
* Describes the parameters for get_glossaries_by_courses.
*
Expand Down Expand Up @@ -330,11 +345,8 @@ public static function view_glossary($id, $mode) {
$mode = $params['mode'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context, $course, $cm) = self::validate_glossary($id);

// Trigger module viewed event.
glossary_view($glossary, $course, $cm, $context, $mode);
Expand Down Expand Up @@ -385,12 +397,9 @@ public static function view_entry($id) {
$id = $params['id'];
$warnings = array();

// Fetch and confirm.
// Get and validate the glossary.
$entry = $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);
$glossary = $DB->get_record('glossary', array('id' => $entry->glossaryid), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
list($glossary, $context) = self::validate_glossary($entry->glossaryid);

if (empty($entry->approved) && $entry->userid != $USER->id && !has_capability('mod/glossary:approve', $context)) {
throw new invalid_parameter_exception('invalidentry');
Expand Down Expand Up @@ -466,11 +475,8 @@ public static function get_entries_by_letter($id, $letter, $from = 0, $limit = 2
$options = $params['options'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
Expand Down Expand Up @@ -603,11 +609,8 @@ public static function get_entries_by_date($id, $order = 'UPDATE', $sort = 'DESC
throw new invalid_parameter_exception('invalidsort');
}

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
Expand Down Expand Up @@ -710,11 +713,8 @@ public static function get_categories($id, $from = 0, $limit = 20) {
$limit = $params['limit'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Fetch the categories.
$count = $DB->count_records('glossary_categories', array('glossaryid' => $id));
Expand Down Expand Up @@ -800,11 +800,8 @@ public static function get_entries_by_category($id, $categoryid, $from = 0, $lim
$options = $params['options'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
Expand Down Expand Up @@ -948,11 +945,8 @@ public static function get_authors($id, $from = 0, $limit = 20, $options = array
$options = $params['options'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Fetch the authors.
$params = array();
Expand Down Expand Up @@ -1084,11 +1078,8 @@ public static function get_entries_by_author($id, $letter, $field = 'LASTNAME',
throw new invalid_parameter_exception('invalidsort');
}

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
Expand Down Expand Up @@ -1231,11 +1222,8 @@ public static function get_entries_by_author_id($id, $authorid, $order = 'CONCEP
throw new invalid_parameter_exception('invalidsort');
}

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Get and validate the glossary.
list($glossary, $context) = self::validate_glossary($id);

// Validate the mode.
$modes = self::get_browse_modes_from_display_format($glossary->displayformat);
Expand Down

0 comments on commit 21e3002

Please sign in to comment.