lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. // This file is part of
  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. // Adds gradeimporter to add new activity page
  17. function tool_devcourse_extend_navigation_course($navigation, $course, $coursecontext) {
  18. $url = new moodle_url('/admin/tool/devcourse/index.php');
  19. $devcoursenode = navigation_node::create('Development course', $url, navigation_node::TYPE_CUSTOM, 'Dev course', 'devcourse');
  20. $navigation->add_node($devcoursenode);
  21. }
  22. function gradeimporter_supports($feature) {
  23. switch ($feature) {
  24. case FEATURE_MOD_ARCHETYPE:
  25. return MOD_ARCHETYPE_RESOURCE;
  26. case FEATURE_GROUPS:
  27. return false;
  28. case FEATURE_GROUPINGS:
  29. return false;
  30. case FEATURE_MOD_INTRO:
  31. return true;
  32. case FEATURE_COMPLETION_TRACKS_VIEWS:
  33. return true;
  34. case FEATURE_GRADE_HAS_GRADE:
  35. return false;
  36. case FEATURE_GRADE_OUTCOMES:
  37. return false;
  38. case FEATURE_BACKUP_MOODLE2:
  39. return true;
  40. case FEATURE_GRADE_HAS_GRADE:
  41. return true;
  42. case FEATURE_GRADE_OUTCOMES:
  43. return true;
  44. default:
  45. return null;
  46. }
  47. }
  48. function gradeimporter_add_instance($data, $mform) {
  49. /*
  50. *Given an object containing all the necessary data,
  51. *(defined by the form in mod_form.php) this function
  52. *creates a new instance and returns the id of this new
  53. *instance.
  54. *
  55. *@param $data: an object from the form in mod_form.php
  56. *@return int: the id of the newly inserted gradeimport record
  57. */
  58. global $DB;
  59. $data->timemodified = time();
  60. $data->id = $DB->insert_record("gradeimporter", $data);
  61. return $data->id;
  62. }
  63. function gradeimporter_update_instance($data) {
  64. /*
  65. *given an object containing all the necessary data,
  66. *(defined by the form in mod_form.php) this function
  67. *updates an existing instance with the new data.
  68. *
  69. *@param $data: an object from the form mod_form.php
  70. *@return boolean: if the record update was a success of fail
  71. */
  72. global $DB;
  73. $data->timemodified = time();
  74. $data->id = $data->instance;
  75. return $DB->update_record('gradeimporter', $data);
  76. }
  77. function gradeimporter_delete_instance($data) {
  78. /*
  79. *Given an id of a gradeimporter instance,
  80. * this function permanently deletes the instance
  81. * and any data that depends on it.
  82. *
  83. *@param int $id: Id of the gradeimporter instance
  84. *@return boolean, if the deletion was a success or
  85. * a failure.
  86. */
  87. global $DB;
  88. if (!$data = $DB->get_record('gradeimporter', array('id' => $id))) {
  89. return false;
  90. }
  91. $cm = get_coursemodule_from_instance('gradeimporter', $gradeimporter->id);
  92. $context = context_module::instance($cm->id);
  93. // Files
  94. $fs = get_file_storage();
  95. $fs->delete_area_files($context->id, 'submissionfiles');
  96. // Delete all files and submissions associated with this instance
  97. $DB->delete_records('gradeimporter_submission', array('gradeimporterid' => $gradeimporter->id));
  98. $DB->delete_records('gradeimporter_feedback', array('gradeimporterid' => $gradeimporter->id));
  99. $DB->delete_records('gradeimporter_submissiontype', array('gradeimporterid' => $gradeimporter->id));
  100. // Delete the instance itself
  101. $DB->delete_records('gradeimporter', array('id' => $id));
  102. return true;
  103. }
  104. /**
  105. * Implementation of pluginfile function to get file from a pluginfile url
  106. */
  107. function mod_gradeimporter_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  108. // Check the contextlevel is as expected - if your plugin is a block, this becomes CONTEXT_BLOCK, etc.
  109. if ($context->contextlevel != CONTEXT_MODULE) {
  110. return false;
  111. }
  112. // Make sure the filearea is one of those used by the plugin.
  113. if ($filearea !== 'submissionfiles') {
  114. return false;
  115. }
  116. // Make sure the user is logged in and has access to the module
  117. // (plugins that are not course modules should leave out the 'cm' part).
  118. require_login($course, true, $cm);
  119. // Check the relevant capabilities - these may vary depending on the filearea being accessed.
  120. if (!has_capability('mod/gradeimporter:view', $context)) {
  121. return false;
  122. }
  123. // Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
  124. $itemid = array_shift($args); // The first item in the $args array.
  125. // Use the itemid to retrieve any relevant data records and perform any security checks to see if the
  126. // user really does have access to the file in question.
  127. // Extract the filename / filepath from the $args array.
  128. $filename = array_pop($args); // The last item in the $args array.
  129. if (!$args) {
  130. $filepath = '/'; // If $args is empty => the path is '/'
  131. } else {
  132. $filepath = '/'.implode('/', $args).'/'; // If $args contains elements of the filepath
  133. }
  134. // Retrieve the file from the Files API.
  135. $fs = get_file_storage();
  136. $file = $fs->get_file($context->id, 'mod_gradeimporter', $filearea, $itemid, $filepath, $filename);
  137. if (!$file) {
  138. echo "didnt find the file";
  139. return false; // The file does not exist.
  140. }
  141. // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
  142. send_stored_file($file, 86400, 0, $forcedownload, $options);
  143. }
  144. function gradeimporter_grade_item_update ($submission, $grades = null) {
  145. global $CFG, $COURSE, $DB;
  146. require_once("{$CFG->libdir}/gradelib.php");
  147. $gradeimporter = $DB->get_record('gradeimporter', array('id' => $submission->gradeimporterid));
  148. $params = array('itemname' => $gradeimporter->name, 'idnumber' => $submission->gradeimporterid, 'reset' => false);
  149. if ($submission->gradebook == 0) {
  150. $params['gradetype'] = GRADE_TYPE_NONE;
  151. } else {
  152. $params['gradetype'] = GRADE_TYPE_VALUE;
  153. $params['grademax'] = $submission->maxgrade;
  154. $params['grademin'] = 0;
  155. }
  156. if ($grades == 'reset') {
  157. $params['reset'] == true;
  158. $grades = null;
  159. }
  160. return grade_update('mod/gradeimporter', $COURSE->id, 'mod', 'gradeimporter', $submission->gradeimporterid, 0, $grades, $params);
  161. }
  162. function gradeimporter_update_grades ($submission, $userid = 0, $nullifnone = true) {
  163. global $CFG, $DB, $COURSE;
  164. require_once("{$CFG->libdir}/gradelib.php");
  165. if ($submission->gradebook == 0) {
  166. gradeimporter_grade_item_update($submission);
  167. return;
  168. }
  169. // If submission is gradeable (gradebook==1) then tries to submit the grades
  170. if ($grades = gradeimporter_get_user_grades($submission, $userid)) {
  171. // Gets student grade from gradeimporter_feedback table
  172. gradeimporter_grade_item_update($submission, $grades);
  173. return;
  174. }
  175. if ($userid and $nullifnone) {
  176. $grade = new stdClass();
  177. $grade->userid = $userid;
  178. $grade->rawgrade = null;
  179. gradeimporter_grade_item_update($submission, $grade);
  180. return;
  181. }
  182. gradeimporter_grade_item_update($submission);
  183. }
  184. function gradeimporter_reset_gradebook ($courseid, $type = '') {
  185. global $CFG, $DB;
  186. $tp = $CFG->prefix;
  187. // Select all grades on gradebook and reset them
  188. $sql = "SELECT gs.*
  189. FROM {$tp}gradeimporter_submission as gs
  190. JOIN {$tp}gradeimporter as g ON g.id = gs.gradeimporterid
  191. WHERE g.course = $courseid";
  192. $submissions = $DB->get_records_sql($sql);
  193. foreach ($submissions as $submission) {
  194. gradeimporter_grade_item_update($submission, 'reset');
  195. }
  196. }
  197. function gradeimporter_get_user_grades($submission, $userid) {
  198. global $DB, $CFG;
  199. $grade = new stdClass();
  200. $grade->userid = $userid;
  201. $tp = $CFG->prefix;
  202. $sql = "SELECT gf.id, gf.grade as grade
  203. FROM {$tp}gradeimporter_feedback as gf
  204. JOIN {$tp}gradeimporter_submission as gs
  205. ON gs.id = gf.submissionid
  206. JOIN {$tp}gradeimporter as gi
  207. ON gi.id = gs.gradeimporterid
  208. WHERE gf.studentid = $userid
  209. AND gi.id = $submission->gradeimporterid
  210. AND gs.gradebook = 1
  211. AND gs.visibility = 1";
  212. $feedbacks = $DB->get_records_sql($sql);
  213. if ($feedbacks) {
  214. $grade->rawgrade = sum_property($feedbacks, 'grade');
  215. return $grade;
  216. }
  217. return null;
  218. }
  219. function sum_property ($array, $property) {
  220. $sum = 0;
  221. foreach ($array as $object) {
  222. $sum += $object->$property;
  223. }
  224. return $sum;
  225. }
  226. function create_grade_item($submission) {
  227. global $CFG, $COURSE;
  228. require_once("{$CFG->libdir}/gradelib.php");
  229. $gradeitem = new grade_item(array('id' => 0, 'courseid' => $COURSE->id));
  230. $data = new stdClass();
  231. $data->itemname = $submission->name;
  232. $data->iteminfo = $submission->intro;
  233. // Blank [idnumer] for now
  234. $data->outcomeid = $submission->gradeimporterid;
  235. $data->cmid = $submission->gradeimporterid;
  236. $data->id = 0;
  237. $data->courseid = $COURSE->id;
  238. $data->aggregationcoef = 0;
  239. $data->itemtype = 'gradeimporter';
  240. grade_item::set_properties($gradeitem, $data);
  241. $gradeitem->itemnumber = $submission->id;
  242. $outcome = grade_outcome::fetch(array('id' => $submission->gradeimporterid));
  243. $gradeitem->gradetype = GRADE_TYPE_SCALE;
  244. $gradeitem->scaleid = $outcome->scaleid;
  245. $gradeitem->insert();
  246. if ($item = grade_item::fetch(array('itemtype' => 'gradeimporter', 'itemmodule' => $gradeitem->itemmodule,
  247. 'iteminstance' => $gradeitem->iteminstance, 'itemnumber' => 0, 'courseid' => $COURSE->id))) {
  248. $gradeitem->set_parent($item->categoryid);
  249. $gradeitem->move_after_sortorder($item->sortorder);
  250. }
  251. }
  252. function insert_grade_outcome($submission, $userid) {
  253. global $CFG, $DB, $COURSE;
  254. if (empty($CFG->enableoutcomes)) {
  255. return;
  256. }
  257. require_once("$CFG->libdir/gradelib.php");
  258. $data = array();
  259. $gradinginfo = grade_get_grades($COURSE->id, 'mod', 'gradeimporter', $submission->gradeimporterid, $userid);
  260. if (!empty($gradinginfo->outcomes)) {
  261. foreach ($gradinginfo->outcomes as $n => $old) {
  262. $data[$n] = 2;
  263. }
  264. }
  265. if (count($data) > 0) {
  266. grade_update_outcomes('mod/gradeimporter', $COURSE->id, 'mod', 'gradeimporter', $submission->gradeimporterid, $userid, $data);
  267. }
  268. }