Merge branch 'master' of https://github.com/tniessen/moodle
[moodle.git] / mod / book / lib.php
index c09c83b..74b11af 100644 (file)
@@ -88,7 +88,12 @@ function book_add_instance($data, $mform) {
         $data->customtitles = 0;
     }
 
-    return $DB->insert_record('book', $data);
+    $id = $DB->insert_record('book', $data);
+
+    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
+    \core_completion\api::update_completion_date_event($data->coursemodule, 'book', $id, $completiontimeexpected);
+
+    return $id;
 }
 
 /**
@@ -112,6 +117,9 @@ function book_update_instance($data, $mform) {
     $book = $DB->get_record('book', array('id'=>$data->id));
     $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
 
+    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
+    \core_completion\api::update_completion_date_event($data->coursemodule, 'book', $book->id, $completiontimeexpected);
+
     return true;
 }
 
@@ -128,6 +136,9 @@ function book_delete_instance($id) {
         return false;
     }
 
+    $cm = get_coursemodule_from_instance('book', $id);
+    \core_completion\api::update_completion_date_event($cm->id, 'book', $id, null);
+
     $DB->delete_records('book_chapters', array('bookid'=>$book->id));
     $DB->delete_records('book', array('id'=>$book->id));
 
@@ -591,6 +602,11 @@ function book_export_contents($cm, $baseurl) {
             $file['userid']       = $fileinfo->get_userid();
             $file['author']       = $fileinfo->get_author();
             $file['license']      = $fileinfo->get_license();
+            $file['mimetype']     = $fileinfo->get_mimetype();
+            $file['isexternalfile'] = $fileinfo->is_external_file();
+            if ($file['isexternalfile']) {
+                $file['repositorytype'] = $fileinfo->get_repository_type();
+            }
             $contents[] = $file;
         }
     }
@@ -677,3 +693,53 @@ function book_check_updates_since(cm_info $cm, $from, $filter = array()) {
 
     return $updates;
 }
+
+/**
+ * Get icon mapping for font-awesome.
+ */
+function mod_book_get_fontawesome_icon_map() {
+    return [
+        'mod_book:chapter' => 'fa-bookmark-o',
+        'mod_book:nav_prev' => 'fa-arrow-left',
+        'mod_book:nav_prev_dis' => 'fa-angle-left',
+        'mod_book:nav_sep' => 'fa-minus',
+        'mod_book:add' => 'fa-plus',
+        'mod_book:nav_next' => 'fa-arrow-right',
+        'mod_book:nav_next_dis' => 'fa-angle-right',
+        'mod_book:nav_exit' => 'fa-arrow-up',
+    ];
+}
+
+/**
+ * Handles creating actions for events.
+ *
+ * @param calendar_event $event
+ * @param \core_calendar\action_factory $factory
+ * @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null
+ */
+function mod_book_core_calendar_provide_event_action(calendar_event $event,
+                                                     \core_calendar\action_factory $factory) {
+    $cm = get_fast_modinfo($event->courseid)->instances['book'][$event->instance];
+    $context = context_module::instance($cm->id);
+
+    if (!has_capability('mod/book:read', $context)) {
+        return null;
+    }
+
+    $course = new stdClass();
+    $course->id = $event->courseid;
+    $completion = new \completion_info($course);
+
+    $completiondata = $completion->get_data($cm, false);
+
+    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
+        return null;
+    }
+
+    return $factory->create_instance(
+        get_string('view'),
+        new \moodle_url('/mod/book/view.php', ['id' => $cm->id]),
+        1,
+        true
+    );
+}
\ No newline at end of file