lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Library of interface functions and constants for module nasatlx from LInE-IME-USP.
  4. * All the core Moodle functions, neeeded to allow the module to work integrated in Moodle should be placed here.
  5. * All the nasatlx specific functions, needed to implement all the module logic, should go to locallib.php.
  6. * This will help to save some memory when
  7. * Moodle is performing actions across all modules.
  8. *
  9. * @author Leônidas O. Brandão
  10. * @version v 0.1 2019/03/04
  11. * @package mod_nasatlx
  12. * @copyright 2014 LInE - http://line.ime.usp.br
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  14. **/
  15. defined('MOODLE_INTERNAL') || die();
  16. /// example constant
  17. //define('NEWMODULE_ULTIMATE_ANSWER', 42);
  18. /// Returns the information on whether the module supports a feature
  19. // @see plugin_supports() in lib/moodlelib.php
  20. // @param string $feature FEATURE_xx constant for requested feature
  21. // @return mixed true if the feature is supported, null if unknown
  22. function nasatlx_supports($feature) {
  23. switch($feature) {
  24. case FEATURE_MOD_INTRO: return true;
  25. case FEATURE_SHOW_DESCRIPTION: return true;
  26. default: return null;
  27. }
  28. }
  29. /// Saves a new instance of the nasatlx into the database
  30. // Given an object containing all the necessary data,
  31. // (defined by the form in mod_form.php) this function
  32. // will create a new instance and return the id number
  33. // of the new instance.
  34. // @param object $nasatlx An object from the form in mod_form.php
  35. // @param mod_nasatlx_mod_form $mform
  36. // @return int The id of the newly inserted nasatlx record
  37. function nasatlx_add_instance(stdClass $nasatlx, mod_nasatlx_mod_form $mform = null) {
  38. global $DB;
  39. $nasatlx->timecreated = time();
  40. # You may have to add extra stuff in here #
  41. return $DB->insert_record('nasatlx', $nasatlx);
  42. }
  43. /// Updates an instance of the nasatlx in the database
  44. // Given an object containing all the necessary data,
  45. // (defined by the form in mod_form.php) this function
  46. // will update an existing instance with new data.
  47. // @param object $nasatlx An object from the form in mod_form.php
  48. // @param mod_nasatlx_mod_form $mform
  49. // @return boolean Success/Fail
  50. function nasatlx_update_instance(stdClass $nasatlx, mod_nasatlx_mod_form $mform = null) {
  51. global $DB;
  52. $nasatlx->timemodified = time();
  53. $nasatlx->id = $nasatlx->instance;
  54. # You may have to add extra stuff in here #
  55. return $DB->update_record('nasatlx', $nasatlx);
  56. }
  57. /// Removes an instance of the nasatlx from the database
  58. // Given an ID of an instance of this module,
  59. // this function will permanently delete the instance
  60. // and any data that depends on it.
  61. // @param int $id Id of the module instance
  62. // @return boolean Success/Failure
  63. function nasatlx_delete_instance($id) {
  64. global $DB;
  65. if (! $nasatlx = $DB->get_record('nasatlx', array('id' => $id))) {
  66. return false;
  67. }
  68. # Delete any dependent records here #
  69. $DB->delete_records('nasatlx', array('id' => $nasatlx->id));
  70. return true;
  71. }
  72. /// Returns a small object with summary information about what a
  73. // user has done with a given particular instance of this module
  74. // Used for user activity reports.
  75. // $return->time = the time they did it
  76. // $return->info = a short text description
  77. // @return stdClass|null
  78. function nasatlx_user_outline($course, $user, $mod, $nasatlx) {
  79. $return = new stdClass();
  80. $return->time = 0;
  81. $return->info = '';
  82. return $return;
  83. }
  84. /// Prints a detailed representation of what a user has done with
  85. // a given particular instance of this module, for user activity reports.
  86. // @param stdClass $course the current course record
  87. // @param stdClass $user the record of the user we are generating report for
  88. // @param cm_info $mod course module info
  89. // @param stdClass $nasatlx the module instance record
  90. // @return void, is supposed to echp directly
  91. function nasatlx_user_complete($course, $user, $mod, $nasatlx) {
  92. }
  93. /// Given a course and a time, this module should find recent activity
  94. // that has occurred in nasatlx activities and print it out.
  95. // Return true if there was output, or false is there was none.
  96. // @return boolean
  97. function nasatlx_print_recent_activity($course, $viewfullnames, $timestart) {
  98. return false; // True if anything was printed, otherwise false
  99. }
  100. /// Prepares the recent activity data
  101. // This callback function is supposed to populate the passed array with
  102. // custom activity records. These records are then rendered into HTML via
  103. // {@link nasatlx_print_recent_mod_activity() }.
  104. // @param array $activities sequentially indexed array of objects with the 'cmid' property
  105. // @param int $index the index in the $activities to use for the next record
  106. // @param int $timestart append activity since this time
  107. // @param int $courseid the id of the course we produce the report for
  108. // @param int $cmid course module id
  109. // @param int $userid check for a particular user's activity only, defaults to 0 (all users)
  110. // @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
  111. // @return void adds items into $activities and increases $index
  112. function nasatlx_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
  113. }
  114. /// Prints single activity item prepared by {@see nasatlx_get_recent_mod_activity() }
  115. // @return void
  116. function nasatlx_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
  117. }
  118. /// Function to be run periodically according to the moodle cron
  119. // This function searches for things that need to be done, such
  120. // as sending out mail, toggling flags etc ...
  121. // @return boolean
  122. // @todo Finish documenting this function
  123. function nasatlx_cron () {
  124. return true;
  125. }
  126. /// Returns all other caps used in the module
  127. // @example return array('moodle/site:accessallgroups');
  128. // @return array
  129. function nasatlx_get_extra_capabilities() {
  130. return array();
  131. }
  132. ////////////////////////////////////////////////////////////////////////////////
  133. // Gradebook API //
  134. ////////////////////////////////////////////////////////////////////////////////
  135. /// Is a given scale used by the instance of nasatlx?
  136. // This function returns if a scale is being used by one nasatlx
  137. // if it has support for grading and scales. Commented code should be
  138. // modified if necessary. See forum, glossary or journal modules
  139. // as reference.
  140. // @param int $nasatlxid ID of an instance of this module
  141. // @return bool true if the scale is used by the given nasatlx instance
  142. function nasatlx_scale_used($nasatlxid, $scaleid) {
  143. global $DB;
  144. /// @example
  145. if ($scaleid and $DB->record_exists('nasatlx', array('id' => $nasatlxid, 'grade' => -$scaleid))) {
  146. return true;
  147. }
  148. else {
  149. return false;
  150. }
  151. }
  152. /// Checks if scale is being used by any instance of nasatlx.
  153. // This is used to find out if scale used anywhere.
  154. // @param $scaleid int
  155. // @return boolean true if the scale is used by any nasatlx instance
  156. function nasatlx_scale_used_anywhere($scaleid) {
  157. global $DB;
  158. /// @example
  159. if ($scaleid and $DB->record_exists('nasatlx', array('grade' => -$scaleid))) {
  160. return true;
  161. }
  162. else {
  163. return false;
  164. }
  165. }
  166. /// Creates or updates grade item for the give nasatlx instance
  167. // Needed by grade_update_mod_grades() in lib/gradelib.php
  168. // @param stdClass $nasatlx instance object with extra cmidnumber and modname property
  169. // @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
  170. // @return void
  171. function nasatlx_grade_item_update(stdClass $nasatlx, $grades=null) {
  172. global $CFG;
  173. require_once($CFG->libdir.'/gradelib.php');
  174. /// @example
  175. $item = array();
  176. $item['itemname'] = clean_param($nasatlx->name, PARAM_NOTAGS);
  177. $item['gradetype'] = GRADE_TYPE_VALUE;
  178. $item['grademax'] = $nasatlx->grade;
  179. $item['grademin'] = 0;
  180. grade_update('mod/nasatlx', $nasatlx->course, 'mod', 'nasatlx', $nasatlx->id, 0, null, $item);
  181. }
  182. /// Update nasatlx grades in the gradebook
  183. // Needed by grade_update_mod_grades() in lib/gradelib.php
  184. // @param stdClass $nasatlx instance object with extra cmidnumber and modname property
  185. // @param int $userid update grade of specific user only, 0 means all participants
  186. // @return void
  187. function nasatlx_update_grades(stdClass $nasatlx, $userid = 0) {
  188. global $CFG, $DB;
  189. require_once($CFG->libdir.'/gradelib.php');
  190. /// @example
  191. $grades = array(); // populate array of grade objects indexed by userid
  192. grade_update('mod/nasatlx', $nasatlx->course, 'mod', 'nasatlx', $nasatlx->id, 0, $grades);
  193. }
  194. ////////////////////////////////////////////////////////////////////////////////
  195. // File API //
  196. ////////////////////////////////////////////////////////////////////////////////
  197. /// Returns the lists of all browsable file areas within the given module context
  198. // The file area 'intro' for the activity introduction field is added automatically
  199. // by {@link file_browser::get_file_info_context_module() }
  200. // @param stdClass $course
  201. // @param stdClass $cm
  202. // @param stdClass $context
  203. // @return array of [(string)filearea] => (string)description
  204. function nasatlx_get_file_areas($course, $cm, $context) {
  205. return array();
  206. }
  207. /// File browsing support for nasatlx file areas
  208. // @package mod_nasatlx
  209. // @category files
  210. // @param file_browser $browser
  211. // @param array $areas
  212. // @param stdClass $course
  213. // @param stdClass $cm
  214. // @param stdClass $context
  215. // @param string $filearea
  216. // @param int $itemid
  217. // @param string $filepath
  218. // @param string $filename
  219. // @return file_info instance or null if not found
  220. function nasatlx_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
  221. return null;
  222. }
  223. /// Serves the files from the nasatlx file areas
  224. // @package mod_nasatlx
  225. // @category files
  226. // @param stdClass $course the course object
  227. // @param stdClass $cm the course module object
  228. // @param stdClass $context the nasatlx's context
  229. // @param string $filearea the name of the file area
  230. // @param array $args extra arguments (itemid, path)
  231. // @param bool $forcedownload whether or not force download
  232. // @param array $options additional options affecting the file serving
  233. function nasatlx_pluginfile ($course, $cm, $context, $filearea, array $args, $forcedownload, array $options=array()) {
  234. global $DB, $CFG;
  235. if ($context->contextlevel != CONTEXT_MODULE) {
  236. send_file_not_found();
  237. }
  238. require_login($course, true, $cm);
  239. send_file_not_found();
  240. }
  241. ////////////////////////////////////////////////////////////////////////////////
  242. // Navigation API //
  243. ////////////////////////////////////////////////////////////////////////////////
  244. /// Extends the global navigation tree by adding nasatlx nodes if there is a relevant content
  245. // This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
  246. // @param navigation_node $navref An object representing the navigation tree node of the nasatlx module instance
  247. // @param stdClass $course
  248. // @param stdClass $module
  249. // @param cm_info $cm
  250. function nasatlx_extend_navigation(navigation_node $navref, stdclass $course, stdclass $module, cm_info $cm) {
  251. }
  252. // Extends the settings navigation with the nasatlx settings
  253. // This function is called when the context for the page is a nasatlx module. This is not called by AJAX
  254. // so it is safe to rely on the $PAGE.
  255. // @param settings_navigation $settingsnav {@link settings_navigation }
  256. // @param navigation_node $nasatlxnode {@link navigation_node }
  257. function nasatlx_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $nasatlxnode=null) {
  258. }
  259. // Used in: settings.php
  260. function is_debugging ($level = null, $debugdisplay = null) {
  261. global $CFG, $USER;
  262. // $CFG->debug = (int)$level;
  263. // $CFG->debugdeveloper = (($CFG->debug & DEBUG_DEVELOPER) === DEBUG_DEVELOPER);
  264. // echo "lib.php: is_debugging: debugdisplay=" . $CFG->debugdisplay . ", USER=$USER<br/>";
  265. //echo "lib.php: is_debugging: NO_DEBUG_DISPLAY=" . NO_DEBUG_DISPLAY . ", USER=$USER<br/>";
  266. if (!NO_DEBUG_DISPLAY) return true; // NO_DEBUG_DISPLAY is object
  267. //if (!isset($CFG->debugdisplay) && $USER) return true;
  268. return false;
  269. }