public function get_page()

in classes/api/base.php [765:880]


    public function get_page($cmid, $wantfeedbackpage = false, $isteacher = false, $submissionuserid = null,
                             $submissionid = null, $gradeid = null) {
        global $USER, $DB;

        $cm = get_coursemodule_from_id('assign', $cmid, 0, false, MUST_EXIST);
        $assign = $DB->get_record('assign', ['id' => $cm->instance]);
        $context = \context_module::instance($cm->id);
        $userid = $USER->id;

        // If $submission_userId is given, then it contains the student's user id.
        // If it is null, it means a student is just looking at the assignment to start working on it, so use the logged in user id.
        $studentuserid = (!empty($submissionuserid)) ? $submissionuserid : $userid;
        $student = $DB->get_record('user', ['id' => $studentuserid]);

        // Determine what page we are dealing with.
        if ($isteacher === true) {
            $requestedpageidfield = ($wantfeedbackpage === true) ? 'feedback_teacher_page_id' : 'submission_teacher_page_id';
        } else {
            $requestedpageidfield = ($wantfeedbackpage === true) ? 'feedback_student_page_id' : 'submission_student_page_id';
        }

        // If the page and corresponding db record already exist just return the URL.
        $pagerecord = $DB->get_record('local_onenote_assign_pages', ['assign_id' => $assign->id, 'user_id' => $student->id]);
        if (!empty($pagerecord)) {
            if (!empty($pagerecord->$requestedpageidfield)) {
                try {
                    $page = $this->apicall('get', '/pages/'.$pagerecord->$requestedpageidfield);
                    $page = $this->process_apicall_response($page, ['links' => null]);

                    // If this user is a teacher and they are viewing a student's submission. Check if the student's OneNote
                    // page was modified after the teacher last viewed the submission.
                    if ($isteacher && 'submission_teacher_page_id' == $requestedpageidfield) {

                        // Delete Teacher's OneNote copy if the student's OneNote time last modified date
                        // is greater than the teacher's.
                        if (isset($page['lastModifiedTime'])
                          && strtotime($page['lastModifiedTime']) < $pagerecord->student_lastmodified) {

                            $this->apicall('delete', '/pages/'.$pagerecord->$requestedpageidfield);
                            $page = null;
                        }
                    }

                    if (isset($page['links']['oneNoteWebUrl']) && isset($page['links']['oneNoteWebUrl']['href'])) {
                        // We have a record and the page exists in OneNote.
                        return $page['links']['oneNoteWebUrl']['href'];
                    }
                } catch (\Exception $e) {
                    $debugdata = ['pageid' => $pagerecord->$requestedpageidfield, 'e' => $e];
                    \local_onenote\utils::debug('Error getting page', 'onenote\api\get_page', $debugdata);
                }
            }

            // User likely deleted the page. Update the db record to reflect it and recreate the page below.
            $pagerecord->$requestedpageidfield = null;
            $DB->update_record('local_onenote_assign_pages', $pagerecord);
        } else {
            // Prepare record object since we will use it further down to insert into database.
            $pagerecord = new \stdClass;
            $pagerecord->assign_id = $assign->id;
            $pagerecord->user_id = $student->id;
            $pagerecord->id = $DB->insert_record('local_onenote_assign_pages', $pagerecord);
        }

        // Get the section id for the course so we can create the page in the approp section.
        $section = $this->get_section($cm->course, $userid);
        if (empty($section)) {
            throw new \moodle_exception('errornosection', 'local_onenote');
        }
        $sectionid = $section->section_id;

        // If we are being called for getting a feedback page.
        if ($wantfeedbackpage) {
            list($postdata, $boundary) =
              $this->prepare_feedback_postdata($submissionid, $assign, $student, $context, $isteacher, $gradeid);
        } else {
            list($postdata, $boundary) = $this->prepare_submission_postdata($submissionid, $assign, $student, $context, $isteacher);
        }

        if (empty($postdata)) {
            throw new \moodle_exception('errornopostdata', 'local_onenote');
        }

        // Create the page. We allow three tries to account for possible connection problems.
        for ($i = 0; $i < 3; $i++) {
            $response = $this->create_page_from_postdata($sectionid, $postdata, $boundary);
            if (!empty($response) && $response != 'connection_error') {
                break;
            }
            usleep(50000);
        }

        // If still there is connection error, return it.
        if ($response == 'connection_error') {
            throw new \moodle_exception('connction_error', 'local_onenote');
        } else if (empty($response)) {
            throw new \moodle_exception('onenote_page_error', 'local_onenote');
        }

        // Update page id.
        $pagerecord->$requestedpageidfield = $response->id;
        // If this user is a teacher and viewing a submission, add a timestamp for the new
        // OneNote page.
        if ($isteacher && 'submission_teacher_page_id' == $requestedpageidfield) {
            if (\local_o365\rest\unified::is_configured() === true) {
                $modtimeparam = 'lastModifiedDateTime';
            } else {
                $modtimeparam = 'lastModifiedTime';
            }
            $pagerecord->teacher_lastviewed = strtotime($response->$modtimeparam);
            $pagerecord->teacher_lastviewed = empty($pagerecord->teacher_lastviewed) ? null : $pagerecord->teacher_lastviewed;
        }
        $DB->update_record('local_onenote_assign_pages', $pagerecord);

        return $response->links->oneNoteWebUrl->href;
    }