lib.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Library of interface functions and constants.
  18. *
  19. * @package colab
  20. * @copyright 2020 Your Name <you@example.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Return if the plugin supports $feature.
  26. *
  27. * @param string $feature Constant representing the feature.
  28. * @return true | null True if the feature is supported, null otherwise.
  29. */
  30. function colab_supports($feature) {
  31. switch ($feature) {
  32. case FEATURE_GRADE_HAS_GRADE:
  33. return true;
  34. case FEATURE_MOD_INTRO:
  35. return true;
  36. default:
  37. return null;
  38. }
  39. }
  40. /**
  41. * Saves a new instance of the colab into the database.
  42. *
  43. * Given an object containing all the necessary data, (defined by the form
  44. * in mod_form.php) this function will create a new instance and return the id
  45. * number of the instance.
  46. *
  47. * @param object $moduleinstance An object from the form.
  48. * @param mod_colab_mod_form $mform The form.
  49. * @return int The id of the newly inserted record.
  50. */
  51. function colab_add_instance($moduleinstance, $mform = null) {
  52. global $DB;
  53. $moduleinstance->timecreated = time();
  54. $id = $DB->insert_record('colab', $moduleinstance);
  55. return $id;
  56. }
  57. /**
  58. * Updates an instance of the colab in the database.
  59. *
  60. * Given an object containing all the necessary data (defined in mod_form.php),
  61. * this function will update an existing instance with new data.
  62. *
  63. * @param object $moduleinstance An object from the form in mod_form.php.
  64. * @param mod_colab_mod_form $mform The form.
  65. * @return bool True if successful, false otherwise.
  66. */
  67. function colab_update_instance($moduleinstance, $mform = null) {
  68. global $DB;
  69. $moduleinstance->timemodified = time();
  70. $moduleinstance->id = $moduleinstance->instance;
  71. return $DB->update_record('colab', $moduleinstance);
  72. }
  73. /**
  74. * Removes an instance of the colab from the database.
  75. *
  76. * @param int $id Id of the module instance.
  77. * @return bool True if successful, false on failure.
  78. */
  79. function colab_delete_instance($id) {
  80. global $DB;
  81. $exists = $DB->get_record('colab', array('id' => $id));
  82. if (!$exists) {
  83. return false;
  84. }
  85. $DB->delete_records('colab', array('id' => $id));
  86. return true;
  87. }
  88. /**
  89. * Is a given scale used by the instance of colab?
  90. *
  91. * This function returns if a scale is being used by one colab
  92. * if it has support for grading and scales.
  93. *
  94. * @param int $moduleinstanceid ID of an instance of this module.
  95. * @param int $scaleid ID of the scale.
  96. * @return bool True if the scale is used by the given colab instance.
  97. */
  98. function colab_scale_used($moduleinstanceid, $scaleid) {
  99. global $DB;
  100. if ($scaleid && $DB->record_exists('colab', array('id' => $moduleinstanceid, 'grade' => -$scaleid))) {
  101. return true;
  102. } else {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Checks if scale is being used by any instance of colab.
  108. *
  109. * This is used to find out if scale used anywhere.
  110. *
  111. * @param int $scaleid ID of the scale.
  112. * @return bool True if the scale is used by any colab instance.
  113. */
  114. function colab_scale_used_anywhere($scaleid) {
  115. global $DB;
  116. if ($scaleid and $DB->record_exists('colab', array('grade' => -$scaleid))) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. /**
  123. * Creates or updates grade item for the given colab instance.
  124. *
  125. * Needed by {@link grade_update_mod_grades()}.
  126. *
  127. * @param stdClass $moduleinstance Instance object with extra cmidnumber and modname property.
  128. * @param bool $reset Reset grades in the gradebook.
  129. * @return void.
  130. */
  131. function colab_grade_item_update($moduleinstance, $reset=false) {
  132. global $CFG;
  133. require_once($CFG->libdir.'/gradelib.php');
  134. $item = array();
  135. $item['itemname'] = clean_param($moduleinstance->name, PARAM_NOTAGS);
  136. $item['gradetype'] = GRADE_TYPE_VALUE;
  137. if ($moduleinstance->grade > 0) {
  138. $item['gradetype'] = GRADE_TYPE_VALUE;
  139. $item['grademax'] = $moduleinstance->grade;
  140. $item['grademin'] = 0;
  141. } else if ($moduleinstance->grade < 0) {
  142. $item['gradetype'] = GRADE_TYPE_SCALE;
  143. $item['scaleid'] = -$moduleinstance->grade;
  144. } else {
  145. $item['gradetype'] = GRADE_TYPE_NONE;
  146. }
  147. if ($reset) {
  148. $item['reset'] = true;
  149. }
  150. grade_update('/mod/colab', $moduleinstance->course, 'mod', 'colab', $moduleinstance->id, 0, null, $item);
  151. }
  152. /**
  153. * Delete grade item for given colab instance.
  154. *
  155. * @param stdClass $moduleinstance Instance object.
  156. * @return grade_item.
  157. */
  158. function colab_grade_item_delete($moduleinstance) {
  159. global $CFG;
  160. require_once($CFG->libdir.'/gradelib.php');
  161. return grade_update('/mod/colab', $moduleinstance->course, 'mod', 'colab',
  162. $moduleinstance->id, 0, null, array('deleted' => 1));
  163. }
  164. /**
  165. * Update colab grades in the gradebook.
  166. *
  167. * Needed by {@link grade_update_mod_grades()}.
  168. *
  169. * @param stdClass $moduleinstance Instance object with extra cmidnumber and modname property.
  170. * @param int $userid Update grade of specific user only, 0 means all participants.
  171. */
  172. function colab_update_grades($moduleinstance, $userid = 0) {
  173. global $CFG, $DB;
  174. require_once($CFG->libdir.'/gradelib.php');
  175. // Populate array of grade objects indexed by userid.
  176. $grades = array();
  177. grade_update('/mod/colab', $moduleinstance->course, 'mod', 'colab', $moduleinstance->id, 0, $grades);
  178. }
  179. /**
  180. * Returns the lists of all browsable file areas within the given module context.
  181. *
  182. * The file area 'intro' for the activity introduction field is added automatically
  183. * by {@link file_browser::get_file_info_context_module()}.
  184. *
  185. * @package colab
  186. * @category files
  187. *
  188. * @param stdClass $course.
  189. * @param stdClass $cm.
  190. * @param stdClass $context.
  191. * @return string[].
  192. */
  193. function colab_get_file_areas($course, $cm, $context) {
  194. return array();
  195. }
  196. /**
  197. * File browsing support for colab file areas.
  198. *
  199. * @package colab
  200. * @category files
  201. *
  202. * @param file_browser $browser.
  203. * @param array $areas.
  204. * @param stdClass $course.
  205. * @param stdClass $cm.
  206. * @param stdClass $context.
  207. * @param string $filearea.
  208. * @param int $itemid.
  209. * @param string $filepath.
  210. * @param string $filename.
  211. * @return file_info Instance or null if not found.
  212. */
  213. function colab_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
  214. return null;
  215. }
  216. /**
  217. * Serves the files from the colab file areas.
  218. *
  219. * @package colab
  220. * @category files
  221. *
  222. * @param stdClass $course The course object.
  223. * @param stdClass $cm The course module object.
  224. * @param stdClass $context The colab's context.
  225. * @param string $filearea The name of the file area.
  226. * @param array $args Extra arguments (itemid, path).
  227. * @param bool $forcedownload Whether or not force download.
  228. * @param array $options Additional options affecting the file serving.
  229. */
  230. function colab_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = array()) {
  231. global $DB, $CFG;
  232. if ($context->contextlevel != CONTEXT_MODULE) {
  233. send_file_not_found();
  234. }
  235. require_login($course, true, $cm);
  236. send_file_not_found();
  237. }
  238. /**
  239. * Extends the global navigation tree by adding colab nodes if there is a relevant content.
  240. *
  241. * This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
  242. *
  243. * @param navigation_node $colabnode An object representing the navigation tree node.
  244. * @param stdClass $course.
  245. * @param stdClass $module.
  246. * @param cm_info $cm.
  247. */
  248. function colab_extend_navigation($colabnode, $course, $module, $cm) {
  249. }
  250. /**
  251. * Extends the settings navigation with the colab settings.
  252. *
  253. * This function is called when the context for the page is a colab module.
  254. * This is not called by AJAX so it is safe to rely on the $PAGE.
  255. *
  256. * @param settings_navigation $settingsnav {@link settings_navigation}
  257. * @param navigation_node $colabnode {@link navigation_node}
  258. */
  259. function colab_extend_settings_navigation($settingsnav, $colabnode = null) {
  260. }